|
| 1 | +/* |
| 2 | +Provides a resource to create a emr user |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_emr" "my_emr" { |
| 8 | + display_strategy = "clusterList" |
| 9 | +} |
| 10 | +
|
| 11 | +resource "tencentcloud_emr_user_manager" "user_manager" { |
| 12 | + instance_id = data.tencentcloud_emr.my_emr.clusters.0.cluster_id |
| 13 | + user_name = "tf-test" |
| 14 | + user_group = "group1" |
| 15 | + password = "tf@123456" |
| 16 | +} |
| 17 | +
|
| 18 | +``` |
| 19 | +
|
| 20 | +Import |
| 21 | +
|
| 22 | +emr user_manager can be imported using the id, e.g. |
| 23 | +
|
| 24 | +``` |
| 25 | +terraform import tencentcloud_emr_user_manager.user_manager instanceId#userName |
| 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 | + emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103" |
| 39 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 40 | +) |
| 41 | + |
| 42 | +func resourceTencentCloudEmrUserManager() *schema.Resource { |
| 43 | + return &schema.Resource{ |
| 44 | + Create: resourceTencentCloudEmrUserManagerCreate, |
| 45 | + Read: resourceTencentCloudEmrUserManagerRead, |
| 46 | + Update: resourceTencentCloudEmrUserManagerUpdate, |
| 47 | + Delete: resourceTencentCloudEmrUserManagerDelete, |
| 48 | + Importer: &schema.ResourceImporter{ |
| 49 | + State: schema.ImportStatePassthrough, |
| 50 | + }, |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "instance_id": { |
| 53 | + Required: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + ForceNew: true, |
| 56 | + Description: "Cluster string ID.", |
| 57 | + }, |
| 58 | + "user_name": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + ForceNew: true, |
| 62 | + Description: "Username.", |
| 63 | + }, |
| 64 | + "user_group": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Required: true, |
| 67 | + ForceNew: true, |
| 68 | + Description: "User group membership.", |
| 69 | + }, |
| 70 | + "password": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Required: true, |
| 73 | + Description: "PassWord.", |
| 74 | + }, |
| 75 | + "user_type": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "User type.", |
| 79 | + }, |
| 80 | + "create_time": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "Create time.", |
| 84 | + }, |
| 85 | + "support_download_keytab": { |
| 86 | + Type: schema.TypeBool, |
| 87 | + Computed: true, |
| 88 | + Description: "If support download keytab.", |
| 89 | + }, |
| 90 | + "download_keytab_url": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "Download keytab url.", |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func resourceTencentCloudEmrUserManagerCreate(d *schema.ResourceData, meta interface{}) error { |
| 100 | + defer logElapsed("resource.tencentcloud_emr_user_manager.create")() |
| 101 | + defer inconsistentCheck(d, meta)() |
| 102 | + |
| 103 | + logId := getLogId(contextNil) |
| 104 | + |
| 105 | + var ( |
| 106 | + request = emr.NewAddUsersForUserManagerRequest() |
| 107 | + response = emr.NewAddUsersForUserManagerResponse() |
| 108 | + instanceId string |
| 109 | + userName string |
| 110 | + ) |
| 111 | + if v, ok := d.GetOk("instance_id"); ok { |
| 112 | + request.InstanceId = helper.String(v.(string)) |
| 113 | + instanceId = v.(string) |
| 114 | + } |
| 115 | + |
| 116 | + userInfoForUserManager := emr.UserInfoForUserManager{} |
| 117 | + if v, ok := d.GetOk("user_name"); ok { |
| 118 | + userInfoForUserManager.UserName = helper.String(v.(string)) |
| 119 | + userName = v.(string) |
| 120 | + } |
| 121 | + if v, ok := d.GetOk("user_group"); ok { |
| 122 | + userInfoForUserManager.UserGroup = helper.String(v.(string)) |
| 123 | + } |
| 124 | + if v, ok := d.GetOk("password"); ok { |
| 125 | + userInfoForUserManager.PassWord = helper.String(v.(string)) |
| 126 | + } |
| 127 | + request.UserManagerUserList = append(request.UserManagerUserList, &userInfoForUserManager) |
| 128 | + |
| 129 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 130 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseEmrClient().AddUsersForUserManager(request) |
| 131 | + if e != nil { |
| 132 | + return retryError(e) |
| 133 | + } else { |
| 134 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 135 | + } |
| 136 | + response = result |
| 137 | + return nil |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + log.Printf("[CRITAL]%s create emr userManager failed, reason:%+v", logId, err) |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + if len(response.Response.FailedUserList) > 0 { |
| 145 | + return fmt.Errorf("add user failed, please try again.") |
| 146 | + } |
| 147 | + |
| 148 | + d.SetId(instanceId + FILED_SP + userName) |
| 149 | + |
| 150 | + return resourceTencentCloudEmrUserManagerRead(d, meta) |
| 151 | +} |
| 152 | + |
| 153 | +func resourceTencentCloudEmrUserManagerRead(d *schema.ResourceData, meta interface{}) error { |
| 154 | + defer logElapsed("resource.tencentcloud_emr_user_manager.read")() |
| 155 | + defer inconsistentCheck(d, meta)() |
| 156 | + |
| 157 | + logId := getLogId(contextNil) |
| 158 | + |
| 159 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 160 | + |
| 161 | + service := EMRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 162 | + |
| 163 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 164 | + if len(idSplit) != 2 { |
| 165 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 166 | + } |
| 167 | + instanceId := idSplit[0] |
| 168 | + userName := idSplit[1] |
| 169 | + |
| 170 | + userManager, err := service.DescribeEmrUserManagerById(ctx, instanceId, userName) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + if userManager == nil { |
| 176 | + d.SetId("") |
| 177 | + log.Printf("[WARN]%s resource `EmrUserManager` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 178 | + return nil |
| 179 | + } |
| 180 | + |
| 181 | + _ = d.Set("instance_id", instanceId) |
| 182 | + |
| 183 | + if userManager.UserManagerUserList != nil { |
| 184 | + for _, userManager := range userManager.UserManagerUserList { |
| 185 | + if userManager.UserName != nil { |
| 186 | + _ = d.Set("user_name", userManager.UserName) |
| 187 | + } |
| 188 | + |
| 189 | + if userManager.UserGroup != nil { |
| 190 | + _ = d.Set("user_group", userManager.UserGroup) |
| 191 | + } |
| 192 | + |
| 193 | + if userManager.UserType != nil { |
| 194 | + _ = d.Set("user_type", userManager.UserType) |
| 195 | + } |
| 196 | + |
| 197 | + if userManager.CreateTime != nil { |
| 198 | + _ = d.Set("create_time", userManager.CreateTime) |
| 199 | + } |
| 200 | + |
| 201 | + if userManager.SupportDownLoadKeyTab != nil { |
| 202 | + _ = d.Set("support_download_keytab", userManager.SupportDownLoadKeyTab) |
| 203 | + } |
| 204 | + |
| 205 | + if userManager.DownLoadKeyTabUrl != nil { |
| 206 | + _ = d.Set("download_keytab_url", userManager.DownLoadKeyTabUrl) |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return nil |
| 212 | +} |
| 213 | + |
| 214 | +func resourceTencentCloudEmrUserManagerUpdate(d *schema.ResourceData, meta interface{}) error { |
| 215 | + defer logElapsed("resource.tencentcloud_emr_user_manager.update")() |
| 216 | + defer inconsistentCheck(d, meta)() |
| 217 | + |
| 218 | + logId := getLogId(contextNil) |
| 219 | + |
| 220 | + request := emr.NewModifyUserManagerPwdRequest() |
| 221 | + |
| 222 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 223 | + if len(idSplit) != 2 { |
| 224 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 225 | + } |
| 226 | + instanceId := idSplit[0] |
| 227 | + userName := idSplit[1] |
| 228 | + |
| 229 | + request.InstanceId = &instanceId |
| 230 | + request.UserName = &userName |
| 231 | + |
| 232 | + if d.HasChange("password") { |
| 233 | + if v, ok := d.GetOk("password"); ok { |
| 234 | + request.PassWord = helper.String(v.(string)) |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 239 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseEmrClient().ModifyUserManagerPwd(request) |
| 240 | + if e != nil { |
| 241 | + return retryError(e) |
| 242 | + } else { |
| 243 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 244 | + } |
| 245 | + return nil |
| 246 | + }) |
| 247 | + if err != nil { |
| 248 | + log.Printf("[CRITAL]%s update emr userManager failed, reason:%+v", logId, err) |
| 249 | + return err |
| 250 | + } |
| 251 | + |
| 252 | + return resourceTencentCloudEmrUserManagerRead(d, meta) |
| 253 | +} |
| 254 | + |
| 255 | +func resourceTencentCloudEmrUserManagerDelete(d *schema.ResourceData, meta interface{}) error { |
| 256 | + defer logElapsed("resource.tencentcloud_emr_user_manager.delete")() |
| 257 | + defer inconsistentCheck(d, meta)() |
| 258 | + |
| 259 | + logId := getLogId(contextNil) |
| 260 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 261 | + |
| 262 | + service := EMRService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 263 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 264 | + if len(idSplit) != 2 { |
| 265 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 266 | + } |
| 267 | + instanceId := idSplit[0] |
| 268 | + userName := idSplit[1] |
| 269 | + |
| 270 | + if err := service.DeleteEmrUserManagerById(ctx, instanceId, userName); err != nil { |
| 271 | + return err |
| 272 | + } |
| 273 | + |
| 274 | + return nil |
| 275 | +} |
0 commit comments