Skip to content

Commit

Permalink
Networking V2: add QoS policy field for Port (gophercloud#781)
Browse files Browse the repository at this point in the history
Add "qos_policy_id" attribute for "openstack_networking_port_v2"
resource.

Update tests and docs.
  • Loading branch information
ozerovandrei committed Jun 23, 2019
1 parent e0d4f92 commit b84fad2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
2 changes: 2 additions & 0 deletions openstack/networking_port_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/extradhcpopts"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsecurity"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -23,6 +24,7 @@ type portExtended struct {
portsecurity.PortSecurityExt
portsbinding.PortsBindingExt
dns.PortDNSExt
policies.QoSPolicyExt
}

func resourceNetworkingPortV2StateRefreshFunc(client *gophercloud.ServiceClient, portID string) resource.StateRefreshFunc {
Expand Down
33 changes: 29 additions & 4 deletions openstack/resource_openstack_networking_port_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"

"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/dns"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/extradhcpopts"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsecurity"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceNetworkingPortV2() *schema.Resource {
Expand Down Expand Up @@ -264,6 +266,13 @@ func resourceNetworkingPortV2() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},

"qos_policy_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -368,6 +377,13 @@ func resourceNetworkingPortV2Create(d *schema.ResourceData, meta interface{}) er
}
}

if qosPolicyID := d.Get("qos_policy_id").(string); qosPolicyID != "" {
finalCreateOpts = policies.PortCreateOptsExt{
CreateOptsBuilder: finalCreateOpts,
QoSPolicyID: qosPolicyID,
}
}

log.Printf("[DEBUG] openstack_networking_port_v2 create options: %#v", finalCreateOpts)

// Create a Neutron port and set extra options if they're specified.
Expand Down Expand Up @@ -451,6 +467,7 @@ func resourceNetworkingPortV2Read(d *schema.ResourceData, meta interface{}) erro
d.Set("binding", flattenNetworkingPortBindingV2(port))
d.Set("dns_name", port.DNSName)
d.Set("dns_assignment", port.DNSAssignment)
d.Set("qos_policy_id", port.QoSPolicyID)

d.Set("region", GetRegion(d, config))

Expand Down Expand Up @@ -611,6 +628,14 @@ func resourceNetworkingPortV2Update(d *schema.ResourceData, meta interface{}) er
}
}

if d.HasChange("qos_policy_id") {
qosPolicyID := d.Get("qos_policy_id").(string)
finalUpdateOpts = policies.PortUpdateOptsExt{
UpdateOptsBuilder: finalUpdateOpts,
QoSPolicyID: &qosPolicyID,
}
}

// At this point, perform the update for all "standard" port changes.
if hasChange {
log.Printf("[DEBUG] openstack_networking_port_v2 %s update options: %#v", d.Id(), finalUpdateOpts)
Expand Down
102 changes: 100 additions & 2 deletions openstack/resource_openstack_networking_port_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsecurity"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

type testPortWithExtensions struct {
ports.Port
portsecurity.PortSecurityExt
policies.QoSPolicyExt
}

func TestAccNetworkingV2Port_basic(t *testing.T) {
Expand Down Expand Up @@ -874,6 +877,70 @@ func TestAccNetworkingV2Port_portBinding_update(t *testing.T) {
})
}

func TestAccNetworkingV2Port_qos_policy_create(t *testing.T) {
var (
port testPortWithExtensions
qosPolicy policies.Policy
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2PortDestroy,
Steps: []resource.TestStep{
{
Config: testAccNetworkingV2Port_qos_policy,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortWithExtensionsExists(
"openstack_networking_port_v2.port_1", &port),
testAccCheckNetworkingV2QoSPolicyExists(
"openstack_networking_qos_policy_v2.qos_policy_1", &qosPolicy),
resource.TestCheckResourceAttrSet(
"openstack_networking_port_v2.port_1", "qos_policy_id"),
),
},
},
})
}

func TestAccNetworkingV2Port_qos_policy_update(t *testing.T) {
var (
port testPortWithExtensions
qosPolicy policies.Policy
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2PortDestroy,
Steps: []resource.TestStep{
{
Config: testAccNetworkingV2Port_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortWithExtensionsExists("openstack_networking_port_v2.port_1", &port),
),
},
{
Config: testAccNetworkingV2Port_qos_policy,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2PortWithExtensionsExists(
"openstack_networking_port_v2.port_1", &port),
testAccCheckNetworkingV2QoSPolicyExists(
"openstack_networking_qos_policy_v2.qos_policy_1", &qosPolicy),
resource.TestCheckResourceAttrSet(
"openstack_networking_port_v2.port_1", "qos_policy_id"),
),
},
},
})
}

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

const testAccNetworkingV2Port_qos_policy = `
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_qos_policy_v2" "qos_policy_1" {
name = "qos_policy_1"
}
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"
}
qos_policy_id = "${openstack_networking_qos_policy_v2.qos_policy_1.id}"
}
`
3 changes: 3 additions & 0 deletions website/docs/r/networking_port_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ The following arguments are supported:

* `dns_name` - (Optional) The port DNS name. Available, when Neutron DNS extension
is enabled.

* `qos_policy_id` - (Optional) Reference to the associated QoS policy.

The `fixed_ip` block supports:

Expand Down Expand Up @@ -208,6 +210,7 @@ The following attributes are exported:
* `binding` - See Argument Reference above.
* `dns_name` - See Argument Reference above.
* `dns_assignment` - The list of maps representing port DNS assignments.
* `qos_policy_id` - See Argument Reference above.

## Import

Expand Down

0 comments on commit b84fad2

Please sign in to comment.