Skip to content

Commit 0b46f95

Browse files
authored
add vpc dhcp (#1846)
* add vpc dhcp * add vpc dhcp
1 parent 6997c1f commit 0b46f95

12 files changed

+902
-0
lines changed

.changelog/1846.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_vpc_dhcp_ip
3+
```

tencentcloud/data_source_tc_postgresql_log_backups.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package tencentcloud
2020

2121
import (
2222
"context"
23+
2324
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2425
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2526
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ Virtual Private Cloud(VPC)
856856
tencentcloud_vpc_snapshot_policy_attachment
857857
tencentcloud_vpc_snapshot_policy_config
858858
tencentcloud_vpc_net_detect
859+
tencentcloud_vpc_dhcp_ip
859860
tencentcloud_subnet
860861
tencentcloud_security_group
861862
tencentcloud_security_group_rule
@@ -1886,6 +1887,9 @@ func Provider() *schema.Provider {
18861887
"tencentcloud_vpc_snapshot_policy_config": resourceTencentCloudVpcSnapshotPolicyConfig(),
18871888
"tencentcloud_vpc_net_detect": resourceTencentCloudVpcNetDetect(),
18881889
"tencentcloud_vpc_flow_log_config": resourceTencentCloudVpcFlowLogConfig(),
1890+
"tencentcloud_vpc_classic_link_attachment": resourceTencentCloudVpcClassicLinkAttachment(),
1891+
"tencentcloud_vpc_dhcp_ip": resourceTencentCloudVpcDhcpIp(),
1892+
"tencentcloud_vpc_dhcp_associate_address": resourceTencentCloudVpcDhcpAssociateAddress(),
18891893
"tencentcloud_ipv6_address_bandwidth": resourceTencentCloudIpv6AddressBandwidth(),
18901894
"tencentcloud_subnet": resourceTencentCloudVpcSubnet(),
18911895
"tencentcloud_route_entry": resourceTencentCloudRouteEntry(),
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Provides a resource to create a vpc classic_link_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_vpc_classic_link_attachment" "classic_link_attachment" {
8+
vpc_id = "vpc-hdvfe0g1"
9+
instance_ids = ["ins-ceynqvnu"]
10+
}
11+
```
12+
13+
Import
14+
15+
vpc classic_link_attachment can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_vpc_classic_link_attachment.classic_link_attachment classic_link_attachment_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"log"
27+
"strings"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
31+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
)
34+
35+
func resourceTencentCloudVpcClassicLinkAttachment() *schema.Resource {
36+
return &schema.Resource{
37+
Create: resourceTencentCloudVpcClassicLinkAttachmentCreate,
38+
Read: resourceTencentCloudVpcClassicLinkAttachmentRead,
39+
Delete: resourceTencentCloudVpcClassicLinkAttachmentDelete,
40+
Importer: &schema.ResourceImporter{
41+
State: schema.ImportStatePassthrough,
42+
},
43+
Schema: map[string]*schema.Schema{
44+
"vpc_id": {
45+
Required: true,
46+
ForceNew: true,
47+
Type: schema.TypeString,
48+
Description: "VPC instance ID.",
49+
},
50+
51+
"instance_ids": {
52+
Required: true,
53+
ForceNew: true,
54+
MaxItems: 1,
55+
Type: schema.TypeSet,
56+
Elem: &schema.Schema{
57+
Type: schema.TypeString,
58+
},
59+
Description: "CVM instance ID. It only support set one instance now.",
60+
},
61+
},
62+
}
63+
}
64+
65+
func resourceTencentCloudVpcClassicLinkAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
66+
defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.create")()
67+
defer inconsistentCheck(d, meta)()
68+
69+
logId := getLogId(contextNil)
70+
71+
var (
72+
request = vpc.NewAttachClassicLinkVpcRequest()
73+
vpcId string
74+
instanceId string
75+
)
76+
if v, ok := d.GetOk("vpc_id"); ok {
77+
vpcId = v.(string)
78+
request.VpcId = helper.String(v.(string))
79+
}
80+
81+
if v, ok := d.GetOk("instance_ids"); ok {
82+
instanceIdsSet := v.(*schema.Set).List()
83+
for i := range instanceIdsSet {
84+
instanceId = instanceIdsSet[i].(string)
85+
request.InstanceIds = append(request.InstanceIds, &instanceId)
86+
}
87+
}
88+
89+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
90+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AttachClassicLinkVpc(request)
91+
if e != nil {
92+
return retryError(e)
93+
} else {
94+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
95+
}
96+
return nil
97+
})
98+
if err != nil {
99+
log.Printf("[CRITAL]%s create vpc ClassicLinkAttachment failed, reason:%+v", logId, err)
100+
return err
101+
}
102+
103+
d.SetId(vpcId + FILED_SP + instanceId)
104+
105+
return resourceTencentCloudVpcClassicLinkAttachmentRead(d, meta)
106+
}
107+
108+
func resourceTencentCloudVpcClassicLinkAttachmentRead(d *schema.ResourceData, meta interface{}) error {
109+
defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.read")()
110+
defer inconsistentCheck(d, meta)()
111+
112+
logId := getLogId(contextNil)
113+
114+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
115+
116+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
117+
118+
idSplit := strings.Split(d.Id(), FILED_SP)
119+
if len(idSplit) != 2 {
120+
return fmt.Errorf("id is broken,%s", d.Id())
121+
}
122+
vpcId := idSplit[0]
123+
instanceId := idSplit[1]
124+
125+
classicLinkAttachment, err := service.DescribeVpcClassicLinkAttachmentById(ctx, vpcId, instanceId)
126+
if err != nil {
127+
return err
128+
}
129+
130+
if classicLinkAttachment == nil {
131+
d.SetId("")
132+
log.Printf("[WARN]%s resource `VpcClassicLinkAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
133+
return nil
134+
}
135+
136+
if classicLinkAttachment.VpcId != nil {
137+
_ = d.Set("vpc_id", classicLinkAttachment.VpcId)
138+
}
139+
140+
if classicLinkAttachment.InstanceId != nil {
141+
_ = d.Set("instance_ids", []*string{classicLinkAttachment.InstanceId})
142+
}
143+
144+
return nil
145+
}
146+
147+
func resourceTencentCloudVpcClassicLinkAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
148+
defer logElapsed("resource.tencentcloud_vpc_classic_link_attachment.delete")()
149+
defer inconsistentCheck(d, meta)()
150+
151+
logId := getLogId(contextNil)
152+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
153+
154+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
155+
idSplit := strings.Split(d.Id(), FILED_SP)
156+
if len(idSplit) != 2 {
157+
return fmt.Errorf("id is broken,%s", d.Id())
158+
}
159+
vpcId := idSplit[0]
160+
instanceId := idSplit[1]
161+
162+
if err := service.DeleteVpcClassicLinkAttachmentById(ctx, vpcId, instanceId); err != nil {
163+
return err
164+
}
165+
166+
return nil
167+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixVpcClassicLinkAttachmentResource_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: testAccVpcClassicLinkAttachment,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_vpc_classic_link_attachment.classic_link_attachment", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_vpc_classic_link_attachment.classic_link_attachment",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccVpcClassicLinkAttachment = `
31+
32+
resource "tencentcloud_vpc_classic_link_attachment" "classic_link_attachment" {
33+
vpc_id = "vpc-hdvfe0g1"
34+
instance_ids = ["ins-ceynqvnu"]
35+
}
36+
37+
`
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Provides a resource to create a vpc dhcp_associate_address
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_vpc_dhcp_associate_address" "dhcp_associate_address" {
8+
dhcp_ip_id = tencentcloud_vpc_dhcp_ip.dhcp_ip.id
9+
address_ip = "111.230.72.219"
10+
}
11+
```
12+
13+
Import
14+
15+
vpc dhcp_associate_address can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_vpc_dhcp_associate_address.dhcp_associate_address dhcp_associate_address_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"log"
27+
"strings"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
31+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
)
34+
35+
func resourceTencentCloudVpcDhcpAssociateAddress() *schema.Resource {
36+
return &schema.Resource{
37+
Create: resourceTencentCloudVpcDhcpAssociateAddressCreate,
38+
Read: resourceTencentCloudVpcDhcpAssociateAddressRead,
39+
Delete: resourceTencentCloudVpcDhcpAssociateAddressDelete,
40+
Importer: &schema.ResourceImporter{
41+
State: schema.ImportStatePassthrough,
42+
},
43+
Schema: map[string]*schema.Schema{
44+
"dhcp_ip_id": {
45+
Required: true,
46+
ForceNew: true,
47+
Type: schema.TypeString,
48+
Description: "`DhcpIp` unique `ID`, like: `dhcpip-9o233uri`. Must be a `DhcpIp` that is not bound to `EIP`.",
49+
},
50+
51+
"address_ip": {
52+
Required: true,
53+
ForceNew: true,
54+
Type: schema.TypeString,
55+
Description: "Elastic public network `IP`. Must be `EIP` not bound to `DhcpIp`.",
56+
},
57+
},
58+
}
59+
}
60+
61+
func resourceTencentCloudVpcDhcpAssociateAddressCreate(d *schema.ResourceData, meta interface{}) error {
62+
defer logElapsed("resource.tencentcloud_vpc_dhcp_associate_address.create")()
63+
defer inconsistentCheck(d, meta)()
64+
65+
logId := getLogId(contextNil)
66+
67+
var (
68+
request = vpc.NewAssociateDhcpIpWithAddressIpRequest()
69+
dhcpIpId string
70+
addressIp string
71+
)
72+
if v, ok := d.GetOk("dhcp_ip_id"); ok {
73+
dhcpIpId = v.(string)
74+
request.DhcpIpId = helper.String(v.(string))
75+
}
76+
77+
if v, ok := d.GetOk("address_ip"); ok {
78+
addressIp = v.(string)
79+
request.AddressIp = helper.String(v.(string))
80+
}
81+
82+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
83+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AssociateDhcpIpWithAddressIp(request)
84+
if e != nil {
85+
return retryError(e)
86+
} else {
87+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
88+
}
89+
return nil
90+
})
91+
if err != nil {
92+
log.Printf("[CRITAL]%s create vpc DhcpAssociateAddress failed, reason:%+v", logId, err)
93+
return err
94+
}
95+
96+
d.SetId(dhcpIpId + FILED_SP + addressIp)
97+
98+
return resourceTencentCloudVpcDhcpAssociateAddressRead(d, meta)
99+
}
100+
101+
func resourceTencentCloudVpcDhcpAssociateAddressRead(d *schema.ResourceData, meta interface{}) error {
102+
defer logElapsed("resource.tencentcloud_vpc_dhcp_associate_address.read")()
103+
defer inconsistentCheck(d, meta)()
104+
105+
logId := getLogId(contextNil)
106+
107+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
108+
109+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
110+
111+
idSplit := strings.Split(d.Id(), FILED_SP)
112+
if len(idSplit) != 2 {
113+
return fmt.Errorf("id is broken,%s", d.Id())
114+
}
115+
dhcpIpId := idSplit[0]
116+
addressIp := idSplit[1]
117+
118+
DhcpAssociateAddress, err := service.DescribeVpcDhcpAssociateAddressById(ctx, dhcpIpId, addressIp)
119+
if err != nil {
120+
return err
121+
}
122+
123+
if DhcpAssociateAddress == nil {
124+
d.SetId("")
125+
log.Printf("[WARN]%s resource `VpcDhcpAssociateAddress` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
126+
return nil
127+
}
128+
129+
if DhcpAssociateAddress.DhcpIpId != nil {
130+
_ = d.Set("dhcp_ip_id", DhcpAssociateAddress.DhcpIpId)
131+
}
132+
133+
if DhcpAssociateAddress.AddressIp != nil {
134+
_ = d.Set("address_ip", DhcpAssociateAddress.AddressIp)
135+
}
136+
137+
return nil
138+
}
139+
140+
func resourceTencentCloudVpcDhcpAssociateAddressDelete(d *schema.ResourceData, meta interface{}) error {
141+
defer logElapsed("resource.tencentcloud_vpc_dhcp_associate_address.delete")()
142+
defer inconsistentCheck(d, meta)()
143+
144+
logId := getLogId(contextNil)
145+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
146+
147+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
148+
idSplit := strings.Split(d.Id(), FILED_SP)
149+
if len(idSplit) != 2 {
150+
return fmt.Errorf("id is broken,%s", d.Id())
151+
}
152+
dhcpIpId := idSplit[0]
153+
154+
if err := service.DeleteVpcDhcpAssociateAddressById(ctx, dhcpIpId); err != nil {
155+
return err
156+
}
157+
158+
return nil
159+
}

0 commit comments

Comments
 (0)