|
| 1 | +/* |
| 2 | +Provides a resource to create a vpc end_point |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_vpc_end_point" "end_point" { |
| 8 | + vpc_id = "vpc-391sv4w3" |
| 9 | + subnet_id = "subnet-ljyn7h30" |
| 10 | + end_point_name = "terraform-test" |
| 11 | + end_point_service_id = "vpcsvc-69y13tdb" |
| 12 | + end_point_vip = "10.0.2.1" |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +vpc end_point can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_vpc_end_point.end_point end_point_id |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 33 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | +) |
| 36 | + |
| 37 | +func resourceTencentCloudVpcEndPoint() *schema.Resource { |
| 38 | + return &schema.Resource{ |
| 39 | + Create: resourceTencentCloudVpcEndPointCreate, |
| 40 | + Read: resourceTencentCloudVpcEndPointRead, |
| 41 | + Update: resourceTencentCloudVpcEndPointUpdate, |
| 42 | + Delete: resourceTencentCloudVpcEndPointDelete, |
| 43 | + Importer: &schema.ResourceImporter{ |
| 44 | + State: schema.ImportStatePassthrough, |
| 45 | + }, |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "vpc_id": { |
| 48 | + Required: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "ID of vpc instance.", |
| 51 | + }, |
| 52 | + |
| 53 | + "subnet_id": { |
| 54 | + Required: true, |
| 55 | + Type: schema.TypeString, |
| 56 | + Description: "ID of subnet instance.", |
| 57 | + }, |
| 58 | + |
| 59 | + "end_point_name": { |
| 60 | + Required: true, |
| 61 | + Type: schema.TypeString, |
| 62 | + Description: "Name of endpoint.", |
| 63 | + }, |
| 64 | + |
| 65 | + "end_point_service_id": { |
| 66 | + Required: true, |
| 67 | + Type: schema.TypeString, |
| 68 | + Description: "ID of endpoint service.", |
| 69 | + }, |
| 70 | + |
| 71 | + "end_point_vip": { |
| 72 | + Optional: true, |
| 73 | + Type: schema.TypeString, |
| 74 | + Description: "VIP of endpoint ip.", |
| 75 | + }, |
| 76 | + |
| 77 | + "end_point_owner": { |
| 78 | + Computed: true, |
| 79 | + Type: schema.TypeString, |
| 80 | + Description: "APPID.", |
| 81 | + }, |
| 82 | + |
| 83 | + "state": { |
| 84 | + Computed: true, |
| 85 | + Type: schema.TypeString, |
| 86 | + Description: "state of end point.", |
| 87 | + }, |
| 88 | + |
| 89 | + "create_time": { |
| 90 | + Computed: true, |
| 91 | + Type: schema.TypeString, |
| 92 | + Description: "Create Time.", |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func resourceTencentCloudVpcEndPointCreate(d *schema.ResourceData, meta interface{}) error { |
| 99 | + defer logElapsed("resource.tencentcloud_vpc_end_point.create")() |
| 100 | + defer inconsistentCheck(d, meta)() |
| 101 | + |
| 102 | + logId := getLogId(contextNil) |
| 103 | + |
| 104 | + var ( |
| 105 | + request = vpc.NewCreateVpcEndPointRequest() |
| 106 | + response = vpc.NewCreateVpcEndPointResponse() |
| 107 | + endPointId string |
| 108 | + ) |
| 109 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 110 | + request.VpcId = helper.String(v.(string)) |
| 111 | + } |
| 112 | + |
| 113 | + if v, ok := d.GetOk("subnet_id"); ok { |
| 114 | + request.SubnetId = helper.String(v.(string)) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOk("end_point_name"); ok { |
| 118 | + request.EndPointName = helper.String(v.(string)) |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := d.GetOk("end_point_service_id"); ok { |
| 122 | + request.EndPointServiceId = helper.String(v.(string)) |
| 123 | + } |
| 124 | + |
| 125 | + if v, ok := d.GetOk("end_point_vip"); ok { |
| 126 | + request.EndPointVip = helper.String(v.(string)) |
| 127 | + } |
| 128 | + |
| 129 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 130 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().CreateVpcEndPoint(request) |
| 131 | + if e != nil { |
| 132 | + return retryError(e) |
| 133 | + } else { |
| 134 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 135 | + } |
| 136 | + response = result |
| 137 | + return nil |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + log.Printf("[CRITAL]%s create vpc endPoint failed, reason:%+v", logId, err) |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + endPointId = *response.Response.EndPoint.EndPointId |
| 145 | + d.SetId(endPointId) |
| 146 | + |
| 147 | + return resourceTencentCloudVpcEndPointRead(d, meta) |
| 148 | +} |
| 149 | + |
| 150 | +func resourceTencentCloudVpcEndPointRead(d *schema.ResourceData, meta interface{}) error { |
| 151 | + defer logElapsed("resource.tencentcloud_vpc_end_point.read")() |
| 152 | + defer inconsistentCheck(d, meta)() |
| 153 | + |
| 154 | + logId := getLogId(contextNil) |
| 155 | + |
| 156 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 157 | + |
| 158 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 159 | + |
| 160 | + endPointId := d.Id() |
| 161 | + |
| 162 | + endPoint, err := service.DescribeVpcEndPointById(ctx, endPointId) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + if endPoint == nil { |
| 168 | + d.SetId("") |
| 169 | + return fmt.Errorf("resource `track` %s does not exist", d.Id()) |
| 170 | + } |
| 171 | + |
| 172 | + if endPoint.VpcId != nil { |
| 173 | + _ = d.Set("vpc_id", endPoint.VpcId) |
| 174 | + } |
| 175 | + |
| 176 | + if endPoint.SubnetId != nil { |
| 177 | + _ = d.Set("subnet_id", endPoint.SubnetId) |
| 178 | + } |
| 179 | + |
| 180 | + if endPoint.EndPointName != nil { |
| 181 | + _ = d.Set("end_point_name", endPoint.EndPointName) |
| 182 | + } |
| 183 | + |
| 184 | + if endPoint.EndPointServiceId != nil { |
| 185 | + _ = d.Set("end_point_service_id", endPoint.EndPointServiceId) |
| 186 | + } |
| 187 | + |
| 188 | + if endPoint.EndPointVip != nil { |
| 189 | + _ = d.Set("end_point_vip", endPoint.EndPointVip) |
| 190 | + } |
| 191 | + |
| 192 | + if endPoint.EndPointOwner != nil { |
| 193 | + _ = d.Set("end_point_owner", endPoint.EndPointOwner) |
| 194 | + } |
| 195 | + |
| 196 | + if endPoint.State != nil { |
| 197 | + _ = d.Set("state", endPoint.State) |
| 198 | + } |
| 199 | + |
| 200 | + if endPoint.CreateTime != nil { |
| 201 | + _ = d.Set("create_time", endPoint.CreateTime) |
| 202 | + } |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func resourceTencentCloudVpcEndPointUpdate(d *schema.ResourceData, meta interface{}) error { |
| 208 | + defer logElapsed("resource.tencentcloud_vpc_end_point.update")() |
| 209 | + defer inconsistentCheck(d, meta)() |
| 210 | + |
| 211 | + logId := getLogId(contextNil) |
| 212 | + |
| 213 | + request := vpc.NewModifyVpcEndPointAttributeRequest() |
| 214 | + |
| 215 | + endPointId := d.Id() |
| 216 | + |
| 217 | + request.EndPointId = &endPointId |
| 218 | + |
| 219 | + unsupportedUpdateFields := []string{ |
| 220 | + "vpc_id", |
| 221 | + "subnet_id", |
| 222 | + "end_point_service_id", |
| 223 | + "end_point_vip", |
| 224 | + } |
| 225 | + for _, field := range unsupportedUpdateFields { |
| 226 | + if d.HasChange(field) { |
| 227 | + return fmt.Errorf("tencentcloud_vpc_end_point update on %s is not support yet", field) |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + if d.HasChange("end_point_name") { |
| 232 | + if v, ok := d.GetOk("end_point_name"); ok { |
| 233 | + request.EndPointName = helper.String(v.(string)) |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 238 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ModifyVpcEndPointAttribute(request) |
| 239 | + if e != nil { |
| 240 | + return retryError(e) |
| 241 | + } else { |
| 242 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 243 | + } |
| 244 | + return nil |
| 245 | + }) |
| 246 | + if err != nil { |
| 247 | + log.Printf("[CRITAL]%s create vpc endPoint failed, reason:%+v", logId, err) |
| 248 | + return err |
| 249 | + } |
| 250 | + |
| 251 | + return resourceTencentCloudVpcEndPointRead(d, meta) |
| 252 | +} |
| 253 | + |
| 254 | +func resourceTencentCloudVpcEndPointDelete(d *schema.ResourceData, meta interface{}) error { |
| 255 | + defer logElapsed("resource.tencentcloud_vpc_end_point.delete")() |
| 256 | + defer inconsistentCheck(d, meta)() |
| 257 | + |
| 258 | + logId := getLogId(contextNil) |
| 259 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 260 | + |
| 261 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 262 | + endPointId := d.Id() |
| 263 | + |
| 264 | + if err := service.DeleteVpcEndPointById(ctx, endPointId); err != nil { |
| 265 | + return nil |
| 266 | + } |
| 267 | + |
| 268 | + return nil |
| 269 | +} |
0 commit comments