consistency updates; struct opts -> interface opts (networking, compute)
diff --git a/openstack/networking/v2/extensions/external/requests.go b/openstack/networking/v2/extensions/external/requests.go
index afdd428..2f04593 100644
--- a/openstack/networking/v2/extensions/external/requests.go
+++ b/openstack/networking/v2/extensions/external/requests.go
@@ -24,12 +24,15 @@
}
// ToNetworkCreateMap casts a CreateOpts struct to a map.
-func (o CreateOpts) ToNetworkCreateMap() map[string]map[string]interface{} {
- outer := o.Parent.ToNetworkCreateMap()
+func (o CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
+ outer, err := o.Parent.ToNetworkCreateMap()
+ if err != nil {
+ return nil, err
+ }
- outer["network"]["router:external"] = o.External
+ outer["network"].(map[string]interface{})["router:external"] = o.External
- return outer
+ return outer, nil
}
// UpdateOpts is the structure used when updating existing external network
@@ -41,10 +44,13 @@
}
// ToNetworkUpdateMap casts an UpdateOpts struct to a map.
-func (o UpdateOpts) ToNetworkUpdateMap() map[string]map[string]interface{} {
- outer := o.Parent.ToNetworkUpdateMap()
+func (o UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
+ outer, err := o.Parent.ToNetworkUpdateMap()
+ if err != nil {
+ return nil, err
+ }
- outer["network"]["router:external"] = o.External
+ outer["network"].(map[string]interface{})["router:external"] = o.External
- return outer
+ return outer, nil
}
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index 39a151a..de34989 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -1,9 +1,10 @@
package networks
import (
- "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
+
+ "github.com/racker/perigee"
)
// AdminState gives users a solid type to work with for create and update
@@ -26,6 +27,12 @@
TenantID string
}
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToNetworkListString() (string, error)
+}
+
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the network attributes you want to see returned. SortKey allows you to sort
@@ -44,17 +51,29 @@
SortDir string `q:"sort_dir"`
}
+// ToNetworkListString formats a ListOpts into a query string.
+func (opts ListOpts) ToNetworkListString() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
// List returns a Pager which allows you to iterate over a collection of
// networks. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
-func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
- // Build query parameters
- q, err := gophercloud.BuildQueryString(&opts)
- if err != nil {
- return pagination.Pager{Err: err}
+func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(c)
+ if opts != nil {
+ query, err := opts.ToNetworkListString()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
}
- u := listURL(c) + q.String()
- return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
+
+ return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page {
return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
})
}
@@ -75,7 +94,7 @@
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type CreateOptsBuilder interface {
- ToNetworkCreateMap() map[string]map[string]interface{}
+ ToNetworkCreateMap() (map[string]interface{}, error)
}
// CreateOpts is the common options struct used in this package's Create
@@ -83,26 +102,23 @@
type CreateOpts networkOpts
// ToNetworkCreateMap casts a CreateOpts struct to a map.
-func (o CreateOpts) ToNetworkCreateMap() map[string]map[string]interface{} {
- inner := make(map[string]interface{})
+func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
+ n := make(map[string]interface{})
- if o.AdminStateUp != nil {
- inner["admin_state_up"] = &o.AdminStateUp
+ if opts.AdminStateUp != nil {
+ n["admin_state_up"] = &opts.AdminStateUp
}
- if o.Name != "" {
- inner["name"] = o.Name
+ if opts.Name != "" {
+ n["name"] = opts.Name
}
- if o.Shared != nil {
- inner["shared"] = &o.Shared
+ if opts.Shared != nil {
+ n["shared"] = &opts.Shared
}
- if o.TenantID != "" {
- inner["tenant_id"] = o.TenantID
+ if opts.TenantID != "" {
+ n["tenant_id"] = opts.TenantID
}
- outer := make(map[string]map[string]interface{})
- outer["network"] = inner
-
- return outer
+ return map[string]interface{}{"network": n}, nil
}
// Create accepts a CreateOpts struct and creates a new network using the values
@@ -115,7 +131,11 @@
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
- reqBody := opts.ToNetworkCreateMap()
+ reqBody, err := opts.ToNetworkCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
// Send request to API
_, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
@@ -132,7 +152,7 @@
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type UpdateOptsBuilder interface {
- ToNetworkUpdateMap() map[string]map[string]interface{}
+ ToNetworkUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts is the common options struct used in this package's Update
@@ -140,23 +160,20 @@
type UpdateOpts networkOpts
// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
-func (o UpdateOpts) ToNetworkUpdateMap() map[string]map[string]interface{} {
- inner := make(map[string]interface{})
+func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
+ n := make(map[string]interface{})
- if o.AdminStateUp != nil {
- inner["admin_state_up"] = &o.AdminStateUp
+ if opts.AdminStateUp != nil {
+ n["admin_state_up"] = &opts.AdminStateUp
}
- if o.Name != "" {
- inner["name"] = o.Name
+ if opts.Name != "" {
+ n["name"] = opts.Name
}
- if o.Shared != nil {
- inner["shared"] = &o.Shared
+ if opts.Shared != nil {
+ n["shared"] = &opts.Shared
}
- outer := make(map[string]map[string]interface{})
- outer["network"] = inner
-
- return outer
+ return map[string]interface{}{"network": n}, nil
}
// Update accepts a UpdateOpts struct and updates an existing network using the
@@ -164,7 +181,11 @@
func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
- reqBody := opts.ToNetworkUpdateMap()
+ reqBody, err := opts.ToNetworkUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
// Send request to API
_, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index 0fbb9f2..afa37f5 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -1,9 +1,10 @@
package ports
import (
- "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
+
+ "github.com/racker/perigee"
)
// AdminState gives users a solid type to work with for create and update
@@ -19,6 +20,12 @@
Down AdminState = &iFalse
)
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToPortListString() (string, error)
+}
+
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the port attributes you want to see returned. SortKey allows you to sort
@@ -40,6 +47,15 @@
SortDir string `q:"sort_dir"`
}
+// ToPortListString formats a ListOpts into a query string.
+func (opts ListOpts) ToPortListString() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
// List returns a Pager which allows you to iterate over a collection of
// ports. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
@@ -47,15 +63,17 @@
// Default policy settings return only those ports that are owned by the tenant
// who submits the request, unless the request is submitted by an user with
// administrative rights.
-func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
- // Build query parameters
- q, err := gophercloud.BuildQueryString(&opts)
- if err != nil {
- return pagination.Pager{Err: err}
+func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(c)
+ if opts != nil {
+ query, err := opts.ToPortListString()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
}
- u := listURL(c) + q.String()
- return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
+ return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page {
return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
})
}
@@ -71,6 +89,14 @@
return res
}
+// CreateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Create operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type CreateOptsBuilder interface {
+ ToPortCreateMap() (map[string]interface{}, error)
+}
+
// CreateOpts represents the attributes used when creating a new port.
type CreateOpts struct {
NetworkID string
@@ -84,51 +110,54 @@
SecurityGroups []string
}
+// ToPortCreateMap casts a CreateOpts struct to a map.
+func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
+ p := make(map[string]interface{})
+
+ if opts.NetworkID == "" {
+ return nil, errNetworkIDRequired
+ }
+ p["network_id"] = opts.NetworkID
+
+ if opts.DeviceID != "" {
+ p["device_id"] = opts.DeviceID
+ }
+ if opts.DeviceOwner != "" {
+ p["device_owner"] = opts.DeviceOwner
+ }
+ if opts.FixedIPs != nil {
+ p["fixed_ips"] = opts.FixedIPs
+ }
+ if opts.SecurityGroups != nil {
+ p["security_groups"] = opts.SecurityGroups
+ }
+ if opts.TenantID != "" {
+ p["tenant_id"] = opts.TenantID
+ }
+ if opts.AdminStateUp != nil {
+ p["admin_state_up"] = &opts.AdminStateUp
+ }
+ if opts.Name != "" {
+ p["name"] = opts.Name
+ }
+ if opts.MACAddress != "" {
+ p["mac_address"] = opts.MACAddress
+ }
+
+ return map[string]interface{}{"port": p}, nil
+}
+
// Create accepts a CreateOpts struct and creates a new network using the values
// provided. You must remember to provide a NetworkID value.
-func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
- type port struct {
- NetworkID string `json:"network_id"`
- Name *string `json:"name,omitempty"`
- AdminStateUp *bool `json:"admin_state_up,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 {
- Port port `json:"port"`
- }
-
- // Validate
- if opts.NetworkID == "" {
- res.Err = errNetworkIDRequired
+ reqBody, err := opts.ToPortCreateMap()
+ if err != nil {
+ res.Err = err
return res
}
- // Populate request body
- reqBody := request{Port: port{
- NetworkID: opts.NetworkID,
- Name: gophercloud.MaybeString(opts.Name),
- AdminStateUp: opts.AdminStateUp,
- TenantID: gophercloud.MaybeString(opts.TenantID),
- MACAddress: gophercloud.MaybeString(opts.MACAddress),
- DeviceID: gophercloud.MaybeString(opts.DeviceID),
- DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
- }}
-
- if opts.FixedIPs != nil {
- reqBody.Port.FixedIPs = opts.FixedIPs
- }
-
- if opts.SecurityGroups != nil {
- reqBody.Port.SecurityGroups = opts.SecurityGroups
- }
-
// Response
_, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
@@ -141,6 +170,14 @@
return res
}
+// UpdateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Update operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type UpdateOptsBuilder interface {
+ ToPortUpdateMap() (map[string]interface{}, error)
+}
+
// UpdateOpts represents the attributes used when updating an existing port.
type UpdateOpts struct {
Name string
@@ -151,39 +188,43 @@
SecurityGroups []string
}
+// ToPortUpdateMap casts an UpdateOpts struct to a map.
+func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
+ p := make(map[string]interface{})
+
+ if opts.DeviceID != "" {
+ p["device_id"] = opts.DeviceID
+ }
+ if opts.DeviceOwner != "" {
+ p["device_owner"] = opts.DeviceOwner
+ }
+ if opts.FixedIPs != nil {
+ p["fixed_ips"] = opts.FixedIPs
+ }
+ if opts.SecurityGroups != nil {
+ p["security_groups"] = opts.SecurityGroups
+ }
+ if opts.AdminStateUp != nil {
+ p["admin_state_up"] = &opts.AdminStateUp
+ }
+ if opts.Name != "" {
+ p["name"] = opts.Name
+ }
+
+ return map[string]interface{}{"port": p}, nil
+}
+
// Update accepts a UpdateOpts struct and updates an existing port using the
// values provided.
-func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
- type port struct {
- Name *string `json:"name,omitempty"`
- AdminStateUp *bool `json:"admin_state_up,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"`
- }
-
- // Populate request body
- reqBody := request{Port: port{
- Name: gophercloud.MaybeString(opts.Name),
- AdminStateUp: opts.AdminStateUp,
- DeviceID: gophercloud.MaybeString(opts.DeviceID),
- DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
- }}
-
- if opts.FixedIPs != nil {
- reqBody.Port.FixedIPs = opts.FixedIPs
- }
-
- if opts.SecurityGroups != nil {
- reqBody.Port.SecurityGroups = opts.SecurityGroups
- }
-
- // Response
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
+
+ reqBody, err := opts.ToPortUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
_, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
diff --git a/openstack/networking/v2/subnets/requests.go b/openstack/networking/v2/subnets/requests.go
index a7e6b53..e164583 100644
--- a/openstack/networking/v2/subnets/requests.go
+++ b/openstack/networking/v2/subnets/requests.go
@@ -1,9 +1,10 @@
package subnets
import (
- "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
+
+ "github.com/racker/perigee"
)
// AdminState gives users a solid type to work with for create and update
@@ -19,6 +20,12 @@
Down AdminState = &iFalse
)
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToSubnetListString() (string, error)
+}
+
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the subnet attributes you want to see returned. SortKey allows you to sort
@@ -39,6 +46,15 @@
SortDir string `q:"sort_dir"`
}
+// ToSubnetListString formats a ListOpts into a query string.
+func (opts ListOpts) ToSubnetListString() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
// List returns a Pager which allows you to iterate over a collection of
// subnets. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
@@ -46,13 +62,15 @@
// Default policy settings return only those subnets that are owned by the tenant
// who submits the request, unless the request is submitted by an user with
// administrative rights.
-func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
- // Build query parameters
- query, err := gophercloud.BuildQueryString(&opts)
- if err != nil {
- return pagination.Pager{Err: err}
+func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(c)
+ if opts != nil {
+ query, err := opts.ToSubnetListString()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
}
- url := listURL(c) + query.String()
return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page {
return SubnetPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
@@ -76,6 +94,14 @@
IPv6 = 6
)
+// CreateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Create operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type CreateOptsBuilder interface {
+ ToSubnetCreateMap() (map[string]interface{}, error)
+}
+
// CreateOpts represents the attributes used when creating a new subnet.
type CreateOpts struct {
// Required
@@ -92,61 +118,60 @@
HostRoutes []HostRoute
}
-// Create accepts a CreateOpts struct and creates a new subnet using the values
-// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
-func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
- var res CreateResult
+// ToSubnetCreateMap casts a CreateOpts struct to a map.
+func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
+ s := make(map[string]interface{})
- // Validate required options
if opts.NetworkID == "" {
- res.Err = errNetworkIDRequired
- return res
+ return nil, errNetworkIDRequired
}
if opts.CIDR == "" {
- res.Err = errCIDRRequired
- return res
+ return nil, errCIDRRequired
}
if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
- res.Err = errInvalidIPType
- return res
+ return nil, errInvalidIPType
}
- type subnet struct {
- NetworkID string `json:"network_id"`
- CIDR string `json:"cidr"`
- Name *string `json:"name,omitempty"`
- TenantID *string `json:"tenant_id,omitempty"`
- AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
- GatewayIP *string `json:"gateway_ip,omitempty"`
- IPVersion int `json:"ip_version,omitempty"`
- EnableDHCP *bool `json:"enable_dhcp,omitempty"`
- DNSNameservers []string `json:"dns_nameservers,omitempty"`
- HostRoutes []HostRoute `json:"host_routes,omitempty"`
- }
- type request struct {
- Subnet subnet `json:"subnet"`
- }
+ s["network_id"] = opts.NetworkID
+ s["cidr"] = opts.CIDR
- reqBody := request{Subnet: subnet{
- NetworkID: opts.NetworkID,
- CIDR: opts.CIDR,
- Name: gophercloud.MaybeString(opts.Name),
- TenantID: gophercloud.MaybeString(opts.TenantID),
- GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
- EnableDHCP: opts.EnableDHCP,
- }}
-
+ if opts.EnableDHCP != nil {
+ s["enable_dhcp"] = &opts.EnableDHCP
+ }
+ if opts.Name != "" {
+ s["name"] = opts.Name
+ }
+ if opts.GatewayIP != "" {
+ s["gateway_ip"] = opts.GatewayIP
+ }
+ if opts.TenantID != "" {
+ s["tenant_id"] = opts.TenantID
+ }
if opts.IPVersion != 0 {
- reqBody.Subnet.IPVersion = opts.IPVersion
+ s["ip_version"] = opts.IPVersion
}
if len(opts.AllocationPools) != 0 {
- reqBody.Subnet.AllocationPools = opts.AllocationPools
+ s["allocation_pools"] = opts.AllocationPools
}
if len(opts.DNSNameservers) != 0 {
- reqBody.Subnet.DNSNameservers = opts.DNSNameservers
+ s["dns_nameservers"] = opts.DNSNameservers
}
if len(opts.HostRoutes) != 0 {
- reqBody.Subnet.HostRoutes = opts.HostRoutes
+ s["host_routes"] = opts.HostRoutes
+ }
+
+ return map[string]interface{}{"subnet": s}, nil
+}
+
+// Create accepts a CreateOpts struct and creates a new subnet using the values
+// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
+func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToSubnetCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
}
_, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
@@ -159,6 +184,12 @@
return res
}
+// UpdateOptsBuilder allows extensions to add additional parameters to the
+// Update request.
+type UpdateOptsBuilder interface {
+ ToSubnetUpdateMap() (map[string]interface{}, error)
+}
+
// UpdateOpts represents the attributes used when updating an existing subnet.
type UpdateOpts struct {
Name string
@@ -168,35 +199,40 @@
EnableDHCP *bool
}
+// ToSubnetUpdateMap casts an UpdateOpts struct to a map.
+func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
+ s := make(map[string]interface{})
+
+ if opts.EnableDHCP != nil {
+ s["enable_dhcp"] = &opts.EnableDHCP
+ }
+ if opts.Name != "" {
+ s["name"] = opts.Name
+ }
+ if opts.GatewayIP != "" {
+ s["gateway_ip"] = opts.GatewayIP
+ }
+ if len(opts.DNSNameservers) != 0 {
+ s["dns_nameservers"] = opts.DNSNameservers
+ }
+ if len(opts.HostRoutes) != 0 {
+ s["host_routes"] = opts.HostRoutes
+ }
+
+ return map[string]interface{}{"subnet": s}, nil
+}
+
// Update accepts a UpdateOpts struct and updates an existing subnet using the
// values provided.
-func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
- type subnet struct {
- Name *string `json:"name,omitempty"`
- GatewayIP *string `json:"gateway_ip,omitempty"`
- DNSNameservers []string `json:"dns_nameservers,omitempty"`
- HostRoutes []HostRoute `json:"host_routes,omitempty"`
- EnableDHCP *bool `json:"enable_dhcp,omitempty"`
- }
- type request struct {
- Subnet subnet `json:"subnet"`
- }
-
- reqBody := request{Subnet: subnet{
- Name: gophercloud.MaybeString(opts.Name),
- GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
- EnableDHCP: opts.EnableDHCP,
- }}
-
- if len(opts.DNSNameservers) != 0 {
- reqBody.Subnet.DNSNameservers = opts.DNSNameservers
- }
-
- if len(opts.HostRoutes) != 0 {
- reqBody.Subnet.HostRoutes = opts.HostRoutes
- }
-
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
+
+ reqBody, err := opts.ToSubnetUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
_, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,