Skip to content

Commit

Permalink
Add ovh_refund and ovh_refund_detail tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jdenoy authored and francois2metz committed Jun 20, 2024
1 parent 0959ff8 commit 3f58663
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/tables/ovh_refund.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Table: ovh_refund

List of all the refunds of your account.

The `ovh_refund` table can be used to query information about your refund information.

## Examples

### List refunds

```sql
select
id,
date,
original_bill_id,
price_with_tax
from
ovh_refund;
```

### Get a refund

```sql
select
id,
date,
original_bill_id,
price_with_tax
from
ovh_refund
where
id = 'AFRxxxxxxx';
```
30 changes: 30 additions & 0 deletions docs/tables/ovh_refund_detail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Table: ovh_refund_detail

Details of the refund of your account.

The `ovh_refund_detail` table can be used to query information about your refund information.

## Examples

### List details of one refund

```sql
select
*
from
ovh_refund_detail
where
refund_id = 'AFRxxxxxxx';
```

### Get detail of one refund

```sql
select
*
from
ovh_refund_detail
where
refund_id = 'AFRxxxxxxxx'
and id = 'AFRxxxxxxxx';
```
2 changes: 2 additions & 0 deletions ovh/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"ovh_cloud_storage_swift": tableOvhCloudStorageSwift(),
"ovh_cloud_volume": tableOvhCloudVolume(),
"ovh_cloud_volume_snapshot": tableOvhCloudVolumeSnapshot(),
"ovh_refund": tableOvhRefund(),
"ovh_refund_detail": tableOvhRefundDetails(),
},
}
return p
Expand Down
159 changes: 159 additions & 0 deletions ovh/table_ovh_refund.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package ovh

import (
"context"
"fmt"
"time"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

type Refund struct {
ID string `json:"refundId"`
Date time.Time `json:"date"`
Url string `json:"url"`
PdfUrl string `json:"pdfUrl"`
OrderId int `json:"orderId"`
OriginalBillId string `json:"originalBillId"`
Password string `json:"password"`
PriceWithTax Price `json:"priceWithTax"`
PriceWithoutTax Price `json:"priceWithoutTax"`
Tax Price `json:"tax"`
}

func tableOvhRefund() *plugin.Table {
return &plugin.Table{
Name: "ovh_refund",
Description: "Refunds of your account.",
List: &plugin.ListConfig{
Hydrate: listRefund,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"id"}),
Hydrate: getRefund,
},
HydrateConfig: []plugin.HydrateConfig{
{Func: getRefundInfo},
},
Columns: []*plugin.Column{
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "ID of the refund.",
},
{
Name: "date",
Hydrate: getRefundInfo,
Type: proto.ColumnType_TIMESTAMP,
Description: "Date of the refund.",
},
{
Name: "url",
Hydrate: getRefundInfo,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Url"),
Description: "URL to download the refund document.",
},
{
Name: "pdf_url",
Hydrate: getRefundInfo,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PdfUrl"),
Description: "URL to download the refund document in PDF format (maybe same as url field).",
},
{
Name: "order_id",
Hydrate: getRefundInfo,
Type: proto.ColumnType_INT,
Transform: transform.FromField("OrderId"),
Description: "Order id.",
},
{
Name: "original_bill_id",
Hydrate: getRefundInfo,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("OriginalBillId"),
Description: "Original Bill id.",
},
{
Name: "password",
Hydrate: getRefundInfo,
Type: proto.ColumnType_STRING,
Description: "Password to download the refund document.",
},
{
Name: "price_with_tax",
Hydrate: getRefundInfo,
Type: proto.ColumnType_DOUBLE,
Transform: transform.FromField("PriceWithTax.Value"),
Description: "Price with tax.",
},
{
Name: "price_without_tax",
Hydrate: getRefundInfo,
Type: proto.ColumnType_DOUBLE,
Transform: transform.FromField("PriceWithoutTax.Value"),
Description: "Price without tax.",
},
{
Name: "tax",
Hydrate: getRefundInfo,
Type: proto.ColumnType_DOUBLE,
Transform: transform.FromField("Tax.Value"),
Description: "Amount of the tax.",
},
},
}
}

func getRefundInfo(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
refund := h.Item.(Refund)

client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_refund.getRefundInfo", "connection_error", err)
return nil, err
}

err = client.Get(fmt.Sprintf("/me/refund/%s", refund.ID), &refund)

if err != nil {
plugin.Logger(ctx).Error("ovh_bill.getRefundInfo", err)
return nil, err
}

return refund, nil
}

func listRefund(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_refund.listRefund", "connection_error", err)
return nil, err
}

var refundsId []string
err = client.Get("/me/refund", &refundsId)

if err != nil {
plugin.Logger(ctx).Error("ovh_refund.listRefund", err)
return nil, err
}

for _, refundId := range refundsId {
var refund Refund
refund.ID = refundId
d.StreamListItem(ctx, refund)
}

return nil, nil
}

func getRefund(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
id := d.Quals.ToEqualsQualValueMap()["id"].GetStringValue()
var refund Refund
refund.ID = id
return refund, nil
}
145 changes: 145 additions & 0 deletions ovh/table_ovh_refund_detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package ovh

import (
"context"
"fmt"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

type RefundPrice struct {
Value float64 `json:"value"`
CurrencyCode string `json:"currencyCode"`
}
type RefundDetail struct {
ID string `json:"refundDetailId"`
RefundID string `json:"refundId"`
Description string `json:"description"`
Domain string `json:"domain"`
Quantity string `json:"quantity"`
TotalPrice RefundPrice `json:"totalPrice"`
UnitPrice RefundPrice `json:"unitPrice"`
}

func tableOvhRefundDetails() *plugin.Table {
return &plugin.Table{
Name: "ovh_refund_detail",
Description: "Detail of a refund.",
List: &plugin.ListConfig{
KeyColumns: plugin.AllColumns([]string{"refund_id"}),
Hydrate: listRefundDetails,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"refund_id", "id"}),
Hydrate: getRefundDetail,
},
Columns: []*plugin.Column{
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "ID of detail's refund.",
},
{
Name: "refund_id",
Transform: transform.FromQual("refund_id"),
Type: proto.ColumnType_STRING,
Description: "ID of refund detail.",
},
{
Name: "description",
Hydrate: getGetRefundDetailInfo,
Type: proto.ColumnType_STRING,
Description: "Description of detail.",
},
{
Name: "domain",
Hydrate: getGetRefundDetailInfo,
Type: proto.ColumnType_STRING,
Description: "Domain.",
},
{
Name: "quantity",
Hydrate: getGetRefundDetailInfo,
Type: proto.ColumnType_STRING,
Description: "Quantity of detail.",
},
{
Name: "total_price",
Hydrate: getGetRefundDetailInfo,
Type: proto.ColumnType_DOUBLE,
Transform: transform.FromField("TotalPrice.Value"),
Description: "Total price of this detail.",
},
{
Name: "unit_price",
Hydrate: getGetRefundDetailInfo,
Type: proto.ColumnType_DOUBLE,
Transform: transform.FromField("UnitPrice.Value"),
Description: "Unit price of this detail.",
},
},
}
}

// This function populate data of refund detail
func getGetRefundDetailInfo(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
refundDetail := h.Item.(RefundDetail)

client, err := connect(ctx, d)

if err != nil {
plugin.Logger(ctx).Error("ovh_refund_detail.getGetBillDetailInfo", "connection_error", err)
return nil, err
}

err = client.Get(fmt.Sprintf("/me/refund/%s/details/%s", refundDetail.RefundID, refundDetail.ID), &refundDetail)

if err != nil {
plugin.Logger(ctx).Error("ovh_refund_detail.getGetBillDetailInfo", err)
return nil, err
}

return refundDetail, nil
}

func listRefundDetails(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_refund_detail.listRefundDetails", "connection_error", err)
return nil, err
}

refundId := d.EqualsQuals["refund_id"].GetStringValue()

// First, we get IDs of refund
var refundDetailsId []string
err = client.Get(fmt.Sprintf("/me/refund/%s/details", refundId), &refundDetailsId)

if err != nil {
plugin.Logger(ctx).Error("ovh_refund_detail.listRefundDetails", err)
return nil, err
}

for _, id := range refundDetailsId {
d.StreamListItem(ctx, RefundDetail{
ID: id,
RefundID: refundId,
})
}

return nil, nil
}

func getRefundDetail(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
refundId := d.EqualsQuals["refund_id"].GetStringValue()
id := d.EqualsQuals["id"].GetStringValue()

h.Item = RefundDetail{
ID: id,
RefundID: refundId,
}

return getGetRefundDetailInfo(ctx, d, h)
}

0 comments on commit 3f58663

Please sign in to comment.