Skip to content

Commit

Permalink
Merge branch 'feat/network-fix' into 'feat/support_unsubscribe'
Browse files Browse the repository at this point in the history
feat: add network interface private ip address modify

See merge request iaasng/terraform-provider-volcengine!296
  • Loading branch information
zpp12354321 committed Jun 27, 2023
2 parents eec2a2a + 4635a5d commit 9802cbd
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 15 deletions.
8 changes: 5 additions & 3 deletions example/networkInterface/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
resource "volcengine_network_interface" "foo" {
subnet_id = "subnet-im67x70vxla88gbssz1hy1z2"
security_group_ids = ["sg-im67wp9lx3i88gbssz3d22b2"]
primary_ip_address = "192.168.0.253"
subnet_id = "subnet-2fe79j7c8o5c059gp68ksxr93"
security_group_ids = ["sg-2fepz3c793g1s59gp67y21r34"]
primary_ip_address = "192.168.5.253"
network_interface_name = "tf-test-up"
description = "tf-test-up"
port_security_enabled = false
project_name = "default"
private_ip_address = ["192.168.5.2"]
//secondary_private_ip_address_count = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ func ResourceVolcengineNetworkInterface() *schema.Resource {
Description: "Set port security enable or disable.",
},
"secondary_private_ip_address_count": {
Type: schema.TypeInt,
Optional: true,
Description: "The count of secondary private ip address.",
Type: schema.TypeInt,
Optional: true,
Computed: true,
ConflictsWith: []string{"private_ip_address"},
Description: "The count of secondary private ip address. This field conflicts with `private_ip_address`.",
},
"private_ip_address": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of private ip address.",
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Computed: true,
Set: schema.HashString,
ConflictsWith: []string{"secondary_private_ip_address_count"},
Description: "The list of private ip address. This field conflicts with `secondary_private_ip_address_count`.",
},
"project_name": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network_interface
import (
"errors"
"fmt"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -46,7 +47,7 @@ func (s *VolcengineNetworkInterfaceService) ReadResources(m map[string]interface
return data, err
}
}

logger.Debug(logger.RespFormat, action, *resp)
results, err = ve.ObtainSdkValue("Result.NetworkInterfaceSets", *resp)
if err != nil {
return data, err
Expand Down Expand Up @@ -84,6 +85,22 @@ func (s *VolcengineNetworkInterfaceService) ReadResource(resourceData *schema.Re
if len(data) == 0 {
return data, fmt.Errorf("network_interface %s not exist ", id)
}
privateIpAddress := make([]string, 0)
if privateIpMap, ok := data["PrivateIpSets"].(map[string]interface{}); ok {
if privateIpSets, ok := privateIpMap["PrivateIpSet"].([]interface{}); ok {
for _, p := range privateIpSets {
if pMap, ok := p.(map[string]interface{}); ok {
isPrimary := pMap["Primary"].(bool)
ip := pMap["PrivateIpAddress"].(string)
if !isPrimary {
privateIpAddress = append(privateIpAddress, ip)
}
}
}
}
}
data["PrivateIpAddress"] = privateIpAddress
data["SecondaryPrivateIpAddressCount"] = len(privateIpAddress)
return data, err
}

Expand Down Expand Up @@ -186,11 +203,141 @@ func (s *VolcengineNetworkInterfaceService) ModifyResource(resourceData *schema.
TargetField: "SecurityGroupIds",
ConvertType: ve.ConvertWithN,
},
"private_ip_address": {
Ignore: true,
},
"secondary_private_ip_address_count": {
Ignore: true,
},
},
},
}
callbacks = append(callbacks, callback)

// 检查private_ip_address改变
if resourceData.HasChange("private_ip_address") {
add, remove, _, _ := ve.GetSetDifference("private_ip_address", resourceData, schema.HashString, false)
if remove.Len() > 0 {
callback = ve.Callback{
Call: ve.SdkCall{
Action: "UnassignPrivateIpAddresses",
ConvertMode: ve.RequestConvertInConvert,
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["NetworkInterfaceId"] = d.Id()
for index, r := range remove.List() {
(*call.SdkParam)["PrivateIpAddress."+strconv.Itoa(index+1)] = r
}
return true, nil
},
Convert: map[string]ve.RequestConvert{
"private_ip_address": {
Ignore: true,
},
"secondary_private_ip_address_count": {
Ignore: true,
},
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, callback)
}
if add.Len() > 0 {
callback = ve.Callback{
Call: ve.SdkCall{
Action: "AssignPrivateIpAddresses",
ConvertMode: ve.RequestConvertInConvert,
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["NetworkInterfaceId"] = d.Id()
for index, r := range add.List() {
(*call.SdkParam)["PrivateIpAddress."+strconv.Itoa(index+1)] = r
}
return true, nil
},
Convert: map[string]ve.RequestConvert{
"private_ip_address": {
Ignore: true,
},
"secondary_private_ip_address_count": {
Ignore: true,
},
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, callback)
}
}
// 检查secondary_private_ip_address_count改变
if resourceData.HasChange("secondary_private_ip_address_count") {
privateIpAddress := resourceData.Get("private_ip_address").(*schema.Set).List()
oldCount, newCount := resourceData.GetChange("secondary_private_ip_address_count")
if oldCount != nil && newCount != nil && newCount != len(privateIpAddress) {
diff := newCount.(int) - oldCount.(int)
if diff > 0 {
callback = ve.Callback{
Call: ve.SdkCall{
Action: "AssignPrivateIpAddresses",
ConvertMode: ve.RequestConvertInConvert,
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["NetworkInterfaceId"] = d.Id()
(*call.SdkParam)["SecondaryPrivateIpAddressCount"] = diff
return true, nil
},
Convert: map[string]ve.RequestConvert{
"private_ip_address": {
Ignore: true,
},
"secondary_private_ip_address_count": {
Ignore: true,
},
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, callback)
} else {
diff *= -1
removeIpAddress := privateIpAddress[:diff]
callback = ve.Callback{
Call: ve.SdkCall{
Action: "UnassignPrivateIpAddresses",
ConvertMode: ve.RequestConvertInConvert,
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
(*call.SdkParam)["NetworkInterfaceId"] = d.Id()
for index, r := range removeIpAddress {
(*call.SdkParam)["PrivateIpAddress."+strconv.Itoa(index+1)] = r
}
return true, nil
},
Convert: map[string]ve.RequestConvert{
"private_ip_address": {
Ignore: true,
},
"secondary_private_ip_address_count": {
Ignore: true,
},
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
},
}
callbacks = append(callbacks, callback)
}
}
}

// 更新Tags
setResourceTagsCallbacks := ve.SetResourceTags(s.Client, "TagResources", "UntagResources", "eni", resourceData, getUniversalInfo)
callbacks = append(callbacks, setResourceTagsCallbacks...)
Expand Down
12 changes: 7 additions & 5 deletions website/docs/r/network_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Provides a resource to manage network interface
## Example Usage
```hcl
resource "volcengine_network_interface" "foo" {
subnet_id = "subnet-im67x70vxla88gbssz1hy1z2"
security_group_ids = ["sg-im67wp9lx3i88gbssz3d22b2"]
primary_ip_address = "192.168.0.253"
subnet_id = "subnet-2fe79j7c8o5c059gp68ksxr93"
security_group_ids = ["sg-2fepz3c793g1s59gp67y21r34"]
primary_ip_address = "192.168.5.253"
network_interface_name = "tf-test-up"
description = "tf-test-up"
port_security_enabled = false
project_name = "default"
private_ip_address = ["192.168.5.2"]
//secondary_private_ip_address_count = 0
}
```
## Argument Reference
Expand All @@ -28,9 +30,9 @@ The following arguments are supported:
* `network_interface_name` - (Optional) The name of the ENI.
* `port_security_enabled` - (Optional) Set port security enable or disable.
* `primary_ip_address` - (Optional, ForceNew) The primary IP address of the ENI.
* `private_ip_address` - (Optional) The list of private ip address.
* `private_ip_address` - (Optional) The list of private ip address. This field conflicts with `secondary_private_ip_address_count`.
* `project_name` - (Optional) The ProjectName of the ENI.
* `secondary_private_ip_address_count` - (Optional) The count of secondary private ip address.
* `secondary_private_ip_address_count` - (Optional) The count of secondary private ip address. This field conflicts with `private_ip_address`.
* `tags` - (Optional) Tags.

The `tags` object supports the following:
Expand Down

0 comments on commit 9802cbd

Please sign in to comment.