Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(identitycenter): add datasource identitycenter provisioned permssion sets #6067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/data-sources/identitycenter_provisioned_permission_sets.md
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.
7 changes: 4 additions & 3 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
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))
}
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()
}
Loading