Modifying opts and model structs with stricter types
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index 6543313..cc0949f 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -91,7 +91,7 @@
q["sort_dir"] = opts.SortDir
}
- u := ListURL(c) + utils.BuildQuery(q)
+ u := listURL(c) + utils.BuildQuery(q)
return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
return PortPage{pagination.LinkedPageBase(r)}
})
@@ -99,7 +99,7 @@
func Get(c *gophercloud.ServiceClient, id string) (*Port, error) {
var p Port
- _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
+ _, err := perigee.Request("GET", getURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
Results: &struct {
Port *Port `json:"port"`
@@ -112,17 +112,6 @@
return &p, nil
}
-type PortOpts struct {
- NetworkID string
- Status string
- Name string
- AdminStateUp *bool
- TenantID string
- MACAddress string
- FixedIPs interface{}
- SecurityGroups []string
-}
-
func maybeString(original string) *string {
if original != "" {
return &original
@@ -130,15 +119,28 @@
return nil
}
-func Create(c *gophercloud.ServiceClient, opts PortOpts) (*Port, error) {
+type CreateOpts struct {
+ NetworkID string
+ Name string
+ AdminStateUp *bool
+ MACAddress string
+ FixedIPs interface{}
+ DeviceID string
+ DeviceOwner string
+ TenantID string
+ SecurityGroups []string
+}
+
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Port, error) {
type port struct {
NetworkID string `json:"network_id,omitempty"`
- Status *string `json:"status,omitempty"`
Name *string `json:"name,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
- TenantID *string `json:"tenant_id,omitempty"`
MACAddress *string `json:"mac_address,omitempty"`
FixedIPs interface{} `json:"fixed_ips,omitempty"`
+ DeviceID *string `json:"device_id,omitempty"`
+ DeviceOwner *string `json:"device_owner,omitempty"`
+ TenantID *string `json:"tenant_id,omitempty"`
SecurityGroups []string `json:"security_groups,omitempty"`
}
type request struct {
@@ -153,11 +155,12 @@
// Populate request body
reqBody := request{Port: port{
NetworkID: opts.NetworkID,
- Status: maybeString(opts.Status),
Name: maybeString(opts.Name),
AdminStateUp: opts.AdminStateUp,
TenantID: maybeString(opts.TenantID),
MACAddress: maybeString(opts.MACAddress),
+ DeviceID: maybeString(opts.DeviceID),
+ DeviceOwner: maybeString(opts.DeviceOwner),
}}
if opts.FixedIPs != nil {
@@ -173,7 +176,7 @@
Port *Port `json:"port"`
}
var res response
- _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
+ _, err := perigee.Request("POST", createURL(c), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
Results: &res,
@@ -187,34 +190,34 @@
return res.Port, nil
}
-func Update(c *gophercloud.ServiceClient, id string, opts PortOpts) (*Port, error) {
+type UpdateOpts struct {
+ Name string
+ AdminStateUp *bool
+ FixedIPs interface{}
+ DeviceID string
+ DeviceOwner string
+ SecurityGroups []string
+}
+
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Port, error) {
type port struct {
- NetworkID string `json:"network_id,omitempty"`
- Status *string `json:"status,omitempty"`
Name *string `json:"name,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
- TenantID *string `json:"tenant_id,omitempty"`
- MACAddress *string `json:"mac_address,omitempty"`
FixedIPs interface{} `json:"fixed_ips,omitempty"`
+ DeviceID *string `json:"device_id,omitempty"`
+ DeviceOwner *string `json:"device_owner,omitempty"`
SecurityGroups []string `json:"security_groups,omitempty"`
}
type request struct {
Port port `json:"port"`
}
- // Validate
- if opts.NetworkID == "" {
- return nil, ErrNetworkIDRequired
- }
-
// Populate request body
reqBody := request{Port: port{
- NetworkID: opts.NetworkID,
- Status: maybeString(opts.Status),
Name: maybeString(opts.Name),
AdminStateUp: opts.AdminStateUp,
- TenantID: maybeString(opts.TenantID),
- MACAddress: maybeString(opts.MACAddress),
+ DeviceID: maybeString(opts.DeviceID),
+ DeviceOwner: maybeString(opts.DeviceOwner),
}}
if opts.FixedIPs != nil {
@@ -230,7 +233,7 @@
Port *Port `json:"port"`
}
var res response
- _, err := perigee.Request("PUT", UpdateURL(c, id), perigee.Options{
+ _, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
Results: &res,
@@ -244,7 +247,7 @@
}
func Delete(c *gophercloud.ServiceClient, id string) error {
- _, err := perigee.Request("DELETE", DeleteURL(c, id), perigee.Options{
+ _, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
diff --git a/openstack/networking/v2/ports/requests_test.go b/openstack/networking/v2/ports/requests_test.go
index cb4cc2c..83b0153 100644
--- a/openstack/networking/v2/ports/requests_test.go
+++ b/openstack/networking/v2/ports/requests_test.go
@@ -10,13 +10,11 @@
th "github.com/rackspace/gophercloud/testhelper"
)
-const TokenID = "123"
+const tokenID = "123"
func ServiceClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{
- Provider: &gophercloud.ProviderClient{
- TokenID: TokenID,
- },
+ Provider: &gophercloud.ProviderClient{TokenID: tokenID},
Endpoint: th.Endpoint(),
}
}
@@ -27,7 +25,7 @@
th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", TokenID)
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
@@ -122,7 +120,7 @@
th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", TokenID)
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
@@ -192,7 +190,7 @@
th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", TokenID)
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
@@ -241,7 +239,7 @@
})
asu := true
- options := PortOpts{Name: "private-port", AdminStateUp: &asu, NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7"}
+ options := CreateOpts{Name: "private-port", AdminStateUp: &asu, NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7"}
n, err := Create(ServiceClient(), options)
th.AssertNoErr(t, err)
@@ -272,13 +270,12 @@
th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", TokenID)
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"port": {
- "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"name": "new_port_name",
"fixed_ips": [
{
@@ -328,9 +325,8 @@
`)
})
- options := PortOpts{
- NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7",
- Name: "new_port_name",
+ options := UpdateOpts{
+ Name: "new_port_name",
FixedIPs: []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
},
@@ -353,7 +349,7 @@
th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", TokenID)
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
w.WriteHeader(http.StatusNoContent)
})
diff --git a/openstack/networking/v2/ports/results.go b/openstack/networking/v2/ports/results.go
index f7f947d..7e90d8d 100644
--- a/openstack/networking/v2/ports/results.go
+++ b/openstack/networking/v2/ports/results.go
@@ -11,19 +11,33 @@
}
type Port struct {
- Status string `mapstructure:"status" json:"status"`
- Name string `mapstructure:"name" json:"name"`
+ // UUID for the port.
+ ID string `mapstructure:"id" json:"id"`
+ // Network that this port is associated with.
+ NetworkID string `mapstructure:"network_id" json:"network_id"`
+ // Human-readable name for the port. Might not be unique.
+ Name string `mapstructure:"name" json:"name"`
+ // Administrative state of port. If false (down), port does not forward packets.
+ AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
+ // Indicates whether network is currently operational. Possible values include
+ // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
+ Status string `mapstructure:"status" json:"status"`
+ // Mac address to use on this port.
+ MACAddress string `mapstructure:"mac_address" json:"mac_address"`
+ // Specifies IP addresses for the port thus associating the port itself with
+ // the subnets where the IP addresses are picked from
+ FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
+ // Owner of network. Only admin users can specify a tenant_id other than its own.
+ TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
+ // Identifies the entity (e.g.: dhcp agent) using this port.
+ DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
+ // Specifies the IDs of any security groups associated with a port.
+ SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
+ // Identifies the device (e.g., virtual server) using this port.
+ DeviceID string `mapstructure:"device_id" json:"device_id"`
+
AllowedAddressPairs []interface{} `mapstructure:"allowed" json:"allowed"`
- AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
- NetworkID string `mapstructure:"network_id" json:"network_id"`
- TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
ExtraDHCPOpts interface{} `mapstructure:"extra_dhcp_opts" json:"extra_dhcp_opts"`
- DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
- MACAddress string `mapstructure:"mac_address" json:"mac_address"`
- FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
- ID string `mapstructure:"id" json:"id"`
- SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
- DeviceID string `mapstructure:"device_id" json:"device_id"`
BindingHostID string `mapstructure:"binding:host_id" json:"binding:host_id"`
BindingVIFDetails interface{} `mapstructure:"binding:vif_details" json:"binding:vif_details"`
BindingVIFType string `mapstructure:"binding:vif_type" json:"binding:vif_type"`
diff --git a/openstack/networking/v2/ports/urls.go b/openstack/networking/v2/ports/urls.go
index 5dc4696..558b399 100644
--- a/openstack/networking/v2/ports/urls.go
+++ b/openstack/networking/v2/ports/urls.go
@@ -2,32 +2,32 @@
import "github.com/rackspace/gophercloud"
-const Version = "v2.0"
+const version = "v2.0"
-func ResourceURL(c *gophercloud.ServiceClient, id string) string {
- return c.ServiceURL(Version, "ports", id)
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, "ports", id)
}
-func RootURL(c *gophercloud.ServiceClient) string {
- return c.ServiceURL(Version, "ports")
+func rootURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(version, "ports")
}
-func ListURL(c *gophercloud.ServiceClient) string {
- return RootURL(c)
+func listURL(c *gophercloud.ServiceClient) string {
+ return rootURL(c)
}
-func GetURL(c *gophercloud.ServiceClient, id string) string {
- return ResourceURL(c, id)
+func getURL(c *gophercloud.ServiceClient, id string) string {
+ return resourceURL(c, id)
}
-func CreateURL(c *gophercloud.ServiceClient) string {
- return RootURL(c)
+func createURL(c *gophercloud.ServiceClient) string {
+ return rootURL(c)
}
-func UpdateURL(c *gophercloud.ServiceClient, id string) string {
- return ResourceURL(c, id)
+func updateURL(c *gophercloud.ServiceClient, id string) string {
+ return resourceURL(c, id)
}
-func DeleteURL(c *gophercloud.ServiceClient, id string) string {
- return ResourceURL(c, id)
+func deleteURL(c *gophercloud.ServiceClient, id string) string {
+ return resourceURL(c, id)
}
diff --git a/openstack/networking/v2/ports/urls_tests.go b/openstack/networking/v2/ports/urls_tests.go
index 089725d..6fb20aa 100644
--- a/openstack/networking/v2/ports/urls_tests.go
+++ b/openstack/networking/v2/ports/urls_tests.go
@@ -7,38 +7,38 @@
th "github.com/rackspace/gophercloud/testhelper"
)
-const Endpoint = "http://localhost:57909/"
+const endpoint = "http://localhost:57909/"
-func EndpointClient() *gophercloud.ServiceClient {
- return &gophercloud.ServiceClient{Endpoint: Endpoint}
+func endpointClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{Endpoint: endpoint}
}
func TestListURL(t *testing.T) {
- actual := ListURL(EndpointClient())
- expected := Endpoint + "v2.0/ports"
+ actual := listURL(endpointClient())
+ expected := endpoint + "v2.0/ports"
th.AssertEquals(t, expected, actual)
}
func TestGetURL(t *testing.T) {
- actual := GetURL(EndpointClient(), "foo")
- expected := Endpoint + "v2.0/ports/foo"
+ actual := getURL(endpointClient(), "foo")
+ expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}
func TestCreateURL(t *testing.T) {
- actual := CreateURL(EndpointClient())
- expected := Endpoint + "v2.0/ports"
+ actual := createURL(endpointClient())
+ expected := endpoint + "v2.0/ports"
th.AssertEquals(t, expected, actual)
}
func TestUpdateURL(t *testing.T) {
- actual := UpdateURL(EndpointClient(), "foo")
- expected := Endpoint + "v2.0/ports/foo"
+ actual := updateURL(endpointClient(), "foo")
+ expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}
func TestDeleteURL(t *testing.T) {
- actual := DeleteURL(EndpointClient(), "foo")
- expected := Endpoint + "v2.0/ports/foo"
+ actual := deleteURL(endpointClient(), "foo")
+ expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}