|
| 1 | +package cdwpg |
| 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 | + cdwpgv20201230 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwpg/v20201230" |
| 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 ResourceTencentCloudCdwpgResetAccountPassword() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudCdwpgResetAccountPasswordCreate, |
| 20 | + Read: resourceTencentCloudCdwpgResetAccountPasswordRead, |
| 21 | + Update: resourceTencentCloudCdwpgResetAccountPasswordUpdate, |
| 22 | + Delete: resourceTencentCloudCdwpgResetAccountPasswordDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "instance_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "Instance id.", |
| 32 | + }, |
| 33 | + |
| 34 | + "user_name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + Description: "Username.", |
| 39 | + }, |
| 40 | + |
| 41 | + "new_password": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + Sensitive: true, |
| 45 | + Description: "New password.", |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func resourceTencentCloudCdwpgResetAccountPasswordCreate(d *schema.ResourceData, meta interface{}) error { |
| 52 | + defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.create")() |
| 53 | + defer tccommon.InconsistentCheck(d, meta)() |
| 54 | + |
| 55 | + instanceId := d.Get("instance_id").(string) |
| 56 | + userName := d.Get("user_name").(string) |
| 57 | + |
| 58 | + d.SetId(strings.Join([]string{instanceId, userName}, tccommon.FILED_SP)) |
| 59 | + |
| 60 | + return resourceTencentCloudCdwpgResetAccountPasswordUpdate(d, meta) |
| 61 | +} |
| 62 | + |
| 63 | +func resourceTencentCloudCdwpgResetAccountPasswordRead(d *schema.ResourceData, meta interface{}) error { |
| 64 | + defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.read")() |
| 65 | + defer tccommon.InconsistentCheck(d, meta)() |
| 66 | + |
| 67 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 68 | + |
| 69 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 70 | + |
| 71 | + service := CdwpgService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 72 | + |
| 73 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 74 | + if len(idSplit) != 2 { |
| 75 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 76 | + } |
| 77 | + instanceId := idSplit[0] |
| 78 | + userName := idSplit[1] |
| 79 | + |
| 80 | + respData, err := service.DescribeCdwpgAccountById(ctx, instanceId) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + if respData == nil { |
| 86 | + d.SetId("") |
| 87 | + log.Printf("[WARN]%s resource `cdwpg_account` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 88 | + return nil |
| 89 | + } |
| 90 | + if respData.InstanceId != nil { |
| 91 | + _ = d.Set("instance_id", respData.InstanceId) |
| 92 | + } |
| 93 | + |
| 94 | + if respData.UserName != nil { |
| 95 | + _ = d.Set("user_name", respData.UserName) |
| 96 | + } |
| 97 | + |
| 98 | + _ = userName |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func resourceTencentCloudCdwpgResetAccountPasswordUpdate(d *schema.ResourceData, meta interface{}) error { |
| 103 | + defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.update")() |
| 104 | + defer tccommon.InconsistentCheck(d, meta)() |
| 105 | + |
| 106 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 107 | + |
| 108 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 109 | + |
| 110 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 111 | + if len(idSplit) != 2 { |
| 112 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 113 | + } |
| 114 | + instanceId := idSplit[0] |
| 115 | + userName := idSplit[1] |
| 116 | + |
| 117 | + needChange := false |
| 118 | + mutableArgs := []string{"new_password"} |
| 119 | + for _, v := range mutableArgs { |
| 120 | + if d.HasChange(v) { |
| 121 | + needChange = true |
| 122 | + break |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if needChange { |
| 127 | + request := cdwpgv20201230.NewResetAccountPasswordRequest() |
| 128 | + |
| 129 | + request.InstanceId = helper.String(instanceId) |
| 130 | + |
| 131 | + request.UserName = helper.String(userName) |
| 132 | + |
| 133 | + if v, ok := d.GetOk("new_password"); ok { |
| 134 | + request.NewPassword = helper.String(v.(string)) |
| 135 | + } |
| 136 | + |
| 137 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 138 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgV20201230Client().ResetAccountPasswordWithContext(ctx, request) |
| 139 | + if e != nil { |
| 140 | + return tccommon.RetryError(e) |
| 141 | + } else { |
| 142 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 143 | + } |
| 144 | + return nil |
| 145 | + }) |
| 146 | + if err != nil { |
| 147 | + log.Printf("[CRITAL]%s update cdwpg account failed, reason:%+v", logId, err) |
| 148 | + return err |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return resourceTencentCloudCdwpgResetAccountPasswordRead(d, meta) |
| 153 | +} |
| 154 | + |
| 155 | +func resourceTencentCloudCdwpgResetAccountPasswordDelete(d *schema.ResourceData, meta interface{}) error { |
| 156 | + defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.delete")() |
| 157 | + defer tccommon.InconsistentCheck(d, meta)() |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
0 commit comments