|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cam list_entities_for_policy |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cam_list_entities_for_policy" "list_entities_for_policy" { |
| 8 | + policy_id = 1 |
| 9 | + entity_filter = "All" |
| 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 dataSourceTencentCloudCamListEntitiesForPolicy() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCamListEntitiesForPolicyRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "policy_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeInt, |
| 31 | + Description: "Policy Id.", |
| 32 | + }, |
| 33 | + |
| 34 | + "rp": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeInt, |
| 37 | + Description: "Per page size, default value is 20.", |
| 38 | + }, |
| 39 | + |
| 40 | + "entity_filter": { |
| 41 | + Optional: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Can take values of 'All', 'User', 'Group', and 'Role'. 'All' represents obtaining all entity types, 'User' represents only obtaining sub accounts, 'Group' represents only obtaining user groups, and 'Role' represents only obtaining roles. The default value is' All '.", |
| 44 | + }, |
| 45 | + |
| 46 | + "list": { |
| 47 | + Computed: true, |
| 48 | + Type: schema.TypeList, |
| 49 | + Description: "Entity ListNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "id": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Entity ID.", |
| 56 | + }, |
| 57 | + "name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Entity NameNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 61 | + }, |
| 62 | + "uin": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Computed: true, |
| 65 | + Description: "Entity UinNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 66 | + }, |
| 67 | + "related_type": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + Description: "Association type. 1. User association; 2 User Group Association.", |
| 71 | + }, |
| 72 | + "attachment_time": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Policy association timeNote: This field may return null, indicating that a valid value cannot be obtained.", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + "result_output_file": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Optional: true, |
| 84 | + Description: "Used to save results.", |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func dataSourceTencentCloudCamListEntitiesForPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer logElapsed("data_source.tencentcloud_cam_list_entities_for_policy.read")() |
| 92 | + defer inconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + logId := getLogId(contextNil) |
| 95 | + |
| 96 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 97 | + |
| 98 | + paramMap := make(map[string]interface{}) |
| 99 | + if v, _ := d.GetOk("policy_id"); v != nil { |
| 100 | + paramMap["PolicyId"] = helper.IntUint64(v.(int)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, _ := d.GetOk("rp"); v != nil { |
| 104 | + paramMap["Rp"] = helper.IntUint64(v.(int)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOk("entity_filter"); ok { |
| 108 | + paramMap["EntityFilter"] = helper.String(v.(string)) |
| 109 | + } |
| 110 | + |
| 111 | + service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 112 | + |
| 113 | + var listEntitiesForPolicy []*cam.AttachEntityOfPolicy |
| 114 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 115 | + result, e := service.DescribeCamListEntitiesForPolicyByFilter(ctx, paramMap) |
| 116 | + if e != nil { |
| 117 | + return retryError(e) |
| 118 | + } |
| 119 | + listEntitiesForPolicy = result |
| 120 | + return nil |
| 121 | + }) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + ids := make([]string, 0, len(listEntitiesForPolicy)) |
| 127 | + tmpList := make([]map[string]interface{}, 0) |
| 128 | + |
| 129 | + if listEntitiesForPolicy != nil { |
| 130 | + for _, attachEntityOfPolicy := range listEntitiesForPolicy { |
| 131 | + attachEntityOfPolicyMap := map[string]interface{}{} |
| 132 | + |
| 133 | + if attachEntityOfPolicy.Id != nil { |
| 134 | + attachEntityOfPolicyMap["id"] = attachEntityOfPolicy.Id |
| 135 | + } |
| 136 | + |
| 137 | + if attachEntityOfPolicy.Name != nil { |
| 138 | + attachEntityOfPolicyMap["name"] = attachEntityOfPolicy.Name |
| 139 | + } |
| 140 | + |
| 141 | + if attachEntityOfPolicy.Uin != nil { |
| 142 | + attachEntityOfPolicyMap["uin"] = attachEntityOfPolicy.Uin |
| 143 | + } |
| 144 | + |
| 145 | + if attachEntityOfPolicy.RelatedType != nil { |
| 146 | + attachEntityOfPolicyMap["related_type"] = attachEntityOfPolicy.RelatedType |
| 147 | + } |
| 148 | + |
| 149 | + if attachEntityOfPolicy.AttachmentTime != nil { |
| 150 | + attachEntityOfPolicyMap["attachment_time"] = attachEntityOfPolicy.AttachmentTime |
| 151 | + } |
| 152 | + |
| 153 | + ids = append(ids, helper.UInt64ToStr(*attachEntityOfPolicy.Uin)) |
| 154 | + tmpList = append(tmpList, attachEntityOfPolicyMap) |
| 155 | + } |
| 156 | + |
| 157 | + _ = d.Set("list", tmpList) |
| 158 | + } |
| 159 | + |
| 160 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 161 | + output, ok := d.GetOk("result_output_file") |
| 162 | + if ok && output.(string) != "" { |
| 163 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 164 | + return e |
| 165 | + } |
| 166 | + } |
| 167 | + return nil |
| 168 | +} |
0 commit comments