|
| 1 | +package organization |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudIdentityCenterGroup() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudIdentityCenterGroupCreate, |
| 20 | + Read: resourceTencentCloudIdentityCenterGroupRead, |
| 21 | + Update: resourceTencentCloudIdentityCenterGroupUpdate, |
| 22 | + Delete: resourceTencentCloudIdentityCenterGroupDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "zone_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + Description: "Zone id.", |
| 31 | + }, |
| 32 | + |
| 33 | + "group_name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + Description: "The name of the user group. Format: Allow English letters, numbers and special characters-. Length: Maximum 128 characters.", |
| 37 | + }, |
| 38 | + |
| 39 | + "create_time": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "Creation time of the user group.", |
| 43 | + }, |
| 44 | + "group_type": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "Type of user group. `Manual`: manual creation, `Synchronized`: external import.", |
| 48 | + }, |
| 49 | + "update_time": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "Modification time for the user group.", |
| 53 | + }, |
| 54 | + "group_id": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "ID of the user group.", |
| 58 | + }, |
| 59 | + "member_count": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Computed: true, |
| 62 | + Description: "Number of team members.", |
| 63 | + }, |
| 64 | + "description": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + Computed: true, |
| 68 | + Description: "A description of the user group. Length: Maximum 1024 characters.", |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func resourceTencentCloudIdentityCenterGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 75 | + defer tccommon.LogElapsed("resource.tencentcloud_identity_center_group.create")() |
| 76 | + defer tccommon.InconsistentCheck(d, meta)() |
| 77 | + |
| 78 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 79 | + |
| 80 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 81 | + |
| 82 | + var ( |
| 83 | + zoneId string |
| 84 | + groupId string |
| 85 | + ) |
| 86 | + var ( |
| 87 | + request = organization.NewCreateGroupRequest() |
| 88 | + response = organization.NewCreateGroupResponse() |
| 89 | + ) |
| 90 | + |
| 91 | + if v, ok := d.GetOk("zone_id"); ok { |
| 92 | + zoneId = v.(string) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("zone_id"); ok { |
| 96 | + request.ZoneId = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("group_name"); ok { |
| 100 | + request.GroupName = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOk("description"); ok { |
| 104 | + request.Description = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 108 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().CreateGroupWithContext(ctx, request) |
| 109 | + if e != nil { |
| 110 | + return tccommon.RetryError(e) |
| 111 | + } else { |
| 112 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 113 | + } |
| 114 | + response = result |
| 115 | + return nil |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + log.Printf("[CRITAL]%s create identity center group failed, reason:%+v", logId, err) |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + groupId = *response.Response.GroupInfo.GroupId |
| 123 | + |
| 124 | + d.SetId(strings.Join([]string{zoneId, groupId}, tccommon.FILED_SP)) |
| 125 | + |
| 126 | + return resourceTencentCloudIdentityCenterGroupRead(d, meta) |
| 127 | +} |
| 128 | + |
| 129 | +func resourceTencentCloudIdentityCenterGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 130 | + defer tccommon.LogElapsed("resource.tencentcloud_identity_center_group.read")() |
| 131 | + defer tccommon.InconsistentCheck(d, meta)() |
| 132 | + |
| 133 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 134 | + |
| 135 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 136 | + |
| 137 | + service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 138 | + |
| 139 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 140 | + if len(idSplit) != 2 { |
| 141 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 142 | + } |
| 143 | + zoneId := idSplit[0] |
| 144 | + groupId := idSplit[1] |
| 145 | + |
| 146 | + _ = d.Set("zone_id", zoneId) |
| 147 | + |
| 148 | + respData, err := service.DescribeIdentityCenterGroupById(ctx, zoneId, groupId) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + if respData == nil { |
| 154 | + d.SetId("") |
| 155 | + log.Printf("[WARN]%s resource `identity_center_group` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 156 | + return nil |
| 157 | + } |
| 158 | + if respData.GroupName != nil { |
| 159 | + _ = d.Set("group_name", respData.GroupName) |
| 160 | + } |
| 161 | + |
| 162 | + if respData.Description != nil { |
| 163 | + _ = d.Set("description", respData.Description) |
| 164 | + } |
| 165 | + |
| 166 | + if respData.CreateTime != nil { |
| 167 | + _ = d.Set("create_time", respData.CreateTime) |
| 168 | + } |
| 169 | + |
| 170 | + if respData.GroupType != nil { |
| 171 | + _ = d.Set("group_type", respData.GroupType) |
| 172 | + } |
| 173 | + |
| 174 | + if respData.UpdateTime != nil { |
| 175 | + _ = d.Set("update_time", respData.UpdateTime) |
| 176 | + } |
| 177 | + |
| 178 | + if respData.GroupId != nil { |
| 179 | + _ = d.Set("group_id", respData.GroupId) |
| 180 | + } |
| 181 | + |
| 182 | + if respData.MemberCount != nil { |
| 183 | + _ = d.Set("member_count", respData.MemberCount) |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func resourceTencentCloudIdentityCenterGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 190 | + defer tccommon.LogElapsed("resource.tencentcloud_identity_center_group.update")() |
| 191 | + defer tccommon.InconsistentCheck(d, meta)() |
| 192 | + |
| 193 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 194 | + |
| 195 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 196 | + |
| 197 | + immutableArgs := []string{"zone_id"} |
| 198 | + for _, v := range immutableArgs { |
| 199 | + if d.HasChange(v) { |
| 200 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 201 | + } |
| 202 | + } |
| 203 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 204 | + if len(idSplit) != 2 { |
| 205 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 206 | + } |
| 207 | + zoneId := idSplit[0] |
| 208 | + groupId := idSplit[1] |
| 209 | + |
| 210 | + needChange := false |
| 211 | + mutableArgs := []string{"group_name", "description"} |
| 212 | + for _, v := range mutableArgs { |
| 213 | + if d.HasChange(v) { |
| 214 | + needChange = true |
| 215 | + break |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if needChange { |
| 220 | + request := organization.NewUpdateGroupRequest() |
| 221 | + |
| 222 | + request.ZoneId = helper.String(zoneId) |
| 223 | + |
| 224 | + request.GroupId = helper.String(groupId) |
| 225 | + |
| 226 | + if v, ok := d.GetOk("group_name"); ok { |
| 227 | + request.NewGroupName = helper.String(v.(string)) |
| 228 | + } |
| 229 | + |
| 230 | + if v, ok := d.GetOk("description"); ok { |
| 231 | + request.NewDescription = helper.String(v.(string)) |
| 232 | + } |
| 233 | + |
| 234 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 235 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().UpdateGroupWithContext(ctx, request) |
| 236 | + if e != nil { |
| 237 | + return tccommon.RetryError(e) |
| 238 | + } else { |
| 239 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 240 | + } |
| 241 | + return nil |
| 242 | + }) |
| 243 | + if err != nil { |
| 244 | + log.Printf("[CRITAL]%s update identity center group failed, reason:%+v", logId, err) |
| 245 | + return err |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return resourceTencentCloudIdentityCenterGroupRead(d, meta) |
| 250 | +} |
| 251 | + |
| 252 | +func resourceTencentCloudIdentityCenterGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 253 | + defer tccommon.LogElapsed("resource.tencentcloud_identity_center_group.delete")() |
| 254 | + defer tccommon.InconsistentCheck(d, meta)() |
| 255 | + |
| 256 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 257 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 258 | + |
| 259 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 260 | + if len(idSplit) != 2 { |
| 261 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 262 | + } |
| 263 | + zoneId := idSplit[0] |
| 264 | + groupId := idSplit[1] |
| 265 | + |
| 266 | + var ( |
| 267 | + request = organization.NewDeleteGroupRequest() |
| 268 | + response = organization.NewDeleteGroupResponse() |
| 269 | + ) |
| 270 | + |
| 271 | + request.ZoneId = helper.String(zoneId) |
| 272 | + |
| 273 | + request.GroupId = helper.String(groupId) |
| 274 | + |
| 275 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 276 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().DeleteGroupWithContext(ctx, request) |
| 277 | + if e != nil { |
| 278 | + return tccommon.RetryError(e) |
| 279 | + } else { |
| 280 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 281 | + } |
| 282 | + response = result |
| 283 | + return nil |
| 284 | + }) |
| 285 | + if err != nil { |
| 286 | + log.Printf("[CRITAL]%s delete identity center group failed, reason:%+v", logId, err) |
| 287 | + return err |
| 288 | + } |
| 289 | + |
| 290 | + _ = response |
| 291 | + return nil |
| 292 | +} |
0 commit comments