|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cam list_attached_user_policy |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cam_list_attached_user_policy" "list_attached_user_policy" { |
| 8 | + target_uin = 100032767426 |
| 9 | + attach_type = 0 |
| 10 | + } |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudCamListAttachedUserPolicy() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCamListAttachedUserPolicyRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "target_uin": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeInt, |
| 31 | + Description: "Target User ID.", |
| 32 | + }, |
| 33 | + |
| 34 | + "attach_type": { |
| 35 | + Required: true, |
| 36 | + Type: schema.TypeInt, |
| 37 | + Description: "0: Return direct association and group association policies, 1: Only return direct association policies, 2: Only return group association policies.", |
| 38 | + }, |
| 39 | + |
| 40 | + "strategy_type": { |
| 41 | + Optional: true, |
| 42 | + Type: schema.TypeInt, |
| 43 | + Description: "Policy type.", |
| 44 | + }, |
| 45 | + |
| 46 | + "keyword": { |
| 47 | + Optional: true, |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "Search Keywords.", |
| 50 | + }, |
| 51 | + |
| 52 | + "policy_list": { |
| 53 | + Computed: true, |
| 54 | + Type: schema.TypeList, |
| 55 | + Description: "Policy List Data.", |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "policy_id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Policy ID.", |
| 62 | + }, |
| 63 | + "policy_name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Policy Name.", |
| 67 | + }, |
| 68 | + "description": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Policy Description.", |
| 72 | + }, |
| 73 | + "add_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Creation time.", |
| 77 | + }, |
| 78 | + "strategy_type": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Policy type (1 represents custom policy, 2 represents preset policy).", |
| 82 | + }, |
| 83 | + "create_mode": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "Creation mode (1 represents policies created by product or project permissions, others represent policies created by policy syntax).", |
| 87 | + }, |
| 88 | + "groups": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Computed: true, |
| 91 | + Description: "Associated information with groupNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 92 | + Elem: &schema.Resource{ |
| 93 | + Schema: map[string]*schema.Schema{ |
| 94 | + "group_id": { |
| 95 | + Type: schema.TypeInt, |
| 96 | + Computed: true, |
| 97 | + Description: "Group ID.", |
| 98 | + }, |
| 99 | + "group_name": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + Description: "Group Name.", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + "deactived": { |
| 108 | + Type: schema.TypeInt, |
| 109 | + Computed: true, |
| 110 | + Description: "Has it been taken offline (0: No 1: Yes)Note: This field may return null, indicating that a valid value cannot be obtained.", |
| 111 | + }, |
| 112 | + "deactived_detail": { |
| 113 | + Type: schema.TypeSet, |
| 114 | + Elem: &schema.Schema{ |
| 115 | + Type: schema.TypeString, |
| 116 | + }, |
| 117 | + Computed: true, |
| 118 | + Description: "List of offline productsNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + |
| 124 | + "result_output_file": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Optional: true, |
| 127 | + Description: "Used to save results.", |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func dataSourceTencentCloudCamListAttachedUserPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 134 | + defer logElapsed("data_source.tencentcloud_cam_list_attached_user_policy.read")() |
| 135 | + defer inconsistentCheck(d, meta)() |
| 136 | + |
| 137 | + logId := getLogId(contextNil) |
| 138 | + |
| 139 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 140 | + |
| 141 | + paramMap := make(map[string]interface{}) |
| 142 | + if v, _ := d.GetOk("target_uin"); v != nil { |
| 143 | + paramMap["TargetUin"] = helper.IntUint64(v.(int)) |
| 144 | + } |
| 145 | + |
| 146 | + if v, _ := d.GetOk("attach_type"); v != nil { |
| 147 | + paramMap["AttachType"] = helper.IntUint64(v.(int)) |
| 148 | + } |
| 149 | + |
| 150 | + if v, _ := d.GetOk("strategy_type"); v != nil { |
| 151 | + paramMap["StrategyType"] = helper.IntUint64(v.(int)) |
| 152 | + } |
| 153 | + |
| 154 | + if v, ok := d.GetOk("keyword"); ok { |
| 155 | + paramMap["Keyword"] = helper.String(v.(string)) |
| 156 | + } |
| 157 | + |
| 158 | + service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 159 | + |
| 160 | + var policyList []*cam.AttachedUserPolicy |
| 161 | + |
| 162 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 163 | + result, e := service.DescribeCamListAttachedUserPolicyByFilter(ctx, paramMap) |
| 164 | + if e != nil { |
| 165 | + return retryError(e) |
| 166 | + } |
| 167 | + policyList = result |
| 168 | + return nil |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + ids := make([]string, 0, len(policyList)) |
| 175 | + tmpList := make([]map[string]interface{}, 0, len(policyList)) |
| 176 | + |
| 177 | + if policyList != nil { |
| 178 | + for _, attachedUserPolicy := range policyList { |
| 179 | + attachedUserPolicyMap := map[string]interface{}{} |
| 180 | + |
| 181 | + if attachedUserPolicy.PolicyId != nil { |
| 182 | + attachedUserPolicyMap["policy_id"] = attachedUserPolicy.PolicyId |
| 183 | + } |
| 184 | + |
| 185 | + if attachedUserPolicy.PolicyName != nil { |
| 186 | + attachedUserPolicyMap["policy_name"] = attachedUserPolicy.PolicyName |
| 187 | + } |
| 188 | + |
| 189 | + if attachedUserPolicy.Description != nil { |
| 190 | + attachedUserPolicyMap["description"] = attachedUserPolicy.Description |
| 191 | + } |
| 192 | + |
| 193 | + if attachedUserPolicy.AddTime != nil { |
| 194 | + attachedUserPolicyMap["add_time"] = attachedUserPolicy.AddTime |
| 195 | + } |
| 196 | + |
| 197 | + if attachedUserPolicy.StrategyType != nil { |
| 198 | + attachedUserPolicyMap["strategy_type"] = attachedUserPolicy.StrategyType |
| 199 | + } |
| 200 | + |
| 201 | + if attachedUserPolicy.CreateMode != nil { |
| 202 | + attachedUserPolicyMap["create_mode"] = attachedUserPolicy.CreateMode |
| 203 | + } |
| 204 | + |
| 205 | + if attachedUserPolicy.Groups != nil { |
| 206 | + groupsList := []interface{}{} |
| 207 | + for _, groups := range attachedUserPolicy.Groups { |
| 208 | + groupsMap := map[string]interface{}{} |
| 209 | + |
| 210 | + if groups.GroupId != nil { |
| 211 | + groupsMap["group_id"] = groups.GroupId |
| 212 | + } |
| 213 | + |
| 214 | + if groups.GroupName != nil { |
| 215 | + groupsMap["group_name"] = groups.GroupName |
| 216 | + } |
| 217 | + |
| 218 | + groupsList = append(groupsList, groupsMap) |
| 219 | + } |
| 220 | + |
| 221 | + attachedUserPolicyMap["groups"] = groupsList |
| 222 | + } |
| 223 | + |
| 224 | + if attachedUserPolicy.Deactived != nil { |
| 225 | + attachedUserPolicyMap["deactived"] = attachedUserPolicy.Deactived |
| 226 | + } |
| 227 | + |
| 228 | + if attachedUserPolicy.DeactivedDetail != nil { |
| 229 | + attachedUserPolicyMap["deactived_detail"] = attachedUserPolicy.DeactivedDetail |
| 230 | + } |
| 231 | + |
| 232 | + ids = append(ids, *attachedUserPolicy.PolicyId) |
| 233 | + tmpList = append(tmpList, attachedUserPolicyMap) |
| 234 | + } |
| 235 | + |
| 236 | + _ = d.Set("policy_list", tmpList) |
| 237 | + } |
| 238 | + |
| 239 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 240 | + output, ok := d.GetOk("result_output_file") |
| 241 | + if ok && output.(string) != "" { |
| 242 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 243 | + return e |
| 244 | + } |
| 245 | + } |
| 246 | + return nil |
| 247 | +} |
0 commit comments