From 3f58663231c93c2a70899181c09ec73d495e150b Mon Sep 17 00:00:00 2001 From: Johan Denoyer Date: Tue, 18 Jun 2024 15:20:34 +0200 Subject: [PATCH] Add ovh_refund and ovh_refund_detail tables --- docs/tables/ovh_refund.md | 33 +++++++ docs/tables/ovh_refund_detail.md | 30 ++++++ ovh/plugin.go | 2 + ovh/table_ovh_refund.go | 159 +++++++++++++++++++++++++++++++ ovh/table_ovh_refund_detail.go | 145 ++++++++++++++++++++++++++++ 5 files changed, 369 insertions(+) create mode 100644 docs/tables/ovh_refund.md create mode 100644 docs/tables/ovh_refund_detail.md create mode 100644 ovh/table_ovh_refund.go create mode 100644 ovh/table_ovh_refund_detail.go diff --git a/docs/tables/ovh_refund.md b/docs/tables/ovh_refund.md new file mode 100644 index 0000000..70f8ab1 --- /dev/null +++ b/docs/tables/ovh_refund.md @@ -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'; +``` diff --git a/docs/tables/ovh_refund_detail.md b/docs/tables/ovh_refund_detail.md new file mode 100644 index 0000000..1728dc6 --- /dev/null +++ b/docs/tables/ovh_refund_detail.md @@ -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'; +``` diff --git a/ovh/plugin.go b/ovh/plugin.go index aa00f84..d184f07 100644 --- a/ovh/plugin.go +++ b/ovh/plugin.go @@ -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 diff --git a/ovh/table_ovh_refund.go b/ovh/table_ovh_refund.go new file mode 100644 index 0000000..aa719e9 --- /dev/null +++ b/ovh/table_ovh_refund.go @@ -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 +} diff --git a/ovh/table_ovh_refund_detail.go b/ovh/table_ovh_refund_detail.go new file mode 100644 index 0000000..f8713e4 --- /dev/null +++ b/ovh/table_ovh_refund_detail.go @@ -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) +}