Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 8 | // 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. |
| 10 | type AdminState *bool |
| 11 | |
| 12 | // Convenience vars for AdminStateUp values. |
| 13 | var ( |
| 14 | iTrue = true |
| 15 | iFalse = false |
| 16 | |
| 17 | Up AdminState = &iTrue |
| 18 | Down AdminState = &iFalse |
| 19 | ) |
| 20 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 21 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 22 | // List request. |
| 23 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 24 | ToSubnetListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 27 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 32 | type ListOpts struct { |
Jamie Hannaford | 5c6e996 | 2014-10-02 10:34:14 +0200 | [diff] [blame] | 33 | 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 47 | // ToSubnetListQuery formats a ListOpts into a query string. |
| 48 | func (opts ListOpts) ToSubnetListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 49 | q, err := gophercloud.BuildQueryString(opts) |
| 50 | if err != nil { |
| 51 | return "", err |
| 52 | } |
| 53 | return q.String(), nil |
| 54 | } |
| 55 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 56 | // 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 Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 61 | // who submits the request, unless the request is submitted by a user with |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 62 | // administrative rights. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 63 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 64 | url := listURL(c) |
| 65 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 66 | query, err := opts.ToSubnetListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 67 | if err != nil { |
| 68 | return pagination.Pager{Err: err} |
| 69 | } |
| 70 | url += query |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 71 | } |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 72 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 73 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 74 | return SubnetPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 75 | }) |
| 76 | } |
| 77 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 78 | // Get retrieves a specific subnet based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 79 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 80 | var res GetResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 81 | _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{ |
| 82 | JSONResponse: &res.Body, |
| 83 | OkCodes: []int{200}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 84 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 85 | return res |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 86 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 87 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 88 | // Valid IP types |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 89 | const ( |
| 90 | IPv4 = 4 |
| 91 | IPv6 = 6 |
| 92 | ) |
| 93 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 94 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 95 | // to be used in the main Create operation in this package. Since many |
| 96 | // extensions decorate or modify the common logic, it is useful for them to |
| 97 | // satisfy a basic interface in order for them to be used. |
| 98 | type CreateOptsBuilder interface { |
| 99 | ToSubnetCreateMap() (map[string]interface{}, error) |
| 100 | } |
| 101 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 102 | // CreateOpts represents the attributes used when creating a new subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 103 | type CreateOpts struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 104 | // 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 Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 113 | EnableDHCP *bool |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 114 | DNSNameservers []string |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 115 | HostRoutes []HostRoute |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 118 | // ToSubnetCreateMap casts a CreateOpts struct to a map. |
| 119 | func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) { |
| 120 | s := make(map[string]interface{}) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 121 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 122 | if opts.NetworkID == "" { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 123 | return nil, errNetworkIDRequired |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 124 | } |
| 125 | if opts.CIDR == "" { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 126 | return nil, errCIDRRequired |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 127 | } |
| 128 | if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 129 | return nil, errInvalidIPType |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 132 | s["network_id"] = opts.NetworkID |
| 133 | s["cidr"] = opts.CIDR |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 134 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 135 | if opts.EnableDHCP != nil { |
| 136 | s["enable_dhcp"] = &opts.EnableDHCP |
| 137 | } |
| 138 | if opts.Name != "" { |
| 139 | s["name"] = opts.Name |
| 140 | } |
| 141 | if opts.GatewayIP != "" { |
| 142 | s["gateway_ip"] = opts.GatewayIP |
| 143 | } |
| 144 | if opts.TenantID != "" { |
| 145 | s["tenant_id"] = opts.TenantID |
| 146 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 147 | if opts.IPVersion != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 148 | s["ip_version"] = opts.IPVersion |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 149 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 150 | if len(opts.AllocationPools) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 151 | s["allocation_pools"] = opts.AllocationPools |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 152 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 153 | if len(opts.DNSNameservers) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 154 | s["dns_nameservers"] = opts.DNSNameservers |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 155 | } |
| 156 | if len(opts.HostRoutes) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 157 | s["host_routes"] = opts.HostRoutes |
| 158 | } |
| 159 | |
| 160 | return map[string]interface{}{"subnet": s}, nil |
| 161 | } |
| 162 | |
| 163 | // Create accepts a CreateOpts struct and creates a new subnet using the values |
| 164 | // provided. You must remember to provide a valid NetworkID, CIDR and IP version. |
| 165 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 166 | var res CreateResult |
| 167 | |
| 168 | reqBody, err := opts.ToSubnetCreateMap() |
| 169 | if err != nil { |
| 170 | res.Err = err |
| 171 | return res |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 172 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 173 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 174 | _, res.Err = c.Request("POST", createURL(c), gophercloud.RequestOpts{ |
| 175 | JSONBody: &reqBody, |
| 176 | JSONResponse: &res.Body, |
| 177 | OkCodes: []int{201}, |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 178 | }) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 179 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 180 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 181 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 182 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 183 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 184 | // Update request. |
| 185 | type UpdateOptsBuilder interface { |
| 186 | ToSubnetUpdateMap() (map[string]interface{}, error) |
| 187 | } |
| 188 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 189 | // UpdateOpts represents the attributes used when updating an existing subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 190 | type UpdateOpts struct { |
| 191 | Name string |
| 192 | GatewayIP string |
| 193 | DNSNameservers []string |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 194 | HostRoutes []HostRoute |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 195 | EnableDHCP *bool |
| 196 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 197 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 198 | // ToSubnetUpdateMap casts an UpdateOpts struct to a map. |
| 199 | func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) { |
| 200 | s := make(map[string]interface{}) |
| 201 | |
| 202 | if opts.EnableDHCP != nil { |
| 203 | s["enable_dhcp"] = &opts.EnableDHCP |
| 204 | } |
| 205 | if opts.Name != "" { |
| 206 | s["name"] = opts.Name |
| 207 | } |
| 208 | if opts.GatewayIP != "" { |
| 209 | s["gateway_ip"] = opts.GatewayIP |
| 210 | } |
| 211 | if len(opts.DNSNameservers) != 0 { |
| 212 | s["dns_nameservers"] = opts.DNSNameservers |
| 213 | } |
| 214 | if len(opts.HostRoutes) != 0 { |
| 215 | s["host_routes"] = opts.HostRoutes |
| 216 | } |
| 217 | |
| 218 | return map[string]interface{}{"subnet": s}, nil |
| 219 | } |
| 220 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 221 | // Update accepts a UpdateOpts struct and updates an existing subnet using the |
| 222 | // values provided. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 223 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 224 | var res UpdateResult |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 225 | |
| 226 | reqBody, err := opts.ToSubnetUpdateMap() |
| 227 | if err != nil { |
| 228 | res.Err = err |
| 229 | return res |
| 230 | } |
| 231 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 232 | _, res.Err = c.Request("PUT", updateURL(c, id), gophercloud.RequestOpts{ |
| 233 | JSONBody: &reqBody, |
| 234 | JSONResponse: &res.Body, |
| 235 | OkCodes: []int{200, 201}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 236 | }) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 237 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 238 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 241 | // Delete accepts a unique ID and deletes the subnet associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 242 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 243 | var res DeleteResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 244 | _, res.Err = c.Request("DELETE", deleteURL(c, id), gophercloud.RequestOpts{ |
| 245 | OkCodes: []int{204}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 246 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 247 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 248 | } |