Skip to content

Commit

Permalink
provider/openstack: fixed_ips implementation for ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Shirinkin committed Nov 5, 2015
1 parent bf11be8 commit 3a63b48
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"

This comment has been minimized.

Copy link
@jtopjian

jtopjian Nov 5, 2015

Contributor

This will need changed to ${openstack_networking_subnet_v2.foo.id} as well.

"ip_address" = "172.24.4.2"
}
}
resource "openstack_compute_instance_v2" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

This comment has been minimized.

Copy link
@jtopjian

jtopjian Nov 5, 2015

Contributor

On first glance, I don't think the above will pass. The IP is invalid and the subnet ID is hard-coded.

One option would be to add a subnet block to this test and reference it with:

${openstack_networking_subnet_v2.foo.id}

This comment has been minimized.

Copy link
@jtopjian

jtopjian Nov 5, 2015

Contributor

I get the following when running the acceptance tests:

--- FAIL: TestAccNetworkingV2Port_basic (23.44s)
        testing.go:137: Step 0 error: Error applying: 1 error(s) occurred:

                * openstack_networking_port_v2.foo: Error creating OpenStack Neutron network: Expected HTTP response code [201 202] when accessing [POST http://10.1.0.112:9696/v2.0/ports], but got 404 instead
                {"NeutronError": {"message": "Subnet 008ba151-0b8c-4a67-98b5-0d2b87666062 could not be found", "type": "SubnetNotFound", "detail": ""}}
FAIL
exit status 1

To run the tests, I'm doing the following:

ubuntu@devstack1:~/go/src/github.com/hashicorp/terraform$ make testacc TEST=./builtin/providers/openstack TESTARGS='-run=AccNetworkingV2Port_basic' 2>&1 | tee ~/foo.log
}
}`, region, region)

resource.Test(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

This comment has been minimized.

Copy link
@jtopjian

jtopjian Nov 5, 2015

Contributor

And here as well.

"ip_address" = "172.24.4.2"
}
}
resource "openstack_compute_instance_v2" "instance_1" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3a63b48

Please sign in to comment.