-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add data_source_huaweicloud_identity_custom_role (#815)
- Loading branch information
1 parent
4e56673
commit 8fa8608
Showing
10 changed files
with
323 additions
and
12 deletions.
There are no files selected for viewing
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,40 @@ | ||
--- | ||
subcategory: "Identity and Access Management (IAM)" | ||
--- | ||
|
||
# huaweicloud\_identity\_custom\_role | ||
|
||
Use this data source to get the ID of an HuaweiCloud custom role. | ||
|
||
The Role in Terraform is the same as Policy on console. however, | ||
The policy name is the display name of Role, the Role name cannot | ||
be found on Console. | ||
|
||
```hcl | ||
data "huaweicloud_identity_custom_role" "role" { | ||
name = "custom_role" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - (Optional, String) Name of the custom policy. | ||
|
||
* `id` - (Optional, String) ID of the custom policy. | ||
|
||
* `domain_id` - (Optional, String) The domain the policy belongs to. | ||
|
||
* `references` - (Optional, Int) The number of citations for the custom policy. | ||
|
||
* `description` - (Optional, String) Description of the custom policy. | ||
|
||
* `type` - (Optional, String) Display mode. Valid options are AX: Account level and XA: Project level. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `policy` - Document of the custom policy. | ||
|
||
* `catalog` - The catalog of the custom policy. | ||
|
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
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
155 changes: 155 additions & 0 deletions
155
huaweicloud/data_source_huaweicloud_identity_custom_role.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,155 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/huaweicloud/golangsdk/openstack/identity/v3.0/policies" | ||
) | ||
|
||
func DataSourceIdentityCustomRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceIdentityCustomRoleRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"name", "id"}, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"name", "id"}, | ||
}, | ||
"domain_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"references": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"catalog": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"policy": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIdentityCustomRoleRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
identityClient, err := config.IAMV3Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud identity client: %s", err) | ||
} | ||
|
||
allPages, err := policies.List(identityClient).AllPages() | ||
if err != nil { | ||
return fmt.Errorf("Unable to query roles: %s", err) | ||
} | ||
|
||
roles, err := policies.ExtractPageRoles(allPages) | ||
|
||
conditions := map[string]interface{}{} | ||
|
||
if v, ok := d.GetOk("name"); ok { | ||
conditions["name"] = v.(string) | ||
} | ||
if v, ok := d.GetOk("id"); ok { | ||
conditions["id"] = v.(string) | ||
} | ||
if v, ok := d.GetOk("domain_id"); ok { | ||
conditions["domain_id"] = v.(string) | ||
} | ||
if v, ok := d.GetOk("references"); ok { | ||
conditions["references"] = v.(int) | ||
} | ||
if v, ok := d.GetOk("description"); ok { | ||
conditions["description"] = v.(string) | ||
} | ||
if v, ok := d.GetOk("type"); ok { | ||
conditions["type"] = v.(string) | ||
} | ||
|
||
var allRoles []policies.Role | ||
|
||
for _, role := range roles { | ||
if rolesFilter(role, conditions) { | ||
allRoles = append(allRoles, role) | ||
} | ||
} | ||
|
||
if len(allRoles) < 1 { | ||
return fmt.Errorf("Your query returned no results. " + | ||
"Please change your search criteria and try again.") | ||
} | ||
|
||
if len(allRoles) > 1 { | ||
log.Printf("[DEBUG] Multiple results found: %#v", allRoles) | ||
return fmt.Errorf("Your query returned more than one result. Please try a more " + | ||
"specific search criteria.") | ||
} | ||
role := allRoles[0] | ||
|
||
return dataSourceIdentityCustomRoleAttributes(d, config, &role) | ||
} | ||
|
||
// dataSourceIdentityRoleV3Attributes populates the fields of an Role resource. | ||
func dataSourceIdentityCustomRoleAttributes(d *schema.ResourceData, config *Config, role *policies.Role) error { | ||
log.Printf("[DEBUG] huaweicloud_identity_role details: %#v", role) | ||
|
||
d.SetId(role.ID) | ||
d.Set("name", role.Name) | ||
d.Set("domain_id", role.DomainId) | ||
d.Set("references", role.References) | ||
d.Set("catalog", role.Catalog) | ||
d.Set("description", role.Description) | ||
d.Set("type", role.Type) | ||
|
||
policy, err := json.Marshal(role.Policy) | ||
if err != nil { | ||
return fmt.Errorf("Error marshalling policy: %s", err) | ||
} | ||
|
||
d.Set("policy", string(policy)) | ||
|
||
return nil | ||
} | ||
|
||
func rolesFilter(role policies.Role, conditions map[string]interface{}) bool { | ||
if v, ok := conditions["name"]; ok && v != role.Name { | ||
return false | ||
} | ||
if v, ok := conditions["id"]; ok && v != role.ID { | ||
return false | ||
} | ||
if v, ok := conditions["domain_id"]; ok && v != role.DomainId { | ||
return false | ||
} | ||
if v, ok := conditions["references"]; ok && v != role.References { | ||
return false | ||
} | ||
if v, ok := conditions["description"]; ok && v != role.Description { | ||
return false | ||
} | ||
if v, ok := conditions["type"]; ok && v != role.Type { | ||
return false | ||
} | ||
return true | ||
} |
77 changes: 77 additions & 0 deletions
77
huaweicloud/data_source_huaweicloud_identity_custom_role_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,77 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccHuaweiCloudIdentityCustomRoleDataSource_basic(t *testing.T) { | ||
var rName = fmt.Sprintf("ACCPTTEST-%s", acctest.RandString(5)) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckAdminOnly(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccHuaweiCloudIdentityCustomRoleDataSource_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckIdentityCustomDataSourceID("data.huaweicloud_identity_custom_role.role_1"), | ||
resource.TestCheckResourceAttr( | ||
"data.huaweicloud_identity_custom_role.role_1", "name", rName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIdentityCustomDataSourceID(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find role data source: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Role data source ID not set") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccHuaweiCloudIdentityCustomRoleDataSource_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_identity_role" test { | ||
name = "%s" | ||
description = "created by terraform" | ||
type = "AX" | ||
policy = <<EOF | ||
{ | ||
"Version": "1.1", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"obs:bucket:GetBucketAcl" | ||
], | ||
"Effect": "Allow", | ||
"Resource": [ | ||
"obs:*:*:bucket:*" | ||
] | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
data "huaweicloud_identity_custom_role" "role_1" { | ||
name = huaweicloud_identity_role.test.name | ||
} | ||
`, rName) | ||
} |
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
10 changes: 10 additions & 0 deletions
10
vendor/github.com/huaweicloud/golangsdk/openstack/identity/v3.0/policies/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
vendor/github.com/huaweicloud/golangsdk/openstack/identity/v3.0/policies/results.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
vendor/github.com/huaweicloud/golangsdk/openstack/identity/v3.0/policies/urls.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.