From bd70a5eaf8a4d5566aaa6c0eaef6c72c009cdcb0 Mon Sep 17 00:00:00 2001 From: profoundwu Date: Tue, 24 Dec 2024 15:04:17 +0800 Subject: [PATCH] feat(cts): add this data source to get the list of users who have performed operations in the last 30 days --- docs/data-sources/cts_users.md | 39 +++++++ huaweicloud/provider.go | 1 + .../data_source_huaweicloud_cts_users_test.go | 32 ++++++ .../cts/data_source_huaweicloud_cts_users.go | 108 ++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 docs/data-sources/cts_users.md create mode 100644 huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_users_test.go create mode 100644 huaweicloud/services/cts/data_source_huaweicloud_cts_users.go diff --git a/docs/data-sources/cts_users.md b/docs/data-sources/cts_users.md new file mode 100644 index 0000000000..8e17ef42c6 --- /dev/null +++ b/docs/data-sources/cts_users.md @@ -0,0 +1,39 @@ +--- +subcategory: "Cloud Trace Service (CTS)" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_cts_users" +description: |- + Use this data source to get the list of users who have performed operations in the last 30 days. +--- + +# huaweicloud_cts_users + +Use this data source to get the list of users who have performed operations in the last 30 days. + +## Example Usage + +```hcl +data "huaweicloud_cts_users" "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. + +* `users` - The list of users who have performed operations in the last 30 days. + + The [users](#users_struct) structure is documented below. + + +The `users` block supports: + +* `name` - The username. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 0962e495f0..c18172f219 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -581,6 +581,7 @@ func Provider() *schema.Provider { "huaweicloud_cts_operations": cts.DataSourceCtsOperations(), "huaweicloud_cts_quotas": cts.DataSourceCtsQuotas(), "huaweicloud_cts_resources": cts.DataSourceCtsResources(), + "huaweicloud_cts_users": cts.DataSourceCtsUsers(), "huaweicloud_cdm_clusters": cdm.DataSourceCdmClusters(), "huaweicloud_cdm_flavors": cdm.DataSourceCdmFlavors(), diff --git a/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_users_test.go b/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_users_test.go new file mode 100644 index 0000000000..ad5a751b55 --- /dev/null +++ b/huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_users_test.go @@ -0,0 +1,32 @@ +package cts + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceUsers_basic(t *testing.T) { + dataSource := "data.huaweicloud_cts_users.test" + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceUsers_basic, + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "users.0.name"), + ), + }, + }, + }) +} + +const testDataSourceUsers_basic = `data "huaweicloud_cts_users" "test" {}` diff --git a/huaweicloud/services/cts/data_source_huaweicloud_cts_users.go b/huaweicloud/services/cts/data_source_huaweicloud_cts_users.go new file mode 100644 index 0000000000..baef329eed --- /dev/null +++ b/huaweicloud/services/cts/data_source_huaweicloud_cts_users.go @@ -0,0 +1,108 @@ +// Generated by PMS #492 +package cts + +import ( + "context" + + "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 DataSourceCtsUsers() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCtsUsersRead, + + 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.`, + }, + "users": { + Type: schema.TypeList, + Computed: true, + Description: `The list of users who have performed operations in the last 30 days.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + Description: `The username.`, + }, + }, + }, + }, + }, + } +} + +type UsersDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newUsersDSWrapper(d *schema.ResourceData, meta interface{}) *UsersDSWrapper { + return &UsersDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceCtsUsersRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newUsersDSWrapper(d, meta) + listUserResourcesRst, err := wrapper.ListUserResources() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listUserResourcesToSchema(listUserResourcesRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API CTS GET /v3/{project_id}/user-resources +func (w *UsersDSWrapper) ListUserResources() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "cts") + if err != nil { + return nil, err + } + + uri := "/v3/{project_id}/user-resources" + return httphelper.New(client). + Method("GET"). + URI(uri). + Request(). + Result() +} + +func (w *UsersDSWrapper) listUserResourcesToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("users", schemas.SliceToList(body.Get("users"), + func(users gjson.Result) any { + return map[string]any{ + "name": users.Get("name").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}