|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cam group_user_account |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cam_group_user_account" "group_user_account" { |
| 8 | + sub_uin = 100033690181 |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudCamGroupUserAccount() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudCamGroupUserAccountRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "uid": { |
| 28 | + Optional: true, |
| 29 | + Type: schema.TypeInt, |
| 30 | + Description: "Sub-user uid.", |
| 31 | + }, |
| 32 | + |
| 33 | + "rp": { |
| 34 | + Optional: true, |
| 35 | + Type: schema.TypeInt, |
| 36 | + Description: "Number per page. The default is 20.", |
| 37 | + }, |
| 38 | + |
| 39 | + "sub_uin": { |
| 40 | + Optional: true, |
| 41 | + Type: schema.TypeInt, |
| 42 | + Description: "Sub-user uin.", |
| 43 | + }, |
| 44 | + |
| 45 | + "total_num": { |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeInt, |
| 48 | + Description: "The total number of user groups the sub-user has joined.", |
| 49 | + }, |
| 50 | + |
| 51 | + "group_info": { |
| 52 | + Computed: true, |
| 53 | + Type: schema.TypeList, |
| 54 | + Description: "User group information.", |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "group_id": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + Description: "User group ID.", |
| 61 | + }, |
| 62 | + "group_name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "User group name.", |
| 66 | + }, |
| 67 | + "create_time": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Create time.", |
| 71 | + }, |
| 72 | + "remark": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Remark.", |
| 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 dataSourceTencentCloudCamGroupUserAccountRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer logElapsed("data_source.tencentcloud_cam_group_user_account.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, ok := d.GetOkExists("uid"); ok { |
| 100 | + paramMap["Uid"] = helper.IntUint64(v.(int)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOkExists("rp"); ok { |
| 104 | + paramMap["Rp"] = helper.IntUint64(v.(int)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOkExists("sub_uin"); ok { |
| 108 | + paramMap["SubUin"] = helper.IntUint64(v.(int)) |
| 109 | + } |
| 110 | + |
| 111 | + service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 112 | + |
| 113 | + var groupInfoList []*cam.GroupInfo |
| 114 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 115 | + result, e := service.DescribeCamGroupUserAccountByFilter(ctx, paramMap) |
| 116 | + if e != nil { |
| 117 | + return retryError(e) |
| 118 | + } |
| 119 | + groupInfoList = result |
| 120 | + return nil |
| 121 | + }) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + ids := make([]string, 0, len(groupInfoList)) |
| 127 | + tmpList := make([]map[string]interface{}, 0, len(groupInfoList)) |
| 128 | + |
| 129 | + if len(groupInfoList) > 0 { |
| 130 | + _ = d.Set("total_num", len(groupInfoList)) |
| 131 | + } |
| 132 | + |
| 133 | + if groupInfoList != nil { |
| 134 | + for _, groupInfo := range groupInfoList { |
| 135 | + groupInfoMap := map[string]interface{}{} |
| 136 | + |
| 137 | + if groupInfo.GroupId != nil { |
| 138 | + groupInfoMap["group_id"] = groupInfo.GroupId |
| 139 | + } |
| 140 | + |
| 141 | + if groupInfo.GroupName != nil { |
| 142 | + groupInfoMap["group_name"] = groupInfo.GroupName |
| 143 | + } |
| 144 | + |
| 145 | + if groupInfo.CreateTime != nil { |
| 146 | + groupInfoMap["create_time"] = groupInfo.CreateTime |
| 147 | + } |
| 148 | + |
| 149 | + if groupInfo.Remark != nil { |
| 150 | + groupInfoMap["remark"] = groupInfo.Remark |
| 151 | + } |
| 152 | + |
| 153 | + ids = append(ids, helper.UInt64ToStr(*groupInfo.GroupId)) |
| 154 | + tmpList = append(tmpList, groupInfoMap) |
| 155 | + } |
| 156 | + |
| 157 | + _ = d.Set("group_info", 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