|
| 1 | +/* |
| 2 | +Provides a resource to create a ciam user group |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_ciam_user_store" "user_store" { |
| 8 | + user_pool_name = "tf_user_store" |
| 9 | + user_pool_desc = "for terraform test" |
| 10 | + user_pool_logo = "https://ciam-prd-1302490086.cos.ap-guangzhou.myqcloud.com/temporary/92630252a2c5422d9663db5feafd619b.png" |
| 11 | +} |
| 12 | +
|
| 13 | +resource "tencentcloud_ciam_user_group" "user_group" { |
| 14 | + display_name = "tf_user_group" |
| 15 | + user_store_id = tencentcloud_ciam_user_store.user_store.id |
| 16 | + description = "for terrafrom test" |
| 17 | +} |
| 18 | +``` |
| 19 | +
|
| 20 | +Import |
| 21 | +
|
| 22 | +ciam user_group can be imported using the id, e.g. |
| 23 | +
|
| 24 | +``` |
| 25 | +terraform import tencentcloud_ciam_user_group.user_group userStoreId#userGroupId |
| 26 | +``` |
| 27 | +*/ |
| 28 | +package tencentcloud |
| 29 | + |
| 30 | +import ( |
| 31 | + "context" |
| 32 | + "fmt" |
| 33 | + "log" |
| 34 | + "strings" |
| 35 | + |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 37 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 38 | + ciam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam/v20220331" |
| 39 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 40 | +) |
| 41 | + |
| 42 | +func resourceTencentCloudCiamUserGroup() *schema.Resource { |
| 43 | + return &schema.Resource{ |
| 44 | + Create: resourceTencentCloudCiamUserGroupCreate, |
| 45 | + Read: resourceTencentCloudCiamUserGroupRead, |
| 46 | + Update: resourceTencentCloudCiamUserGroupUpdate, |
| 47 | + Delete: resourceTencentCloudCiamUserGroupDelete, |
| 48 | + Importer: &schema.ResourceImporter{ |
| 49 | + State: schema.ImportStatePassthrough, |
| 50 | + }, |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "user_store_id": { |
| 53 | + Required: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "User Store ID.", |
| 56 | + }, |
| 57 | + "display_name": { |
| 58 | + Required: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "User Group Name.", |
| 61 | + }, |
| 62 | + "description": { |
| 63 | + Optional: true, |
| 64 | + Type: schema.TypeString, |
| 65 | + Description: "User Group Description.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func resourceTencentCloudCiamUserGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 72 | + defer logElapsed("resource.tencentcloud_ciam_user_group.create")() |
| 73 | + defer inconsistentCheck(d, meta)() |
| 74 | + |
| 75 | + logId := getLogId(contextNil) |
| 76 | + |
| 77 | + var ( |
| 78 | + request = ciam.NewCreateUserGroupRequest() |
| 79 | + response = ciam.NewCreateUserGroupResponse() |
| 80 | + userStoreId string |
| 81 | + userGroupId string |
| 82 | + ) |
| 83 | + if v, ok := d.GetOk("user_store_id"); ok { |
| 84 | + userStoreId = v.(string) |
| 85 | + request.UserStoreId = helper.String(v.(string)) |
| 86 | + } |
| 87 | + |
| 88 | + if v, ok := d.GetOk("display_name"); ok { |
| 89 | + request.DisplayName = helper.String(v.(string)) |
| 90 | + } |
| 91 | + |
| 92 | + if v, ok := d.GetOk("description"); ok { |
| 93 | + request.Description = helper.String(v.(string)) |
| 94 | + } |
| 95 | + |
| 96 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 97 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCiamClient().CreateUserGroup(request) |
| 98 | + if e != nil { |
| 99 | + return retryError(e) |
| 100 | + } else { |
| 101 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 102 | + } |
| 103 | + response = result |
| 104 | + return nil |
| 105 | + }) |
| 106 | + if err != nil { |
| 107 | + log.Printf("[CRITAL]%s create ciam userGroup failed, reason:%+v", logId, err) |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + userGroupId = *response.Response.UserGroupId |
| 112 | + |
| 113 | + d.SetId(userStoreId + FILED_SP + userGroupId) |
| 114 | + |
| 115 | + return resourceTencentCloudCiamUserGroupRead(d, meta) |
| 116 | +} |
| 117 | + |
| 118 | +func resourceTencentCloudCiamUserGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 119 | + defer logElapsed("resource.tencentcloud_ciam_user_group.read")() |
| 120 | + defer inconsistentCheck(d, meta)() |
| 121 | + |
| 122 | + logId := getLogId(contextNil) |
| 123 | + |
| 124 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 125 | + |
| 126 | + service := CiamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 127 | + |
| 128 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 129 | + if len(idSplit) != 2 { |
| 130 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 131 | + } |
| 132 | + userStoreId := idSplit[0] |
| 133 | + userGroupId := idSplit[1] |
| 134 | + |
| 135 | + userGroup, err := service.DescribeCiamUserGroupById(ctx, userStoreId, userGroupId) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + if userGroup == nil { |
| 141 | + d.SetId("") |
| 142 | + log.Printf("[WARN]%s resource `CiamUserGroup` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + if userGroup.DisplayName != nil { |
| 147 | + _ = d.Set("display_name", userGroup.DisplayName) |
| 148 | + } |
| 149 | + |
| 150 | + if userGroup.UserStoreId != nil { |
| 151 | + _ = d.Set("user_store_id", userGroup.UserStoreId) |
| 152 | + } |
| 153 | + |
| 154 | + if userGroup.Description != nil { |
| 155 | + _ = d.Set("description", userGroup.Description) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudCiamUserGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer logElapsed("resource.tencentcloud_ciam_user_group.update")() |
| 163 | + defer inconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + logId := getLogId(contextNil) |
| 166 | + |
| 167 | + request := ciam.NewUpdateUserGroupRequest() |
| 168 | + |
| 169 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 170 | + if len(idSplit) != 2 { |
| 171 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 172 | + } |
| 173 | + userStoreId := idSplit[0] |
| 174 | + userGroupId := idSplit[1] |
| 175 | + |
| 176 | + request.UserStoreId = &userStoreId |
| 177 | + request.UserGroupId = &userGroupId |
| 178 | + |
| 179 | + needChange := false |
| 180 | + mutableArgs := []string{ |
| 181 | + "display_name", "user_store_id", "description", |
| 182 | + } |
| 183 | + |
| 184 | + for _, v := range mutableArgs { |
| 185 | + if d.HasChange(v) { |
| 186 | + needChange = true |
| 187 | + break |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if needChange { |
| 192 | + |
| 193 | + if v, ok := d.GetOk("display_name"); ok { |
| 194 | + request.DisplayName = helper.String(v.(string)) |
| 195 | + } |
| 196 | + |
| 197 | + if v, ok := d.GetOk("user_store_id"); ok { |
| 198 | + request.UserStoreId = helper.String(v.(string)) |
| 199 | + } |
| 200 | + |
| 201 | + if v, ok := d.GetOk("description"); ok { |
| 202 | + request.Description = helper.String(v.(string)) |
| 203 | + } |
| 204 | + |
| 205 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 206 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCiamClient().UpdateUserGroup(request) |
| 207 | + if e != nil { |
| 208 | + return retryError(e) |
| 209 | + } else { |
| 210 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 211 | + } |
| 212 | + return nil |
| 213 | + }) |
| 214 | + if err != nil { |
| 215 | + log.Printf("[CRITAL]%s update ciam userGroup failed, reason:%+v", logId, err) |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | + return resourceTencentCloudCiamUserGroupRead(d, meta) |
| 221 | +} |
| 222 | + |
| 223 | +func resourceTencentCloudCiamUserGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 224 | + defer logElapsed("resource.tencentcloud_ciam_user_group.delete")() |
| 225 | + defer inconsistentCheck(d, meta)() |
| 226 | + |
| 227 | + logId := getLogId(contextNil) |
| 228 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 229 | + |
| 230 | + service := CiamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 231 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 232 | + if len(idSplit) != 2 { |
| 233 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 234 | + } |
| 235 | + userStoreId := idSplit[0] |
| 236 | + userGroupId := idSplit[1] |
| 237 | + |
| 238 | + if err := service.DeleteCiamUserGroupById(ctx, userStoreId, userGroupId); err != nil { |
| 239 | + return err |
| 240 | + } |
| 241 | + |
| 242 | + return nil |
| 243 | +} |
0 commit comments