blob: 153b3a2d9dce292b7dbc8d99d5bdc468b3e88174 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
4 "strconv"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
Jamie Hannaford686c4962014-09-23 10:46:20 +020012// ListOpts allows the filtering and sorting of paginated collections through
13// the API. Filtering is achieved by passing in struct field values that map to
14// the subnet attributes you want to see returned. SortKey allows you to sort
15// by a particular subnet attribute. SortDir sets the direction, and is either
16// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford0708c002014-09-17 16:08:49 +020017type ListOpts struct {
18 Name string
19 EnableDHCP *bool
20 NetworkID string
21 TenantID string
22 IPVersion int
23 GatewayIP string
24 CIDR string
25 ID string
Jamie Hannafordf84171d2014-09-18 14:00:01 +020026 Limit int
Jamie Hannaford686c4962014-09-23 10:46:20 +020027 Marker string
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020028 SortKey string
29 SortDir string
Jamie Hannaford0708c002014-09-17 16:08:49 +020030}
31
Jamie Hannaford686c4962014-09-23 10:46:20 +020032// List returns a Pager which allows you to iterate over a collection of
33// subnets. It accepts a ListOpts struct, which allows you to filter and sort
34// the returned collection for greater efficiency.
35//
36// Default policy settings return only those subnets that are owned by the tenant
37// who submits the request, unless the request is submitted by an user with
38// administrative rights.
Jamie Hannaford0708c002014-09-17 16:08:49 +020039func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
40 // Build query parameters
41 q := make(map[string]string)
42 if opts.Name != "" {
43 q["name"] = opts.Name
44 }
45 if opts.EnableDHCP != nil {
46 q["enable_dhcp"] = strconv.FormatBool(*opts.EnableDHCP)
47 }
48 if opts.NetworkID != "" {
49 q["network_id"] = opts.NetworkID
50 }
51 if opts.TenantID != "" {
52 q["tenant_id"] = opts.TenantID
53 }
54 if opts.IPVersion != 0 {
55 q["ip_version"] = strconv.Itoa(opts.IPVersion)
56 }
57 if opts.GatewayIP != "" {
58 q["gateway_ip"] = opts.GatewayIP
59 }
60 if opts.CIDR != "" {
61 q["cidr"] = opts.CIDR
62 }
63 if opts.ID != "" {
64 q["id"] = opts.ID
65 }
Jamie Hannafordf84171d2014-09-18 14:00:01 +020066 if opts.Limit != 0 {
67 q["limit"] = strconv.Itoa(opts.Limit)
68 }
Jamie Hannaford686c4962014-09-23 10:46:20 +020069 if opts.Marker != "" {
70 q["marker"] = opts.Marker
Jamie Hannafordf84171d2014-09-18 14:00:01 +020071 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020072 if opts.SortKey != "" {
73 q["sort_key"] = opts.SortKey
74 }
75 if opts.SortDir != "" {
76 q["sort_dir"] = opts.SortDir
77 }
Jamie Hannaford0708c002014-09-17 16:08:49 +020078
Jamie Hannaford686c4962014-09-23 10:46:20 +020079 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannaford0708c002014-09-17 16:08:49 +020080 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040081 return SubnetPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannaford0708c002014-09-17 16:08:49 +020082 })
83}
84
Jamie Hannaford686c4962014-09-23 10:46:20 +020085// Get retrieves a specific subnet based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020086func Get(c *gophercloud.ServiceClient, id string) GetResult {
87 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020088 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaford0708c002014-09-17 16:08:49 +020089 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020090 Results: &res.Resp,
91 OkCodes: []int{200},
Jamie Hannaford0708c002014-09-17 16:08:49 +020092 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020093 return res
Jamie Hannaford0708c002014-09-17 16:08:49 +020094}
Jamie Hannaford63631432014-09-18 11:40:09 +020095
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// Valid IP types
Jamie Hannaford63631432014-09-18 11:40:09 +020097const (
98 IPv4 = 4
99 IPv6 = 6
100)
101
Jamie Hannaford686c4962014-09-23 10:46:20 +0200102// CreateOpts represents the attributes used when creating a new subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200103type CreateOpts struct {
Jamie Hannaford63631432014-09-18 11:40:09 +0200104 // Required
105 NetworkID string
106 CIDR string
107 // Optional
108 Name string
109 TenantID string
110 AllocationPools []AllocationPool
111 GatewayIP string
112 IPVersion int
Jamie Hannaford63631432014-09-18 11:40:09 +0200113 EnableDHCP *bool
Jamie Hannaford965ae702014-09-22 14:58:19 +0200114 DNSNameservers []string
115 HostRoutes []interface{}
Jamie Hannaford63631432014-09-18 11:40:09 +0200116}
117
Jamie Hannaford686c4962014-09-23 10:46:20 +0200118// Create accepts a CreateOpts struct and creates a new subnet using the values
119// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200120func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
121 var res CreateResult
122
Jamie Hannaford63631432014-09-18 11:40:09 +0200123 // Validate required options
124 if opts.NetworkID == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200125 res.Err = errNetworkIDRequired
126 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200127 }
128 if opts.CIDR == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200129 res.Err = errCIDRRequired
130 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200131 }
132 if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200133 res.Err = errInvalidIPType
134 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200135 }
136
137 type subnet struct {
138 NetworkID string `json:"network_id"`
139 CIDR string `json:"cidr"`
140 Name *string `json:"name,omitempty"`
141 TenantID *string `json:"tenant_id,omitempty"`
142 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
143 GatewayIP *string `json:"gateway_ip,omitempty"`
144 IPVersion int `json:"ip_version,omitempty"`
Jamie Hannaford63631432014-09-18 11:40:09 +0200145 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200146 DNSNameservers []string `json:"dns_nameservers,omitempty"`
147 HostRoutes []interface{} `json:"host_routes,omitempty"`
Jamie Hannaford63631432014-09-18 11:40:09 +0200148 }
149 type request struct {
150 Subnet subnet `json:"subnet"`
151 }
152
153 reqBody := request{Subnet: subnet{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200154 NetworkID: opts.NetworkID,
155 CIDR: opts.CIDR,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200156 Name: gophercloud.MaybeString(opts.Name),
157 TenantID: gophercloud.MaybeString(opts.TenantID),
158 GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200159 EnableDHCP: opts.EnableDHCP,
Jamie Hannaford63631432014-09-18 11:40:09 +0200160 }}
161
Jamie Hannaford63631432014-09-18 11:40:09 +0200162 if opts.IPVersion != 0 {
163 reqBody.Subnet.IPVersion = opts.IPVersion
164 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200165 if len(opts.AllocationPools) != 0 {
166 reqBody.Subnet.AllocationPools = opts.AllocationPools
167 }
Jamie Hannaford965ae702014-09-22 14:58:19 +0200168 if len(opts.DNSNameservers) != 0 {
169 reqBody.Subnet.DNSNameservers = opts.DNSNameservers
170 }
171 if len(opts.HostRoutes) != 0 {
172 reqBody.Subnet.HostRoutes = opts.HostRoutes
173 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200174
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200175 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaford63631432014-09-18 11:40:09 +0200176 MoreHeaders: c.Provider.AuthenticatedHeaders(),
177 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200178 Results: &res.Resp,
Jamie Hannaford63631432014-09-18 11:40:09 +0200179 OkCodes: []int{201},
180 })
Jamie Hannaford63631432014-09-18 11:40:09 +0200181
Jamie Hannafordd9036422014-09-23 17:50:24 +0200182 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200183}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200184
Jamie Hannaford686c4962014-09-23 10:46:20 +0200185// UpdateOpts represents the attributes used when updating an existing subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200186type UpdateOpts struct {
187 Name string
188 GatewayIP string
189 DNSNameservers []string
190 HostRoutes []interface{}
191 EnableDHCP *bool
192}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200193
Jamie Hannaford686c4962014-09-23 10:46:20 +0200194// Update accepts a UpdateOpts struct and updates an existing subnet using the
195// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200196func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200197 type subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200198 Name *string `json:"name,omitempty"`
199 GatewayIP *string `json:"gateway_ip,omitempty"`
200 DNSNameservers []string `json:"dns_nameservers,omitempty"`
201 HostRoutes []interface{} `json:"host_routes,omitempty"`
202 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200203 }
204 type request struct {
205 Subnet subnet `json:"subnet"`
206 }
207
208 reqBody := request{Subnet: subnet{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200209 Name: gophercloud.MaybeString(opts.Name),
210 GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200211 EnableDHCP: opts.EnableDHCP,
212 }}
213
Jamie Hannaford965ae702014-09-22 14:58:19 +0200214 if len(opts.DNSNameservers) != 0 {
215 reqBody.Subnet.DNSNameservers = opts.DNSNameservers
216 }
217
218 if len(opts.HostRoutes) != 0 {
219 reqBody.Subnet.HostRoutes = opts.HostRoutes
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200220 }
221
Jamie Hannafordd9036422014-09-23 17:50:24 +0200222 var res UpdateResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200223 _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200224 MoreHeaders: c.Provider.AuthenticatedHeaders(),
225 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200226 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200227 OkCodes: []int{200, 201},
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200228 })
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200229
Jamie Hannafordd9036422014-09-23 17:50:24 +0200230 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200231}
232
Jamie Hannaford686c4962014-09-23 10:46:20 +0200233// Delete accepts a unique ID and deletes the subnet associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200234func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
235 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200236 _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200237 MoreHeaders: c.Provider.AuthenticatedHeaders(),
238 OkCodes: []int{204},
239 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200240 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200241}