From 71e33537041c3b0bb74e98dc50a165d87ab26b31 Mon Sep 17 00:00:00 2001 From: tongyiming Date: Tue, 19 Sep 2023 20:04:28 +0800 Subject: [PATCH] fix rocketmq enable_public (#2134) * fix rocketmq enable_public * add changelog * update * update --------- Co-authored-by: mikatong --- .changelog/2134.txt | 3 + tencentcloud/extension_trocket.go | 6 + tencentcloud/resource_tc_ckafka_instance.go | 2 - .../resource_tc_trocket_rocketmq_instance.go | 113 ++++++++++++------ ...ource_tc_trocket_rocketmq_instance_test.go | 84 ++++++++++++- tencentcloud/service_tencentcloud_trocket.go | 8 +- website/docs/r/ckafka_instance.html.markdown | 2 - .../r/trocket_rocketmq_instance.html.markdown | 34 +++++- 8 files changed, 204 insertions(+), 48 deletions(-) create mode 100644 .changelog/2134.txt create mode 100644 tencentcloud/extension_trocket.go diff --git a/.changelog/2134.txt b/.changelog/2134.txt new file mode 100644 index 0000000000..4316506d32 --- /dev/null +++ b/.changelog/2134.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/tencentcloud_trocket_rocketmq_instance: fix `enable_public` read problem +``` diff --git a/tencentcloud/extension_trocket.go b/tencentcloud/extension_trocket.go new file mode 100644 index 0000000000..9e5a8375fa --- /dev/null +++ b/tencentcloud/extension_trocket.go @@ -0,0 +1,6 @@ +package tencentcloud + +const ( + ENDPOINT_TYPE_PUBLIC = "PUBLIC" + ENDPOINT_TYPE_VPC = "VPC" +) diff --git a/tencentcloud/resource_tc_ckafka_instance.go b/tencentcloud/resource_tc_ckafka_instance.go index 1edba20af4..f4109173e1 100644 --- a/tencentcloud/resource_tc_ckafka_instance.go +++ b/tencentcloud/resource_tc_ckafka_instance.go @@ -1,8 +1,6 @@ /* Use this resource to create ckafka instance. -~> **NOTE:** It only support create prepaid ckafka instance. - Example Usage Basic Instance diff --git a/tencentcloud/resource_tc_trocket_rocketmq_instance.go b/tencentcloud/resource_tc_trocket_rocketmq_instance.go index 42e99fc7e2..510471717c 100644 --- a/tencentcloud/resource_tc_trocket_rocketmq_instance.go +++ b/tencentcloud/resource_tc_trocket_rocketmq_instance.go @@ -1,20 +1,41 @@ /* Provides a resource to create a rocketmq 5.x instance +~> **NOTE:** It only support create postpaid rocketmq 5.x instance. + Example Usage +Basic Instance ```hcl resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" { instance_type = "EXPERIMENT" - name = "test" + name = "rocketmq-instance" + sku_code = "experiment_500" + remark = "remark" + vpc_id = "vpc-xxxxxx" + subnet_id = "subnet-xxxxxx" + tags = { + tag_key = "rocketmq" + tag_value = "5.x" + } +} +``` + +Enable Public Instance +```hcl +resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance_public" { + instance_type = "EXPERIMENT" + name = "rocketmq-enable-public-instance" sku_code = "experiment_500" - remark = "test" + remark = "remark" vpc_id = "vpc-xxxxxx" subnet_id = "subnet-xxxxxx" tags = { tag_key = "rocketmq" tag_value = "5.x" } + enable_public = true + bandwidth = 1 } ``` @@ -97,14 +118,14 @@ func resourceTencentCloudTrocketRocketmqInstance() *schema.Resource { Optional: true, Computed: true, Type: schema.TypeBool, - Description: "Whether to enable the public network.", + Description: "Whether to enable the public network. Must set `bandwidth` when `enable_public` equal true.", }, "bandwidth": { Optional: true, Computed: true, Type: schema.TypeInt, - Description: "Public network bandwidth.", + Description: "Public network bandwidth. `bandwidth` must be greater than zero when `enable_public` equal true.", }, "ip_rules": { @@ -139,6 +160,18 @@ func resourceTencentCloudTrocketRocketmqInstance() *schema.Resource { Type: schema.TypeInt, Description: "Message retention time in hours.", }, + + "public_end_point": { + Type: schema.TypeString, + Computed: true, + Description: "Public network access address.", + }, + + "vpc_end_point": { + Type: schema.TypeString, + Computed: true, + Description: "VPC access address.", + }, }, } } @@ -150,9 +183,11 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m logId := getLogId(contextNil) ctx := context.WithValue(context.TODO(), logIdKey, logId) var ( - request = trocket.NewCreateInstanceRequest() - response = trocket.NewCreateInstanceResponse() - instanceId string + request = trocket.NewCreateInstanceRequest() + response = trocket.NewCreateInstanceResponse() + instanceId string + enablePublic bool + bandwidth int ) if v, ok := d.GetOk("instance_type"); ok { request.InstanceType = helper.String(v.(string)) @@ -178,13 +213,18 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m request.VpcList = []*trocket.VpcInfo{&vpcInfo} if v, ok := d.GetOkExists("enable_public"); ok { - request.EnablePublic = helper.Bool(v.(bool)) + enablePublic = v.(bool) + request.EnablePublic = helper.Bool(enablePublic) } if v, ok := d.GetOkExists("bandwidth"); ok { - request.Bandwidth = helper.IntInt64(v.(int)) + bandwidth = v.(int) + request.Bandwidth = helper.IntInt64(bandwidth) } + if enablePublic && bandwidth <= 0 { + return fmt.Errorf("`bandwidth` must be greater than zero when `enable_public` equal true.") + } if v, ok := d.GetOk("ip_rules"); ok { for _, item := range v.([]interface{}) { dMap := item.(map[string]interface{}) @@ -281,40 +321,43 @@ func resourceTencentCloudTrocketRocketmqInstanceRead(d *schema.ResourceData, met _ = d.Set("remark", rocketmqInstance.Remark) } - if len(rocketmqInstance.EndpointList) != 0 { - endpoint := rocketmqInstance.EndpointList[0] - if endpoint.VpcId != nil { - _ = d.Set("vpc_id", endpoint.VpcId) - } - - if endpoint.SubnetId != nil { - _ = d.Set("subnet_id", endpoint.SubnetId) + var enablePublic bool + for _, endpoint := range rocketmqInstance.EndpointList { + endpointType := endpoint.Type + if endpointType == nil { + continue } - - if len(endpoint.IpRules) != 0 { - ipRuleList := make([]interface{}, 0) - for _, ipRule := range endpoint.IpRules { - ipRuleMap := make(map[string]interface{}) - ipRuleMap["ip"] = ipRule.Ip - ipRuleMap["allow"] = ipRule.Allow - ipRuleMap["remark"] = ipRule.Remark - ipRuleList = append(ipRuleList, ipRuleMap) + if *endpointType == ENDPOINT_TYPE_PUBLIC { + enablePublic = true + if len(endpoint.IpRules) != 0 { + ipRuleList := make([]interface{}, 0) + for _, ipRule := range endpoint.IpRules { + ipRuleMap := make(map[string]interface{}) + ipRuleMap["ip"] = ipRule.Ip + ipRuleMap["allow"] = ipRule.Allow + ipRuleMap["remark"] = ipRule.Remark + ipRuleList = append(ipRuleList, ipRuleMap) + } + _ = d.Set("ip_rules", ipRuleList) } - _ = d.Set("ip_rules", ipRuleList) - } - if endpoint.Bandwidth != nil { - _ = d.Set("bandwidth", endpoint.Bandwidth) + if endpoint.Bandwidth != nil { + _ = d.Set("bandwidth", endpoint.Bandwidth) + } + _ = d.Set("public_end_point", endpoint.EndpointUrl) } + if *endpointType == ENDPOINT_TYPE_VPC { + if endpoint.VpcId != nil { + _ = d.Set("vpc_id", endpoint.VpcId) + } - if endpoint.Type != nil { - if *endpoint.Type == "PUBLIC" { - _ = d.Set("enable_public", true) - } else { - _ = d.Set("enable_public", true) + if endpoint.SubnetId != nil { + _ = d.Set("subnet_id", endpoint.SubnetId) } + _ = d.Set("vpc_end_point", endpoint.EndpointUrl) } } + _ = d.Set("enable_public", enablePublic) if rocketmqInstance.MessageRetention != nil { _ = d.Set("message_retention", rocketmqInstance.MessageRetention) diff --git a/tencentcloud/resource_tc_trocket_rocketmq_instance_test.go b/tencentcloud/resource_tc_trocket_rocketmq_instance_test.go index b276b257f5..b39c73e072 100644 --- a/tencentcloud/resource_tc_trocket_rocketmq_instance_test.go +++ b/tencentcloud/resource_tc_trocket_rocketmq_instance_test.go @@ -14,7 +14,27 @@ func TestAccTencentCloudTrocketRocketmqInstanceResource_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccTrocketRocketmqInstance, - Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "id")), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "id"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "name", "rocketmq-instance"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "sku_code", "experiment_500"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "remark", "remark"), + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "vpc_end_point"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "tags.tag_key", "rocketmq"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "tags.tag_value", "5.x"), + ), + }, + { + Config: testAccTrocketRocketmqInstanceUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "id"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "name", "rocketmq-instance-update"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "sku_code", "experiment_500"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "remark", "remark update"), + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "vpc_end_point"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "tags.tag_key", "rocketmq"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance", "tags.tag_value", "5.x.x"), + ), }, { ResourceName: "tencentcloud_trocket_rocketmq_instance.rocketmq_instance", @@ -25,17 +45,75 @@ func TestAccTencentCloudTrocketRocketmqInstanceResource_basic(t *testing.T) { }) } +func TestAccTencentCloudTrocketRocketmqInstanceResource_enablePublic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTrocketRocketmqInstancePublic, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "id"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "name", "rocketmq-enable-public-instance"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "enable_public", "true"), + resource.TestCheckResourceAttr("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "bandwidth", "1"), + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "public_end_point"), + resource.TestCheckResourceAttrSet("tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", "vpc_end_point"), + ), + }, + { + ResourceName: "tencentcloud_trocket_rocketmq_instance.rocketmq_instance_public", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + const testAccTrocketRocketmqInstance = ` resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" { instance_type = "EXPERIMENT" - name = "test" + name = "rocketmq-instance" + sku_code = "experiment_500" + remark = "remark" + vpc_id = "vpc-3a9fo1k9" + subnet_id = "subnet-8nby1yxg" + tags = { + tag_key = "rocketmq" + tag_value = "5.x" + } +} +` + +const testAccTrocketRocketmqInstanceUpdate = ` +resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" { + instance_type = "EXPERIMENT" + name = "rocketmq-instance-update" + sku_code = "experiment_500" + remark = "remark update" + vpc_id = "vpc-3a9fo1k9" + subnet_id = "subnet-8nby1yxg" + tags = { + tag_key = "rocketmq" + tag_value = "5.x.x" + } +} +` + +const testAccTrocketRocketmqInstancePublic = ` +resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance_public" { + instance_type = "EXPERIMENT" + name = "rocketmq-enable-public-instance" sku_code = "experiment_500" - remark = "test" + remark = "remark" vpc_id = "vpc-3a9fo1k9" subnet_id = "subnet-8nby1yxg" tags = { tag_key = "rocketmq" tag_value = "5.x" } + enable_public = true + bandwidth = 1 } ` diff --git a/tencentcloud/service_tencentcloud_trocket.go b/tencentcloud/service_tencentcloud_trocket.go index d643e90108..476b1a5122 100644 --- a/tencentcloud/service_tencentcloud_trocket.go +++ b/tencentcloud/service_tencentcloud_trocket.go @@ -73,7 +73,13 @@ func (me *TrocketService) TrocketRocketmqInstanceStateRefreshFunc(instanceId str if err != nil { return nil, "", err } - + if *object.InstanceStatus == "RUNNING" { + for _, endpoint := range object.EndpointList { + if *endpoint.Status != "OPEN" { + return object, "PROCESSING", nil + } + } + } return object, helper.PString(object.InstanceStatus), nil } } diff --git a/website/docs/r/ckafka_instance.html.markdown b/website/docs/r/ckafka_instance.html.markdown index 371cc9a2a1..9d7a0a8800 100644 --- a/website/docs/r/ckafka_instance.html.markdown +++ b/website/docs/r/ckafka_instance.html.markdown @@ -11,8 +11,6 @@ description: |- Use this resource to create ckafka instance. -~> **NOTE:** It only support create prepaid ckafka instance. - ## Example Usage ### Basic Instance diff --git a/website/docs/r/trocket_rocketmq_instance.html.markdown b/website/docs/r/trocket_rocketmq_instance.html.markdown index 62d5695c86..1ed0723970 100644 --- a/website/docs/r/trocket_rocketmq_instance.html.markdown +++ b/website/docs/r/trocket_rocketmq_instance.html.markdown @@ -11,20 +11,43 @@ description: |- Provides a resource to create a rocketmq 5.x instance +~> **NOTE:** It only support create postpaid rocketmq 5.x instance. + ## Example Usage +### Basic Instance + ```hcl resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance" { instance_type = "EXPERIMENT" - name = "test" + name = "rocketmq-instance" + sku_code = "experiment_500" + remark = "remark" + vpc_id = "vpc-xxxxxx" + subnet_id = "subnet-xxxxxx" + tags = { + tag_key = "rocketmq" + tag_value = "5.x" + } +} +``` + +### Enable Public Instance + +```hcl +resource "tencentcloud_trocket_rocketmq_instance" "rocketmq_instance_public" { + instance_type = "EXPERIMENT" + name = "rocketmq-enable-public-instance" sku_code = "experiment_500" - remark = "test" + remark = "remark" vpc_id = "vpc-xxxxxx" subnet_id = "subnet-xxxxxx" tags = { tag_key = "rocketmq" tag_value = "5.x" } + enable_public = true + bandwidth = 1 } ``` @@ -37,8 +60,8 @@ The following arguments are supported: * `sku_code` - (Required, String) SKU code. Available specifications are as follows: experiment_500, basic_1k, basic_2k, basic_4k, basic_6k. * `subnet_id` - (Required, String) Subnet id. * `vpc_id` - (Required, String) VPC id. -* `bandwidth` - (Optional, Int) Public network bandwidth. -* `enable_public` - (Optional, Bool) Whether to enable the public network. +* `bandwidth` - (Optional, Int) Public network bandwidth. `bandwidth` must be greater than zero when `enable_public` equal true. +* `enable_public` - (Optional, Bool) Whether to enable the public network. Must set `bandwidth` when `enable_public` equal true. * `ip_rules` - (Optional, List) Public network access whitelist. * `message_retention` - (Optional, Int) Message retention time in hours. * `remark` - (Optional, String) Remark. @@ -55,7 +78,8 @@ The `ip_rules` object supports the following: In addition to all arguments above, the following attributes are exported: * `id` - ID of the resource. - +* `public_end_point` - Public network access address. +* `vpc_end_point` - VPC access address. ## Import