From c91f267bab17106c21776b3ea87719d2aae81fc8 Mon Sep 17 00:00:00 2001 From: profoundwu Date: Fri, 20 Dec 2024 15:42:47 +0800 Subject: [PATCH] feat(cts): add data source to get the list of resources involved in the traces. (#6068) --- docs/data-sources/cts_resources.md | 41 ++++++ huaweicloud/provider.go | 1 + ...a_source_huaweicloud_cts_resources_test.go | 33 +++++ .../data_source_huaweicloud_cts_resources.go | 117 ++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 docs/data-sources/cts_resources.md create mode 100644 huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_resources_test.go create mode 100644 huaweicloud/services/cts/data_source_huaweicloud_cts_resources.go diff --git a/docs/data-sources/cts_resources.md b/docs/data-sources/cts_resources.md new file mode 100644 index 0000000000..95d3aebd87 --- /dev/null +++ b/docs/data-sources/cts_resources.md @@ -0,0 +1,41 @@ +--- +subcategory: "Cloud Trace Service (CTS)" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_cts_resources" +description: |- + Use this data source to get the list of resources involved in the traces. +--- + +# huaweicloud_cts_resources + +Use this data source to get the list of resources involved in the traces. + +## Example Usage + +```hcl +data "huaweicloud_cts_resources" "test" {} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) Specifies the region in which to query the resource. + If omitted, the provider-level region will be used. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `resources` - The resource list. + + The [resources](#resources_struct) structure is documented below. + + +The `resources` block supports: + +* `service_type` - The cloud service type. + +* `resource` - The resources corresponding to the cloud services. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index d0cd8f5b80..691089299e 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -580,6 +580,7 @@ func Provider() *schema.Provider { "huaweicloud_cts_trackers": cts.DataSourceCtsTrackers(), "huaweicloud_cts_operations": cts.DataSourceCtsOperations(), "huaweicloud_cts_quotas": cts.DataSourceCtsQuotas(), + "huaweicloud_cts_resources": cts.DataSourceCtsResources(), "huaweicloud_cdm_clusters": cdm.DataSourceCdmClusters(), "huaweicloud_cdm_flavors": cdm.DataSourceCdmFlavors(), diff --git a/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_resources_test.go b/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_resources_test.go new file mode 100644 index 0000000000..df1224aa07 --- /dev/null +++ b/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_resources_test.go @@ -0,0 +1,33 @@ +package cts + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceCtsResources_basic(t *testing.T) { + dataSource := "data.huaweicloud_cts_resources.test" + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceCtsResources_basic, + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "resources.0.service_type"), + resource.TestCheckResourceAttrSet(dataSource, "resources.0.resource.#"), + ), + }, + }, + }) +} + +const testDataSourceCtsResources_basic = `data "huaweicloud_cts_resources" "test" {}` diff --git a/huaweicloud/services/cts/data_source_huaweicloud_cts_resources.go b/huaweicloud/services/cts/data_source_huaweicloud_cts_resources.go new file mode 100644 index 0000000000..aa8bce59a6 --- /dev/null +++ b/huaweicloud/services/cts/data_source_huaweicloud_cts_resources.go @@ -0,0 +1,117 @@ +// Generated by PMS #487 +package cts + +import ( + "context" + "strings" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/tidwall/gjson" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" +) + +func DataSourceCtsResources() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCtssRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, + }, + "resources": { + Type: schema.TypeList, + Computed: true, + Description: `The resource list.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "service_type": { + Type: schema.TypeString, + Computed: true, + Description: `The cloud service type.`, + }, + "resource": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: `The resources corresponding to the cloud services.`, + }, + }, + }, + }, + }, + } +} + +type sDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newsDSWrapper(d *schema.ResourceData, meta interface{}) *sDSWrapper { + return &sDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceCtssRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newsDSWrapper(d, meta) + lisTraResRst, err := wrapper.ListTraceResources() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listTraceResourcesToSchema(lisTraResRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API CTS GET /v3/{domain_id}/resources +func (w *sDSWrapper) ListTraceResources() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "cts") + if err != nil { + return nil, err + } + + uri := "/v3/{domain_id}/resources" + uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID) + return httphelper.New(client). + Method("GET"). + URI(uri). + Request(). + Result() +} + +func (w *sDSWrapper) listTraceResourcesToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("resources", schemas.SliceToList(body.Get("resources"), + func(resources gjson.Result) any { + return map[string]any{ + "service_type": resources.Get("service_type").Value(), + "resource": schemas.SliceToStrList(resources.Get("resource")), + } + }, + )), + ) + return mErr.ErrorOrNil() +}