-
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.
- Loading branch information
1 parent
551eba2
commit 784ae26
Showing
7 changed files
with
223 additions
and
9 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
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,138 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/huaweicloud/golangsdk" | ||
sdkroles "github.com/huaweicloud/golangsdk/openstack/identity/v3/roles" | ||
) | ||
|
||
func dataSourceIAMRoleV3() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceIAMRoleV3Read, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"projects": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"domains": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"project_domains": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"others": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIAMRoleV3Read(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
client, err := agencyClient(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HuaweiCloud client: %s", err) | ||
} | ||
|
||
domainID, err := getDomainID(config, client) | ||
if err != nil { | ||
return fmt.Errorf("Error getting the domain id, err=%s", err) | ||
|
||
} | ||
|
||
roles, err := dsAllRolesOfDomain(domainID, client) | ||
if err != nil { | ||
return err | ||
} | ||
if roles != nil { | ||
d.Set("projects", roles["XA"]) | ||
d.Set("domains", roles["AX"]) | ||
d.Set("project_domains", roles["AA"]) | ||
d.Set("others", roles["XX"]) | ||
} | ||
d.SetId("roles") | ||
return nil | ||
} | ||
|
||
func dsListRolesOfDomain(domainID string, client *golangsdk.ServiceClient) (map[string]map[string]string, error) { | ||
old := client.Endpoint | ||
defer func() { client.Endpoint = old }() | ||
client.Endpoint = "https://iam.myhwclouds.com:443/v3/" | ||
|
||
opts := sdkroles.ListOpts{ | ||
DomainID: domainID, | ||
} | ||
allPages, err := sdkroles.List(client, &opts).AllPages() | ||
if err != nil { | ||
return nil, fmt.Errorf("List roles failed, err=%s", err) | ||
} | ||
|
||
all, err := sdkroles.ExtractRoles(allPages) | ||
if err != nil { | ||
return nil, fmt.Errorf("Extract roles failed, err=%s", err) | ||
} | ||
if len(all) == 0 { | ||
return nil, nil | ||
} | ||
|
||
r := map[string]map[string]string{ | ||
"AX": make(map[string]string, 0), | ||
"XA": make(map[string]string, 0), | ||
"AA": make(map[string]string, 0), | ||
"XX": make(map[string]string, 0), | ||
} | ||
for _, item := range all { | ||
rtype, ok := item.Extra["type"].(string) | ||
if !ok { | ||
log.Printf("[DEBUG] Can not retrieve type of role:%#v", item) | ||
continue | ||
} | ||
|
||
dn, ok := item.Extra["display_name"].(string) | ||
if !ok { | ||
log.Printf("[DEBUG] Can not retrieve name ofrole:%#v", item) | ||
continue | ||
} | ||
|
||
desc, ok := item.Extra["description"].(string) | ||
if !ok { | ||
log.Printf("[DEBUG] Can not retrieve description of role:%#v", item) | ||
continue | ||
} | ||
|
||
r[rtype][dn] = desc | ||
} | ||
return r, nil | ||
} | ||
|
||
func dsAllRolesOfDomain(domainID string, client *golangsdk.ServiceClient) (map[string]map[string]string, error) { | ||
roles, err := dsListRolesOfDomain("", client) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error listing global roles, err=%s", err) | ||
} | ||
|
||
customRoles, err := dsListRolesOfDomain(domainID, client) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error listing domain's custom roles, err=%s", err) | ||
} | ||
|
||
if customRoles == nil { | ||
return roles, nil | ||
} | ||
if roles == nil { | ||
return customRoles, nil | ||
} | ||
for k, v := range customRoles { | ||
for k1, v1 := range v { | ||
roles[k][k1] = v1 | ||
} | ||
} | ||
return roles, nil | ||
} |
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,28 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccIAMRoleV3DataSource_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccIAMRoleV3DataSource_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet( | ||
"data.huaweicloud_iam_role_v3.roles", "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccIAMRoleV3DataSource_basic = ` | ||
data "huaweicloud_iam_role_v3" "roles" { | ||
} | ||
` |
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
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 @@ | ||
--- | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_iam_role_v3" | ||
sidebar_current: "docs-huaweicloud-datasource-iam-role-v3" | ||
description: |- | ||
Get all the IAM roles including global and a domain's custom ones. | ||
--- | ||
|
||
# huaweicloud\_iam\_role_v3 | ||
|
||
Use this data source to get all the IAM roles a domain can use. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_iam_role_v3" "roles" { | ||
} | ||
``` | ||
|
||
**Note**: It can not set `tenant_name` in `provider "huaweicloud"` when | ||
using this data source. | ||
|
||
## Argument Reference | ||
|
||
|
||
## Attributes Reference | ||
|
||
* `projects` - The list of roles which can be granted only to a project. Each | ||
role will include its name and description. | ||
|
||
* `domains` - The list of roles which can be granted only to a domain. Each | ||
role will include its name and description. | ||
|
||
* `project_domains` - The list of roles which can be granted to a project or | ||
domain. Each role will include its name and description. | ||
|
||
* `others` - The list of roles which can be granted to other service, like | ||
object storage service. Each role will include its name and description. |
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