provider/openstack: fixed_ips implementation for ports

This commit is contained in:
Kirill Shirinkin 2015-11-05 19:23:05 +01:00
parent bf11be82c8
commit 3a63b48f97
5 changed files with 60 additions and 2 deletions

View File

@ -150,7 +150,10 @@ func TestAccNetworkingV2Network_fullstack(t *testing.T) {
admin_state_up = "true"
security_groups = ["${openstack_compute_secgroup_v2.foo.id}"]
depends_on = ["openstack_networking_subnet_v2.foo"]
fixed_ips {
"subnet_id" = "008ba151-0b8c-4a67-98b5-0d2b87666062"
"ip_address" = "172.24.4.2"
}
}
resource "openstack_compute_instance_v2" "foo" {

View File

@ -78,6 +78,23 @@ func resourceNetworkingPortV2() *schema.Resource {
ForceNew: true,
Computed: true,
},
"fixed_ips": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
@ -98,6 +115,7 @@ func resourceNetworkingPortV2Create(d *schema.ResourceData, meta interface{}) er
DeviceOwner: d.Get("device_owner").(string),
SecurityGroups: resourcePortSecurityGroupsV2(d),
DeviceID: d.Get("device_id").(string),
FixedIPs: resourcePortFixedIpsV2(d),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
@ -146,6 +164,7 @@ func resourceNetworkingPortV2Read(d *schema.ResourceData, meta interface{}) erro
d.Set("device_owner", p.DeviceOwner)
d.Set("security_groups", p.SecurityGroups)
d.Set("device_id", p.DeviceID)
d.Set("fixed_ips", p.FixedIPs)
return nil
}
@ -179,6 +198,10 @@ func resourceNetworkingPortV2Update(d *schema.ResourceData, meta interface{}) er
updateOpts.DeviceID = d.Get("device_id").(string)
}
if d.HasChange("fixed_ips") {
updateOpts.FixedIPs = resourcePortFixedIpsV2(d)
}
log.Printf("[DEBUG] Updating Port %s with options: %+v", d.Id(), updateOpts)
_, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract()
@ -223,6 +246,20 @@ func resourcePortSecurityGroupsV2(d *schema.ResourceData) []string {
return groups
}
func resourcePortFixedIpsV2(d *schema.ResourceData) []ports.IP {
rawIP := d.Get("fixed_ips").([]interface{})
ip := make([]ports.IP, len(rawIP))
for i, raw := range rawIP {
rawMap := raw.(map[string]interface{})
ip[i] = ports.IP{
SubnetID: rawMap["subnet_id"].(string),
IPAddress: rawMap["ip_address"].(string),
}
}
return ip
}
func resourcePortAdminStateUpV2(d *schema.ResourceData) *bool {
value := false

View File

@ -30,6 +30,10 @@ func TestAccNetworkingV2Port_basic(t *testing.T) {
name = "port_1"
network_id = "${openstack_networking_network_v2.foo.id}"
admin_state_up = "true"
fixed_ips {
ip_address = "192.168.0.0"
subnet_id = "008ba151-0b8c-4a67-98b5-0d2b87666062"
}
}`, region, region)
resource.Test(t, resource.TestCase{

View File

@ -42,7 +42,10 @@ resource "openstack_networking_port_v2" "port_1" {
admin_state_up = "true"
security_groups = ["${openstack_compute_secgroup_v2.secgroup_1.id}"]
depends_on = ["openstack_networking_subnet_v2.subnet_1"]
fixed_ips {
"subnet_id" = "008ba151-0b8c-4a67-98b5-0d2b87666062"
"ip_address" = "172.24.4.2"
}
}
resource "openstack_compute_instance_v2" "instance_1" {

View File

@ -60,6 +60,17 @@ The following arguments are supported:
* `device_id` - (Optional) The ID of the device attached to the port. Changing this
creates a new port.
* `fixed_ips` - (Optional) An array of desired IPs for this port.
The `fixed_ips` block supports:
* `subnet_id` - (Required) Subnet in which to allocate IP address for
this port.
* `ip_address` - (Required) IP address desired in the subnet for this
port.
## Attributes Reference
The following attributes are exported: