alicloud/data_source_alicloud_ram_role_policy_attachments.go (140 lines of code) (raw):

package alicloud import ( "fmt" "time" "github.com/PaesslerAG/jsonpath" util "github.com/alibabacloud-go/tea-utils/service" "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) func dataSourceAliCloudRamRolePolicyAttachments() *schema.Resource { return &schema.Resource{ Read: dataSourceAliCloudRamRolePolicyAttachmentRead, Schema: map[string]*schema.Schema{ "ids": { Type: schema.TypeList, Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, }, "role_name": { Type: schema.TypeString, Required: true, ForceNew: true, }, "attachments": { Type: schema.TypeList, Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "attach_date": { Type: schema.TypeString, Computed: true, }, "description": { Type: schema.TypeString, Computed: true, }, "policy_name": { Type: schema.TypeString, Computed: true, }, "policy_type": { Type: schema.TypeString, Computed: true, }, "id": { Type: schema.TypeString, Computed: true, }, }, }, }, "output_file": { Type: schema.TypeString, Optional: true, ForceNew: true, }, }, } } func dataSourceAliCloudRamRolePolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) var objects []map[string]interface{} idsMap := make(map[string]string) if v, ok := d.GetOk("ids"); ok { for _, vv := range v.([]interface{}) { if vv == nil { continue } idsMap[vv.(string)] = vv.(string) } } var request map[string]interface{} var response map[string]interface{} var query map[string]interface{} action := "ListPoliciesForRole" var err error request = make(map[string]interface{}) query = make(map[string]interface{}) if v, ok := d.GetOk("role_name"); ok { request["RoleName"] = v } request["RoleName"] = d.Get("role_name") runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*time.Second) err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { response, err = client.RpcPost("Ram", "2015-05-01", action, query, request, true) if err != nil { if NeedRetry(err) { wait() return resource.RetryableError(err) } return resource.NonRetryableError(err) } addDebug(action, response, request) return nil }) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } resp, _ := jsonpath.Get("$.Policies.Policy[*]", response) result, _ := resp.([]interface{}) for _, v := range result { item := v.(map[string]interface{}) if len(idsMap) > 0 { if _, ok := idsMap[fmt.Sprint("role:", item["PolicyName"], ":", item["PolicyType"], ":", request["RoleName"])]; !ok { continue } } objects = append(objects, item) } ids := make([]string, 0) names := make([]interface{}, 0) s := make([]map[string]interface{}, 0) for _, objectRaw := range objects { mapping := map[string]interface{}{} mapping["id"] = fmt.Sprint("role:", objectRaw["PolicyName"], ":", objectRaw["PolicyType"], ":", request["RoleName"]) mapping["attach_date"] = objectRaw["AttachDate"] mapping["description"] = objectRaw["Description"] mapping["policy_name"] = objectRaw["PolicyName"] mapping["policy_type"] = objectRaw["PolicyType"] ids = append(ids, fmt.Sprint(mapping["id"])) names = append(names, objectRaw[""]) s = append(s, mapping) } d.SetId(dataResourceIdHash(ids)) if err := d.Set("ids", ids); err != nil { return WrapError(err) } if err := d.Set("attachments", s); err != nil { return WrapError(err) } if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { writeToFile(output.(string), s) } return nil }