Skip to content

Commit 4084a99

Browse files
authored
feat(billing): add consumption list datasource (#2176)
1 parent b694b5e commit 4084a99

File tree

5 files changed

+382
-0
lines changed

5 files changed

+382
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Billing"
3+
page_title: "Scaleway: scaleway_billing_consumptions"
4+
---
5+
6+
# scaleway_billing_consumptions
7+
8+
Gets information about your Consumptions.
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Find your detailed monthly consumption list
14+
data "scaleway_billing_consumptions" "my-consumption" {
15+
organization_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
- `organization_id` - (Defaults to [provider](../index.md#organization_d) `organization_id`) The ID of the organization the consumption list is associated with.
22+
23+
## Attributes Reference
24+
25+
In addition to all arguments above, the following attributes are exported:
26+
27+
- `consumptions` - List of found consumptions
28+
- `value` - The monetary value of the consumption.
29+
- `description` - The description of the consumption.
30+
- `project_id` - The project ID of the consumption.
31+
- `category` - The category of the consumption.
32+
- `operation_path` - The unique identifier of the product.
33+
- `updated_at` - The last consumption update date.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"crypto/sha256"
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
billing "github.com/scaleway/scaleway-sdk-go/api/billing/v2alpha1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
)
13+
14+
func dataSourceScalewayBillingConsumptions() *schema.Resource {
15+
return &schema.Resource{
16+
ReadContext: dataSourceScalewayBillingConsumptionsRead,
17+
Schema: map[string]*schema.Schema{
18+
"organization_id": organizationIDSchema(),
19+
"consumptions": {
20+
Type: schema.TypeList,
21+
Computed: true,
22+
Elem: &schema.Resource{
23+
Schema: map[string]*schema.Schema{
24+
"value": {
25+
Computed: true,
26+
Type: schema.TypeString,
27+
},
28+
"description": {
29+
Computed: true,
30+
Type: schema.TypeString,
31+
},
32+
"project_id": {
33+
Computed: true,
34+
Type: schema.TypeString,
35+
},
36+
"category": {
37+
Computed: true,
38+
Type: schema.TypeString,
39+
},
40+
"operation_path": {
41+
Computed: true,
42+
Type: schema.TypeString,
43+
},
44+
},
45+
},
46+
},
47+
"updated_at": {
48+
Computed: true,
49+
Type: schema.TypeString,
50+
},
51+
},
52+
}
53+
}
54+
55+
func dataSourceScalewayBillingConsumptionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
56+
api := billingAPI(meta)
57+
58+
res, err := api.GetConsumption(&billing.GetConsumptionRequest{
59+
OrganizationID: d.Get("organization_id").(string),
60+
}, scw.WithContext(ctx))
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
consumptions := []interface{}(nil)
66+
for _, consumption := range res.Consumptions {
67+
rawConsumption := make(map[string]interface{})
68+
rawConsumption["value"] = consumption.Value.String()
69+
rawConsumption["description"] = consumption.Description
70+
rawConsumption["project_id"] = consumption.ProjectID
71+
rawConsumption["category"] = consumption.Category
72+
rawConsumption["operation_path"] = consumption.OperationPath
73+
74+
consumptions = append(consumptions, rawConsumption)
75+
}
76+
77+
hashedID := sha256.Sum256([]byte(d.Get("organization_id").(string)))
78+
d.SetId(fmt.Sprintf("%x", hashedID))
79+
_ = d.Set("updated_at", flattenTime(res.UpdatedAt))
80+
_ = d.Set("consumptions", consumptions)
81+
82+
return nil
83+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceBillingConsumption_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: tt.ProviderFactories,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
data "scaleway_billing_consumptions" "my-consumption" {}
20+
`,
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.value"),
23+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.description"),
24+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.project_id"),
25+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.category"),
26+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "consumptions.0.operation_path"),
27+
resource.TestCheckResourceAttrSet("data.scaleway_billing_consumptions.my-consumption", "updated_at"),
28+
),
29+
},
30+
},
31+
})
32+
}

scaleway/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
191191
"scaleway_baremetal_os": dataSourceScalewayBaremetalOs(),
192192
"scaleway_baremetal_server": dataSourceScalewayBaremetalServer(),
193193
"scaleway_billing_invoices": dataSourceScalewayBillingInvoices(),
194+
"scaleway_billing_consumptions": dataSourceScalewayBillingConsumptions(),
194195
"scaleway_cockpit": dataSourceScalewayCockpit(),
195196
"scaleway_cockpit_plan": dataSourceScalewayCockpitPlan(),
196197
"scaleway_domain_record": dataSourceScalewayDomainRecord(),

0 commit comments

Comments
 (0)