|
| 1 | +package tco |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudIdentityCenterGroups() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudIdentityCenterGroupsRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "zone_id": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "Space ID.", |
| 22 | + }, |
| 23 | + |
| 24 | + "filter": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Description: "Filter criterion. Format: <Attribute> <Operator> <Value>, case-insensitive. Currently, <Attribute> supports only GroupName, and <Operator> supports only eq (Equals) and sw (Start With). For example, Filter = \"GroupName sw test\" indicates querying all user groups with names starting with test; Filter = \"GroupName eq testgroup\" indicates querying the user group with the name testgroup.", |
| 28 | + }, |
| 29 | + |
| 30 | + "group_type": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "User group type. Manual: manually created; Synchronized: externally imported.", |
| 34 | + }, |
| 35 | + |
| 36 | + "filter_users": { |
| 37 | + Type: schema.TypeSet, |
| 38 | + Optional: true, |
| 39 | + Description: "Filtered user. IsSelected=1 will be returned for the user group associated with this user.", |
| 40 | + Elem: &schema.Schema{ |
| 41 | + Type: schema.TypeString, |
| 42 | + }, |
| 43 | + }, |
| 44 | + |
| 45 | + "sort_field": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + Description: "Sorting field, which currently only supports CreateTime. The default is the CreateTime field.", |
| 49 | + }, |
| 50 | + |
| 51 | + "sort_type": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Sorting type. Desc: descending order; Asc: ascending order. It should be set along with SortField.", |
| 55 | + }, |
| 56 | + |
| 57 | + "groups": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Description: "User group list.", |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "group_name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + Description: "User group name.", |
| 67 | + }, |
| 68 | + "description": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Description: "User group description.", |
| 72 | + }, |
| 73 | + "create_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Optional: true, |
| 76 | + Description: "Creation time of the user group.", |
| 77 | + }, |
| 78 | + "group_type": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Optional: true, |
| 81 | + Description: "User group type. Manual: manually created; Synchronized: externally imported.", |
| 82 | + }, |
| 83 | + "update_time": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Optional: true, |
| 86 | + Description: "Modification time of the user group.", |
| 87 | + }, |
| 88 | + "group_id": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Optional: true, |
| 91 | + Description: "User group ID.", |
| 92 | + }, |
| 93 | + "member_count": { |
| 94 | + Type: schema.TypeInt, |
| 95 | + Optional: true, |
| 96 | + Description: "Number of group members.", |
| 97 | + }, |
| 98 | + "is_selected": { |
| 99 | + Type: schema.TypeBool, |
| 100 | + Optional: true, |
| 101 | + Description: "If the input parameter FilterUsers is provided, return true when the user is in the user group; otherwise, return false.", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + |
| 107 | + "result_output_file": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Optional: true, |
| 110 | + Description: "Used to save results.", |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func dataSourceTencentCloudIdentityCenterGroupsRead(d *schema.ResourceData, meta interface{}) error { |
| 117 | + defer tccommon.LogElapsed("data_source.tencentcloud_identity_center_groups.read")() |
| 118 | + defer tccommon.InconsistentCheck(d, meta)() |
| 119 | + |
| 120 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 121 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 122 | + |
| 123 | + service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 124 | + |
| 125 | + paramMap := make(map[string]interface{}) |
| 126 | + if v, ok := d.GetOk("zone_id"); ok { |
| 127 | + paramMap["ZoneId"] = helper.String(v.(string)) |
| 128 | + } |
| 129 | + |
| 130 | + if v, ok := d.GetOk("filter"); ok { |
| 131 | + paramMap["Filter"] = helper.String(v.(string)) |
| 132 | + } |
| 133 | + |
| 134 | + if v, ok := d.GetOk("group_type"); ok { |
| 135 | + paramMap["GroupType"] = helper.String(v.(string)) |
| 136 | + } |
| 137 | + |
| 138 | + if v, ok := d.GetOk("filter_users"); ok { |
| 139 | + filterUsersList := []*string{} |
| 140 | + filterUsersSet := v.(*schema.Set).List() |
| 141 | + for i := range filterUsersSet { |
| 142 | + filterUsers := filterUsersSet[i].(string) |
| 143 | + filterUsersList = append(filterUsersList, helper.String(filterUsers)) |
| 144 | + } |
| 145 | + paramMap["FilterUsers"] = filterUsersList |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("sort_field"); ok { |
| 149 | + paramMap["SortField"] = helper.String(v.(string)) |
| 150 | + } |
| 151 | + |
| 152 | + if v, ok := d.GetOk("sort_type"); ok { |
| 153 | + paramMap["SortType"] = helper.String(v.(string)) |
| 154 | + } |
| 155 | + |
| 156 | + var groups []*organization.GroupInfo |
| 157 | + |
| 158 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 159 | + result, e := service.DescribeIdentityCenterGroupsByFilter(ctx, paramMap) |
| 160 | + if e != nil { |
| 161 | + return tccommon.RetryError(e) |
| 162 | + } |
| 163 | + groups = result |
| 164 | + return nil |
| 165 | + }) |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + groupsList := make([]map[string]interface{}, 0, len(groups)) |
| 171 | + ids := make([]string, 0, len(groups)) |
| 172 | + for _, group := range groups { |
| 173 | + groupsMap := map[string]interface{}{} |
| 174 | + |
| 175 | + if group.GroupName != nil { |
| 176 | + groupsMap["group_name"] = group.GroupName |
| 177 | + } |
| 178 | + |
| 179 | + if group.Description != nil { |
| 180 | + groupsMap["description"] = group.Description |
| 181 | + } |
| 182 | + |
| 183 | + if group.CreateTime != nil { |
| 184 | + groupsMap["create_time"] = group.CreateTime |
| 185 | + } |
| 186 | + |
| 187 | + if group.GroupType != nil { |
| 188 | + groupsMap["group_type"] = group.GroupType |
| 189 | + } |
| 190 | + |
| 191 | + if group.UpdateTime != nil { |
| 192 | + groupsMap["update_time"] = group.UpdateTime |
| 193 | + } |
| 194 | + |
| 195 | + if group.GroupId != nil { |
| 196 | + groupsMap["group_id"] = group.GroupId |
| 197 | + ids = append(ids, *group.GroupId) |
| 198 | + } |
| 199 | + |
| 200 | + if group.MemberCount != nil { |
| 201 | + groupsMap["member_count"] = group.MemberCount |
| 202 | + } |
| 203 | + |
| 204 | + if group.IsSelected != nil { |
| 205 | + groupsMap["is_selected"] = group.IsSelected |
| 206 | + } |
| 207 | + |
| 208 | + groupsList = append(groupsList, groupsMap) |
| 209 | + } |
| 210 | + |
| 211 | + _ = d.Set("groups", groupsList) |
| 212 | + |
| 213 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 214 | + |
| 215 | + output, ok := d.GetOk("result_output_file") |
| 216 | + if ok && output.(string) != "" { |
| 217 | + if e := tccommon.WriteToFile(output.(string), groupsList); e != nil { |
| 218 | + return e |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return nil |
| 223 | +} |
0 commit comments