|
| 1 | +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! |
| 2 | +package alicloud |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/PaesslerAG/jsonpath" |
| 10 | + util "github.com/alibabacloud-go/tea-utils/service" |
| 11 | + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 15 | +) |
| 16 | + |
| 17 | +func dataSourceAliCloudRamSystemPolicys() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Read: dataSourceAliCloudRamSystemPolicyRead, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "ids": { |
| 22 | + Type: schema.TypeList, |
| 23 | + Optional: true, |
| 24 | + ForceNew: true, |
| 25 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 26 | + Computed: true, |
| 27 | + }, |
| 28 | + "name_regex": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + ValidateFunc: validation.ValidateRegexp, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + "names": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "policys": { |
| 40 | + Type: schema.TypeList, |
| 41 | + Computed: true, |
| 42 | + Elem: &schema.Resource{ |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "attachment_count": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "create_time": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "description": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "policy_name": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "policy_type": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "update_date": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "id": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + "output_file": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + ForceNew: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func dataSourceAliCloudRamSystemPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 85 | + client := meta.(*connectivity.AliyunClient) |
| 86 | + |
| 87 | + var objects []map[string]interface{} |
| 88 | + var nameRegex *regexp.Regexp |
| 89 | + if v, ok := d.GetOk("name_regex"); ok { |
| 90 | + r, err := regexp.Compile(v.(string)) |
| 91 | + if err != nil { |
| 92 | + return WrapError(err) |
| 93 | + } |
| 94 | + nameRegex = r |
| 95 | + } |
| 96 | + |
| 97 | + idsMap := make(map[string]string) |
| 98 | + if v, ok := d.GetOk("ids"); ok { |
| 99 | + for _, vv := range v.([]interface{}) { |
| 100 | + if vv == nil { |
| 101 | + continue |
| 102 | + } |
| 103 | + idsMap[vv.(string)] = vv.(string) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + var request map[string]interface{} |
| 108 | + var response map[string]interface{} |
| 109 | + var query map[string]interface{} |
| 110 | + action := "ListPolicies" |
| 111 | + var err error |
| 112 | + request = make(map[string]interface{}) |
| 113 | + query = make(map[string]interface{}) |
| 114 | + request["PolicyType"] = "System" |
| 115 | + runtime := util.RuntimeOptions{} |
| 116 | + runtime.SetAutoretry(true) |
| 117 | + request["MaxResults"] = PageSizeLarge |
| 118 | + for { |
| 119 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 120 | + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { |
| 121 | + response, err = client.RpcPost("Ram", "2015-05-01", action, query, request, true) |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + if NeedRetry(err) { |
| 125 | + wait() |
| 126 | + return resource.RetryableError(err) |
| 127 | + } |
| 128 | + return resource.NonRetryableError(err) |
| 129 | + } |
| 130 | + addDebug(action, response, request) |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) |
| 135 | + } |
| 136 | + |
| 137 | + resp, _ := jsonpath.Get("$.Policies.Policy[*]", response) |
| 138 | + |
| 139 | + result, _ := resp.([]interface{}) |
| 140 | + for _, v := range result { |
| 141 | + item := v.(map[string]interface{}) |
| 142 | + if nameRegex != nil && !nameRegex.MatchString(fmt.Sprint(item["PolicyName"])) { |
| 143 | + continue |
| 144 | + } |
| 145 | + objects = append(objects, item) |
| 146 | + } |
| 147 | + |
| 148 | + if nextToken, ok := response["Marker"].(string); ok && nextToken != "" { |
| 149 | + request["Marker"] = nextToken |
| 150 | + } else { |
| 151 | + break |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + ids := make([]string, 0) |
| 156 | + names := make([]interface{}, 0) |
| 157 | + s := make([]map[string]interface{}, 0) |
| 158 | + for _, objectRaw := range objects { |
| 159 | + mapping := map[string]interface{}{} |
| 160 | + |
| 161 | + mapping["id"] = objectRaw[""] |
| 162 | + |
| 163 | + mapping["attachment_count"] = objectRaw["AttachmentCount"] |
| 164 | + mapping["create_time"] = objectRaw["CreateDate"] |
| 165 | + mapping["description"] = objectRaw["Description"] |
| 166 | + mapping["policy_name"] = objectRaw["PolicyName"] |
| 167 | + mapping["policy_type"] = objectRaw["PolicyType"] |
| 168 | + mapping["update_date"] = objectRaw["UpdateDate"] |
| 169 | + |
| 170 | + ids = append(ids, fmt.Sprint(mapping["id"])) |
| 171 | + names = append(names, objectRaw[""]) |
| 172 | + s = append(s, mapping) |
| 173 | + } |
| 174 | + |
| 175 | + d.SetId(dataResourceIdHash(ids)) |
| 176 | + if err := d.Set("ids", ids); err != nil { |
| 177 | + return WrapError(err) |
| 178 | + } |
| 179 | + |
| 180 | + if err := d.Set("names", names); err != nil { |
| 181 | + return WrapError(err) |
| 182 | + } |
| 183 | + if err := d.Set("policys", s); err != nil { |
| 184 | + return WrapError(err) |
| 185 | + } |
| 186 | + |
| 187 | + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { |
| 188 | + writeToFile(output.(string), s) |
| 189 | + } |
| 190 | + return nil |
| 191 | +} |
0 commit comments