blob: f66a6ac7af03cddb30b9139397cd323c34a8d66f [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
Jamie Hannaford0708c002014-09-17 16:08:49 +02004 "github.com/rackspace/gophercloud"
Jamie Hannaford0708c002014-09-17 16:08:49 +02005 "github.com/rackspace/gophercloud/pagination"
6)
7
Jamie Hannaforda2976ab2014-10-09 10:32:58 +02008// AdminState gives users a solid type to work with for create and update
9// operations. It is recommended that users use the `Up` and `Down` enums.
10type AdminState *bool
11
12// Convenience vars for AdminStateUp values.
13var (
14 iTrue = true
15 iFalse = false
16
17 Up AdminState = &iTrue
18 Down AdminState = &iFalse
19)
20
Jon Perritt04851d32014-10-14 02:07:13 -050021// ListOptsBuilder allows extensions to add additional parameters to the
22// List request.
23type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050024 ToSubnetListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050025}
26
Jamie Hannaford686c4962014-09-23 10:46:20 +020027// ListOpts allows the filtering and sorting of paginated collections through
28// the API. Filtering is achieved by passing in struct field values that map to
29// the subnet attributes you want to see returned. SortKey allows you to sort
30// by a particular subnet attribute. SortDir sets the direction, and is either
31// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford0708c002014-09-17 16:08:49 +020032type ListOpts struct {
Jamie Hannaford5c6e9962014-10-02 10:34:14 +020033 Name string `q:"name"`
34 EnableDHCP *bool `q:"enable_dhcp"`
35 NetworkID string `q:"network_id"`
36 TenantID string `q:"tenant_id"`
37 IPVersion int `q:"ip_version"`
38 GatewayIP string `q:"gateway_ip"`
39 CIDR string `q:"cidr"`
40 ID string `q:"id"`
41 Limit int `q:"limit"`
42 Marker string `q:"marker"`
43 SortKey string `q:"sort_key"`
44 SortDir string `q:"sort_dir"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020045}
46
Jon Perritt26780d52014-10-14 11:35:58 -050047// ToSubnetListQuery formats a ListOpts into a query string.
48func (opts ListOpts) ToSubnetListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050049 q, err := gophercloud.BuildQueryString(opts)
50 if err != nil {
51 return "", err
52 }
53 return q.String(), nil
54}
55
Jamie Hannaford686c4962014-09-23 10:46:20 +020056// List returns a Pager which allows you to iterate over a collection of
57// subnets. It accepts a ListOpts struct, which allows you to filter and sort
58// the returned collection for greater efficiency.
59//
60// Default policy settings return only those subnets that are owned by the tenant
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070061// who submits the request, unless the request is submitted by a user with
Jamie Hannaford686c4962014-09-23 10:46:20 +020062// administrative rights.
Jon Perritt04851d32014-10-14 02:07:13 -050063func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
64 url := listURL(c)
65 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050066 query, err := opts.ToSubnetListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050067 if err != nil {
68 return pagination.Pager{Err: err}
69 }
70 url += query
Jamie Hannaford92523e32014-10-02 11:08:36 +020071 }
Jamie Hannaford0708c002014-09-17 16:08:49 +020072
Ash Wilsonb8b16f82014-10-20 10:19:49 -040073 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
74 return SubnetPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford0708c002014-09-17 16:08:49 +020075 })
76}
77
Jamie Hannaford686c4962014-09-23 10:46:20 +020078// Get retrieves a specific subnet based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020079func Get(c *gophercloud.ServiceClient, id string) GetResult {
80 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050081 _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{
82 JSONResponse: &res.Body,
Jamie Hannaford0708c002014-09-17 16:08:49 +020083 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020084 return res
Jamie Hannaford0708c002014-09-17 16:08:49 +020085}
Jamie Hannaford63631432014-09-18 11:40:09 +020086
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// Valid IP types
Jamie Hannaford63631432014-09-18 11:40:09 +020088const (
89 IPv4 = 4
90 IPv6 = 6
91)
92
Jon Perritt04851d32014-10-14 02:07:13 -050093// CreateOptsBuilder is the interface options structs have to satisfy in order
94// to be used in the main Create operation in this package. Since many
95// extensions decorate or modify the common logic, it is useful for them to
96// satisfy a basic interface in order for them to be used.
97type CreateOptsBuilder interface {
98 ToSubnetCreateMap() (map[string]interface{}, error)
99}
100
Jamie Hannaford686c4962014-09-23 10:46:20 +0200101// CreateOpts represents the attributes used when creating a new subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102type CreateOpts struct {
Jamie Hannaford63631432014-09-18 11:40:09 +0200103 // Required
104 NetworkID string
105 CIDR string
106 // Optional
107 Name string
108 TenantID string
109 AllocationPools []AllocationPool
110 GatewayIP string
111 IPVersion int
Jamie Hannaford63631432014-09-18 11:40:09 +0200112 EnableDHCP *bool
Jamie Hannaford965ae702014-09-22 14:58:19 +0200113 DNSNameservers []string
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200114 HostRoutes []HostRoute
Jamie Hannaford63631432014-09-18 11:40:09 +0200115}
116
Jon Perritt04851d32014-10-14 02:07:13 -0500117// ToSubnetCreateMap casts a CreateOpts struct to a map.
118func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
119 s := make(map[string]interface{})
Jamie Hannafordd9036422014-09-23 17:50:24 +0200120
Jamie Hannaford63631432014-09-18 11:40:09 +0200121 if opts.NetworkID == "" {
Jon Perritt04851d32014-10-14 02:07:13 -0500122 return nil, errNetworkIDRequired
Jamie Hannaford63631432014-09-18 11:40:09 +0200123 }
124 if opts.CIDR == "" {
Jon Perritt04851d32014-10-14 02:07:13 -0500125 return nil, errCIDRRequired
Jamie Hannaford63631432014-09-18 11:40:09 +0200126 }
127 if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
Jon Perritt04851d32014-10-14 02:07:13 -0500128 return nil, errInvalidIPType
Jamie Hannaford63631432014-09-18 11:40:09 +0200129 }
130
Jon Perritt04851d32014-10-14 02:07:13 -0500131 s["network_id"] = opts.NetworkID
132 s["cidr"] = opts.CIDR
Jamie Hannaford63631432014-09-18 11:40:09 +0200133
Jon Perritt04851d32014-10-14 02:07:13 -0500134 if opts.EnableDHCP != nil {
135 s["enable_dhcp"] = &opts.EnableDHCP
136 }
137 if opts.Name != "" {
138 s["name"] = opts.Name
139 }
140 if opts.GatewayIP != "" {
141 s["gateway_ip"] = opts.GatewayIP
142 }
143 if opts.TenantID != "" {
144 s["tenant_id"] = opts.TenantID
145 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200146 if opts.IPVersion != 0 {
Jon Perritt04851d32014-10-14 02:07:13 -0500147 s["ip_version"] = opts.IPVersion
Jamie Hannaford63631432014-09-18 11:40:09 +0200148 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200149 if len(opts.AllocationPools) != 0 {
Jon Perritt04851d32014-10-14 02:07:13 -0500150 s["allocation_pools"] = opts.AllocationPools
Jamie Hannaford63631432014-09-18 11:40:09 +0200151 }
Jamie Hannaford965ae702014-09-22 14:58:19 +0200152 if len(opts.DNSNameservers) != 0 {
Jon Perritt04851d32014-10-14 02:07:13 -0500153 s["dns_nameservers"] = opts.DNSNameservers
Jamie Hannaford965ae702014-09-22 14:58:19 +0200154 }
155 if len(opts.HostRoutes) != 0 {
Jon Perritt04851d32014-10-14 02:07:13 -0500156 s["host_routes"] = opts.HostRoutes
157 }
158
159 return map[string]interface{}{"subnet": s}, nil
160}
161
162// Create accepts a CreateOpts struct and creates a new subnet using the values
163// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
164func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
165 var res CreateResult
166
167 reqBody, err := opts.ToSubnetCreateMap()
168 if err != nil {
169 res.Err = err
170 return res
Jamie Hannaford965ae702014-09-22 14:58:19 +0200171 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200172
Ash Wilson4bf41a32015-02-12 15:52:44 -0500173 _, res.Err = c.Request("POST", createURL(c), gophercloud.RequestOpts{
174 JSONBody: &reqBody,
175 JSONResponse: &res.Body,
Jamie Hannaford63631432014-09-18 11:40:09 +0200176 })
Jamie Hannaford63631432014-09-18 11:40:09 +0200177
Jamie Hannafordd9036422014-09-23 17:50:24 +0200178 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200179}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200180
Jon Perritt04851d32014-10-14 02:07:13 -0500181// UpdateOptsBuilder allows extensions to add additional parameters to the
182// Update request.
183type UpdateOptsBuilder interface {
184 ToSubnetUpdateMap() (map[string]interface{}, error)
185}
186
Jamie Hannaford686c4962014-09-23 10:46:20 +0200187// UpdateOpts represents the attributes used when updating an existing subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200188type UpdateOpts struct {
189 Name string
190 GatewayIP string
191 DNSNameservers []string
Jamie Hannaford5d93f562014-10-09 10:53:32 +0200192 HostRoutes []HostRoute
Jamie Hannaford965ae702014-09-22 14:58:19 +0200193 EnableDHCP *bool
194}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200195
Jon Perritt04851d32014-10-14 02:07:13 -0500196// ToSubnetUpdateMap casts an UpdateOpts struct to a map.
197func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
198 s := make(map[string]interface{})
199
200 if opts.EnableDHCP != nil {
201 s["enable_dhcp"] = &opts.EnableDHCP
202 }
203 if opts.Name != "" {
204 s["name"] = opts.Name
205 }
206 if opts.GatewayIP != "" {
207 s["gateway_ip"] = opts.GatewayIP
208 }
209 if len(opts.DNSNameservers) != 0 {
210 s["dns_nameservers"] = opts.DNSNameservers
211 }
212 if len(opts.HostRoutes) != 0 {
213 s["host_routes"] = opts.HostRoutes
214 }
215
216 return map[string]interface{}{"subnet": s}, nil
217}
218
Jamie Hannaford686c4962014-09-23 10:46:20 +0200219// Update accepts a UpdateOpts struct and updates an existing subnet using the
220// values provided.
Jon Perritt04851d32014-10-14 02:07:13 -0500221func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200222 var res UpdateResult
Jon Perritt04851d32014-10-14 02:07:13 -0500223
224 reqBody, err := opts.ToSubnetUpdateMap()
225 if err != nil {
226 res.Err = err
227 return res
228 }
229
Ash Wilson59fb6c42015-02-12 16:21:13 -0500230 _, res.Err = c.Request("PUT", updateURL(c, id), gophercloud.RequestOpts{
231 JSONBody: &reqBody,
232 JSONResponse: &res.Body,
233 OkCodes: []int{200, 201},
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200234 })
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200235
Jamie Hannafordd9036422014-09-23 17:50:24 +0200236 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200237}
238
Jamie Hannaford686c4962014-09-23 10:46:20 +0200239// Delete accepts a unique ID and deletes the subnet associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200240func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
241 var res DeleteResult
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100242 _, res.Err = c.Request("DELETE", deleteURL(c, id), gophercloud.RequestOpts{})
Jamie Hannafordd9036422014-09-23 17:50:24 +0200243 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200244}