Skip to content
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

resource/aws_network_interface: Use first of private_ips as primary #1672

Closed
wants to merge 4 commits into from
Closed
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
42 changes: 28 additions & 14 deletions aws/resource_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ func resourceAwsNetworkInterface() *schema.Resource {
},

"private_ips": &schema.Schema{
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"private_ips_count": &schema.Schema{
Expand Down Expand Up @@ -119,7 +118,7 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
request.Groups = expandStringList(security_groups)
}

private_ips := d.Get("private_ips").(*schema.Set).List()
private_ips := d.Get("private_ips").([]interface{})
if len(private_ips) != 0 {
request.PrivateIpAddresses = expandPrivateIPAddresses(private_ips)
}
Expand Down Expand Up @@ -274,21 +273,21 @@ func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{})
if d.HasChange("private_ips") {
o, n := d.GetChange("private_ips")
if o == nil {
o = new(schema.Set)
o = []interface{}{}
}
if n == nil {
n = new(schema.Set)
n = []interface{}{}
}

os := o.(*schema.Set)
ns := n.(*schema.Set)
os := o.([]interface{})
ns := n.([]interface{})

// Unassign old IP addresses
unassignIps := os.Difference(ns)
if unassignIps.Len() != 0 {
unassignIps := difference(ns, os)
if len(unassignIps) != 0 {
input := &ec2.UnassignPrivateIpAddressesInput{
NetworkInterfaceId: aws.String(d.Id()),
PrivateIpAddresses: expandStringList(unassignIps.List()),
PrivateIpAddresses: expandStringList(unassignIps),
}
_, err := conn.UnassignPrivateIpAddresses(input)
if err != nil {
Expand All @@ -297,11 +296,11 @@ func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{})
}

// Assign new IP addresses
assignIps := ns.Difference(os)
if assignIps.Len() != 0 {
assignIps := difference(os, ns)
if len(assignIps) != 0 {
input := &ec2.AssignPrivateIpAddressesInput{
NetworkInterfaceId: aws.String(d.Id()),
PrivateIpAddresses: expandStringList(assignIps.List()),
PrivateIpAddresses: expandStringList(assignIps),
}
_, err := conn.AssignPrivateIpAddresses(input)
if err != nil {
Expand All @@ -326,7 +325,7 @@ func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{})

if d.HasChange("private_ips_count") {
o, n := d.GetChange("private_ips_count")
private_ips := d.Get("private_ips").(*schema.Set).List()
private_ips := d.Get("private_ips").([]interface{})
private_ips_filtered := private_ips[:0]
primary_ip := d.Get("private_ip")

Expand Down Expand Up @@ -433,3 +432,18 @@ func resourceAwsEniAttachmentHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%d-", m["device_index"].(int)))
return hashcode.String(buf.String())
}

// returns items from list2 which are not contained in list1
func difference(list1, list2 []interface{}) []interface{} {
result := []interface{}{}
Items:
for _, item2 := range list2 {
for _, item1 := range list1 {
if item1 == item2 {
continue Items
}
}
result = append(result, item2)
}
return result
}
2 changes: 1 addition & 1 deletion website/docs/r/network_interface.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The following arguments are supported:

* `subnet_id` - (Required) Subnet ID to create the ENI in.
* `description` - (Optional) A description for the network interface.
* `private_ips` - (Optional) List of private IPs to assign to the ENI.
* `private_ips` - (Optional) List of private IPs to assign to the ENI; the first listed will be the primary.
* `private_ips_count` - (Optional) Number of private IPs to assign to the ENI.
* `security_groups` - (Optional) List of security group IDs to assign to the ENI.
* `attachment` - (Optional) Block to define the attachment of the ENI. Documented below.
Expand Down