|
| 1 | +// Generated by PMS #509 |
| 2 | +package identitycenter |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-multierror" |
| 9 | + "github.com/hashicorp/go-uuid" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/tidwall/gjson" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceIdentitycenterSystemPolicyAttachments() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceIdentitycenterSystemPolicyAttachmentsRead, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "region": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Computed: true, |
| 28 | + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, |
| 29 | + }, |
| 30 | + "instance_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: `Specifies the ID of an IAM Identity Center instance.`, |
| 34 | + }, |
| 35 | + "permission_set_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + Description: `Specifies the ID of a permission set.`, |
| 39 | + }, |
| 40 | + "policies": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Computed: true, |
| 43 | + Description: `The list of IAM system-defined policies.`, |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: `The ID of the IAM system-defined policy.`, |
| 50 | + }, |
| 51 | + "name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: `The name of the IAM system-defined policy.`, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +type SystemPolicyAttachmentsDSWrapper struct { |
| 64 | + *schemas.ResourceDataWrapper |
| 65 | + Config *config.Config |
| 66 | +} |
| 67 | + |
| 68 | +func newSystemPolicyAttachmentsDSWrapper(d *schema.ResourceData, meta interface{}) *SystemPolicyAttachmentsDSWrapper { |
| 69 | + return &SystemPolicyAttachmentsDSWrapper{ |
| 70 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 71 | + Config: meta.(*config.Config), |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func dataSourceIdentitycenterSystemPolicyAttachmentsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 76 | + wrapper := newSystemPolicyAttachmentsDSWrapper(d, meta) |
| 77 | + lisManRolInPerSetRst, err := wrapper.ListManagedRolesInPermissionSet() |
| 78 | + if err != nil { |
| 79 | + return diag.FromErr(err) |
| 80 | + } |
| 81 | + |
| 82 | + id, err := uuid.GenerateUUID() |
| 83 | + if err != nil { |
| 84 | + return diag.FromErr(err) |
| 85 | + } |
| 86 | + d.SetId(id) |
| 87 | + |
| 88 | + err = wrapper.listManagedRolesInPermissionSetToSchema(lisManRolInPerSetRst) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(err) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// @API IDENTITYCENTER GET /v1/instances/{instance_id}/permission-sets/{permission_set_id}/managed-roles |
| 97 | +func (w *SystemPolicyAttachmentsDSWrapper) ListManagedRolesInPermissionSet() (*gjson.Result, error) { |
| 98 | + client, err := w.NewClient(w.Config, "identitycenter") |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + uri := "/v1/instances/{instance_id}/permission-sets/{permission_set_id}/managed-roles" |
| 104 | + uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string)) |
| 105 | + uri = strings.ReplaceAll(uri, "{permission_set_id}", w.Get("permission_set_id").(string)) |
| 106 | + return httphelper.New(client). |
| 107 | + Method("GET"). |
| 108 | + URI(uri). |
| 109 | + MarkerPager("attached_managed_roles", "page_info.next_marker", "marker"). |
| 110 | + Request(). |
| 111 | + Result() |
| 112 | +} |
| 113 | + |
| 114 | +func (w *SystemPolicyAttachmentsDSWrapper) listManagedRolesInPermissionSetToSchema(body *gjson.Result) error { |
| 115 | + d := w.ResourceData |
| 116 | + mErr := multierror.Append(nil, |
| 117 | + d.Set("region", w.Config.GetRegion(w.ResourceData)), |
| 118 | + d.Set("policies", schemas.SliceToList(body.Get("attached_managed_roles"), |
| 119 | + func(policies gjson.Result) any { |
| 120 | + return map[string]any{ |
| 121 | + "id": policies.Get("role_id").Value(), |
| 122 | + "name": policies.Get("role_name").Value(), |
| 123 | + } |
| 124 | + }, |
| 125 | + )), |
| 126 | + ) |
| 127 | + return mErr.ErrorOrNil() |
| 128 | +} |
0 commit comments