Skip to content

Commit b39561f

Browse files
committed
2 parents 3ae7199 + 1189b29 commit b39561f

20 files changed

+993
-9
lines changed

.changelog/2573.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:deprecation
2+
resource/tencentcloud_vpc_ipv6_eni_address
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_eni_ipv6_address
7+
```

.changelog/2574.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_eni_ipv4_address
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,8 @@ func Provider() *schema.Provider {
10501050
"tencentcloud_eni": vpc.ResourceTencentCloudEni(),
10511051
"tencentcloud_eni_attachment": vpc.ResourceTencentCloudEniAttachment(),
10521052
"tencentcloud_eni_sg_attachment": vpc.ResourceTencentCloudEniSgAttachment(),
1053+
"tencentcloud_eni_ipv6_address": vpc.ResourceTencentCloudEniIpv6Address(),
1054+
"tencentcloud_eni_ipv4_address": vpc.ResourceTencentCloudEniIpv4Address(),
10531055
"tencentcloud_ccn": ccn.ResourceTencentCloudCcn(),
10541056
"tencentcloud_ccn_attachment": ccn.ResourceTencentCloudCcnAttachment(),
10551057
"tencentcloud_ccn_bandwidth_limit": ccn.ResourceTencentCloudCcnBandwidthLimit(),

tencentcloud/provider.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ Virtual Private Cloud(VPC)
11991199
tencentcloud_eni
12001200
tencentcloud_eni_attachment
12011201
tencentcloud_eni_sg_attachment
1202+
tencentcloud_eni_ipv4_address
1203+
tencentcloud_eni_ipv6_address
12021204
tencentcloud_vpc
12031205
tencentcloud_vpc_acl
12041206
tencentcloud_vpc_acl_attachment
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
package vpc
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
10+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
12+
)
13+
14+
func ResourceTencentCloudEniIpv4Address() *schema.Resource {
15+
return &schema.Resource{
16+
Create: resourceTencentCloudEniIpv4AddressCreate,
17+
Read: resourceTencentCloudEniIpv4AddressRead,
18+
Delete: resourceTencentCloudEniIpv4AddressDelete,
19+
Importer: &schema.ResourceImporter{
20+
State: schema.ImportStatePassthrough,
21+
},
22+
Schema: map[string]*schema.Schema{
23+
"network_interface_id": {
24+
Required: true,
25+
ForceNew: true,
26+
Type: schema.TypeString,
27+
Description: "The ID of the ENI instance, such as `eni-m6dyj72l`.",
28+
},
29+
30+
"private_ip_addresses": {
31+
Optional: true,
32+
Type: schema.TypeSet,
33+
Computed: true,
34+
ForceNew: true,
35+
ConflictsWith: []string{"secondary_private_ip_address_count", "qos_level"},
36+
Description: "The information on private IP addresses, of which you can specify a maximum of 10 at a time. You should provide either this parameter or SecondaryPrivateIpAddressCount, or both.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"private_ip_address": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
ForceNew: true,
43+
Description: "Private IP address.",
44+
},
45+
"primary": {
46+
Type: schema.TypeBool,
47+
Optional: true,
48+
ForceNew: true,
49+
Computed: true,
50+
Description: "Whether it is a primary IP.",
51+
},
52+
"public_ip_address": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
ForceNew: true,
56+
Computed: true,
57+
Description: "Public IP address.",
58+
},
59+
"address_id": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
ForceNew: true,
63+
Computed: true,
64+
Description: "EIP instance ID, such as `eip-11112222`.",
65+
},
66+
"description": {
67+
Type: schema.TypeString,
68+
Optional: true,
69+
ForceNew: true,
70+
Computed: true,
71+
Description: "Private IP description.",
72+
},
73+
"is_wan_ip_blocked": {
74+
Type: schema.TypeBool,
75+
Optional: true,
76+
ForceNew: true,
77+
Computed: true,
78+
Description: "Whether the public IP is blocked.",
79+
},
80+
"state": {
81+
Type: schema.TypeString,
82+
Optional: true,
83+
ForceNew: true,
84+
Computed: true,
85+
Description: "IP status: `PENDING`: Creating, `MIGRATING`: Migrating, `DELETING`: Deleting, `AVAILABLE`: Available.",
86+
},
87+
"qos_level": {
88+
Type: schema.TypeString,
89+
Optional: true,
90+
ForceNew: true,
91+
Computed: true,
92+
Description: "IP service level. Values: PT` (Gold), `AU` (Silver), `AG `(Bronze) and DEFAULT` (Default).",
93+
},
94+
},
95+
},
96+
},
97+
98+
"secondary_private_ip_address_count": {
99+
Optional: true,
100+
Type: schema.TypeInt,
101+
Computed: true,
102+
ForceNew: true,
103+
ConflictsWith: []string{"private_ip_addresses"},
104+
Description: "The number of newly-applied private IP addresses. You should provide either this parameter or PrivateIpAddresses, or both. The total number of private IP addresses cannot exceed the quota.",
105+
},
106+
107+
"qos_level": {
108+
Optional: true,
109+
Computed: true,
110+
ForceNew: true,
111+
ConflictsWith: []string{"private_ip_addresses"},
112+
Type: schema.TypeString,
113+
Description: "IP service level. It is used together with `SecondaryPrivateIpAddressCount`. Values: PT`(Gold), `AU`(Silver), `AG `(Bronze) and DEFAULT (Default).",
114+
},
115+
},
116+
}
117+
}
118+
119+
func resourceTencentCloudEniIpv4AddressCreate(d *schema.ResourceData, meta interface{}) error {
120+
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv4_address.create")()
121+
defer tccommon.InconsistentCheck(d, meta)()
122+
123+
logId := tccommon.GetLogId(tccommon.ContextNil)
124+
125+
var (
126+
request = vpc.NewAssignPrivateIpAddressesRequest()
127+
networkInterfaceId string
128+
)
129+
if v, ok := d.GetOk("network_interface_id"); ok {
130+
networkInterfaceId = v.(string)
131+
request.NetworkInterfaceId = helper.String(v.(string))
132+
}
133+
134+
if v, ok := d.GetOk("private_ip_addresses"); ok {
135+
for _, item := range v.(*schema.Set).List() {
136+
dMap := item.(map[string]interface{})
137+
privateIpAddressSpecification := vpc.PrivateIpAddressSpecification{}
138+
if v, ok := dMap["private_ip_address"]; ok {
139+
privateIpAddressSpecification.PrivateIpAddress = helper.String(v.(string))
140+
}
141+
if v, ok := dMap["primary"]; ok {
142+
privateIpAddressSpecification.Primary = helper.Bool(v.(bool))
143+
}
144+
if v, ok := dMap["public_ip_address"]; ok && v != "" {
145+
privateIpAddressSpecification.PublicIpAddress = helper.String(v.(string))
146+
}
147+
if v, ok := dMap["address_id"]; ok && v != "" {
148+
privateIpAddressSpecification.AddressId = helper.String(v.(string))
149+
}
150+
if v, ok := dMap["description"]; ok && v != "" {
151+
privateIpAddressSpecification.Description = helper.String(v.(string))
152+
}
153+
if v, ok := dMap["is_wan_ip_blocked"]; ok {
154+
privateIpAddressSpecification.IsWanIpBlocked = helper.Bool(v.(bool))
155+
}
156+
if v, ok := dMap["state"]; ok && v != "" {
157+
privateIpAddressSpecification.State = helper.String(v.(string))
158+
}
159+
if v, ok := dMap["qos_level"]; ok && v != "" {
160+
privateIpAddressSpecification.QosLevel = helper.String(v.(string))
161+
}
162+
request.PrivateIpAddresses = append(request.PrivateIpAddresses, &privateIpAddressSpecification)
163+
}
164+
}
165+
166+
if v, ok := d.GetOkExists("secondary_private_ip_address_count"); ok {
167+
request.SecondaryPrivateIpAddressCount = helper.IntUint64(v.(int))
168+
}
169+
170+
if v, ok := d.GetOk("qos_level"); ok {
171+
request.QosLevel = helper.String(v.(string))
172+
}
173+
174+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
175+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssignPrivateIpAddresses(request)
176+
if e != nil {
177+
return tccommon.RetryError(e)
178+
} else {
179+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
180+
}
181+
return nil
182+
})
183+
if err != nil {
184+
log.Printf("[CRITAL]%s create vpc eniIpv4Address failed, reason:%+v", logId, err)
185+
return err
186+
}
187+
188+
d.SetId(networkInterfaceId)
189+
190+
return resourceTencentCloudEniIpv4AddressRead(d, meta)
191+
}
192+
193+
func resourceTencentCloudEniIpv4AddressRead(d *schema.ResourceData, meta interface{}) error {
194+
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv4_address.read")()
195+
defer tccommon.InconsistentCheck(d, meta)()
196+
197+
logId := tccommon.GetLogId(tccommon.ContextNil)
198+
199+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
200+
201+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
202+
203+
networkInterfaceId := d.Id()
204+
205+
enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId})
206+
207+
if err != nil {
208+
return err
209+
}
210+
211+
if len(enis) < 1 {
212+
d.SetId("")
213+
log.Printf("[WARN]%s resource `VpcIpv6EniAddress` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
214+
return nil
215+
}
216+
217+
eni := enis[0]
218+
219+
if eni.NetworkInterfaceId != nil {
220+
_ = d.Set("network_interface_id", eni.NetworkInterfaceId)
221+
}
222+
223+
ipv4s := make([]map[string]interface{}, 0, len(eni.PrivateIpAddressSet))
224+
for _, ipv4 := range eni.PrivateIpAddressSet {
225+
if !*ipv4.Primary {
226+
ipv4s = append(ipv4s, map[string]interface{}{
227+
"private_ip_address": ipv4.PrivateIpAddress,
228+
"primary": ipv4.Primary,
229+
"public_ip_address": ipv4.AddressId,
230+
"address_id": ipv4.AddressId,
231+
"description": ipv4.Description,
232+
"is_wan_ip_blocked": ipv4.IsWanIpBlocked,
233+
"state": ipv4.State,
234+
"qos_level": ipv4.QosLevel,
235+
})
236+
}
237+
}
238+
239+
_ = d.Set("network_interface_id", networkInterfaceId)
240+
_ = d.Set("private_ip_addresses", ipv4s)
241+
_ = d.Set("secondary_private_ip_address_count", len(ipv4s))
242+
if len(eni.PrivateIpAddressSet) > 0 {
243+
_ = d.Set("qos_level", eni.PrivateIpAddressSet[0].QosLevel)
244+
}
245+
246+
return nil
247+
}
248+
249+
func resourceTencentCloudEniIpv4AddressDelete(d *schema.ResourceData, meta interface{}) error {
250+
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv6_address.delete")()
251+
defer tccommon.InconsistentCheck(d, meta)()
252+
253+
logId := tccommon.GetLogId(tccommon.ContextNil)
254+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
255+
256+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
257+
networkInterfaceId := d.Id()
258+
259+
enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId})
260+
261+
if err != nil {
262+
return err
263+
}
264+
265+
if len(enis) < 1 {
266+
d.SetId("")
267+
log.Printf("[WARN]%s resource `EniIpv4Address` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
268+
return nil
269+
}
270+
271+
eni := enis[0]
272+
ipv4s := make([]*string, 0, len(eni.PrivateIpAddressSet))
273+
for _, ipv4 := range eni.PrivateIpAddressSet {
274+
if !*ipv4.Primary {
275+
ipv4s = append(ipv4s, ipv4.PrivateIpAddress)
276+
}
277+
}
278+
279+
if err := service.DeleteEniIpv4AddressById(ctx, networkInterfaceId, ipv4s); err != nil {
280+
return err
281+
}
282+
283+
return nil
284+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Provides a resource to create a vpc eni_ipv4_address
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_enis" "eni" {
7+
name = "Primary ENI"
8+
}
9+
10+
resource "tencentcloud_eni_ipv4_address" "eni_ipv4_address" {
11+
network_interface_id = data.tencentcloud_enis.eni.enis.0.id
12+
secondary_private_ip_address_count = 3
13+
}
14+
```
15+
16+
Import
17+
18+
vpc eni_ipv4_address can be imported using the id, e.g.
19+
20+
```
21+
terraform import tencentcloud_eni_ipv4_address.eni_ipv4_address eni_id
22+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package vpc_test
2+
3+
import (
4+
"testing"
5+
6+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudNeedFixEniIpv4AddressResource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
18+
Providers: tcacctest.AccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccEniIpv4Address,
22+
Check: resource.ComposeTestCheckFunc(
23+
//testAccCheckBandwidthPackageAttachmentExists("tencentcloud_vpc_ipv6_eni_address"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "network_interface_id"),
25+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.private_ip_address"),
26+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.primary"),
27+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.address_id"),
28+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.description"),
29+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.is_wan_ip_blocked"),
30+
resource.TestCheckResourceAttrSet("tencentcloud_eni_ipv4_address.eni_ipv4_address", "private_ip_addresses.0.state"),
31+
),
32+
},
33+
{
34+
ResourceName: "tencentcloud_eni_ipv4_address.eni_ipv4_address",
35+
ImportState: true,
36+
ImportStateVerify: true,
37+
},
38+
},
39+
})
40+
}
41+
42+
const testAccEniIpv4Address = `
43+
44+
data "tencentcloud_enis" "eni" {
45+
name = "Primary ENI"
46+
}
47+
48+
resource "tencentcloud_eni_ipv4_address" "eni_ipv4_address" {
49+
network_interface_id = data.tencentcloud_enis.eni.enis.0.id
50+
secondary_private_ip_address_count = 3
51+
}
52+
53+
`

0 commit comments

Comments
 (0)