Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tencentcloudstack/terraform-provi…
Browse files Browse the repository at this point in the history
…der-tencentcloud into feat/waf
  • Loading branch information
SevenEarth committed Sep 20, 2023
2 parents 8a0b712 + 71e3353 commit 79c3105
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .changelog/2134.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/tencentcloud_trocket_rocketmq_instance: fix `enable_public` read problem
```
6 changes: 6 additions & 0 deletions tencentcloud/extension_trocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tencentcloud

const (
ENDPOINT_TYPE_PUBLIC = "PUBLIC"
ENDPOINT_TYPE_VPC = "VPC"
)
2 changes: 0 additions & 2 deletions tencentcloud/resource_tc_ckafka_instance.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
Use this resource to create ckafka instance.
~> **NOTE:** It only support create prepaid ckafka instance.
Example Usage
Basic Instance
Expand Down
113 changes: 78 additions & 35 deletions tencentcloud/resource_tc_trocket_rocketmq_instance.go
Original file line number Diff line number Diff line change
@@ -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
}
```
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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.",
},
},
}
}
Expand All @@ -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))
Expand All @@ -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{})
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 81 additions & 3 deletions tencentcloud/resource_tc_trocket_rocketmq_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
`
8 changes: 7 additions & 1 deletion tencentcloud/service_tencentcloud_trocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 0 additions & 2 deletions website/docs/r/ckafka_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ description: |-

Use this resource to create ckafka instance.

~> **NOTE:** It only support create prepaid ckafka instance.

## Example Usage

### Basic Instance
Expand Down
Loading

0 comments on commit 79c3105

Please sign in to comment.