|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccTencentCloudKafkaInstance(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + resource.Test(t, resource.TestCase{ |
| 15 | + PreCheck: func() { testAccPreCheck(t) }, |
| 16 | + Providers: testAccProviders, |
| 17 | + CheckDestroy: testAccTencentCloudKafkaInstanceDestroy, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + { |
| 20 | + Config: testAccKafkaInstance, |
| 21 | + Check: resource.ComposeTestCheckFunc( |
| 22 | + testAccCheckKafkaInstanceExists("tencentcloud_ckafka_instance.kafka_instance"), |
| 23 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "instance_name", "ckafka-instance-tf-test"), |
| 24 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "zone_id", "100006"), |
| 25 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "period", "1"), |
| 26 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "msg_retention_time", "1300"), |
| 27 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "renew_flag", "0"), |
| 28 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "kafka_version", "1.1.1"), |
| 29 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "disk_size", "500"), |
| 30 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "disk_type", "CLOUD_BASIC"), |
| 31 | + ), |
| 32 | + }, |
| 33 | + { |
| 34 | + Config: testAccKafkaInstanceUpdate, |
| 35 | + Check: resource.ComposeTestCheckFunc( |
| 36 | + testAccCheckKafkaInstanceExists("tencentcloud_ckafka_instance.kafka_instance"), |
| 37 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "instance_name", "ckafka-instance-tf-test"), |
| 38 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "zone_id", "100086"), |
| 39 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "period", "1"), |
| 40 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "msg_retention_time", "1200"), |
| 41 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "renew_flag", "0"), |
| 42 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "kafka_version", "1.1.1"), |
| 43 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "disk_size", "500"), |
| 44 | + resource.TestCheckResourceAttr("tencentcloud_ckafka_instance.kafka_instance", "disk_type", "CLOUD_BASIC"), |
| 45 | + ), |
| 46 | + }, |
| 47 | + { |
| 48 | + ResourceName: "tencentcloud_ckafka_instance.kafka_instance", |
| 49 | + ImportState: true, |
| 50 | + ImportStateVerify: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func testAccTencentCloudKafkaInstanceDestroy(s *terraform.State) error { |
| 57 | + logId := getLogId(contextNil) |
| 58 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 59 | + ckafkcService := CkafkaService{ |
| 60 | + client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn, |
| 61 | + } |
| 62 | + for _, r := range s.RootModule().Resources { |
| 63 | + if r.Type != "tencentcloud_ckafka_instance" { |
| 64 | + continue |
| 65 | + } |
| 66 | + _, has, error := ckafkcService.DescribeInstanceById(ctx, r.Primary.ID) |
| 67 | + if error != nil { |
| 68 | + return error |
| 69 | + } |
| 70 | + if !has { |
| 71 | + return nil |
| 72 | + } |
| 73 | + return fmt.Errorf("ckafka instance still exists: %s", r.Primary.ID) |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func testAccCheckKafkaInstanceExists(n string) resource.TestCheckFunc { |
| 79 | + return func(s *terraform.State) error { |
| 80 | + logId := getLogId(contextNil) |
| 81 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 82 | + |
| 83 | + rs, ok := s.RootModule().Resources[n] |
| 84 | + if !ok { |
| 85 | + return fmt.Errorf("ckafka instance %s is not found", n) |
| 86 | + } |
| 87 | + if rs.Primary.ID == "" { |
| 88 | + return fmt.Errorf("ckafka instance id is not set") |
| 89 | + } |
| 90 | + ckafkcService := CkafkaService{ |
| 91 | + client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn, |
| 92 | + } |
| 93 | + |
| 94 | + var exist bool |
| 95 | + outErr := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 96 | + _, has, inErr := ckafkcService.DescribeInstanceById(ctx, rs.Primary.ID) |
| 97 | + if inErr != nil { |
| 98 | + return retryError(inErr) |
| 99 | + } |
| 100 | + exist = has |
| 101 | + return nil |
| 102 | + }) |
| 103 | + if outErr != nil { |
| 104 | + return outErr |
| 105 | + } |
| 106 | + if !exist { |
| 107 | + return fmt.Errorf("ckafka instance doesn't exist: %s", rs.Primary.ID) |
| 108 | + } |
| 109 | + return nil |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +const testAccKafkaInstance = ` |
| 114 | +resource "tencentcloud_route_table" "rtb_test" { |
| 115 | + name = "rtb-test" |
| 116 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 117 | +} |
| 118 | +
|
| 119 | +resource "tencentcloud_subnet" "subnet_test" { |
| 120 | + name = "subnet-test" |
| 121 | + cidr_block = "10.0.1.0/24" |
| 122 | + availability_zone = "ap-guangzhou-6" |
| 123 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 124 | + route_table_id = "${tencentcloud_route_table.rtb_test.id}" |
| 125 | +} |
| 126 | +
|
| 127 | +resource "tencentcloud_vpc" "vpc_test" { |
| 128 | + name = "vpc-test" |
| 129 | + cidr_block = "10.0.0.0/16" |
| 130 | +} |
| 131 | +
|
| 132 | +resource "tencentcloud_ckafka_instance" "kafka_instance" { |
| 133 | + instance_name = "ckafka-instance-tf-test" |
| 134 | + zone_id = 100006 |
| 135 | + period = 1 |
| 136 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 137 | + subnet_id = "${tencentcloud_subnet.subnet_test.id}" |
| 138 | + msg_retention_time = 1300 |
| 139 | + renew_flag = 0 |
| 140 | + kafka_version = "1.1.1" |
| 141 | + disk_size = 500 |
| 142 | + disk_type = "CLOUD_BASIC" |
| 143 | +
|
| 144 | +
|
| 145 | + config { |
| 146 | + auto_create_topic_enable = true |
| 147 | + default_num_partitions = 3 |
| 148 | + default_replication_factor = 3 |
| 149 | + } |
| 150 | +
|
| 151 | + dynamic_retention_config { |
| 152 | + enable = 1 |
| 153 | + } |
| 154 | +} |
| 155 | +` |
| 156 | + |
| 157 | +const testAccKafkaInstanceUpdate = ` |
| 158 | +resource "tencentcloud_route_table" "rtb_test" { |
| 159 | + name = "rtb-test" |
| 160 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 161 | +} |
| 162 | +
|
| 163 | +resource "tencentcloud_subnet" "subnet_test" { |
| 164 | + name = "subnet-test" |
| 165 | + cidr_block = "10.0.1.0/24" |
| 166 | + availability_zone = "ap-guangzhou-6" |
| 167 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 168 | + route_table_id = "${tencentcloud_route_table.rtb_test.id}" |
| 169 | +} |
| 170 | +
|
| 171 | +resource "tencentcloud_vpc" "vpc_test" { |
| 172 | + name = "vpc-test" |
| 173 | + cidr_block = "10.0.0.0/16" |
| 174 | +} |
| 175 | +
|
| 176 | +resource "tencentcloud_ckafka_instance" "kafka_instance" { |
| 177 | + instance_name = "ckafka-instance-tf-test" |
| 178 | + zone_id = 100006 |
| 179 | + period = 1 |
| 180 | + vpc_id = "${tencentcloud_vpc.vpc_test.id}" |
| 181 | + subnet_id = "${tencentcloud_subnet.subnet_test.id}" |
| 182 | + msg_retention_time = 1200 |
| 183 | + renew_flag = 0 |
| 184 | + kafka_version = "1.1.1" |
| 185 | + disk_size = 500 |
| 186 | + disk_type = "CLOUD_BASIC" |
| 187 | +
|
| 188 | +
|
| 189 | + config { |
| 190 | + auto_create_topic_enable = true |
| 191 | + default_num_partitions = 3 |
| 192 | + default_replication_factor = 3 |
| 193 | + } |
| 194 | +
|
| 195 | + dynamic_retention_config { |
| 196 | + enable = 1 |
| 197 | + } |
| 198 | +} |
| 199 | +` |
0 commit comments