Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/resources/dms_rocketmq_node_batch_restart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "Distributed Message Service (DMS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dms_rocketmq_node_batch_restart"
description: |-
Using this resource to batch restart nodes of a RocketMQ instance within HuaweiCloud.
---

# huaweicloud_dms_rocketmq_node_batch_restart

Using this resource to batch restart nodes of a RocketMQ instance within HuaweiCloud.

-> This resource is only a one-time action resource for restarting nodes of RocketMQ instance. Deleting this resource
will not clear the corresponding request record, but will only remove the resource information from the tfstate file.

## Example Usage

```hcl
variable "instance_id" {}

data "huaweicloud_dms_rocketmq_instance_nodes" "test" {
instance_id = var.instance_id
}

resource "huaweicloud_dms_rocketmq_node_batch_restart" "test" {
instance_id = var.instance_id

nodes = [
data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[0].id,
data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[1].id
]
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to restart the nodes of a RocketMQ instance.
If omitted, the provider-level region will be used. Changing this parameter will create a new resource.

* `instance_id` - (Required, String, NonUpdatable) Specifies the ID of the RocketMQ instance to which nodes belong.

* `nodes` - (Required, List, NonUpdatable) Specifies the list of node IDs to be restarted.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID.
5 changes: 3 additions & 2 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2285,12 +2285,13 @@ func Provider() *schema.Provider {
"huaweicloud_dms_rocketmq_instance": rocketmq.ResourceDmsRocketMQInstance(),
"huaweicloud_dms_rocketmq_consumer_group": rocketmq.ResourceDmsRocketMQConsumerGroup(),
"huaweicloud_dms_rocketmq_consumption_verify": rocketmq.ResourceDmsRocketMQConsumptionVerify(),
"huaweicloud_dms_rocketmq_dead_letter_resend": rocketmq.ResourceDmsRocketMQDeadLetterResend(),
"huaweicloud_dms_rocketmq_message_offset_reset": rocketmq.ResourceDmsRocketMQMessageOffsetReset(),
"huaweicloud_dms_rocketmq_message_send": rocketmq.ResourceRocketMQMessageSend(),
"huaweicloud_dms_rocketmq_dead_letter_resend": rocketmq.ResourceDmsRocketMQDeadLetterResend(),
"huaweicloud_dms_rocketmq_migration_task": rocketmq.ResourceDmsRocketmqMigrationTask(),
"huaweicloud_dms_rocketmq_node_batch_restart": rocketmq.ResourceNodeBatchRestart(),
"huaweicloud_dms_rocketmq_topic": rocketmq.ResourceDmsRocketMQTopic(),
"huaweicloud_dms_rocketmq_user": rocketmq.ResourceDmsRocketMQUser(),
"huaweicloud_dms_rocketmq_migration_task": rocketmq.ResourceDmsRocketmqMigrationTask(),

"huaweicloud_dns_custom_line": dns.ResourceCustomLine(),
"huaweicloud_dns_ptrrecord": dns.ResourcePtrRecord(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rocketmq

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccNodeBatchRestart_basic(t *testing.T) {
var (
dataSourceName = "huaweicloud_dms_rocketmq_node_batch_restart.test"
instanceID = acceptance.HW_DMS_ROCKETMQ_INSTANCE_ID
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckDMSRocketMQInstanceID(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNodeBatchRestart_basic(instanceID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "instance_id", instanceID),
resource.TestCheckResourceAttr(dataSourceName, "nodes.#", "2"),
resource.TestCheckResourceAttrSet(dataSourceName, "nodes.0"),
),
},
},
})
}

func testAccNodeBatchRestart_basic(instanceID string) string {
return fmt.Sprintf(`
data "huaweicloud_dms_rocketmq_instance_nodes" "test" {
instance_id = "%[1]s"
}

resource "huaweicloud_dms_rocketmq_node_batch_restart" "test" {
instance_id = "%[1]s"

nodes = [
try(data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[0].id),
try(data.huaweicloud_dms_rocketmq_instance_nodes.test.nodes[1].id)
]
}
`, instanceID)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package rocketmq

import (
"context"
"strings"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

var nodeBatchRestartNonUpdatableParams = []string{"instance_id", "nodes"}

// @API RocketMQ POST /v2/{project_id}/{engine}/instances/{instance_id}/restart
func ResourceNodeBatchRestart() *schema.Resource {
return &schema.Resource{
CreateContext: resourceNodeBatchRestartCreate,
ReadContext: resourceNodeBatchRestartRead,
DeleteContext: resourceNodeBatchRestartDelete,

CustomizeDiff: customdiff.All(
config.FlexibleForceNew(nodeBatchRestartNonUpdatableParams),
),

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `The region in which to restart the nodes of a RocketMQ instance.`,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the RocketMQ instance to be restarted.`,
},
"nodes": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of node names to be restarted.`,
},

// Internal
"enable_force_new": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false),
Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}),
},
},
}
}

func resourceNodeBatchRestartCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("dmsv2", region)
if err != nil {
return diag.Errorf("error creating DMS client: %s", err)
}

httpUrl := "v2/{project_id}/rocketmq/instances/{instance_id}/restart"
httpUrl = strings.ReplaceAll(httpUrl, "{project_id}", client.ProjectID)
httpUrl = strings.ReplaceAll(httpUrl, "{instance_id}", d.Get("instance_id").(string))
restartPath := client.Endpoint + httpUrl

nodesRaw := d.Get("nodes").([]interface{})
nodes := make([]string, len(nodesRaw))
for i, node := range nodesRaw {
nodes[i] = node.(string)
}

opt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
JSONBody: map[string]interface{}{
"nodes": nodes,
},
}

_, err = client.Request("POST", restartPath, &opt)
if err != nil {
return diag.Errorf("error restarting nodes of the RocketMQ instance: %s", err)
}

randUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate resource ID: %s", err)
}
d.SetId(randUUID)

return resourceNodeBatchRestartRead(ctx, d, meta)
}

func resourceNodeBatchRestartRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceNodeBatchRestartDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := `This resource is a one-time action resource used to restart nodes of a RocketMQ instance. Deleting
this resource will not clear the restart operation record, but will only remove the resource information from the
tfstate file.`
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}
Loading