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

Networking v2: Add no_fixed_ip to openstack_networking_port_v2 #433

Merged
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
25 changes: 21 additions & 4 deletions openstack/resource_openstack_networking_port_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func resourceNetworkingPortV2() *schema.Resource {
Computed: true,
},
"fixed_ip": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Type: schema.TypeList,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"no_fixed_ip"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_id": &schema.Schema{
Expand All @@ -105,6 +106,12 @@ func resourceNetworkingPortV2() *schema.Resource {
},
},
},
"no_fixed_ip": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"fixed_ip"},
},
"allowed_address_pairs": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -327,7 +334,7 @@ func resourceNetworkingPortV2Update(d *schema.ResourceData, meta interface{}) er
updateOpts.DeviceID = d.Get("device_id").(string)
}

if d.HasChange("fixed_ip") {
if d.HasChange("fixed_ip") || d.HasChange("no_fixed_ip") {
hasChange = true
updateOpts.FixedIPs = resourcePortFixedIpsV2(d)
}
Expand Down Expand Up @@ -378,6 +385,16 @@ func resourcePortSecurityGroupsV2(v *schema.Set) []string {
}

func resourcePortFixedIpsV2(d *schema.ResourceData) interface{} {
// if no_fixed_ip was specified, then just return
// an empty array. Since no_fixed_ip is mutually
// exclusive to fixed_ip, we can safely do this.
//
// Since we're only concerned about no_fixed_ip
// being set to "true", GetOk is used.
if _, ok := d.GetOk("no_fixed_ip"); ok {
return []interface{}{}
}

rawIP := d.Get("fixed_ip").([]interface{})

if len(rawIP) == 0 {
Expand Down
128 changes: 128 additions & 0 deletions openstack/resource_openstack_networking_port_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,58 @@ func TestAccNetworkingV2Port_noSecurityGroups(t *testing.T) {
})
}

func TestAccNetworkingV2Port_noFixedIP(t *testing.T) {
var port ports.Port

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2PortDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2Port_noFixedIP_1,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortExists("openstack_networking_port_v2.port_1", &port),
resource.TestCheckResourceAttr(
"openstack_networking_port_v2.port_1", "all_fixed_ips.#", "0"),
),
},
resource.TestStep{
Config: testAccNetworkingV2Port_noFixedIP_2,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortExists("openstack_networking_port_v2.port_1", &port),
resource.TestCheckResourceAttr(
"openstack_networking_port_v2.port_1", "all_fixed_ips.#", "1"),
),
},
resource.TestStep{
Config: testAccNetworkingV2Port_noFixedIP_1,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortExists("openstack_networking_port_v2.port_1", &port),
resource.TestCheckResourceAttr(
"openstack_networking_port_v2.port_1", "all_fixed_ips.#", "0"),
),
},
resource.TestStep{
Config: testAccNetworkingV2Port_noFixedIP_3,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortExists("openstack_networking_port_v2.port_1", &port),
resource.TestCheckResourceAttr(
"openstack_networking_port_v2.port_1", "all_fixed_ips.#", "2"),
),
},
resource.TestStep{
Config: testAccNetworkingV2Port_noFixedIP_1,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortExists("openstack_networking_port_v2.port_1", &port),
resource.TestCheckResourceAttr(
"openstack_networking_port_v2.port_1", "all_fixed_ips.#", "0"),
),
},
},
})
}

func testAccCheckNetworkingV2PortDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
Expand Down Expand Up @@ -1387,3 +1439,79 @@ resource "openstack_networking_port_v2" "instance_port" {
}
}
`

const testAccNetworkingV2Port_noFixedIP_1 = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
name = "subnet_1"
cidr = "192.168.199.0/24"
ip_version = 4
network_id = "${openstack_networking_network_v2.network_1.id}"
}

resource "openstack_networking_port_v2" "port_1" {
name = "port_1"
admin_state_up = "true"
network_id = "${openstack_networking_network_v2.network_1.id}"
no_fixed_ip = true
}
`

const testAccNetworkingV2Port_noFixedIP_2 = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
name = "subnet_1"
cidr = "192.168.199.0/24"
ip_version = 4
network_id = "${openstack_networking_network_v2.network_1.id}"
}

resource "openstack_networking_port_v2" "port_1" {
name = "port_1"
admin_state_up = "true"
network_id = "${openstack_networking_network_v2.network_1.id}"

fixed_ip {
subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
ip_address = "192.168.199.23"
}
}
`

const testAccNetworkingV2Port_noFixedIP_3 = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
name = "subnet_1"
cidr = "192.168.199.0/24"
ip_version = 4
network_id = "${openstack_networking_network_v2.network_1.id}"
}

resource "openstack_networking_port_v2" "port_1" {
name = "port_1"
admin_state_up = "true"
network_id = "${openstack_networking_network_v2.network_1.id}"

fixed_ip {
subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
ip_address = "192.168.199.23"
}

fixed_ip {
subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
ip_address = "192.168.199.24"
}
}
`
12 changes: 8 additions & 4 deletions website/docs/r/networking_port_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ The following arguments are supported:
* `device_id` - (Optional) The ID of the device attached to the port. Changing this
creates a new port.

* `fixed_ip` - (Optional) An array of desired IPs for this port. The structure is
described below.
* `fixed_ip` - (Optional - Conflicts with `no_fixed_ip`) An array of desired IPs for
this port. The structure is described below.

* `no_fixed_ip` - (Optional - Conflicts with `fixed_ip`) Create a port with no fixed
IP address. This will also remove any fixed IPs previously set on a port. `true`
is the only valid value for this argument.

* `allowed_address_pairs` - (Optional) An IP/MAC Address pair of additional IP
addresses that can be active on this port. The structure is described
Expand All @@ -84,8 +88,8 @@ this port.
* `ip_address` - (Optional) IP address desired in the subnet for this port. If
you don't specify `ip_address`, an available IP address from the specified
subnet will be allocated to this port. This field will not be populated if it
is left blank. To retrieve the assigned IP address, use the `all_fixed_ips`
attribute.
is left blank or omitted. To retrieve the assigned IP address, use the
`all_fixed_ips` attribute.

The `allowed_address_pairs` block supports:

Expand Down