Add allowed_address_pairs

Original code from Rob Wilson <roobert@gmail.com>
This commit is contained in:
Federico Ceratto 2016-08-18 16:23:01 +01:00
parent e251d5c7bd
commit 5bc8736dc8
1 changed files with 61 additions and 9 deletions

View File

@ -96,6 +96,25 @@ func resourceNetworkingPortV2() *schema.Resource {
}, },
}, },
}, },
"allowed_address_pairs": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"mac_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
}, },
} }
} }
@ -108,15 +127,16 @@ func resourceNetworkingPortV2Create(d *schema.ResourceData, meta interface{}) er
} }
createOpts := ports.CreateOpts{ createOpts := ports.CreateOpts{
Name: d.Get("name").(string), Name: d.Get("name").(string),
AdminStateUp: resourcePortAdminStateUpV2(d), AdminStateUp: resourcePortAdminStateUpV2(d),
NetworkID: d.Get("network_id").(string), NetworkID: d.Get("network_id").(string),
MACAddress: d.Get("mac_address").(string), MACAddress: d.Get("mac_address").(string),
TenantID: d.Get("tenant_id").(string), TenantID: d.Get("tenant_id").(string),
DeviceOwner: d.Get("device_owner").(string), DeviceOwner: d.Get("device_owner").(string),
SecurityGroups: resourcePortSecurityGroupsV2(d), SecurityGroups: resourcePortSecurityGroupsV2(d),
DeviceID: d.Get("device_id").(string), DeviceID: d.Get("device_id").(string),
FixedIPs: resourcePortFixedIpsV2(d), FixedIPs: resourcePortFixedIpsV2(d),
AllowedAddressPairs: resourceAllowedAddressPairsV2(d),
} }
log.Printf("[DEBUG] Create Options: %#v", createOpts) log.Printf("[DEBUG] Create Options: %#v", createOpts)
@ -176,6 +196,16 @@ func resourceNetworkingPortV2Read(d *schema.ResourceData, meta interface{}) erro
} }
d.Set("fixed_ip", ips) d.Set("fixed_ip", ips)
// Convert AllowedAddressPairs to list of map
var pairs []map[string]interface{}
for _, pairObject := range p.AllowedAddressPairs {
pair := make(map[string]interface{})
pair["ip_address"] = pairObject.IPAddress
pair["mac_address"] = pairObject.MACAddress
pairs = append(pairs, pair)
}
d.Set("allowed_address_pairs", pairs)
return nil return nil
} }
@ -212,6 +242,10 @@ func resourceNetworkingPortV2Update(d *schema.ResourceData, meta interface{}) er
updateOpts.FixedIPs = resourcePortFixedIpsV2(d) updateOpts.FixedIPs = resourcePortFixedIpsV2(d)
} }
if d.HasChange("allowed_address_pairs") {
updateOpts.AllowedAddressPairs = resourceAllowedAddressPairsV2(d)
}
log.Printf("[DEBUG] Updating Port %s with options: %+v", d.Id(), updateOpts) log.Printf("[DEBUG] Updating Port %s with options: %+v", d.Id(), updateOpts)
_, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract() _, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract()
@ -272,7 +306,25 @@ func resourcePortFixedIpsV2(d *schema.ResourceData) interface{} {
} }
} }
return ip return ip
}
func resourceAllowedAddressPairsV2(d *schema.ResourceData) []ports.AddressPair {
// ports.AddressPair
rawPairs := d.Get("allowed_address_pairs").([]interface{})
if len(rawPairs) == 0 {
return nil
}
pairs := make([]ports.AddressPair, len(rawPairs))
for i, raw := range rawPairs {
rawMap := raw.(map[string]interface{})
pairs[i] = ports.AddressPair{
IPAddress: rawMap["ip_address"].(string),
MACAddress: rawMap["mac_address"].(string),
}
}
return pairs
} }
func resourcePortAdminStateUpV2(d *schema.ResourceData) *bool { func resourcePortAdminStateUpV2(d *schema.ResourceData) *bool {