|
| 1 | +package rocketmq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-uuid" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + |
| 13 | + "github.com/chnsz/golangsdk" |
| 14 | + |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 17 | +) |
| 18 | + |
| 19 | +var instanceRestartNonUpdatableParams = []string{"instance_id", "nodes"} |
| 20 | + |
| 21 | +// @API RocketMQ POST /v2/{project_id}/{engine}/instances/{instance_id}/restart |
| 22 | +func ResourceDmsRocketMQInstanceRestart() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + CreateContext: resourceDmsRocketMQInstanceRestartCreate, |
| 25 | + ReadContext: resourceDmsRocketMQInstanceRestartRead, |
| 26 | + DeleteContext: resourceDmsRocketMQInstanceRestartDelete, |
| 27 | + |
| 28 | + CustomizeDiff: customdiff.All( |
| 29 | + config.FlexibleForceNew(instanceRestartNonUpdatableParams), |
| 30 | + ), |
| 31 | + |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "region": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + ForceNew: true, |
| 37 | + Computed: true, |
| 38 | + Description: `The region where the RocketMQ instance is located.`, |
| 39 | + }, |
| 40 | + "instance_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + Description: `The ID of the RocketMQ instance to be restarted.`, |
| 44 | + }, |
| 45 | + "nodes": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Required: true, |
| 48 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 49 | + Description: `The list of node names to be restarted.`, |
| 50 | + }, |
| 51 | + |
| 52 | + // Internal |
| 53 | + "enable_force_new": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 57 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func resourceDmsRocketMQInstanceRestartCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 64 | + cfg := meta.(*config.Config) |
| 65 | + region := cfg.GetRegion(d) |
| 66 | + client, err := cfg.NewServiceClient("dmsv2", region) |
| 67 | + if err != nil { |
| 68 | + return diag.Errorf("error creating DMS client: %s", err) |
| 69 | + } |
| 70 | + |
| 71 | + instanceId := d.Get("instance_id").(string) |
| 72 | + |
| 73 | + httpUrl := "v2/{project_id}/rocketmq/instances/{instance_id}/restart" |
| 74 | + httpUrl = strings.ReplaceAll(httpUrl, "{project_id}", client.ProjectID) |
| 75 | + httpUrl = strings.ReplaceAll(httpUrl, "{instance_id}", instanceId) |
| 76 | + restartPath := client.Endpoint + httpUrl |
| 77 | + |
| 78 | + opt := golangsdk.RequestOpts{ |
| 79 | + KeepResponseBody: true, |
| 80 | + MoreHeaders: map[string]string{ |
| 81 | + "Content-Type": "application/json", |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + // Add nodes parameter to request body if specified |
| 86 | + if nodesRaw := d.Get("nodes").([]interface{}); len(nodesRaw) > 0 { |
| 87 | + nodes := make([]string, len(nodesRaw)) |
| 88 | + for i, node := range nodesRaw { |
| 89 | + nodes[i] = node.(string) |
| 90 | + } |
| 91 | + opt.JSONBody = map[string]interface{}{ |
| 92 | + "nodes": nodes, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + _, err = client.Request("POST", restartPath, &opt) |
| 97 | + if err != nil { |
| 98 | + return diag.Errorf("error restarting RocketMQ instance: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + randUUID, err := uuid.GenerateUUID() |
| 102 | + if err != nil { |
| 103 | + return diag.Errorf("unable to generate resource ID: %s", err) |
| 104 | + } |
| 105 | + d.SetId(randUUID) |
| 106 | + |
| 107 | + return resourceDmsRocketMQInstanceRestartRead(ctx, d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceDmsRocketMQInstanceRestartRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func resourceDmsRocketMQInstanceRestartDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 115 | + errorMsg := `This resource is a one-time action resource used to restart RocketMQ instance. Deleting this resource will |
| 116 | +not clear the restart operation record, but will only remove the resource information from the tfstate file.` |
| 117 | + return diag.Diagnostics{ |
| 118 | + diag.Diagnostic{ |
| 119 | + Severity: diag.Warning, |
| 120 | + Summary: errorMsg, |
| 121 | + }, |
| 122 | + } |
| 123 | +} |
0 commit comments