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 | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 8 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | // List request. |
| 10 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 11 | ToSubnetListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 12 | } |
| 13 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 14 | // ListOpts allows the filtering and sorting of paginated collections through |
| 15 | // the API. Filtering is achieved by passing in struct field values that map to |
| 16 | // the subnet attributes you want to see returned. SortKey allows you to sort |
| 17 | // by a particular subnet attribute. SortDir sets the direction, and is either |
| 18 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 19 | type ListOpts struct { |
Jamie Hannaford | 5c6e996 | 2014-10-02 10:34:14 +0200 | [diff] [blame] | 20 | Name string `q:"name"` |
| 21 | EnableDHCP *bool `q:"enable_dhcp"` |
| 22 | NetworkID string `q:"network_id"` |
| 23 | TenantID string `q:"tenant_id"` |
| 24 | IPVersion int `q:"ip_version"` |
| 25 | GatewayIP string `q:"gateway_ip"` |
| 26 | CIDR string `q:"cidr"` |
| 27 | ID string `q:"id"` |
| 28 | Limit int `q:"limit"` |
| 29 | Marker string `q:"marker"` |
| 30 | SortKey string `q:"sort_key"` |
| 31 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 34 | // ToSubnetListQuery formats a ListOpts into a query string. |
| 35 | func (opts ListOpts) ToSubnetListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 37 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 40 | // List returns a Pager which allows you to iterate over a collection of |
| 41 | // subnets. It accepts a ListOpts struct, which allows you to filter and sort |
| 42 | // the returned collection for greater efficiency. |
| 43 | // |
| 44 | // 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] | 45 | // 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] | 46 | // administrative rights. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 47 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 48 | url := listURL(c) |
| 49 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 50 | query, err := opts.ToSubnetListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 51 | if err != nil { |
| 52 | return pagination.Pager{Err: err} |
| 53 | } |
| 54 | url += query |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 55 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 56 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 57 | return SubnetPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 58 | }) |
| 59 | } |
| 60 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 61 | // Get retrieves a specific subnet based on its unique ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 62 | func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 63 | _, r.Err = c.Get(getURL(c, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 64 | return |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 65 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 66 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 67 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 68 | // to be used in the main Create operation in this package. Since many |
| 69 | // extensions decorate or modify the common logic, it is useful for them to |
| 70 | // satisfy a basic interface in order for them to be used. |
| 71 | type CreateOptsBuilder interface { |
| 72 | ToSubnetCreateMap() (map[string]interface{}, error) |
| 73 | } |
| 74 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 75 | // CreateOpts represents the attributes used when creating a new subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 76 | type CreateOpts struct { |
jrperritt | bc54861 | 2016-04-13 17:03:59 -0500 | [diff] [blame] | 77 | NetworkID string `json:"network_id" required:"true"` |
| 78 | CIDR string `json:"cidr" required:"true"` |
| 79 | Name string `json:"name,omitempty"` |
| 80 | TenantID string `json:"tenant_id,omitempty"` |
| 81 | AllocationPools []AllocationPool `json:"allocation_pools,omitempty"` |
| 82 | GatewayIP *string `json:"gateway_ip"` |
| 83 | IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"` |
| 84 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
| 85 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 86 | HostRoutes []HostRoute `json:"host_routes,omitempty"` |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 89 | // ToSubnetCreateMap casts a CreateOpts struct to a map. |
| 90 | func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 91 | return gophercloud.BuildRequestBody(opts, "subnet") |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Create accepts a CreateOpts struct and creates a new subnet using the values |
| 95 | // provided. You must remember to provide a valid NetworkID, CIDR and IP version. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 96 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 97 | b, err := opts.ToSubnetCreateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 98 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 99 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 100 | return |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 101 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 102 | _, r.Err = c.Post(createURL(c), b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 103 | return |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 104 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 105 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 106 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 107 | // Update request. |
| 108 | type UpdateOptsBuilder interface { |
| 109 | ToSubnetUpdateMap() (map[string]interface{}, error) |
| 110 | } |
| 111 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 112 | // UpdateOpts represents the attributes used when updating an existing subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 113 | type UpdateOpts struct { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 114 | Name string `json:"name,omitempty"` |
| 115 | GatewayIP string `json:"gateway_ip,omitempty"` |
| 116 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 117 | HostRoutes []HostRoute `json:"host_routes,omitempty"` |
| 118 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 119 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 120 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 121 | // ToSubnetUpdateMap casts an UpdateOpts struct to a map. |
| 122 | func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 123 | return gophercloud.BuildRequestBody(opts, "subnet") |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 126 | // Update accepts a UpdateOpts struct and updates an existing subnet using the |
| 127 | // values provided. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 128 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 129 | b, err := opts.ToSubnetUpdateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 130 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 131 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 132 | return |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 133 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 134 | _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 135 | OkCodes: []int{200, 201}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 136 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 137 | return |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 140 | // Delete accepts a unique ID and deletes the subnet associated with it. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 141 | func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 142 | _, r.Err = c.Delete(deleteURL(c, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 143 | return |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 144 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 145 | |
jrperritt | 5d1d835 | 2015-06-28 19:08:09 -0600 | [diff] [blame] | 146 | // 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] | 147 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 148 | count := 0 |
| 149 | id := "" |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 150 | pages, err := List(client, nil).AllPages() |
| 151 | if err != nil { |
| 152 | return "", err |
| 153 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 154 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 155 | all, err := ExtractSubnets(pages) |
| 156 | if err != nil { |
| 157 | return "", err |
| 158 | } |
| 159 | |
| 160 | for _, s := range all { |
| 161 | if s.Name == name { |
| 162 | count++ |
| 163 | id = s.ID |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | switch count { |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 168 | case 0: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 169 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "subnet"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 170 | case 1: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 171 | return id, nil |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 172 | default: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 173 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "subnet"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 174 | } |
| 175 | } |