Skip to content

Commit 89c44b3

Browse files
committed
feat(dms): add new resource to restart nodes
1 parent 3bbdf0e commit 89c44b3

File tree

4 files changed

+227
-2
lines changed

4 files changed

+227
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
subcategory: "Distributed Message Service (DMS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_dms_rocketmq_node_batch_restart"
5+
description: |-
6+
Using this resource to batch restart nodes of a RocketMQ instance within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_dms_rocketmq_node_batch_restart
10+
11+
Using this resource to batch restart nodes of a RocketMQ instance within HuaweiCloud.
12+
13+
-> This resource is only a one-time action resource for restarting nodes of RocketMQ instance. Deleting this resource
14+
will not clear the corresponding request record, but will only remove the resource information from the tfstate file.
15+
16+
## Example Usage
17+
18+
```hcl
19+
variable "instance_id" {}
20+
21+
data "huaweicloud_dms_rocketmq_instance_nodes" "test" {
22+
instance_id = var.instance_id
23+
}
24+
25+
resource "huaweicloud_dms_rocketmq_node_batch_restart" "test" {
26+
instance_id = var.instance_id
27+
28+
nodes = [
29+
data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[0].id,
30+
data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[1].id
31+
]
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `region` - (Optional, String, ForceNew) Specifies the region in which to restart the nodes of a RocketMQ instance.
40+
If omitted, the provider-level region will be used. Changing this parameter will create a new resource.
41+
42+
* `instance_id` - (Required, String, NonUpdatable) Specifies the ID of the RocketMQ instance to which nodes belongs.
43+
44+
* `nodes` - (Required, List, NonUpdatable) Specifies the list of node IDs to be restarted.
45+
46+
## Attribute Reference
47+
48+
In addition to all arguments above, the following attributes are exported:
49+
50+
* `id` - The resource ID.

huaweicloud/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,13 @@ func Provider() *schema.Provider {
22852285
"huaweicloud_dms_rocketmq_instance": rocketmq.ResourceDmsRocketMQInstance(),
22862286
"huaweicloud_dms_rocketmq_consumer_group": rocketmq.ResourceDmsRocketMQConsumerGroup(),
22872287
"huaweicloud_dms_rocketmq_consumption_verify": rocketmq.ResourceDmsRocketMQConsumptionVerify(),
2288+
"huaweicloud_dms_rocketmq_dead_letter_resend": rocketmq.ResourceDmsRocketMQDeadLetterResend(),
22882289
"huaweicloud_dms_rocketmq_message_offset_reset": rocketmq.ResourceDmsRocketMQMessageOffsetReset(),
22892290
"huaweicloud_dms_rocketmq_message_send": rocketmq.ResourceRocketMQMessageSend(),
2290-
"huaweicloud_dms_rocketmq_dead_letter_resend": rocketmq.ResourceDmsRocketMQDeadLetterResend(),
2291+
"huaweicloud_dms_rocketmq_migration_task": rocketmq.ResourceDmsRocketmqMigrationTask(),
2292+
"huaweicloud_dms_rocketmq_node_batch_restart": rocketmq.ResourceDmsRocketMQInstanceRestart(),
22912293
"huaweicloud_dms_rocketmq_topic": rocketmq.ResourceDmsRocketMQTopic(),
22922294
"huaweicloud_dms_rocketmq_user": rocketmq.ResourceDmsRocketMQUser(),
2293-
"huaweicloud_dms_rocketmq_migration_task": rocketmq.ResourceDmsRocketmqMigrationTask(),
22942295

22952296
"huaweicloud_dns_custom_line": dns.ResourceCustomLine(),
22962297
"huaweicloud_dns_ptrrecord": dns.ResourcePtrRecord(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package rocketmq
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccInstanceRestart_basic(t *testing.T) {
13+
var (
14+
dataSourceName = "huaweicloud_dms_rocketmq_node_batch_restart.test"
15+
instanceID = acceptance.HW_DMS_ROCKETMQ_INSTANCE_ID
16+
)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acceptance.TestAccPreCheck(t)
21+
acceptance.TestAccPreCheckDMSRocketMQInstanceID(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccInstanceRestart_basic(instanceID),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr(dataSourceName, "instance_id", instanceID),
29+
resource.TestCheckResourceAttr(dataSourceName, "nodes.#", "2"),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "nodes.0"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccInstanceRestart_basic(instanceID string) string {
38+
return fmt.Sprintf(`
39+
data "huaweicloud_dms_rocketmq_instance_nodes" "test" {
40+
instance_id = "%[1]s"
41+
}
42+
43+
resource "huaweicloud_dms_rocketmq_node_batch_restart" "test" {
44+
instance_id = "%[1]s"
45+
46+
nodes = [
47+
try(data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[0].id),
48+
try(data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[1].id)
49+
]
50+
}
51+
`, instanceID)
52+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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: resourceInstanceRestartCreate,
25+
ReadContext: resourceInstanceRestartRead,
26+
DeleteContext: resourceInstanceRestartDelete,
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 in which to restart the nodes of a RocketMQ instance.`,
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 resourceInstanceRestartCreate(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+
nodesRaw := d.Get("nodes").([]interface{})
79+
nodes := make([]string, len(nodesRaw))
80+
for i, node := range nodesRaw {
81+
nodes[i] = node.(string)
82+
}
83+
84+
opt := golangsdk.RequestOpts{
85+
KeepResponseBody: true,
86+
MoreHeaders: map[string]string{
87+
"Content-Type": "application/json",
88+
},
89+
JSONBody: map[string]interface{}{
90+
"nodes": nodes,
91+
},
92+
}
93+
94+
_, err = client.Request("POST", restartPath, &opt)
95+
if err != nil {
96+
return diag.Errorf("error restarting nodes of the RocketMQ instance: %s", err)
97+
}
98+
99+
randUUID, err := uuid.GenerateUUID()
100+
if err != nil {
101+
return diag.Errorf("unable to generate resource ID: %s", err)
102+
}
103+
d.SetId(randUUID)
104+
105+
return resourceInstanceRestartRead(ctx, d, meta)
106+
}
107+
108+
func resourceInstanceRestartRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
109+
return nil
110+
}
111+
112+
func resourceInstanceRestartDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
113+
errorMsg := `This resource is a one-time action resource used to restart nodes of a RocketMQ instance. Deleting
114+
this resource will not clear the restart operation record, but will only remove the resource information from the
115+
tfstate file.`
116+
return diag.Diagnostics{
117+
diag.Diagnostic{
118+
Severity: diag.Warning,
119+
Summary: errorMsg,
120+
},
121+
}
122+
}

0 commit comments

Comments
 (0)