|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" |
| 11 | + |
| 12 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudMongodbInstanceParams() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudMongodbInstanceParamsCreate, |
| 19 | + Read: resourceTencentCloudMongodbInstanceParamsRead, |
| 20 | + Update: resourceTencentCloudMongodbInstanceParamsUpdate, |
| 21 | + Delete: resourceTencentCloudMongodbInstanceParamsDelete, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "instance_id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + Description: "Instance id.", |
| 28 | + }, |
| 29 | + |
| 30 | + "instance_params": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Required: true, |
| 33 | + Description: "Specify the parameter name and value to be modified.", |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "key": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + Description: "Parameter names that need to be modified.", |
| 40 | + }, |
| 41 | + "value": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + Description: "The value corresponding to the parameter name to be modified.", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + |
| 50 | + "modify_type": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Operation types, including:\n" + |
| 54 | + " - IMMEDIATELY: Adjust immediately;\n" + |
| 55 | + " - DELAY: Delay adjustment;\n" + |
| 56 | + "Optional field. If this parameter is not configured, it defaults to immediate adjustment.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func resourceTencentCloudMongodbInstanceParamsCreate(d *schema.ResourceData, meta interface{}) error { |
| 63 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_params.create")() |
| 64 | + defer tccommon.InconsistentCheck(d, meta)() |
| 65 | + |
| 66 | + var ( |
| 67 | + instanceId string |
| 68 | + ) |
| 69 | + if v, ok := d.GetOk("instance_id"); ok { |
| 70 | + instanceId = v.(string) |
| 71 | + } |
| 72 | + |
| 73 | + d.SetId(instanceId) |
| 74 | + |
| 75 | + return resourceTencentCloudMongodbInstanceParamsUpdate(d, meta) |
| 76 | +} |
| 77 | + |
| 78 | +func resourceTencentCloudMongodbInstanceParamsRead(d *schema.ResourceData, meta interface{}) error { |
| 79 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_params.read")() |
| 80 | + defer tccommon.InconsistentCheck(d, meta)() |
| 81 | + |
| 82 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 83 | + |
| 84 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 85 | + |
| 86 | + service := MongodbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 87 | + |
| 88 | + instanceId := d.Id() |
| 89 | + |
| 90 | + _ = d.Set("instance_id", instanceId) |
| 91 | + |
| 92 | + instanceParams := d.Get("instance_params").([]interface{}) |
| 93 | + paramNames := make([]string, 0) |
| 94 | + for _, instanceParam := range instanceParams { |
| 95 | + instanceParamMap := instanceParam.(map[string]interface{}) |
| 96 | + paramNames = append(paramNames, instanceParamMap["key"].(string)) |
| 97 | + } |
| 98 | + respData, err := service.DescribeMongodbInstanceParamValues(ctx, instanceId, paramNames) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + if respData == nil { |
| 104 | + d.SetId("") |
| 105 | + log.Printf("[WARN]%s resource `mongodb_instance_params` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + resInstanceParams := make([]interface{}, 0) |
| 110 | + for k, v := range respData { |
| 111 | + resInstanceParams = append(resInstanceParams, map[string]interface{}{ |
| 112 | + "key": k, |
| 113 | + "value": v, |
| 114 | + }) |
| 115 | + } |
| 116 | + _ = d.Set("instance_params", resInstanceParams) |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func resourceTencentCloudMongodbInstanceParamsUpdate(d *schema.ResourceData, meta interface{}) error { |
| 122 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_params.update")() |
| 123 | + defer tccommon.InconsistentCheck(d, meta)() |
| 124 | + |
| 125 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 126 | + |
| 127 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 128 | + |
| 129 | + instanceId := d.Id() |
| 130 | + |
| 131 | + needChange := false |
| 132 | + mutableArgs := []string{"instance_params"} |
| 133 | + for _, v := range mutableArgs { |
| 134 | + if d.HasChange(v) { |
| 135 | + needChange = true |
| 136 | + break |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if needChange { |
| 141 | + request := mongodb.NewModifyInstanceParamsRequest() |
| 142 | + request.InstanceId = helper.String(d.Get("instance_id").(string)) |
| 143 | + |
| 144 | + if v, ok := d.GetOk("instance_params"); ok { |
| 145 | + for _, item := range v.([]interface{}) { |
| 146 | + instanceParamsMap := item.(map[string]interface{}) |
| 147 | + modifyMongoDBParamType := mongodb.ModifyMongoDBParamType{} |
| 148 | + if v, ok := instanceParamsMap["key"]; ok { |
| 149 | + modifyMongoDBParamType.Key = helper.String(v.(string)) |
| 150 | + } |
| 151 | + if v, ok := instanceParamsMap["value"]; ok { |
| 152 | + modifyMongoDBParamType.Value = helper.String(v.(string)) |
| 153 | + } |
| 154 | + request.InstanceParams = append(request.InstanceParams, &modifyMongoDBParamType) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if v, ok := d.GetOk("modify_type"); ok { |
| 159 | + request.ModifyType = helper.String(v.(string)) |
| 160 | + } |
| 161 | + |
| 162 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 163 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMongodbClient().ModifyInstanceParamsWithContext(ctx, request) |
| 164 | + if e != nil { |
| 165 | + return tccommon.RetryError(e) |
| 166 | + } else { |
| 167 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 168 | + } |
| 169 | + return nil |
| 170 | + }) |
| 171 | + if err != nil { |
| 172 | + log.Printf("[CRITAL]%s update mongodb instance params failed, reason:%+v", logId, err) |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + service := MongodbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 177 | + instanceParams := d.Get("instance_params").([]interface{}) |
| 178 | + paramMap := make(map[string]string) |
| 179 | + paramNames := make([]string, 0) |
| 180 | + |
| 181 | + for _, instanceParam := range instanceParams { |
| 182 | + instanceParamMap := instanceParam.(map[string]interface{}) |
| 183 | + key := instanceParamMap["key"].(string) |
| 184 | + value := instanceParamMap["value"].(string) |
| 185 | + paramMap[key] = value |
| 186 | + paramNames = append(paramNames, key) |
| 187 | + } |
| 188 | + |
| 189 | + err = resource.Retry(6*tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 190 | + respMap, e := service.DescribeMongodbInstanceParamValues(ctx, instanceId, paramNames) |
| 191 | + if e != nil { |
| 192 | + return tccommon.RetryError(e) |
| 193 | + } |
| 194 | + |
| 195 | + for k, v := range paramMap { |
| 196 | + if vv, ok := respMap[k]; ok { |
| 197 | + if v != vv { |
| 198 | + return resource.RetryableError(fmt.Errorf("param %s is still being updated", k)) |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + return nil |
| 203 | + }) |
| 204 | + if err != nil { |
| 205 | + log.Printf("[CRITAL]%s update mongodb instance params failed, reason:%+v", logId, err) |
| 206 | + return err |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + _ = instanceId |
| 211 | + return resourceTencentCloudMongodbInstanceParamsRead(d, meta) |
| 212 | +} |
| 213 | + |
| 214 | +func resourceTencentCloudMongodbInstanceParamsDelete(d *schema.ResourceData, meta interface{}) error { |
| 215 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_params.delete")() |
| 216 | + defer tccommon.InconsistentCheck(d, meta)() |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments