|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dlc describe_user_roles |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dlc_describe_user_roles" "describe_user_roles" { |
| 8 | + fuzzy = "1" |
| 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 | + dlc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dlc/v20210125" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudDlcDescribeUserRoles() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudDlcDescribeUserRolesRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "fuzzy": { |
| 28 | + Optional: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "List according to ARN blur.", |
| 31 | + }, |
| 32 | + |
| 33 | + "sort_by": { |
| 34 | + Optional: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "The return results are sorted according to this field.", |
| 37 | + }, |
| 38 | + |
| 39 | + "sorting": { |
| 40 | + Optional: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "Positive or inverted, such as DESC.", |
| 43 | + }, |
| 44 | + |
| 45 | + "user_roles": { |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeList, |
| 48 | + Description: "User role information.", |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "role_id": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "Character ID.", |
| 55 | + }, |
| 56 | + "app_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "User app ID.", |
| 60 | + }, |
| 61 | + "uin": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "User ID.", |
| 65 | + }, |
| 66 | + "arn": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Role Permissions.", |
| 70 | + }, |
| 71 | + "modify_time": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Recently modify the time stamp.", |
| 75 | + }, |
| 76 | + "desc": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Character description information.", |
| 80 | + }, |
| 81 | + "role_name": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "Role NameNote: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 85 | + }, |
| 86 | + "creator": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "Creator UinNote: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 90 | + }, |
| 91 | + "cos_permission_list": { |
| 92 | + Type: schema.TypeList, |
| 93 | + Computed: true, |
| 94 | + Description: "COS authorization path listNote: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 95 | + Elem: &schema.Resource{ |
| 96 | + Schema: map[string]*schema.Schema{ |
| 97 | + "cos_path": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "COS pathNote: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 101 | + }, |
| 102 | + "permissions": { |
| 103 | + Type: schema.TypeSet, |
| 104 | + Elem: &schema.Schema{ |
| 105 | + Type: schema.TypeString, |
| 106 | + }, |
| 107 | + Computed: true, |
| 108 | + Description: "Permissions [Read, WRITE]Note: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + "permission_json": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "CAM strategy jsonNote: This field may return NULL, indicating that the valid value cannot be obtained.", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + |
| 122 | + "result_output_file": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Optional: true, |
| 125 | + Description: "Used to save results.", |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func dataSourceTencentCloudDlcDescribeUserRolesRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + defer logElapsed("data_source.tencentcloud_dlc_describe_user_roles.read")() |
| 133 | + defer inconsistentCheck(d, meta)() |
| 134 | + |
| 135 | + logId := getLogId(contextNil) |
| 136 | + |
| 137 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 138 | + |
| 139 | + paramMap := make(map[string]interface{}) |
| 140 | + if v, ok := d.GetOk("fuzzy"); ok { |
| 141 | + paramMap["Fuzzy"] = helper.String(v.(string)) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOk("sort_by"); ok { |
| 145 | + paramMap["SortBy"] = helper.String(v.(string)) |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("sorting"); ok { |
| 149 | + paramMap["Sorting"] = helper.String(v.(string)) |
| 150 | + } |
| 151 | + |
| 152 | + service := DlcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 153 | + |
| 154 | + var userRoles []*dlc.UserRole |
| 155 | + |
| 156 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 157 | + result, e := service.DescribeDlcDescribeUserRolesByFilter(ctx, paramMap) |
| 158 | + if e != nil { |
| 159 | + return retryError(e) |
| 160 | + } |
| 161 | + userRoles = result |
| 162 | + return nil |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + ids := make([]string, 0, len(userRoles)) |
| 169 | + tmpList := make([]map[string]interface{}, 0, len(userRoles)) |
| 170 | + |
| 171 | + if userRoles != nil { |
| 172 | + for _, userRole := range userRoles { |
| 173 | + userRoleMap := map[string]interface{}{} |
| 174 | + |
| 175 | + if userRole.RoleId != nil { |
| 176 | + userRoleMap["role_id"] = userRole.RoleId |
| 177 | + } |
| 178 | + |
| 179 | + if userRole.AppId != nil { |
| 180 | + userRoleMap["app_id"] = userRole.AppId |
| 181 | + } |
| 182 | + |
| 183 | + if userRole.Uin != nil { |
| 184 | + userRoleMap["uin"] = userRole.Uin |
| 185 | + } |
| 186 | + |
| 187 | + if userRole.Arn != nil { |
| 188 | + userRoleMap["arn"] = userRole.Arn |
| 189 | + } |
| 190 | + |
| 191 | + if userRole.ModifyTime != nil { |
| 192 | + userRoleMap["modify_time"] = userRole.ModifyTime |
| 193 | + } |
| 194 | + |
| 195 | + if userRole.Desc != nil { |
| 196 | + userRoleMap["desc"] = userRole.Desc |
| 197 | + } |
| 198 | + |
| 199 | + if userRole.RoleName != nil { |
| 200 | + userRoleMap["role_name"] = userRole.RoleName |
| 201 | + } |
| 202 | + |
| 203 | + if userRole.Creator != nil { |
| 204 | + userRoleMap["creator"] = userRole.Creator |
| 205 | + } |
| 206 | + |
| 207 | + if userRole.CosPermissionList != nil { |
| 208 | + var cosPermissionListList []interface{} |
| 209 | + for _, cosPermissionList := range userRole.CosPermissionList { |
| 210 | + cosPermissionListMap := map[string]interface{}{} |
| 211 | + |
| 212 | + if cosPermissionList.CosPath != nil { |
| 213 | + cosPermissionListMap["cos_path"] = cosPermissionList.CosPath |
| 214 | + } |
| 215 | + |
| 216 | + if cosPermissionList.Permissions != nil { |
| 217 | + cosPermissionListMap["permissions"] = cosPermissionList.Permissions |
| 218 | + } |
| 219 | + |
| 220 | + cosPermissionListList = append(cosPermissionListList, cosPermissionListMap) |
| 221 | + } |
| 222 | + |
| 223 | + userRoleMap["cos_permission_list"] = cosPermissionListList |
| 224 | + } |
| 225 | + |
| 226 | + if userRole.PermissionJson != nil { |
| 227 | + userRoleMap["permission_json"] = userRole.PermissionJson |
| 228 | + } |
| 229 | + |
| 230 | + ids = append(ids, helper.Int64ToStr(*userRole.RoleId)) |
| 231 | + tmpList = append(tmpList, userRoleMap) |
| 232 | + } |
| 233 | + |
| 234 | + _ = d.Set("user_roles", tmpList) |
| 235 | + } |
| 236 | + |
| 237 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 238 | + output, ok := d.GetOk("result_output_file") |
| 239 | + if ok && output.(string) != "" { |
| 240 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 241 | + return e |
| 242 | + } |
| 243 | + } |
| 244 | + return nil |
| 245 | +} |
0 commit comments