Modifying opts and model structs with stricter types
diff --git a/openstack/networking/v2/subnets/requests.go b/openstack/networking/v2/subnets/requests.go
index f592739..ff36765 100644
--- a/openstack/networking/v2/subnets/requests.go
+++ b/openstack/networking/v2/subnets/requests.go
@@ -89,12 +89,20 @@
return &s, nil
}
+// maybeString returns nil for empty strings and nil for empty.
+func maybeString(original string) *string {
+ if original != "" {
+ return &original
+ }
+ return nil
+}
+
const (
IPv4 = 4
IPv6 = 6
)
-type SubnetOpts struct {
+type CreateOpts struct {
// Required
NetworkID string
CIDR string
@@ -104,19 +112,12 @@
AllocationPools []AllocationPool
GatewayIP string
IPVersion int
- ID string
EnableDHCP *bool
+ DNSNameservers []string
+ HostRoutes []interface{}
}
-// maybeString returns nil for empty strings and nil for empty.
-func maybeString(original string) *string {
- if original != "" {
- return &original
- }
- return nil
-}
-
-func Create(c *gophercloud.ServiceClient, opts SubnetOpts) (*Subnet, error) {
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Subnet, error) {
// Validate required options
if opts.NetworkID == "" {
return nil, ErrNetworkIDRequired
@@ -136,8 +137,9 @@
AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
GatewayIP *string `json:"gateway_ip,omitempty"`
IPVersion int `json:"ip_version,omitempty"`
- ID *string `json:"id,omitempty"`
EnableDHCP *bool `json:"enable_dhcp,omitempty"`
+ DNSNameservers []string `json:"dns_nameservers,omitempty"`
+ HostRoutes []interface{} `json:"host_routes,omitempty"`
}
type request struct {
Subnet subnet `json:"subnet"`
@@ -149,17 +151,21 @@
Name: maybeString(opts.Name),
TenantID: maybeString(opts.TenantID),
GatewayIP: maybeString(opts.GatewayIP),
- ID: maybeString(opts.ID),
EnableDHCP: opts.EnableDHCP,
}}
if opts.IPVersion != 0 {
reqBody.Subnet.IPVersion = opts.IPVersion
}
-
if len(opts.AllocationPools) != 0 {
reqBody.Subnet.AllocationPools = opts.AllocationPools
}
+ if len(opts.DNSNameservers) != 0 {
+ reqBody.Subnet.DNSNameservers = opts.DNSNameservers
+ }
+ if len(opts.HostRoutes) != 0 {
+ reqBody.Subnet.HostRoutes = opts.HostRoutes
+ }
type response struct {
Subnet *Subnet `json:"subnet"`
@@ -179,38 +185,38 @@
return res.Subnet, nil
}
-func Update(c *gophercloud.ServiceClient, id string, opts SubnetOpts) (*Subnet, error) {
- if opts.CIDR != "" {
- return nil, ErrCIDRNotUpdatable
- }
- if opts.IPVersion != 0 {
- return nil, ErrIPVersionNotUpdatable
- }
+type UpdateOpts struct {
+ Name string
+ GatewayIP string
+ DNSNameservers []string
+ HostRoutes []interface{}
+ EnableDHCP *bool
+}
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Subnet, error) {
type subnet struct {
- NetworkID string `json:"network_id,omitempty"`
- Name *string `json:"name,omitempty"`
- TenantID *string `json:"tenant_id,omitempty"`
- AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
- GatewayIP *string `json:"gateway_ip,omitempty"`
- ID *string `json:"id,omitempty"`
- EnableDHCP *bool `json:"enable_dhcp,omitempty"`
+ Name *string `json:"name,omitempty"`
+ GatewayIP *string `json:"gateway_ip,omitempty"`
+ DNSNameservers []string `json:"dns_nameservers,omitempty"`
+ HostRoutes []interface{} `json:"host_routes,omitempty"`
+ EnableDHCP *bool `json:"enable_dhcp,omitempty"`
}
type request struct {
Subnet subnet `json:"subnet"`
}
reqBody := request{Subnet: subnet{
- NetworkID: opts.NetworkID,
Name: maybeString(opts.Name),
- TenantID: maybeString(opts.TenantID),
GatewayIP: maybeString(opts.GatewayIP),
- ID: maybeString(opts.ID),
EnableDHCP: opts.EnableDHCP,
}}
- if len(opts.AllocationPools) != 0 {
- reqBody.Subnet.AllocationPools = opts.AllocationPools
+ if len(opts.DNSNameservers) != 0 {
+ reqBody.Subnet.DNSNameservers = opts.DNSNameservers
+ }
+
+ if len(opts.HostRoutes) != 0 {
+ reqBody.Subnet.HostRoutes = opts.HostRoutes
}
type response struct {
diff --git a/openstack/networking/v2/subnets/requests_test.go b/openstack/networking/v2/subnets/requests_test.go
index edf59e7..63e9cc2 100644
--- a/openstack/networking/v2/subnets/requests_test.go
+++ b/openstack/networking/v2/subnets/requests_test.go
@@ -92,7 +92,7 @@
EnableDHCP: true,
NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
- DNSNameservers: []interface{}{},
+ DNSNameservers: []string{},
AllocationPools: []AllocationPool{
AllocationPool{
Start: "10.0.0.2",
@@ -110,7 +110,7 @@
EnableDHCP: true,
NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
TenantID: "4fd44f30292945e481c7b8a0c8908869",
- DNSNameservers: []interface{}{},
+ DNSNameservers: []string{},
AllocationPools: []AllocationPool{
AllocationPool{
Start: "192.0.0.2",
@@ -177,7 +177,7 @@
th.AssertEquals(t, s.EnableDHCP, true)
th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
- th.AssertDeepEquals(t, s.DNSNameservers, []interface{}{})
+ th.AssertDeepEquals(t, s.DNSNameservers, []string{})
th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
AllocationPool{
Start: "192.0.0.2",
@@ -237,7 +237,7 @@
`)
})
- opts := SubnetOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
+ opts := CreateOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
s, err := Create(ServiceClient(), opts)
th.AssertNoErr(t, err)
@@ -245,7 +245,7 @@
th.AssertEquals(t, s.EnableDHCP, true)
th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
- th.AssertDeepEquals(t, s.DNSNameservers, []interface{}{})
+ th.AssertDeepEquals(t, s.DNSNameservers, []string{})
th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
AllocationPool{
Start: "192.168.199.2",
@@ -303,7 +303,7 @@
`)
})
- opts := SubnetOpts{Name: "my_new_subnet"}
+ opts := UpdateOpts{Name: "my_new_subnet"}
s, err := Update(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts)
th.AssertNoErr(t, err)
@@ -311,15 +311,6 @@
th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
}
-func TestCertainAttrsCannotBeUpdated(t *testing.T) {
- opts := SubnetOpts{IPVersion: 6, CIDR: "192.0.0.1/24"}
- _, err := Update(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts)
-
- if err == nil {
- t.Errorf("An error was expected when updating IPVersion and CIDR, none was raised")
- }
-}
-
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
diff --git a/openstack/networking/v2/subnets/results.go b/openstack/networking/v2/subnets/results.go
index 3de2668..d916a1c 100644
--- a/openstack/networking/v2/subnets/results.go
+++ b/openstack/networking/v2/subnets/results.go
@@ -11,17 +11,28 @@
}
type Subnet struct {
- Name string `mapstructure:"name" json:"name"`
- EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"`
- NetworkID string `mapstructure:"network_id" json:"network_id"`
- TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
- DNSNameservers []interface{} `mapstructure:"dns_nameservers" json:"dns_nameservers"`
+ // UUID representing the subnet
+ ID string `mapstructure:"id" json:"id"`
+ // UUID of the parent network
+ NetworkID string `mapstructure:"network_id" json:"network_id"`
+ // Human-readable name for the subnet. Might not be unique.
+ Name string `mapstructure:"name" json:"name"`
+ // IP version, either `4' or `6'
+ IPVersion int `mapstructure:"ip_version" json:"ip_version"`
+ // CIDR representing IP range for this subnet, based on IP version
+ CIDR string `mapstructure:"cidr" json:"cidr"`
+ // Default gateway used by devices in this subnet
+ GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"`
+ // DNS name servers used by hosts in this subnet.
+ DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"`
+ // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool.
AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"`
- HostRoutes []interface{} `mapstructure:"host_routes" json:"host_routes"`
- IPVersion int `mapstructure:"ip_version" json:"ip_version"`
- GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"`
- CIDR string `mapstructure:"cidr" json:"cidr"`
- ID string `mapstructure:"id" json:"id"`
+ // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
+ HostRoutes []interface{} `mapstructure:"host_routes" json:"host_routes"`
+ // Specifies whether DHCP is enabled for this subnet or not.
+ EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"`
+ // Owner of network. Only admin users can specify a tenant_id other than its own.
+ TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
}
type SubnetPage struct {