|
| 1 | +/* |
| 2 | +Provides a resource to create a redis parameter template |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_redis_param_template" "param_template" { |
| 8 | + name = "example-template" |
| 9 | + description = "This is an example redis param template." |
| 10 | + product_type = 6 |
| 11 | + params { |
| 12 | + key = "timeout" |
| 13 | + value = "7200" |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Copy from another template |
| 19 | +```hcl |
| 20 | +resource "tencentcloud_redis_param_template" "param_template" { |
| 21 | + name = "example-copied" |
| 22 | + description = "This is an copied redis param template from xxx." |
| 23 | + template_id = "xxx" |
| 24 | + params { |
| 25 | + key = "timeout" |
| 26 | + value = "7200" |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +
|
| 32 | +Import |
| 33 | +
|
| 34 | +redis param_template can be imported using the id, e.g. |
| 35 | +``` |
| 36 | +$ terraform import tencentcloud_redis_param_template.param_template param_template_id |
| 37 | +``` |
| 38 | +*/ |
| 39 | +package tencentcloud |
| 40 | + |
| 41 | +import ( |
| 42 | + "context" |
| 43 | + "fmt" |
| 44 | + |
| 45 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 46 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 47 | + redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" |
| 48 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 49 | +) |
| 50 | + |
| 51 | +func resourceTencentCloudRedisParamTemplate() *schema.Resource { |
| 52 | + return &schema.Resource{ |
| 53 | + Read: resourceTencentCloudRedisParamTemplateRead, |
| 54 | + Create: resourceTencentCloudRedisParamTemplateCreate, |
| 55 | + Update: resourceTencentCloudRedisParamTemplateUpdate, |
| 56 | + Delete: resourceTencentCloudRedisParamTemplateDelete, |
| 57 | + Importer: &schema.ResourceImporter{ |
| 58 | + State: schema.ImportStatePassthrough, |
| 59 | + }, |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "name": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Required: true, |
| 64 | + Description: "Parameter template name.", |
| 65 | + }, |
| 66 | + "description": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Optional: true, |
| 69 | + Description: "Parameter template description.", |
| 70 | + }, |
| 71 | + "product_type": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Optional: true, |
| 74 | + ConflictsWith: []string{"template_id"}, |
| 75 | + Description: "Specify product type. Valid values: 1 (Redis 2.8 Memory Edition in cluster architecture), 2 (Redis 2.8 Memory Edition in standard architecture), 3 (CKV 3.2 Memory Edition in standard architecture), 4 (CKV 3.2 Memory Edition in cluster architecture), 5 (Redis 2.8 Memory Edition in standalone architecture), 6 (Redis 4.0 Memory Edition in standard architecture), 7 (Redis 4.0 Memory Edition in cluster architecture), 8 (Redis 5.0 Memory Edition in standard architecture), 9 (Redis 5.0 Memory Edition in cluster architecture). If `template_id` is specified, this parameter can be left blank; otherwise, it is required.", |
| 76 | + }, |
| 77 | + "template_id": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Optional: true, |
| 80 | + ConflictsWith: []string{"product_type"}, |
| 81 | + Description: "Specify which existed template import from.", |
| 82 | + }, |
| 83 | + "params_override": { |
| 84 | + Type: schema.TypeList, |
| 85 | + Optional: true, |
| 86 | + Description: "Specify override parameter list, NOTE: Do not remove override params once set, removing will not take effects to current value.", |
| 87 | + Elem: &schema.Resource{ |
| 88 | + Schema: map[string]*schema.Schema{ |
| 89 | + "key": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Required: true, |
| 92 | + Description: "Parameter key e.g. `timeout`, check https://www.tencentcloud.com/document/product/239/39796 for more reference.", |
| 93 | + }, |
| 94 | + "value": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Required: true, |
| 97 | + Description: "Parameter value, check https://www.tencentcloud.com/document/product/239/39796 for more reference.", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + "param_details": { |
| 103 | + Type: schema.TypeList, |
| 104 | + Computed: true, |
| 105 | + Description: "Readonly full parameter list details.", |
| 106 | + Elem: &schema.Resource{ |
| 107 | + Schema: map[string]*schema.Schema{ |
| 108 | + "name": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "Parameter key name.", |
| 112 | + }, |
| 113 | + "param_type": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "Parameter type.", |
| 117 | + }, |
| 118 | + "default": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + Description: "Default value.", |
| 122 | + }, |
| 123 | + "description": { |
| 124 | + Type: schema.TypeString, |
| 125 | + Computed: true, |
| 126 | + Description: "Parameter description.", |
| 127 | + }, |
| 128 | + "current_value": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Computed: true, |
| 131 | + Description: "Current value.", |
| 132 | + }, |
| 133 | + "need_reboot": { |
| 134 | + Type: schema.TypeInt, |
| 135 | + Computed: true, |
| 136 | + Description: "Indicates whether to reboot redis instance if modified.", |
| 137 | + }, |
| 138 | + "max": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Computed: true, |
| 141 | + Description: "Maximum value.", |
| 142 | + }, |
| 143 | + "min": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Computed: true, |
| 146 | + Description: "Minimum value.", |
| 147 | + }, |
| 148 | + "enum_value": { |
| 149 | + Type: schema.TypeList, |
| 150 | + Computed: true, |
| 151 | + Description: "Enum values.", |
| 152 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudRedisParamTemplateCreate(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer logElapsed("resource.tencentcloud_redis_param_template.create")() |
| 163 | + defer inconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + logId := getLogId(contextNil) |
| 166 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 167 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 168 | + service := RedisService{client: client} |
| 169 | + |
| 170 | + var ( |
| 171 | + request = redis.NewCreateParamTemplateRequest() |
| 172 | + id string |
| 173 | + ) |
| 174 | + |
| 175 | + if v, ok := d.GetOk("name"); ok { |
| 176 | + request.Name = helper.String(v.(string)) |
| 177 | + } |
| 178 | + |
| 179 | + if v, ok := d.GetOk("description"); ok { |
| 180 | + request.Description = helper.String(v.(string)) |
| 181 | + } |
| 182 | + |
| 183 | + if v, ok := d.GetOk("product_type"); ok { |
| 184 | + request.ProductType = helper.IntUint64(v.(int)) |
| 185 | + } |
| 186 | + |
| 187 | + if v, ok := d.GetOk("template_id"); ok { |
| 188 | + request.TemplateId = helper.String(v.(string)) |
| 189 | + } |
| 190 | + |
| 191 | + if v, ok := d.GetOk("params_override"); ok { |
| 192 | + for _, item := range v.([]interface{}) { |
| 193 | + dMap := item.(map[string]interface{}) |
| 194 | + instanceParam := redis.InstanceParam{} |
| 195 | + if v, ok := dMap["key"]; ok { |
| 196 | + instanceParam.Key = helper.String(v.(string)) |
| 197 | + } |
| 198 | + if v, ok := dMap["value"]; ok { |
| 199 | + instanceParam.Value = helper.String(v.(string)) |
| 200 | + } |
| 201 | + |
| 202 | + request.ParamList = append(request.ParamList, &instanceParam) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 207 | + resTemplateId, e := service.CreateParamTemplate(ctx, request) |
| 208 | + if e != nil { |
| 209 | + return retryError(e) |
| 210 | + } |
| 211 | + id = resTemplateId |
| 212 | + return nil |
| 213 | + }) |
| 214 | + |
| 215 | + if err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + if id == "" { |
| 220 | + return fmt.Errorf("cannot get redis template id") |
| 221 | + } |
| 222 | + |
| 223 | + d.SetId(id) |
| 224 | + return resourceTencentCloudRedisParamTemplateRead(d, meta) |
| 225 | +} |
| 226 | + |
| 227 | +func resourceTencentCloudRedisParamTemplateRead(d *schema.ResourceData, meta interface{}) error { |
| 228 | + defer logElapsed("resource.tencentcloud_redis_param_template.read")() |
| 229 | + defer inconsistentCheck(d, meta)() |
| 230 | + |
| 231 | + logId := getLogId(contextNil) |
| 232 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 233 | + |
| 234 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 235 | + |
| 236 | + id := d.Id() |
| 237 | + |
| 238 | + paramTemplate, err := service.DescribeParamTemplateInfo(ctx, id) |
| 239 | + |
| 240 | + if err != nil { |
| 241 | + return err |
| 242 | + } |
| 243 | + |
| 244 | + if paramTemplate == nil { |
| 245 | + d.SetId("") |
| 246 | + return fmt.Errorf("resource `param_template` %s does not exist", id) |
| 247 | + } |
| 248 | + |
| 249 | + d.SetId(id) |
| 250 | + |
| 251 | + if paramTemplate.Name != nil { |
| 252 | + _ = d.Set("name", paramTemplate.Name) |
| 253 | + } |
| 254 | + |
| 255 | + if paramTemplate.Description != nil { |
| 256 | + _ = d.Set("description", paramTemplate.Description) |
| 257 | + } |
| 258 | + |
| 259 | + if _, ok := d.GetOk("product_type"); ok && paramTemplate.ProductType != nil { |
| 260 | + _ = d.Set("product_type", paramTemplate.ProductType) |
| 261 | + } |
| 262 | + |
| 263 | + if len(paramTemplate.Items) > 0 { |
| 264 | + result := make([]interface{}, 0) |
| 265 | + for i := range paramTemplate.Items { |
| 266 | + item := paramTemplate.Items[i] |
| 267 | + listMap := map[string]interface{}{ |
| 268 | + "name": item.Name, |
| 269 | + "param_type": item.ParamType, |
| 270 | + "default": item.Default, |
| 271 | + "description": item.Description, |
| 272 | + "current_value": item.CurrentValue, |
| 273 | + "need_reboot": item.NeedReboot, |
| 274 | + "max": item.Max, |
| 275 | + "min": item.Min, |
| 276 | + "enum_value": item.EnumValue, |
| 277 | + } |
| 278 | + |
| 279 | + result = append(result, listMap) |
| 280 | + } |
| 281 | + _ = d.Set("param_details", result) |
| 282 | + } |
| 283 | + |
| 284 | + return nil |
| 285 | +} |
| 286 | + |
| 287 | +func resourceTencentCloudRedisParamTemplateUpdate(d *schema.ResourceData, meta interface{}) error { |
| 288 | + defer logElapsed("resource.tencentcloud_redis_param_template.update")() |
| 289 | + defer inconsistentCheck(d, meta)() |
| 290 | + |
| 291 | + logId := getLogId(contextNil) |
| 292 | + |
| 293 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 294 | + request := redis.NewModifyParamTemplateRequest() |
| 295 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 296 | + service := RedisService{client: client} |
| 297 | + |
| 298 | + templateId := d.Id() |
| 299 | + |
| 300 | + request.TemplateId = &templateId |
| 301 | + |
| 302 | + if d.HasChange("name") { |
| 303 | + request.Name = helper.String(d.Get("name").(string)) |
| 304 | + } |
| 305 | + |
| 306 | + if d.HasChange("description") { |
| 307 | + request.Description = helper.String(d.Get("description").(string)) |
| 308 | + } |
| 309 | + |
| 310 | + if d.HasChange("product_type") { |
| 311 | + return fmt.Errorf("`product_type` do not support change now.") |
| 312 | + } |
| 313 | + |
| 314 | + if d.HasChange("template_id") { |
| 315 | + return fmt.Errorf("`template_id` do not support change now.") |
| 316 | + } |
| 317 | + |
| 318 | + if d.HasChange("params_override") { |
| 319 | + if v, ok := d.GetOk("params_override"); ok { |
| 320 | + for _, item := range v.([]interface{}) { |
| 321 | + dMap := item.(map[string]interface{}) |
| 322 | + request.ParamList = append(request.ParamList, &redis.InstanceParam{ |
| 323 | + Key: helper.String(dMap["key"].(string)), |
| 324 | + Value: helper.String(dMap["value"].(string)), |
| 325 | + }) |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + err := service.ModifyParamTemplate(ctx, request) |
| 331 | + |
| 332 | + if err != nil { |
| 333 | + return err |
| 334 | + } |
| 335 | + |
| 336 | + return resourceTencentCloudRedisParamTemplateRead(d, meta) |
| 337 | +} |
| 338 | + |
| 339 | +func resourceTencentCloudRedisParamTemplateDelete(d *schema.ResourceData, meta interface{}) error { |
| 340 | + defer logElapsed("resource.tencentcloud_redis_param_template.delete")() |
| 341 | + defer inconsistentCheck(d, meta)() |
| 342 | + |
| 343 | + logId := getLogId(contextNil) |
| 344 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 345 | + |
| 346 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 347 | + |
| 348 | + id := d.Id() |
| 349 | + |
| 350 | + request := redis.NewDeleteParamTemplateRequest() |
| 351 | + request.TemplateId = &id |
| 352 | + if err := service.DeleteParamTemplate(ctx, request); err != nil { |
| 353 | + return err |
| 354 | + } |
| 355 | + |
| 356 | + return nil |
| 357 | +} |
0 commit comments