-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(identitycenter): add datasource identitycenter provisioned permi…
…ssion sets
- Loading branch information
1 parent
905a902
commit e0d344a
Showing
4 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
docs/data-sources/identitycenter_provisioned_permission_sets.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
<a name="permission_set_provisioning_status_struct"></a> | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
162 changes: 162 additions & 0 deletions
162
...ices/identitycenter/data_source_huaweicloud_identitycenter_provisioned_permission_sets.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |