From e0d344a4c48108dfa2b898026ada07c87f227364 Mon Sep 17 00:00:00 2001 From: luoping-12345 Date: Fri, 20 Dec 2024 10:34:19 +0800 Subject: [PATCH] feat(identitycenter): add datasource identitycenter provisioned permission sets --- ...ntitycenter_provisioned_permission_sets.md | 63 +++++++ huaweicloud/provider.go | 7 +- ...center_provisioned_permission_sets_test.go | 45 +++++ ...ntitycenter_provisioned_permission_sets.go | 162 ++++++++++++++++++ 4 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 docs/data-sources/identitycenter_provisioned_permission_sets.md create mode 100644 huaweicloud/services/acceptance/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets_test.go create mode 100644 huaweicloud/services/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets.go diff --git a/docs/data-sources/identitycenter_provisioned_permission_sets.md b/docs/data-sources/identitycenter_provisioned_permission_sets.md new file mode 100644 index 0000000000..7afe4885d4 --- /dev/null +++ b/docs/data-sources/identitycenter_provisioned_permission_sets.md @@ -0,0 +1,63 @@ +--- +subcategory: "IAM Identity Center" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_identitycenter_provisioned_permission_sets" +description: |- + Use this data source to get the Identity Center provisioned permission sets. +--- + +# huaweicloud_identitycenter_provisioned_permission_sets + +Use this data source to get the Identity Center provisioned permission sets. + +## Example Usage + +```hcl +variable "instance_id" {} +variable "permission_set_id" {} +variable "target_type" {} + +data "huaweicloud_identitycenter_provisioned_permission_sets" "test" { + instance_id = var.instance_id + permission_set_id = var.permission_set_id + target_type = var.target_type +} +``` + +## 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. + +* `instance_id` - (Required, String) Specifies the ID of an IAM Identity Center instance. + +* `permission_set_id` - (Required, String) Specifies the ID of a permission set. + +* `target_id` - (Optional, String) Specifies the account ID. + +* `target_type` - (Required, String) Specifies the type of the principal to be attached. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `permission_set_provisioning_status` - The authorization details of a permission set. + + The [permission_set_provisioning_status](#permission_set_provisioning_status_struct) structure is documented below. + + +The `permission_set_provisioning_status` block supports: + +* `status` - The authorization status of a permission set. + +* `account_id` - The ID of a specified account. + +* `created_at` - The time when a permission set was created. + +* `failure_reason` - The failure reason. + +* `permission_set_id` - The ID of a permission set. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index b16211091a..0f344ed501 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -856,9 +856,10 @@ func Provider() *schema.Provider { "huaweicloud_identity_agencies": iam.DataSourceIdentityAgencies(), "huaweicloud_identity_providers": iam.DataSourceIamIdentityProviders(), - "huaweicloud_identitycenter_instance": identitycenter.DataSourceIdentityCenter(), - "huaweicloud_identitycenter_groups": identitycenter.DataSourceIdentityCenterGroups(), - "huaweicloud_identitycenter_users": identitycenter.DataSourceIdentityCenterUsers(), + "huaweicloud_identitycenter_instance": identitycenter.DataSourceIdentityCenter(), + "huaweicloud_identitycenter_groups": identitycenter.DataSourceIdentityCenterGroups(), + "huaweicloud_identitycenter_users": identitycenter.DataSourceIdentityCenterUsers(), + "huaweicloud_identitycenter_provisioned_permission_sets": identitycenter.DataSourceIdentitycenterProvisionedPermissionSets(), "huaweicloud_iec_bandwidths": iec.DataSourceBandWidths(), "huaweicloud_iec_eips": iec.DataSourceEips(), diff --git a/huaweicloud/services/acceptance/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets_test.go b/huaweicloud/services/acceptance/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets_test.go new file mode 100644 index 0000000000..6dd3d8f561 --- /dev/null +++ b/huaweicloud/services/acceptance/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets_test.go @@ -0,0 +1,45 @@ +package identitycenter + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceIdentitycenterProvisionedPermissionSets_basic(t *testing.T) { + dataSource := "data.huaweicloud_identitycenter_provisioned_permission_sets.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckMultiAccount(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceDataSourceIdentitycenterProvisionedPermissionSets_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(rName, "permission_set_provisioning_status"), + ), + }, + }, + }) +} + +func testDataSourceDataSourceIdentitycenterProvisionedPermissionSets_basic(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_identitycenter_provisioned_permission_sets" "test" { + instance_id = data.huaweicloud_identitycenter_instance.system.id + permission_set_id = huaweicloud_identitycenter_permission_set.test.id + target_type = "ACCOUNT" +} +`, testPermissionSet_basic(name)) +} diff --git a/huaweicloud/services/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets.go b/huaweicloud/services/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets.go new file mode 100644 index 0000000000..e39d208197 --- /dev/null +++ b/huaweicloud/services/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets.go @@ -0,0 +1,162 @@ +// Generated by PMS #485 +package identitycenter + +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" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func DataSourceIdentitycenterProvisionedPermissionSets() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIdentitycenterProvisionedPermissionSetsRead, + + 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.`, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the ID of an IAM Identity Center instance.`, + }, + "permission_set_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the ID of a permission set.`, + }, + "target_id": { + Type: schema.TypeString, + Optional: true, + Description: `Specifies the account ID.`, + }, + "target_type": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the type of the principal to be attached.`, + }, + "permission_set_provisioning_status": { + Type: schema.TypeList, + Computed: true, + Description: `The authorization details of a permission set.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "status": { + Type: schema.TypeString, + Computed: true, + Description: `The authorization status of a permission set.`, + }, + "account_id": { + Type: schema.TypeString, + Computed: true, + Description: `The ID of a specified account.`, + }, + "created_at": { + Type: schema.TypeString, + Computed: true, + Description: `The time when a permission set was created.`, + }, + "failure_reason": { + Type: schema.TypeString, + Computed: true, + Description: `The failure reason.`, + }, + "permission_set_id": { + Type: schema.TypeString, + Computed: true, + Description: `The ID of a permission set.`, + }, + }, + }, + }, + }, + } +} + +type ProvisionedPermissionSetsDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newProvisionedPermissionSetsDSWrapper(d *schema.ResourceData, meta interface{}) *ProvisionedPermissionSetsDSWrapper { + return &ProvisionedPermissionSetsDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceIdentitycenterProvisionedPermissionSetsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newProvisionedPermissionSetsDSWrapper(d, meta) + proPerSetRst, err := wrapper.ProvisionPermissionSet() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.provisionPermissionSetToSchema(proPerSetRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API IDENTITYCENTER POST /v1/instances/{instance_id}/permission-sets/{permission_set_id}/provision +func (w *ProvisionedPermissionSetsDSWrapper) ProvisionPermissionSet() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "identitycenter") + if err != nil { + return nil, err + } + + uri := "/v1/instances/{instance_id}/permission-sets/{permission_set_id}/provision" + uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string)) + uri = strings.ReplaceAll(uri, "{permission_set_id}", w.Get("permission_set_id").(string)) + params := map[string]any{ + "target_id": w.Get("target_id"), + "target_type": w.Get("target_type"), + } + params = utils.RemoveNil(params) + return httphelper.New(client). + Method("POST"). + URI(uri). + Body(params). + Request(). + Result() +} + +func (w *ProvisionedPermissionSetsDSWrapper) provisionPermissionSetToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("permission_set_provisioning_status", schemas.ObjectToList(body.Get("permission_set_provisioning_status"), + func(perSetProStatus gjson.Result) any { + return map[string]any{ + "status": perSetProStatus.Get("status").Value(), + "account_id": perSetProStatus.Get("account_id").Value(), + "created_at": perSetProStatus.Get("created_date").Value(), + "failure_reason": perSetProStatus.Get("failure_reason").Value(), + "permission_set_id": perSetProStatus.Get("permission_set_id").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}