|
| 1 | +/* |
| 2 | +Provides a resource to create a ckafka route |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_ckafka_route" "route" { |
| 8 | + instance_id = "ckafka-xxxxxx" |
| 9 | + vip_type = 3 |
| 10 | + vpc_id = "vpc-xxxxxx" |
| 11 | + subnet_id = "subnet-xxxxxx" |
| 12 | + access_type = 0 |
| 13 | + public_network = 3 |
| 14 | +} |
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | + ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" |
| 30 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 31 | +) |
| 32 | + |
| 33 | +func resourceTencentCloudCkafkaRoute() *schema.Resource { |
| 34 | + return &schema.Resource{ |
| 35 | + Create: resourceTencentCloudCkafkaRouteCreate, |
| 36 | + Read: resourceTencentCloudCkafkaRouteRead, |
| 37 | + Update: resourceTencentCloudCkafkaRouteUpdate, |
| 38 | + Delete: resourceTencentCloudCkafkaRouteDelete, |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "instance_id": { |
| 44 | + Required: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "Instance id.", |
| 47 | + }, |
| 48 | + |
| 49 | + "vip_type": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeInt, |
| 52 | + Description: "Routing network type (3:vpc routing; 4: standard support routing; 7: professional support routing).", |
| 53 | + }, |
| 54 | + |
| 55 | + "vpc_id": { |
| 56 | + Optional: true, |
| 57 | + Computed: true, |
| 58 | + Type: schema.TypeString, |
| 59 | + Description: "Vpc id.", |
| 60 | + }, |
| 61 | + |
| 62 | + "subnet_id": { |
| 63 | + Optional: true, |
| 64 | + Computed: true, |
| 65 | + Type: schema.TypeString, |
| 66 | + Description: "Subnet id.", |
| 67 | + }, |
| 68 | + |
| 69 | + "access_type": { |
| 70 | + Optional: true, |
| 71 | + Computed: true, |
| 72 | + Type: schema.TypeInt, |
| 73 | + Description: "Access type. Valid values:\n" + |
| 74 | + "- 0: PLAINTEXT (in clear text, supported by both the old version and the community version without user information)\n" + |
| 75 | + "- 1: SASL_PLAINTEXT (in clear text, but at the beginning of the data, authentication will be logged in through SASL, which is only supported by the community version)\n" + |
| 76 | + "- 2: SSL (SSL encrypted communication without user information, supported by both older and community versions)\n" + |
| 77 | + "- 3: SASL_SSL (SSL encrypted communication. When the data starts, authentication will be logged in through SASL. Only the community version supports it).", |
| 78 | + }, |
| 79 | + |
| 80 | + "auth_flag": { |
| 81 | + Optional: true, |
| 82 | + Type: schema.TypeInt, |
| 83 | + Description: "Auth flag.", |
| 84 | + }, |
| 85 | + |
| 86 | + "caller_appid": { |
| 87 | + Optional: true, |
| 88 | + Type: schema.TypeInt, |
| 89 | + Description: "Caller appid.", |
| 90 | + }, |
| 91 | + |
| 92 | + "public_network": { |
| 93 | + Optional: true, |
| 94 | + Type: schema.TypeInt, |
| 95 | + Description: "Public network.", |
| 96 | + }, |
| 97 | + |
| 98 | + "ip": { |
| 99 | + Optional: true, |
| 100 | + Computed: true, |
| 101 | + Type: schema.TypeString, |
| 102 | + Description: "Ip.", |
| 103 | + }, |
| 104 | + "vip_list": { |
| 105 | + Type: schema.TypeList, |
| 106 | + Computed: true, |
| 107 | + Description: "Virtual IP list.", |
| 108 | + Elem: &schema.Resource{ |
| 109 | + Schema: map[string]*schema.Schema{ |
| 110 | + "vip": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Computed: true, |
| 113 | + Description: "Virtual IP.", |
| 114 | + }, |
| 115 | + "vport": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Computed: true, |
| 118 | + Description: "Virtual port.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + "broker_vip_list": { |
| 124 | + Type: schema.TypeList, |
| 125 | + Computed: true, |
| 126 | + Description: "Virtual IP list (1 to 1 broker nodes).", |
| 127 | + Elem: &schema.Resource{ |
| 128 | + Schema: map[string]*schema.Schema{ |
| 129 | + "vip": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "Virtual IP.", |
| 133 | + }, |
| 134 | + "vport": { |
| 135 | + Type: schema.TypeString, |
| 136 | + Computed: true, |
| 137 | + Description: "Virtual port.", |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func resourceTencentCloudCkafkaRouteCreate(d *schema.ResourceData, meta interface{}) error { |
| 147 | + defer logElapsed("resource.tencentcloud_ckafka_route.create")() |
| 148 | + defer inconsistentCheck(d, meta)() |
| 149 | + |
| 150 | + logId := getLogId(contextNil) |
| 151 | + |
| 152 | + var ( |
| 153 | + request = ckafka.NewCreateRouteRequest() |
| 154 | + response = ckafka.NewCreateRouteResponse() |
| 155 | + instanceId string |
| 156 | + ) |
| 157 | + if v, ok := d.GetOk("instance_id"); ok { |
| 158 | + instanceId = v.(string) |
| 159 | + request.InstanceId = helper.String(instanceId) |
| 160 | + } |
| 161 | + |
| 162 | + if v, ok := d.GetOkExists("vip_type"); ok { |
| 163 | + request.VipType = helper.IntInt64(v.(int)) |
| 164 | + } |
| 165 | + |
| 166 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 167 | + request.VpcId = helper.String(v.(string)) |
| 168 | + } |
| 169 | + |
| 170 | + if v, ok := d.GetOk("subnet_id"); ok { |
| 171 | + request.SubnetId = helper.String(v.(string)) |
| 172 | + } |
| 173 | + |
| 174 | + if v, ok := d.GetOkExists("access_type"); ok { |
| 175 | + request.AccessType = helper.IntInt64(v.(int)) |
| 176 | + } |
| 177 | + |
| 178 | + if v, ok := d.GetOkExists("auth_flag"); ok { |
| 179 | + request.AuthFlag = helper.IntInt64(v.(int)) |
| 180 | + } |
| 181 | + |
| 182 | + if v, ok := d.GetOkExists("caller_appid"); ok { |
| 183 | + request.CallerAppid = helper.IntInt64(v.(int)) |
| 184 | + } |
| 185 | + |
| 186 | + if v, ok := d.GetOkExists("public_network"); ok { |
| 187 | + request.PublicNetwork = helper.IntInt64(v.(int)) |
| 188 | + } |
| 189 | + |
| 190 | + if v, ok := d.GetOk("ip"); ok { |
| 191 | + request.Ip = helper.String(v.(string)) |
| 192 | + } |
| 193 | + |
| 194 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 195 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCkafkaClient().CreateRoute(request) |
| 196 | + if e != nil { |
| 197 | + return retryError(e) |
| 198 | + } else { |
| 199 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 200 | + } |
| 201 | + response = result |
| 202 | + return nil |
| 203 | + }) |
| 204 | + if err != nil { |
| 205 | + log.Printf("[CRITAL]%s create ckafka route failed, reason:%+v", logId, err) |
| 206 | + return err |
| 207 | + } |
| 208 | + log.Printf("Result.Data: %+v", response.Response.Result.Data) |
| 209 | + routeIdInt64 := *response.Response.Result.Data.RouteDTO.RouteId |
| 210 | + flowIdInt64 := *response.Response.Result.Data.FlowId |
| 211 | + reouteIdString := strconv.FormatInt(routeIdInt64, 10) |
| 212 | + d.SetId(instanceId + FILED_SP + reouteIdString) |
| 213 | + |
| 214 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 215 | + |
| 216 | + conf := BuildStateChangeConf([]string{}, []string{"0"}, 1*readRetryTimeout, time.Second, service.CkafkaRouteStateRefreshFunc(flowIdInt64, []string{})) |
| 217 | + |
| 218 | + if _, e := conf.WaitForState(); e != nil { |
| 219 | + return e |
| 220 | + } |
| 221 | + |
| 222 | + return resourceTencentCloudCkafkaRouteRead(d, meta) |
| 223 | +} |
| 224 | + |
| 225 | +func resourceTencentCloudCkafkaRouteRead(d *schema.ResourceData, meta interface{}) error { |
| 226 | + defer logElapsed("resource.tencentcloud_ckafka_route.read")() |
| 227 | + defer inconsistentCheck(d, meta)() |
| 228 | + |
| 229 | + logId := getLogId(contextNil) |
| 230 | + |
| 231 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 232 | + |
| 233 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 234 | + |
| 235 | + items := strings.Split(d.Id(), FILED_SP) |
| 236 | + if len(items) < 2 { |
| 237 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 238 | + } |
| 239 | + instanceId := items[0] |
| 240 | + routeId := items[1] |
| 241 | + routeIdInt64, err := strconv.ParseInt(routeId, 10, 64) |
| 242 | + if err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + route, err := service.DescribeCkafkaRouteById(ctx, instanceId, routeIdInt64) |
| 246 | + if err != nil { |
| 247 | + return err |
| 248 | + } |
| 249 | + _ = d.Set("instance_id", instanceId) |
| 250 | + if route == nil { |
| 251 | + d.SetId("") |
| 252 | + log.Printf("[WARN]%s resource `CkafkaRoute` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 253 | + return nil |
| 254 | + } |
| 255 | + |
| 256 | + if route.VipType != nil { |
| 257 | + _ = d.Set("vip_type", route.VipType) |
| 258 | + } |
| 259 | + |
| 260 | + if route.VpcId != nil { |
| 261 | + _ = d.Set("vpc_id", route.VpcId) |
| 262 | + } |
| 263 | + |
| 264 | + if route.Subnet != nil { |
| 265 | + _ = d.Set("subnet_id", route.Subnet) |
| 266 | + } |
| 267 | + |
| 268 | + if route.AccessType != nil { |
| 269 | + _ = d.Set("access_type", route.AccessType) |
| 270 | + } |
| 271 | + |
| 272 | + if len(route.VipList) > 0 { |
| 273 | + _ = d.Set("ip", route.VipList[0].Vip) |
| 274 | + } |
| 275 | + vipList := make([]map[string]interface{}, 0) |
| 276 | + for _, vip := range route.VipList { |
| 277 | + vipList = append(vipList, map[string]interface{}{ |
| 278 | + "vip": vip.Vip, |
| 279 | + "vport": vip.Vport, |
| 280 | + }) |
| 281 | + } |
| 282 | + _ = d.Set("vip_list", vipList) |
| 283 | + brokerVipList := make([]map[string]interface{}, 0) |
| 284 | + for _, brokerVip := range route.BrokerVipList { |
| 285 | + brokerVipList = append(brokerVipList, map[string]interface{}{ |
| 286 | + "vip": brokerVip.Vip, |
| 287 | + "vport": brokerVip.Vport, |
| 288 | + }) |
| 289 | + } |
| 290 | + _ = d.Set("broker_vip_list", brokerVipList) |
| 291 | + |
| 292 | + return nil |
| 293 | +} |
| 294 | + |
| 295 | +func resourceTencentCloudCkafkaRouteUpdate(d *schema.ResourceData, meta interface{}) error { |
| 296 | + defer logElapsed("resource.tencentcloud_ckafka_route.update")() |
| 297 | + defer inconsistentCheck(d, meta)() |
| 298 | + |
| 299 | + immutableArgs := []string{"instance_id", "vip_type", "vpc_id", "subnet_id", "access_type", "auth_flag", "caller_appid", "public_network", "ip"} |
| 300 | + |
| 301 | + for _, v := range immutableArgs { |
| 302 | + if d.HasChange(v) { |
| 303 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 304 | + } |
| 305 | + } |
| 306 | + return resourceTencentCloudCkafkaRouteRead(d, meta) |
| 307 | +} |
| 308 | + |
| 309 | +func resourceTencentCloudCkafkaRouteDelete(d *schema.ResourceData, meta interface{}) error { |
| 310 | + defer logElapsed("resource.tencentcloud_ckafka_route.delete")() |
| 311 | + defer inconsistentCheck(d, meta)() |
| 312 | + |
| 313 | + logId := getLogId(contextNil) |
| 314 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 315 | + |
| 316 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 317 | + items := strings.Split(d.Id(), FILED_SP) |
| 318 | + if len(items) < 2 { |
| 319 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 320 | + } |
| 321 | + instanceId := items[0] |
| 322 | + routeId := items[1] |
| 323 | + routeIdInt64, err := strconv.ParseInt(routeId, 10, 64) |
| 324 | + if err != nil { |
| 325 | + return err |
| 326 | + } |
| 327 | + if err := service.DeleteCkafkaRouteById(ctx, instanceId, routeIdInt64); err != nil { |
| 328 | + return err |
| 329 | + } |
| 330 | + |
| 331 | + return nil |
| 332 | +} |
0 commit comments