Skip to content

Commit 5995b1c

Browse files
authored
add vpc resource (#1880)
* add vpc resource * add changelog
1 parent 5d24836 commit 5995b1c

10 files changed

+480
-0
lines changed

.changelog/1880.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-data-source
2+
tencentcloud_vpn_default_health_check_ip
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_ccn_instances_reject_attach
7+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Use this data source to query detailed information of vpn default_health_check_ip
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_vpn_default_health_check_ip" "default_health_check_ip" {
8+
vpn_gateway_id = "vpngw-gt8bianl"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudVpnDefaultHealthCheckIp() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudVpnDefaultHealthCheckIpRead,
26+
Schema: map[string]*schema.Schema{
27+
"vpn_gateway_id": {
28+
Required: true,
29+
Type: schema.TypeString,
30+
Description: "vpn gateway id.",
31+
},
32+
33+
"health_check_local_ip": {
34+
Computed: true,
35+
Type: schema.TypeString,
36+
Description: "local ip of health check.",
37+
},
38+
39+
"health_check_remote_ip": {
40+
Computed: true,
41+
Type: schema.TypeString,
42+
Description: "remote ip for health check.",
43+
},
44+
45+
"result_output_file": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Used to save results.",
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceTencentCloudVpnDefaultHealthCheckIpRead(d *schema.ResourceData, meta interface{}) error {
55+
defer logElapsed("data_source.tencentcloud_vpn_default_health_check_ip.read")()
56+
defer inconsistentCheck(d, meta)()
57+
58+
logId := getLogId(contextNil)
59+
60+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
61+
62+
var vpnGwId string
63+
res := make(map[string]interface{})
64+
65+
paramMap := make(map[string]interface{})
66+
if v, ok := d.GetOk("vpn_gateway_id"); ok {
67+
vpnGwId = v.(string)
68+
paramMap["VpnGatewayId"] = helper.String(v.(string))
69+
}
70+
71+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
72+
73+
var defaultHealthCheck *vpc.GenerateVpnConnectionDefaultHealthCheckIpResponseParams
74+
75+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
76+
result, e := service.DescribeVpnDefaultHealthCheckIp(ctx, paramMap)
77+
if e != nil {
78+
return retryError(e)
79+
}
80+
defaultHealthCheck = result
81+
return nil
82+
})
83+
if err != nil {
84+
return err
85+
}
86+
87+
if defaultHealthCheck.HealthCheckLocalIp != nil {
88+
_ = d.Set("health_check_local_ip", defaultHealthCheck.HealthCheckLocalIp)
89+
res["health_check_local_ip"] = defaultHealthCheck.HealthCheckLocalIp
90+
}
91+
92+
if defaultHealthCheck.HealthCheckRemoteIp != nil {
93+
_ = d.Set("health_check_remote_ip", defaultHealthCheck.HealthCheckRemoteIp)
94+
res["health_check_remote_ip"] = defaultHealthCheck.HealthCheckRemoteIp
95+
}
96+
97+
d.SetId(vpnGwId)
98+
output, ok := d.GetOk("result_output_file")
99+
if ok && output.(string) != "" {
100+
if e := writeToFile(output.(string), res); e != nil {
101+
return e
102+
}
103+
}
104+
return nil
105+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudVpnDefaultHealthCheckIpDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccVpnDefaultHealthCheckIpDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_vpn_default_health_check_ip.default_health_check_ip")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccVpnDefaultHealthCheckIpDataSource = `
26+
27+
data "tencentcloud_vpn_default_health_check_ip" "default_health_check_ip" {
28+
vpn_gateway_id = "vpngw-gt8bianl"
29+
}
30+
31+
`

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ Cloud Connect Network(CCN)
247247
tencentcloud_ccn_bandwidth_limit
248248
tencentcloud_ccn_routes
249249
tencentcloud_ccn_instances_accept_attach
250+
tencentcloud_ccn_instances_reject_attach
250251
tencentcloud_ccn_instances_reset_attach
251252
252253
CVM Dedicated Host(CDH)
@@ -955,6 +956,7 @@ VPN Connections(VPN)
955956
tencentcloud_vpn_gateways
956957
tencentcloud_vpn_gateway_routes
957958
tencentcloud_vpn_customer_gateway_vendors
959+
tencentcloud_vpn_default_health_check_ip
958960
959961
Resource
960962
tencentcloud_vpn_customer_gateway
@@ -1617,6 +1619,7 @@ func Provider() *schema.Provider {
16171619
"tencentcloud_vpn_gateway_routes": dataSourceTencentCloudVpnGatewayRoutes(),
16181620
"tencentcloud_vpn_connections": dataSourceTencentCloudVpnConnections(),
16191621
"tencentcloud_vpn_customer_gateway_vendors": dataSourceTencentCloudVpnCustomerGatewayVendors(),
1622+
"tencentcloud_vpn_default_health_check_ip": dataSourceTencentCloudVpnDefaultHealthCheckIp(),
16201623
"tencentcloud_ha_vips": dataSourceTencentCloudHaVips(),
16211624
"tencentcloud_ha_vip_eip_attachments": dataSourceTencentCloudHaVipEipAttachments(),
16221625
"tencentcloud_ccn_instances": dataSourceTencentCloudCcnInstances(),
@@ -2080,6 +2083,7 @@ func Provider() *schema.Provider {
20802083
"tencentcloud_ccn_bandwidth_limit": resourceTencentCloudCcnBandwidthLimit(),
20812084
"tencentcloud_ccn_routes": resourceTencentCloudCcnRoutes(),
20822085
"tencentcloud_ccn_instances_accept_attach": resourceTencentCloudCcnInstancesAcceptAttach(),
2086+
"tencentcloud_ccn_instances_reject_attach": resourceTencentCloudCcnInstancesRejectAttach(),
20832087
"tencentcloud_ccn_instances_reset_attach": resourceTencentCloudCcnInstancesResetAttach(),
20842088
"tencentcloud_dc_instance": resourceTencentCloudDcInstance(),
20852089
"tencentcloud_dcx": resourceTencentCloudDcxInstance(),
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
Provides a resource to create a vpc ccn_instances_reject_attach, you can use this resource to approve cross-region attachment.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_ccn_instances_reject_attach" "ccn_instances_reject_attach" {
8+
ccn_id = "ccn-39lqkygf"
9+
instances {
10+
instance_id = "vpc-j9yhbzpn"
11+
instance_region = "ap-guangzhou"
12+
instance_type = "VPC"
13+
}
14+
}
15+
```
16+
*/
17+
package tencentcloud
18+
19+
import (
20+
"fmt"
21+
"log"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
26+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
27+
)
28+
29+
func resourceTencentCloudCcnInstancesRejectAttach() *schema.Resource {
30+
return &schema.Resource{
31+
Create: resourceTencentCloudCcnInstancesRejectAttachCreate,
32+
Read: resourceTencentCloudCcnInstancesRejectAttachRead,
33+
Delete: resourceTencentCloudCcnInstancesRejectAttachDelete,
34+
Importer: &schema.ResourceImporter{
35+
State: schema.ImportStatePassthrough,
36+
},
37+
Schema: map[string]*schema.Schema{
38+
"ccn_id": {
39+
Required: true,
40+
ForceNew: true,
41+
Type: schema.TypeString,
42+
Description: "CCN Instance ID.",
43+
},
44+
45+
"instances": {
46+
Required: true,
47+
ForceNew: true,
48+
Type: schema.TypeList,
49+
Description: "Reject List Of Attachment Instances.",
50+
Elem: &schema.Resource{
51+
Schema: map[string]*schema.Schema{
52+
"instance_id": {
53+
Type: schema.TypeString,
54+
Required: true,
55+
Description: "Attachment Instance ID.",
56+
},
57+
"instance_region": {
58+
Type: schema.TypeString,
59+
Required: true,
60+
Description: "Instance Region.",
61+
},
62+
"instance_type": {
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Description: "InstanceType: `VPC`, `DIRECTCONNECT`, `BMVPC`, `VPNGW`.",
66+
},
67+
"description": {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
Description: "Description.",
71+
},
72+
"route_table_id": {
73+
Type: schema.TypeString,
74+
Optional: true,
75+
Description: "ID of the routing table associated with the instance. Note: This field may return null, indicating that no valid value can be obtained.",
76+
},
77+
},
78+
},
79+
},
80+
},
81+
}
82+
}
83+
84+
func resourceTencentCloudCcnInstancesRejectAttachCreate(d *schema.ResourceData, meta interface{}) error {
85+
defer logElapsed("resource.tencentcloud_ccn_instances_reject_attach.read")()
86+
defer inconsistentCheck(d, meta)()
87+
88+
logId := getLogId(contextNil)
89+
90+
var (
91+
request = vpc.NewRejectAttachCcnInstancesRequest()
92+
ccnId string
93+
)
94+
if v, ok := d.GetOk("ccn_id"); ok {
95+
ccnId = v.(string)
96+
request.CcnId = helper.String(v.(string))
97+
}
98+
99+
if v, ok := d.GetOk("instances"); ok {
100+
for _, item := range v.([]interface{}) {
101+
dMap := item.(map[string]interface{})
102+
ccnInstance := vpc.CcnInstance{}
103+
if v, ok := dMap["instance_id"]; ok {
104+
ccnInstance.InstanceId = helper.String(v.(string))
105+
}
106+
if v, ok := dMap["instance_region"]; ok {
107+
ccnInstance.InstanceRegion = helper.String(v.(string))
108+
}
109+
if v, ok := dMap["instance_type"]; ok {
110+
ccnInstance.InstanceType = helper.String(v.(string))
111+
}
112+
if v, ok := dMap["description"]; ok {
113+
ccnInstance.Description = helper.String(v.(string))
114+
}
115+
if v, ok := dMap["route_table_id"]; ok {
116+
routeTableId := v.(string)
117+
if routeTableId != "" {
118+
ccnInstance.RouteTableId = helper.String(v.(string))
119+
}
120+
}
121+
request.Instances = append(request.Instances, &ccnInstance)
122+
}
123+
}
124+
125+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
126+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().RejectAttachCcnInstances(request)
127+
if e != nil {
128+
return retryError(e)
129+
} else {
130+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
131+
}
132+
return nil
133+
})
134+
if err != nil {
135+
return fmt.Errorf("[CRITAL]%s operate vpc ccnInstancesRejectAttach failed, reason:%+v", logId, err)
136+
}
137+
138+
d.SetId(ccnId)
139+
140+
return resourceTencentCloudCcnInstancesRejectAttachRead(d, meta)
141+
}
142+
143+
func resourceTencentCloudCcnInstancesRejectAttachRead(d *schema.ResourceData, meta interface{}) error {
144+
defer logElapsed("resource.tencentcloud_ccn_instances_reject_attach.read")()
145+
defer inconsistentCheck(d, meta)()
146+
147+
return nil
148+
}
149+
150+
func resourceTencentCloudCcnInstancesRejectAttachDelete(d *schema.ResourceData, meta interface{}) error {
151+
defer logElapsed("resource.tencentcloud_ccn_instances_reject_attach.delete")()
152+
defer inconsistentCheck(d, meta)()
153+
154+
return nil
155+
}

tencentcloud/service_tencentcloud_dcg.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,61 @@ func (me *VpcService) CreateDirectConnectGatewayCcnRoute(ctx context.Context, dc
471471
return
472472
}
473473

474+
// not used, because if support, it will cause resource destroyed
475+
func (me *VpcService) ReplaceDirectConnectGatewayCcnRoute(ctx context.Context, dcgId, cidr string, asPaths []string) (routeId string, errRet error) {
476+
477+
logId := getLogId(ctx)
478+
479+
request := vpc.NewReplaceDirectConnectGatewayCcnRoutesRequest()
480+
request.DirectConnectGatewayId = &dcgId
481+
482+
var ccnRoute vpc.DirectConnectGatewayCcnRoute
483+
ccnRoute.DestinationCidrBlock = &cidr
484+
ccnRoute.ASPath = make([]*string, 0, len(asPaths))
485+
486+
for index := range asPaths {
487+
ccnRoute.ASPath = append(ccnRoute.ASPath, &asPaths[index])
488+
}
489+
request.Routes = []*vpc.DirectConnectGatewayCcnRoute{&ccnRoute}
490+
ratelimit.Check(request.GetAction())
491+
response, err := me.client.UseVpcClient().ReplaceDirectConnectGatewayCcnRoutes(request)
492+
493+
defer func() {
494+
if errRet != nil {
495+
responseStr := ""
496+
if response != nil {
497+
responseStr = response.ToJsonString()
498+
}
499+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s],response body [%s], reason[%s]\n",
500+
logId,
501+
request.GetAction(),
502+
request.ToJsonString(),
503+
responseStr,
504+
errRet.Error())
505+
}
506+
}()
507+
508+
if err != nil {
509+
errRet = err
510+
return
511+
}
512+
513+
routeIdTemp, has, err := me.GetCcnRouteId(ctx, dcgId, cidr, asPaths)
514+
515+
if err != nil {
516+
errRet = err
517+
return
518+
}
519+
520+
if has == 1 {
521+
routeId = routeIdTemp
522+
return
523+
} else {
524+
errRet = fmt.Errorf("after api `ReplaceDirectConnectGatewayCcnRoutes`, api `DescribeDirectConnectGatewayCcnRoutes` return null route info")
525+
}
526+
return
527+
}
528+
474529
func (me *VpcService) DeleteDirectConnectGatewayCcnRoute(ctx context.Context, dcgId, routeId string) (errRet error) {
475530

476531
logId := getLogId(ctx)

0 commit comments

Comments
 (0)