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 ( |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 10 | // AdminState gives users a solid type to work with for create and update |
| 11 | // operations. It is recommended that users use the `Up` and `Down` enums. |
| 12 | type AdminState *bool |
| 13 | |
| 14 | // Convenience vars for AdminStateUp values. |
| 15 | var ( |
| 16 | iTrue = true |
| 17 | iFalse = false |
| 18 | |
| 19 | Up AdminState = &iTrue |
| 20 | Down AdminState = &iFalse |
| 21 | ) |
| 22 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 23 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 24 | // List request. |
| 25 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 26 | ToSubnetListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 27 | } |
| 28 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 29 | // ListOpts allows the filtering and sorting of paginated collections through |
| 30 | // the API. Filtering is achieved by passing in struct field values that map to |
| 31 | // the subnet attributes you want to see returned. SortKey allows you to sort |
| 32 | // by a particular subnet attribute. SortDir sets the direction, and is either |
| 33 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 34 | type ListOpts struct { |
Jamie Hannaford | 5c6e996 | 2014-10-02 10:34:14 +0200 | [diff] [blame] | 35 | Name string `q:"name"` |
| 36 | EnableDHCP *bool `q:"enable_dhcp"` |
| 37 | NetworkID string `q:"network_id"` |
| 38 | TenantID string `q:"tenant_id"` |
| 39 | IPVersion int `q:"ip_version"` |
| 40 | GatewayIP string `q:"gateway_ip"` |
| 41 | CIDR string `q:"cidr"` |
| 42 | ID string `q:"id"` |
| 43 | Limit int `q:"limit"` |
| 44 | Marker string `q:"marker"` |
| 45 | SortKey string `q:"sort_key"` |
| 46 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 49 | // ToSubnetListQuery formats a ListOpts into a query string. |
| 50 | func (opts ListOpts) ToSubnetListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 51 | q, err := gophercloud.BuildQueryString(opts) |
| 52 | if err != nil { |
| 53 | return "", err |
| 54 | } |
| 55 | return q.String(), nil |
| 56 | } |
| 57 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 58 | // List returns a Pager which allows you to iterate over a collection of |
| 59 | // subnets. It accepts a ListOpts struct, which allows you to filter and sort |
| 60 | // the returned collection for greater efficiency. |
| 61 | // |
| 62 | // 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] | 63 | // 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] | 64 | // administrative rights. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 65 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 66 | url := listURL(c) |
| 67 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 68 | query, err := opts.ToSubnetListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 69 | if err != nil { |
| 70 | return pagination.Pager{Err: err} |
| 71 | } |
| 72 | url += query |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 73 | } |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 74 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 75 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 76 | return SubnetPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 77 | }) |
| 78 | } |
| 79 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 80 | // Get retrieves a specific subnet based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 81 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 82 | var res GetResult |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 83 | _, res.Err = c.Get(getURL(c, id), &res.Body, nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 84 | return res |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 85 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 86 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 87 | // Valid IP types |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 88 | const ( |
| 89 | IPv4 = 4 |
| 90 | IPv6 = 6 |
| 91 | ) |
| 92 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 93 | // 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. |
| 97 | type CreateOptsBuilder interface { |
| 98 | ToSubnetCreateMap() (map[string]interface{}, error) |
| 99 | } |
| 100 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 101 | // CreateOpts represents the attributes used when creating a new subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 102 | type CreateOpts struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 103 | // 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 Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 112 | EnableDHCP *bool |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 113 | DNSNameservers []string |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 114 | HostRoutes []HostRoute |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 117 | // ToSubnetCreateMap casts a CreateOpts struct to a map. |
| 118 | func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) { |
| 119 | s := make(map[string]interface{}) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 120 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 121 | if opts.NetworkID == "" { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 122 | return nil, errNetworkIDRequired |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 123 | } |
| 124 | if opts.CIDR == "" { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 125 | return nil, errCIDRRequired |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 126 | } |
| 127 | if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 128 | return nil, errInvalidIPType |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 131 | s["network_id"] = opts.NetworkID |
| 132 | s["cidr"] = opts.CIDR |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 133 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 134 | 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 Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 146 | if opts.IPVersion != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 147 | s["ip_version"] = opts.IPVersion |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 148 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 149 | if len(opts.AllocationPools) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 150 | s["allocation_pools"] = opts.AllocationPools |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 151 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 152 | if len(opts.DNSNameservers) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 153 | s["dns_nameservers"] = opts.DNSNameservers |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 154 | } |
| 155 | if len(opts.HostRoutes) != 0 { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 156 | 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. |
| 164 | func 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 171 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 172 | |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 173 | _, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 174 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 175 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 176 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 177 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 178 | // Update request. |
| 179 | type UpdateOptsBuilder interface { |
| 180 | ToSubnetUpdateMap() (map[string]interface{}, error) |
| 181 | } |
| 182 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 183 | // UpdateOpts represents the attributes used when updating an existing subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 184 | type UpdateOpts struct { |
| 185 | Name string |
| 186 | GatewayIP string |
| 187 | DNSNameservers []string |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 188 | HostRoutes []HostRoute |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 189 | EnableDHCP *bool |
| 190 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 191 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 192 | // ToSubnetUpdateMap casts an UpdateOpts struct to a map. |
| 193 | func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) { |
| 194 | s := make(map[string]interface{}) |
| 195 | |
| 196 | if opts.EnableDHCP != nil { |
| 197 | s["enable_dhcp"] = &opts.EnableDHCP |
| 198 | } |
| 199 | if opts.Name != "" { |
| 200 | s["name"] = opts.Name |
| 201 | } |
| 202 | if opts.GatewayIP != "" { |
| 203 | s["gateway_ip"] = opts.GatewayIP |
| 204 | } |
Keith Byrne | fa1b308 | 2015-09-10 17:08:44 +0100 | [diff] [blame^] | 205 | if opts.DNSNameservers != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 206 | s["dns_nameservers"] = opts.DNSNameservers |
| 207 | } |
Keith Byrne | fa1b308 | 2015-09-10 17:08:44 +0100 | [diff] [blame^] | 208 | if opts.HostRoutes != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 209 | s["host_routes"] = opts.HostRoutes |
| 210 | } |
| 211 | |
| 212 | return map[string]interface{}{"subnet": s}, nil |
| 213 | } |
| 214 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 215 | // Update accepts a UpdateOpts struct and updates an existing subnet using the |
| 216 | // values provided. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 217 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 218 | var res UpdateResult |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 219 | |
| 220 | reqBody, err := opts.ToSubnetUpdateMap() |
| 221 | if err != nil { |
| 222 | res.Err = err |
| 223 | return res |
| 224 | } |
| 225 | |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 226 | _, res.Err = c.Put(updateURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 227 | OkCodes: []int{200, 201}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 228 | }) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 229 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 230 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 233 | // Delete accepts a unique ID and deletes the subnet associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 234 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 235 | var res DeleteResult |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 236 | _, res.Err = c.Delete(deleteURL(c, id), nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 237 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 238 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 239 | |
jrperritt | 5d1d835 | 2015-06-28 19:08:09 -0600 | [diff] [blame] | 240 | // IDFromName is a convenience function that returns a subnet's ID given its name. |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 241 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
| 242 | subnetCount := 0 |
| 243 | subnetID := "" |
| 244 | if name == "" { |
jrperritt | dbec8c0 | 2015-06-28 18:59:43 -0600 | [diff] [blame] | 245 | return "", fmt.Errorf("A subnet name must be provided.") |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 246 | } |
| 247 | pager := List(client, nil) |
| 248 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 249 | subnetList, err := ExtractSubnets(page) |
| 250 | if err != nil { |
| 251 | return false, err |
| 252 | } |
| 253 | |
| 254 | for _, s := range subnetList { |
| 255 | if s.Name == name { |
| 256 | subnetCount++ |
| 257 | subnetID = s.ID |
| 258 | } |
| 259 | } |
| 260 | return true, nil |
| 261 | }) |
| 262 | |
| 263 | switch subnetCount { |
| 264 | case 0: |
jrperritt | dbec8c0 | 2015-06-28 18:59:43 -0600 | [diff] [blame] | 265 | return "", fmt.Errorf("Unable to find subnet: %s", name) |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 266 | case 1: |
| 267 | return subnetID, nil |
| 268 | default: |
jrperritt | dbec8c0 | 2015-06-28 18:59:43 -0600 | [diff] [blame] | 269 | return "", fmt.Errorf("Found %d subnets matching %s", subnetCount, name) |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 270 | } |
| 271 | } |