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

provider/openstack: Add fixed ips to openstack ports #3772

Merged
merged 2 commits into from
Nov 6, 2015
Merged
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
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" = "${openstack_networking_subnet_v2.foo.id}"
"ip_address" = "192.168.199.23"
}
}

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 @@ -10,13 +10,15 @@ import (

"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/openstack/networking/v2/ports"
"github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
)

func TestAccNetworkingV2Port_basic(t *testing.T) {
region := os.Getenv(OS_REGION_NAME)

var network networks.Network
var port ports.Port
var subnet subnets.Subnet

var testAccNetworkingV2Port_basic = fmt.Sprintf(`
resource "openstack_networking_network_v2" "foo" {
Expand All @@ -25,12 +27,24 @@ func TestAccNetworkingV2Port_basic(t *testing.T) {
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "foo" {
region = "%s"
name = "subnet_1"
network_id = "${openstack_networking_network_v2.foo.id}"
cidr = "192.168.199.0/24"
ip_version = 4
}

resource "openstack_networking_port_v2" "foo" {
region = "%s"
name = "port_1"
network_id = "${openstack_networking_network_v2.foo.id}"
admin_state_up = "true"
}`, region, region)
fixed_ips {
subnet_id = "${openstack_networking_subnet_v2.foo.id}"
ip_address = "192.168.199.23"
}
}`, region, region, region)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -40,6 +54,7 @@ func TestAccNetworkingV2Port_basic(t *testing.T) {
resource.TestStep{
Config: testAccNetworkingV2Port_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.foo", &subnet),
testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.foo", &network),
testAccCheckNetworkingV2PortExists(t, "openstack_networking_port_v2.foo", &port),
),
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"
"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