Skip to content

Commit 378ebce

Browse files
committed
feat(rocketmq): the instance supports tls_mode field
1 parent 46eb60d commit 378ebce

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

docs/resources/dms_rocketmq_instance.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ The following arguments are supported:
7878
The description can contain a maximum of `1,024` characters.
7979

8080
* `ssl_enable` - (Optional, Bool, ForceNew) Specifies whether the RocketMQ SASL_SSL is enabled. Defaults to **false**.
81-
Changing this parameter will create a new resource.
81+
Changing this parameter will create a new resource.
82+
If this parameter is set to **true**, `tls_mode` can be omitted or must be set to **SSL**.
83+
If this parameter is set to **false**, `tls_mode` cannot be set to **SSL**.
8284

8385
* `ipv6_enable` - (Optional, Bool, ForceNew) Specifies whether to support IPv6. Defaults to **false**.
8486
Changing this parameter will create a new resource.
@@ -113,6 +115,12 @@ The following arguments are supported:
113115
* `configs` - (Optional, List) Specifies the instance configs.
114116
The [configs](#dms_configs) structure is documented below.
115117

118+
* `tls_mode` - (Optional, String) Specifies TLS mode of the instance.
119+
The valid values are as follows:
120+
+ **PLAINTEXT**
121+
+ **SSL**
122+
+ **PERMISSIVE**
123+
116124
<a name="dms_configs"></a>
117125
The `configs` block supports:
118126

huaweicloud/services/acceptance/rocketmq/resource_huaweicloud_dms_rocketmq_instance_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestAccDmsRocketMQInstance_basic(t *testing.T) {
7171
resource.TestCheckResourceAttr(resourceName, "storage_space", "500"),
7272
resource.TestCheckResourceAttrPair(resourceName, "engine_version", "data.huaweicloud_dms_rocketmq_flavors.test", "versions.0"),
7373
resource.TestCheckResourceAttrPair(resourceName, "flavor_id", "data.huaweicloud_dms_rocketmq_flavors.test", "flavors.0.id"),
74+
resource.TestCheckResourceAttr(resourceName, "tls_mode", "SSL"),
7475
),
7576
},
7677
{
@@ -89,6 +90,7 @@ func TestAccDmsRocketMQInstance_basic(t *testing.T) {
8990
resource.TestCheckResourceAttrPair(resourceName, "flavor_id", "data.huaweicloud_dms_rocketmq_flavors.test", "flavors.1.id"),
9091
resource.TestCheckResourceAttr(resourceName, "configs.0.name", "fileReservedTime"),
9192
resource.TestCheckResourceAttr(resourceName, "configs.0.value", "72"),
93+
resource.TestCheckResourceAttr(resourceName, "tls_mode", "PLAINTEXT"),
9294
),
9395
},
9496
{
@@ -337,6 +339,7 @@ resource "huaweicloud_dms_rocketmq_instance" "test" {
337339
flavor_id = local.flavor.id
338340
storage_space = 500
339341
broker_num = 1
342+
tls_mode = "SSL"
340343
341344
tags = {
342345
key1 = "value1"
@@ -381,6 +384,7 @@ resource "huaweicloud_dms_rocketmq_instance" "test" {
381384
flavor_id = local.newFlavor.id
382385
storage_space = 1200
383386
broker_num = 2
387+
tls_mode = "PLAINTEXT"
384388
385389
configs {
386390
name = "fileReservedTime"

huaweicloud/services/rocketmq/common.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package rocketmq
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
7+
"fmt"
8+
"strings"
9+
"time"
610

711
"github.com/chnsz/golangsdk"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
813

914
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
1015
)
@@ -32,3 +37,57 @@ func handleMultiOperationsError(err error) (bool, error) {
3237
}
3338
return false, err
3439
}
40+
41+
func getInstanceTaskById(client *golangsdk.ServiceClient, instanceId, taskId string) (interface{}, error) {
42+
getTaskHttpUrl := "v2/{project_id}/instances/{instance_id}/tasks/{task_id}"
43+
getTaskPath := client.Endpoint + getTaskHttpUrl
44+
getTaskPath = strings.ReplaceAll(getTaskPath, "{project_id}", client.ProjectID)
45+
getTaskPath = strings.ReplaceAll(getTaskPath, "{instance_id}", instanceId)
46+
getTaskPath = strings.ReplaceAll(getTaskPath, "{task_id}", taskId)
47+
opt := golangsdk.RequestOpts{
48+
KeepResponseBody: true,
49+
}
50+
resp, err := client.Request("GET", getTaskPath, &opt)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
return utils.FlattenResponse(resp)
56+
}
57+
58+
func refreshInstanceTaskStatus(client *golangsdk.ServiceClient, instanceID, taskId string) resource.StateRefreshFunc {
59+
return func() (interface{}, string, error) {
60+
respBody, err := getInstanceTaskById(client, instanceID, taskId)
61+
if err != nil {
62+
return respBody, "ERROR", err
63+
}
64+
65+
// status options: DELETED, SUCCESS, EXECUTING, FAILED, CREATED
66+
// CREATED means the task is in progress.
67+
status := utils.PathSearch("tasks[0].status", respBody, "").(string)
68+
if status == "FAILED" {
69+
return respBody, "FAILED", fmt.Errorf("unexpect status (%s)", status)
70+
}
71+
72+
if status == "SUCCESS" {
73+
return respBody, "COMPLETED", nil
74+
}
75+
76+
return "continue", "PENDING", nil
77+
}
78+
}
79+
80+
func waitForInstanceTaskStatusCompleted(ctx context.Context, client *golangsdk.ServiceClient, instanceId, taskId string,
81+
timeout time.Duration) error {
82+
stateConf := &resource.StateChangeConf{
83+
Pending: []string{"PENDING"},
84+
Target: []string{"COMPLETED"},
85+
Refresh: refreshInstanceTaskStatus(client, instanceId, taskId),
86+
Timeout: timeout,
87+
Delay: 10 * time.Second,
88+
PollInterval: 20 * time.Second,
89+
}
90+
91+
_, err := stateConf.WaitForStateContext(ctx)
92+
return err
93+
}

huaweicloud/services/rocketmq/resource_huaweicloud_dms_rocketmq_instance.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"net/http"
89
"strings"
910
"time"
1011

@@ -38,6 +39,7 @@ type dmsError struct {
3839
// @API RocketMQ POST /v2/{engine}/{project_id}/instances/{instance_id}/extend
3940
// @API RocketMQ PUT /v2/{project_id}/rocketmq/instances/{instance_id}/configs
4041
// @API RocketMQ GET /v2/{project_id}/rocketmq/instances/{instance_id}/configs
42+
// @API RocketMQ POST /v2/{project_id}/{engine}/instances/{instance_id}/plain-ssl-switch
4143
// @API EIP GET /v1/{project_id}/publicips
4244
// @API BSS GET /v2/orders/customer-orders/details/{order_id}
4345
// @API BSS POST /v2/orders/subscriptions/resources/autorenew/{instance_id}
@@ -184,6 +186,14 @@ func ResourceDmsRocketMQInstance() *schema.Resource {
184186
Computed: true,
185187
Description: `Specifies the instance configs.`,
186188
},
189+
// From the behavior of the Console page, this parameter is required.
190+
// In order to ensure that the default value is returned in future, set Computed behavior.
191+
"tls_mode": {
192+
Type: schema.TypeString,
193+
Optional: true,
194+
Computed: true,
195+
Description: `The TLS mode of the instance.`,
196+
},
187197
"status": {
188198
Type: schema.TypeString,
189199
Computed: true,
@@ -506,6 +516,7 @@ func buildCreateRocketmqInstanceBodyParams(d *schema.ResourceData, cfg *config.C
506516
"publicip_id": utils.ValueIgnoreEmpty(d.Get("publicip_id")),
507517
"broker_num": utils.ValueIgnoreEmpty(d.Get("broker_num")),
508518
"enterprise_project_id": utils.ValueIgnoreEmpty(cfg.GetEnterpriseProjectID(d)),
519+
"tls_mode": utils.ValueIgnoreEmpty(d.Get("tls_mode")),
509520
}
510521
if chargingMode, ok := d.GetOk("charging_mode"); ok && chargingMode == "prePaid" {
511522
bodyParams["bss_param"] = buildCreateRocketmqInstanceBodyBssParams(d)
@@ -579,6 +590,46 @@ func buildRocketmqConfigsRequestBody(configs []interface{}) []map[string]string
579590
return rst
580591
}
581592

593+
func updateRocketmqInstanceTLSMode(ctx context.Context, client *golangsdk.ServiceClient, timeout time.Duration,
594+
instanceId string, tlsMode string) error {
595+
httpUrl := "v2/{project_id}/rocketmq/instances/{instance_id}/plain-ssl-switch"
596+
updatePath := client.Endpoint + httpUrl
597+
updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID)
598+
updatePath = strings.ReplaceAll(updatePath, "{instance_id}", instanceId)
599+
600+
updateOpt := golangsdk.RequestOpts{
601+
KeepResponseBody: true,
602+
JSONBody: map[string]interface{}{
603+
"tls_mode": tlsMode,
604+
},
605+
}
606+
607+
retryFunc := func() (interface{}, bool, error) {
608+
resp, err := client.Request("POST", updatePath, &updateOpt)
609+
retry, err := handleMultiOperationsError(err)
610+
return resp, retry, err
611+
}
612+
resp, err := common.RetryContextWithWaitForState(&common.RetryContextWithWaitForStateParam{
613+
Ctx: ctx,
614+
RetryFunc: retryFunc,
615+
WaitFunc: rocketmqInstanceStateRefreshFunc(client, instanceId),
616+
WaitTarget: []string{"RUNNING"},
617+
Timeout: timeout,
618+
DelayTimeout: 10 * time.Second,
619+
PollInterval: 20 * time.Second,
620+
})
621+
if err != nil {
622+
return err
623+
}
624+
625+
respBody, err := utils.FlattenResponse(resp.(*http.Response))
626+
if err != nil {
627+
return err
628+
}
629+
630+
return waitForInstanceTaskStatusCompleted(ctx, client, instanceId, utils.PathSearch("job_id", respBody, "").(string), timeout)
631+
}
632+
582633
func resourceDmsRocketMQInstanceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
583634
cfg := meta.(*config.Config)
584635
region := cfg.GetRegion(d)
@@ -714,6 +765,14 @@ func resourceDmsRocketMQInstanceUpdate(ctx context.Context, d *schema.ResourceDa
714765
}
715766
}
716767

768+
if d.HasChange("tls_mode") {
769+
err = updateRocketmqInstanceTLSMode(ctx, updateRocketmqInstanceClient, d.Timeout(schema.TimeoutUpdate),
770+
instanceId, d.Get("tls_mode").(string))
771+
if err != nil {
772+
return diag.Errorf("error updating SSL mode of the RocketMQ instance (%s): %s", instanceId, err)
773+
}
774+
}
775+
717776
return resourceDmsRocketMQInstanceRead(ctx, d, meta)
718777
}
719778

@@ -882,6 +941,7 @@ func resourceDmsRocketMQInstanceRead(_ context.Context, d *schema.ResourceData,
882941
d.Set("resource_spec_code", utils.PathSearch("resource_spec_code", getRocketmqInstanceRespBody, nil)),
883942
d.Set("cross_vpc_accesses", crossVpcAccess),
884943
d.Set("charging_mode", chargingMode),
944+
d.Set("tls_mode", utils.PathSearch("tls_mode", getRocketmqInstanceRespBody, nil)),
885945
)
886946

887947
// get configs

0 commit comments

Comments
 (0)