diff --git a/examples/data-sources/hpegl_metal_usage/README.md b/examples/data-sources/hpegl_metal_usage/README.md deleted file mode 100644 index d2b013f..0000000 --- a/examples/data-sources/hpegl_metal_usage/README.md +++ /dev/null @@ -1,114 +0,0 @@ -# Obtaining usage information from the physical infrastructure - -This is an example of querying the physical infrastructure to obtain consumption information for compute, -networks and storage resources. - -To run the example: -* Authenticate against a portal using steeld login -* Update `variables.tf` OR provider overrides on the command line -* Run with a command similar to -``` -terraform apply \ - -var ="billing_months=01-2020" -``` - -After running `terraform apply` a list of details resources will be emitted in json format. - -Note: If additional resources have been created outside of Terraform these will be including in the usage. - -## Example Output -``` -compute_consuption = [ - { - "allocated" = "2020-01-21T17:37:18Z" - "cost" = "51.00" - "freed" = "" - "hourly_rate" = "0.75" - "id" = "785b60f4-29d0-4715-8f9a-5e1744539ccd" - "machine_size" = "Any" - "machine_size_id" = "944e7b2c-a181-4aa2-afcc-35480b07caa4" - "monthly_rate" = "275" - "name" = "test-0" - "team_id" = "" - "usage_hours" = 68 - }, - { - "allocated" = "2020-01-24T13:10:55Z" - "cost" = "0.75" - "freed" = "" - "hourly_rate" = "0.75" - "id" = "ca57f104-f2a2-4e39-99ad-d0568b235ebc" - "machine_size" = "Any" - "machine_size_id" = "944e7b2c-a181-4aa2-afcc-35480b07caa4" - "monthly_rate" = "275" - "name" = "tf-0" - "team_id" = "" - "usage_hours" = 1 - }, -] -volume_consuption = [ - { - "allocated" = "2020-01-21T17:37:18Z" - "cost" = "0.01" - "flavor" = "Fast" - "flavor_id" = "fe2f751e-ef4c-4d60-afb4-f2c87059ed41" - "freed" = "" - "hourly_rate" = "0.0001" - "id" = "472b5f03-095d-4cd1-8fc6-98cbc6de947c" - "monthly_rate" = "0.0025" - "name" = "large-volume-0" - "size" = 5 - "team_id" = "" - "usage_hours" = 68 - }, - { - "allocated" = "2020-01-24T10:19:55Z" - "cost" = "0.01" - "flavor" = "Fast" - "flavor_id" = "fe2f751e-ef4c-4d60-afb4-f2c87059ed41" - "freed" = "" - "hourly_rate" = "0.0001" - "id" = "ac8571e9-9948-41d6-a6ad-d39a4c5ceec3" - "monthly_rate" = "0.0025" - "name" = "v1" - "size" = 33 - "team_id" = "" - "usage_hours" = 3 - }, -] - -``` - -### Argument Reference - -The usage block takes the following arguments. - -- `billing_months` - The month to compute the usage for in MM-YYY format, e.g. 01-2020. If empty the current month is assumed. - -### Attribute Reference - -In addition to the arguments listed above, the following attributes are exported: - -- `host_usage` - List of all host usages over the billing period. - - `name` - The name of the host that caused the billing record. - - `size` - The size of the machine allocated. - - `size_id` - The machines size ID. - - `cost` - The cost of the machine in the location currency. - - `hourly_rate` - The charge rate for the machine per hour. - - `monthly_rate` - The monthly maximum chagable for the machine. - - `usage_hours` - The number of hours the host was provosinoed for. - - `team_id`- The team ID of the chargable entity, if not the caller. - - `allocated` -The timestamp of when the host was allocated/ - - `freed` - The timestamp of when the host was released, an empty string represents a host that is still operational. -- `volume_usage` - List of all volume usage over the billing period. - - `name` - The name of the volume that caused the billing record. - - `flavor` - The flavor of the volume allocated. - - `flavor_id` - The volume flavor size ID. - - `size` - The size of the volume allocated, units are GiB. - - `cost` - The cost of the volume in the location currency. - - `hourly_rate` - The charge rate for the volume per hour. - - `monthly_rate` - The monthly maximum chagable for the volume. - - `usage_hours` - The number of hours the volume was provosinoed for. - - `team_id`- The team ID of the chargable entity, if not the caller. - - `allocated` -The timestamp of when the volume was allocated/ - - `freed` - The timestamp of when the volume was released, an empty string represents a host that is still operational. \ No newline at end of file diff --git a/examples/data-sources/hpegl_metal_usage/data-source.tf b/examples/data-sources/hpegl_metal_usage/data-source.tf deleted file mode 100644 index 4509b31..0000000 --- a/examples/data-sources/hpegl_metal_usage/data-source.tf +++ /dev/null @@ -1,17 +0,0 @@ -// (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP - -variable "start" { - default = "2020-01-13T07:44:00Z" -} - -data "hpegl_metal_usage" "used" { - start = var.start -} - -output "compute_consumption" { - value = data.hpegl_metal_usage.used.host_usage -} - -output "volume_consumption" { - value = data.hpegl_metal_usage.used.volume_usage -} diff --git a/internal/acceptance_test/datasource_usage_test.go b/internal/acceptance_test/datasource_usage_test.go deleted file mode 100644 index ce82ed5..0000000 --- a/internal/acceptance_test/datasource_usage_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// (C) Copyright 2020, 2022-2023 Hewlett Packard Enterprise Development LP - -package acceptance_test - -import ( - "fmt" - "testing" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" -) - -func TestAccDataSourceUsages_Basic(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testUsageConfigBasic(), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("data.hpegl_metal_usage.used", "id"), - ), - }, - }, - }) -} - -func testUsageConfigBasic() string { - return fmt.Sprintf(` - provider "hpegl" { - metal { - } - alias = "test" - } - - data "hpegl_metal_usage" "used" { - provider = hpegl.test - start = %q - } - `, time.Now().Format(time.RFC3339)) -} diff --git a/internal/resources/datasource_usage.go b/internal/resources/datasource_usage.go deleted file mode 100644 index 79e27b5..0000000 --- a/internal/resources/datasource_usage.go +++ /dev/null @@ -1,340 +0,0 @@ -// (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP. - -package resources - -import ( - "time" - - "github.com/antihax/optional" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - rest "github.com/hewlettpackard/hpegl-metal-client/v1/pkg/client" - "github.com/hewlettpackard/hpegl-metal-terraform-resources/pkg/client" -) - -const ( - //uBillingMonths = "billing_months" - - hUsage = "host_usage" - vUsage = "volume_usage" - - //uCost = "cost" - //uRateHourly = "hourly_rate" - //uRateMonthly = "monthly_rate" - //uCurrency = "currency" - - // standard fields for all usage elements - uProjectID = "project_id" - uLocationID = "location_id" - uAllocated = "allocated" - uFreed = "freed" - uReady = "ready" - uUsageStart = "start" - uUsageEnd = "end" - uUsageHours = "usage_hours" - uError = "error" - - // Host speciific extras - uMachineSizeName = "machine_size" - uMachineSizeID = "machine_size_id" - uHostName = "name" - uHostID = "id" - - // Volume specific extras - uVolumeName = "name" - uVolumeID = "id" - uFlavorName = "flavor" - uFlavorID = "falvor_id" - uCapacity = "capacity" -) - -func usageSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - uUsageStart: { - Type: schema.TypeString, - Required: true, - Description: "Start time for usage calculation, format is RFC 3339 e.g. 2018-05-13T07:44:12Z", - }, - uUsageEnd: { - Type: schema.TypeString, - Optional: true, - Description: "End time for usage calculation, format is RFC 3339 e.g. 2018-05-13T07:44:12Z", - }, - hUsage: { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - uHostID: { - Type: schema.TypeString, - Computed: true, - Description: "Name of the MachineSize requested when host was created", - }, - uHostName: { - Type: schema.TypeString, - Computed: true, - Description: "Name of the associated Host", - }, - uMachineSizeName: { - Type: schema.TypeString, - Computed: true, - Description: "Name of the MachineSize requested when host was created", - }, - uMachineSizeID: { - Type: schema.TypeString, - Computed: true, - Description: "ID of the MachineSize requested when host was created", - }, - // uCost: { - // Type: schema.TypeString, - // Computed: true, - // }, - // uRateHourly: { - // Type: schema.TypeString, - // Computed: true, - // }, - // uRateMonthly: { - // Type: schema.TypeString, - // Computed: true, - // }, - uUsageHours: { - Type: schema.TypeInt, - Computed: true, - Description: "The difference between the UsageEnd and UsageStart rounded up to the UsageHours", - }, - uProjectID: { - Type: schema.TypeString, - Computed: true, - Description: "Project ID that created the host", - }, - uFreed: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when resource machine was freed", - }, - uReady: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when resource machine was ready", - }, - uAllocated: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when resource machine was allocated", - }, - uUsageStart: { - Type: schema.TypeString, - Computed: true, - Description: "The start of the usage reporting window or when the host was allocated", - }, - uUsageEnd: { - Type: schema.TypeString, - Computed: true, - Description: "The end of the usage reporting window or when the host was freed", - }, - uLocationID: { - Type: schema.TypeString, - Computed: true, - }, - uError: { - Type: schema.TypeString, - Computed: true, - Description: "Any error message", - }, - }, - }, - }, - vUsage: { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - uVolumeID: { - Type: schema.TypeString, - Computed: true, - }, - uVolumeName: { - Type: schema.TypeString, - Computed: true, - }, - uFlavorName: { - Type: schema.TypeString, - Computed: true, - }, - uFlavorID: { - Type: schema.TypeString, - Computed: true, - }, - uCapacity: { - Type: schema.TypeFloat, - Computed: true, - }, - // uCost: { - // Type: schema.TypeString, - // Computed: true, - // }, - // uRateHourly: { - // Type: schema.TypeString, - // Computed: true, - // }, - // uRateMonthly: { - // Type: schema.TypeString, - // Computed: true, - // }, - uUsageHours: { - Type: schema.TypeInt, - Computed: true, - Description: "The difference between the UsageEnd and UsageStart rounded up to the UsageHours", - }, - uProjectID: { - Type: schema.TypeString, - Computed: true, - Description: "Project ID that created the volume", - }, - uFreed: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when the volume was freed", - }, - uReady: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when resource machine was ready", - }, - uAllocated: { - Type: schema.TypeString, - Computed: true, - Description: "Timestamp of when the volume was allocated", - }, - uUsageStart: { - Type: schema.TypeString, - Computed: true, - Description: "The start of the usage reporting window or when the volume was allocated", - }, - uUsageEnd: { - Type: schema.TypeString, - Computed: true, - Description: "The end of the usage reporting window or when the volume was freed", - }, - uLocationID: { - Type: schema.TypeString, - Computed: true, - }, - uError: { - Type: schema.TypeString, - Computed: true, - Description: "Any error message", - }, - }, - }, - }, - } -} - -func DataSourceUsage() *schema.Resource { - return &schema.Resource{ - Read: resourceMetalUsageRead, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - - Schema: usageSchema(), - Description: "Get a usage report", - } -} - -//nolint: funlen // Ignoring function length check on existing function -func resourceMetalUsageRead(d *schema.ResourceData, meta interface{}) (err error) { - defer wrapResourceError(&err, "failed to get usage") - - p, err := client.GetClientFromMetaMap(meta) - if err != nil { - return err - } - - var gOps *rest.UsageReportsApiGetOpts - - start, err := time.Parse(time.RFC3339, d.Get(uUsageStart).(string)) - if err != nil { - return err - } - if d.Get(uUsageEnd).(string) != "" { - end, err := time.Parse(time.RFC3339, d.Get(uUsageEnd).(string)) - if err != nil { - return err - } - - gOps = &rest.UsageReportsApiGetOpts{ - End: optional.NewString(end.String()), - } - } - - ctx := p.GetContext() - usage, _, err := p.Client.UsageReportsApi.Get(ctx, start.Format(time.RFC3339), gOps) - if err != nil { - return err - } - used := make([]map[string]interface{}, 0, len(usage.Hosts)) - for _, use := range usage.Hosts { - uData := map[string]interface{}{ - uHostID: use.HostID, - uHostName: use.HostName, - uMachineSizeName: use.MachineSizeName, - uMachineSizeID: use.MachineSizeID, - uUsageHours: use.UsageHours, - uProjectID: use.ProjectID, - uLocationID: use.LocationID, - uUsageStart: use.UsageStart.Format(time.RFC3339), - uUsageEnd: use.UsageEnd.Format(time.RFC3339), - uAllocated: use.Allocated.Format(time.RFC3339), - uFreed: use.Freed.Format(time.RFC3339), - uReady: use.Ready.Format(time.RFC3339), - uError: use.Error, - } - // Patch up zero times to be empty strings - if use.Freed.IsZero() { - uData[uFreed] = "" - } - if use.Ready.IsZero() { - uData[uReady] = uData[uAllocated] - } - used = append(used, uData) - } - if err := d.Set(hUsage, used); err != nil { - return err - } - - volUsed := make([]map[string]interface{}, 0, len(usage.Volumes)) - for _, use := range usage.Volumes { - uData := map[string]interface{}{ - "id": use.VolumeID, - vName: use.VolumeName, - uCapacity: use.Capacity, - uFlavorID: use.FlavorID, - uFlavorName: use.FlavorName, - uUsageHours: use.UsageHours, - uProjectID: use.ProjectID, - uLocationID: use.LocationID, - uUsageStart: use.UsageStart.Format(time.RFC3339), - uUsageEnd: use.UsageEnd.Format(time.RFC3339), - uAllocated: use.Allocated.Format(time.RFC3339), - uFreed: use.Freed.Format(time.RFC3339), - uReady: use.Ready.Format(time.RFC3339), - uError: use.Error, - } - // Patch up zero times to be empty strings - if use.Freed.IsZero() { - uData[uFreed] = "" - } - if use.Ready.IsZero() { - uData[uReady] = uData[uAllocated] - } - volUsed = append(volUsed, uData) - } - if err := d.Set(vUsage, volUsed); err != nil { - return err - } - - d.SetId("usage") - return nil -} diff --git a/pkg/registration/registration.go b/pkg/registration/registration.go index cff9a1b..ebb26d8 100644 --- a/pkg/registration/registration.go +++ b/pkg/registration/registration.go @@ -21,7 +21,6 @@ const ( qAvailableResource = mPrefix + "_available_resources" qAvailableImages = mPrefix + "_available_images" - qUsage = mPrefix + "_usage" // These constants are used to set the optional hpegl provider "metal" block field-names projectID = "project_id" @@ -40,7 +39,6 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ qAvailableResource: resources.DataSourceAvailableResources(), qAvailableImages: resources.DataSourceImage(), - qUsage: resources.DataSourceUsage(), } }