Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(billing): add consumption list datasource #2176

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/data-sources/billing_consumption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Billing"
page_title: "Scaleway: scaleway_billing_consumptions"
---

# scaleway_billing_consumptions

Gets information about your Consumptions.

## Example Usage

```hcl
# Find your detailed monthly consumption list
data "scaleway_billing_consumptions" "my-consumption" {
organization_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```

## Argument Reference

- `organization_id` - (Defaults to [provider](../index.md#organization_d) `organization_id`) The ID of the organization the consumption list is associated with.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `consumptions` - List of found consumptions
- `value` - The monetary value of the consumption.
- `description` - The description of the consumption.
- `project_id` - The project ID of the consumption.
- `category` - The category of the consumption.
- `operation_path` - The unique identifier of the product.
- `updated_at` - The last consumption update date.
83 changes: 83 additions & 0 deletions scaleway/data_source_billing_consumption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package scaleway

import (
"context"
"crypto/sha256"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
billing "github.com/scaleway/scaleway-sdk-go/api/billing/v2alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayBillingConsumptions() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceScalewayBillingConsumptionsRead,
Schema: map[string]*schema.Schema{
"organization_id": organizationIDSchema(),
"consumptions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Computed: true,
Type: schema.TypeString,
},
"description": {
Computed: true,
Type: schema.TypeString,
},
"project_id": {
Computed: true,
Type: schema.TypeString,
},
"category": {
Computed: true,
Type: schema.TypeString,
},
"operation_path": {
Computed: true,
Type: schema.TypeString,
},
},
},
},
"updated_at": {
Computed: true,
Type: schema.TypeString,
},
},
}
}

func dataSourceScalewayBillingConsumptionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := billingAPI(meta)

res, err := api.GetConsumption(&billing.GetConsumptionRequest{
OrganizationID: d.Get("organization_id").(string),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

consumptions := []interface{}(nil)
for _, consumption := range res.Consumptions {
rawConsumption := make(map[string]interface{})
rawConsumption["value"] = consumption.Value.String()
rawConsumption["description"] = consumption.Description
rawConsumption["project_id"] = consumption.ProjectID
rawConsumption["category"] = consumption.Category
rawConsumption["operation_path"] = consumption.OperationPath

consumptions = append(consumptions, rawConsumption)
}

hashedID := sha256.Sum256([]byte(d.Get("organization_id").(string)))
d.SetId(fmt.Sprintf("%x", hashedID))
_ = d.Set("updated_at", flattenTime(res.UpdatedAt))
_ = d.Set("consumptions", consumptions)

return nil
}
32 changes: 32 additions & 0 deletions scaleway/data_source_billing_consumption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package scaleway

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccScalewayDataSourceBillingConsumption_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_billing_consumptions" "my-consumption" {}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.value"),
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.description"),
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.project_id"),
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.category"),
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.operation_path"),
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "updated_at"),
),
},
},
})
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_baremetal_os": dataSourceScalewayBaremetalOs(),
"scaleway_baremetal_server": dataSourceScalewayBaremetalServer(),
"scaleway_billing_invoices": dataSourceScalewayBillingInvoices(),
"scaleway_billing_consumptions": dataSourceScalewayBillingConsumptions(),
"scaleway_cockpit": dataSourceScalewayCockpit(),
"scaleway_cockpit_plan": dataSourceScalewayCockpitPlan(),
"scaleway_domain_record": dataSourceScalewayDomainRecord(),
Expand Down
Loading
Loading