Skip to content

feat(eni): [116613116] add eni ipv6 address #2573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/2573.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:deprecation
resource/tencentcloud_vpc_ipv6_eni_address
```

```release-note:new-resource
tencentcloud_eni_ipv6_address
```
1 change: 1 addition & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ func Provider() *schema.Provider {
"tencentcloud_eni": vpc.ResourceTencentCloudEni(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changelog 需要补下

"tencentcloud_eni_attachment": vpc.ResourceTencentCloudEniAttachment(),
"tencentcloud_eni_sg_attachment": vpc.ResourceTencentCloudEniSgAttachment(),
"tencentcloud_eni_ipv6_address": vpc.ResourceTencentCloudEniIpv6Address(),
"tencentcloud_ccn": ccn.ResourceTencentCloudCcn(),
"tencentcloud_ccn_attachment": ccn.ResourceTencentCloudCcnAttachment(),
"tencentcloud_ccn_bandwidth_limit": ccn.ResourceTencentCloudCcnBandwidthLimit(),
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ Virtual Private Cloud(VPC)
tencentcloud_eni
tencentcloud_eni_attachment
tencentcloud_eni_sg_attachment
tencentcloud_eni_ipv6_address
tencentcloud_vpc
tencentcloud_vpc_acl
tencentcloud_vpc_acl_attachment
Expand Down
251 changes: 251 additions & 0 deletions tencentcloud/services/vpc/resource_tc_eni_ipv6_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package vpc

import (
"context"
"fmt"
"log"
"time"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func ResourceTencentCloudEniIpv6Address() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudEniIpv6AddressCreate,
Read: resourceTencentCloudEniIpv6AddressRead,
Delete: resourceTencentCloudEniIpv6AddressDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"network_interface_id": {
Required: true,
Type: schema.TypeString,
ForceNew: true,
Description: "ENI instance `ID`, in the form of `eni-m6dyj72l`.",
},

"ipv6_addresses": {
Optional: true,
Type: schema.TypeSet,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"ipv6_address_count"},
Description: "The specified `IPv6` address list, up to 10 can be specified at a time. Combined with the input parameter `Ipv6AddressCount` to calculate the quota. Mandatory one with Ipv6AddressCount.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "`IPv6` address, in the form of: `3402:4e00:20:100:0:8cd9:2a67:71f3`.",
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Description.",
},
"primary": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Whether to master `IP`.",
},
"address_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "`EIP` instance `ID`, such as:`eip-hxlqja90`.",
},
"is_wan_ip_blocked": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Whether the public network IP is blocked.",
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "`IPv6` address status: `PENDING`: pending, `MIGRATING`: migrating, `DELETING`: deleting, `AVAILABLE`: available.",
},
},
},
},

"ipv6_address_count": {
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeInt,
ConflictsWith: []string{"ipv6_addresses"},
Description: "The number of automatically assigned IPv6 addresses and the total number of private IP addresses cannot exceed the quota. This should be combined with the input parameter `ipv6_addresses` for quota calculation. At least one of them, either this or 'Ipv6Addresses', must be provided.",
},
},
}
}

func resourceTencentCloudEniIpv6AddressCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv6_address.create")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

var (
request = vpc.NewAssignIpv6AddressesRequest()
response = vpc.NewAssignIpv6AddressesResponse()
networkInterfaceId string
)

if v, ok := d.GetOk("network_interface_id"); ok {
networkInterfaceId = v.(string)
request.NetworkInterfaceId = helper.String(v.(string))
}

if v, ok := d.GetOk("ipv6_addresses"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
ipv6Address := vpc.Ipv6Address{}
if v, ok := dMap["address"]; ok {
ipv6Address.Address = helper.String(v.(string))
}
if v, ok := dMap["primary"]; ok {
ipv6Address.Primary = helper.Bool(v.(bool))
}
if v, ok := dMap["address_id"]; ok {
ipv6Address.AddressId = helper.String(v.(string))
}
if v, ok := dMap["description"]; ok {
ipv6Address.Description = helper.String(v.(string))
}
if v, ok := dMap["is_wan_ip_blocked"]; ok {
ipv6Address.IsWanIpBlocked = helper.Bool(v.(bool))
}
if v, ok := dMap["state"]; ok {
ipv6Address.State = helper.String(v.(string))
}
request.Ipv6Addresses = append(request.Ipv6Addresses, &ipv6Address)
}
}

if v, ok := d.GetOkExists("ipv6_address_count"); ok {
request.Ipv6AddressCount = helper.IntUint64(v.(int))
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().AssignIpv6Addresses(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create vpc ipv6EniAddress failed, reason:%+v", logId, err)
return err
}

addressSet := response.Response.Ipv6AddressSet
if len(addressSet) < 1 {
return fmt.Errorf("assign ipv6 addresses failed.")
}

time.Sleep(5 * time.Second)

d.SetId(networkInterfaceId)

return resourceTencentCloudEniIpv6AddressRead(d, meta)
}

func resourceTencentCloudEniIpv6AddressRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv6_address.read")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

networkInterfaceId := d.Id()

enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId})

if err != nil {
return err
}

if len(enis) < 1 {
d.SetId("")
log.Printf("[WARN]%s resource `VpcIpv6EniAddress` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}

eni := enis[0]

ipv6s := make([]map[string]interface{}, 0, len(eni.Ipv6AddressSet))
for _, ipv6 := range eni.Ipv6AddressSet {
ipv6s = append(ipv6s, map[string]interface{}{
"address": ipv6.Address,
"primary": ipv6.Primary,
"address_id": ipv6.AddressId,
"description": ipv6.Description,
"is_wan_ip_blocked": ipv6.IsWanIpBlocked,
"state": ipv6.State,
})
}

_ = d.Set("network_interface_id", networkInterfaceId)
_ = d.Set("ipv6_addresses", ipv6s)
_ = d.Set("ipv6_address_count", len(eni.Ipv6AddressSet))

return nil
}

func resourceTencentCloudEniIpv6AddressDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_eni_ipv6_address.delete")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
networkInterfaceId := d.Id()

enis, err := service.DescribeEniById(ctx, []string{networkInterfaceId})

if err != nil {
return err
}

if len(enis) < 1 {
d.SetId("")
log.Printf("[WARN]%s resource `VpcIpv6EniAddress` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}

eni := enis[0]
ipv6s := make([]*string, 0, len(eni.Ipv6AddressSet))
for _, ipv6 := range eni.Ipv6AddressSet {
ipv6s = append(ipv6s, ipv6.Address)
}

if err := service.DeleteEniIpv6AddressById(ctx, networkInterfaceId, ipv6s); err != nil {
return err
}

return nil
}
53 changes: 53 additions & 0 deletions tencentcloud/services/vpc/resource_tc_eni_ipv6_address.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Provides a resource to create a vpc eni_ipv6_address

Example Usage

```hcl
data "tencentcloud_availability_zones" "zones" {}

resource "tencentcloud_vpc" "vpc" {
name = "vpc-example"
cidr_block = "10.0.0.0/16"
}

resource "tencentcloud_subnet" "subnet" {
availability_zone = data.tencentcloud_availability_zones.zones.zones.0.name
name = "subnet-example"
vpc_id = tencentcloud_vpc.vpc.id
cidr_block = "10.0.0.0/16"
is_multicast = false
}

resource "tencentcloud_eni" "eni" {
name = "eni-example"
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id
description = "eni desc."
ipv4_count = 1
}

resource "tencentcloud_vpc_ipv6_cidr_block" "example" {
vpc_id = tencentcloud_vpc.vpc.id
}

resource "tencentcloud_vpc_ipv6_subnet_cidr_block" "example" {
vpc_id = tencentcloud_vpc.vpc.id
ipv6_subnet_cidr_blocks {
subnet_id = tencentcloud_subnet.subnet.id
ipv6_cidr_block = "2402:4e00:1018:6700::/64"
}
}

resource "tencentcloud_eni_ipv6_address" "ipv6_eni_address" {
network_interface_id = tencentcloud_eni.eni.id
ipv6_address_count = 1
}
```

Import

vpc eni_ipv6_address can be imported using the id, e.g.

```
terraform import tencentcloud_eni_ipv6_address.ipv6_eni_address eni_id
```
Loading