Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 1 | package networks |
| 2 | |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 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 | 01e1492 | 2014-09-11 15:23: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 | ToNetworkListQuery() (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 network attributes you want to see returned. SortKey allows you to sort |
| 17 | // by a particular network attribute. SortDir sets the direction, and is either |
| 18 | // `asc' or `desc'. Marker and Limit are used for pagination. |
| 19 | type ListOpts struct { |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 20 | Status string `q:"status"` |
| 21 | Name string `q:"name"` |
| 22 | AdminStateUp *bool `q:"admin_state_up"` |
| 23 | TenantID string `q:"tenant_id"` |
| 24 | Shared *bool `q:"shared"` |
| 25 | ID string `q:"id"` |
| 26 | Marker string `q:"marker"` |
| 27 | Limit int `q:"limit"` |
| 28 | SortKey string `q:"sort_key"` |
| 29 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 32 | // ToNetworkListQuery formats a ListOpts into a query string. |
| 33 | func (opts ListOpts) ToNetworkListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 34 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 35 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 38 | // List returns a Pager which allows you to iterate over a collection of |
| 39 | // networks. It accepts a ListOpts struct, which allows you to filter and sort |
| 40 | // the returned collection for greater efficiency. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 41 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 42 | url := listURL(c) |
| 43 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 44 | query, err := opts.ToNetworkListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 45 | if err != nil { |
| 46 | return pagination.Pager{Err: err} |
| 47 | } |
| 48 | url += query |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 49 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 50 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 51 | return NetworkPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 52 | }) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 55 | // Get retrieves a specific network based on its unique ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 56 | func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 57 | _, r.Err = c.Get(getURL(c, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 58 | return |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 59 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 60 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 61 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 62 | // to be used in the main Create operation in this package. Since many |
| 63 | // extensions decorate or modify the common logic, it is useful for them to |
| 64 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 65 | type CreateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 66 | ToNetworkCreateMap() (map[string]interface{}, error) |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 69 | // CreateOpts satisfies the CreateOptsBuilder interface |
| 70 | type CreateOpts struct { |
| 71 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 72 | Name string `json:"name,omitempty"` |
| 73 | Shared *bool `json:"shared,omitempty"` |
| 74 | TenantID string `json:"tenant_id,omitempty"` |
| 75 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 76 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 77 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 78 | func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 79 | return gophercloud.BuildRequestBody(opts, "network") |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 82 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 83 | // provided. This operation does not actually require a request body, i.e. the |
| 84 | // CreateOpts struct argument can be empty. |
| 85 | // |
| 86 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 87 | // network. An admin user, however, has the option of specifying another tenant |
| 88 | // ID in the CreateOpts struct. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 89 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 90 | b, err := opts.ToNetworkCreateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 91 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 92 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 93 | return |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 94 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 95 | _, r.Err = c.Post(createURL(c), b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 96 | return |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 97 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 98 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 99 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 100 | // to be used in the main Update operation in this package. Since many |
| 101 | // extensions decorate or modify the common logic, it is useful for them to |
| 102 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 103 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 104 | ToNetworkUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 107 | // UpdateOpts satisfies the UpdateOptsBuilder interface |
| 108 | type UpdateOpts struct { |
| 109 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 110 | Name string `json:"name,omitempty"` |
| 111 | Shared *bool `json:"shared,omitempty"` |
| 112 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 113 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 114 | // ToNetworkUpdateMap casts a UpdateOpts struct to a map. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 115 | func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 116 | return gophercloud.BuildRequestBody(opts, "network") |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 119 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 120 | // values provided. For more information, see the Create function. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 121 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 122 | b, err := opts.ToNetworkUpdateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 123 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 124 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 125 | return |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 126 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 127 | _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 128 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 129 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 130 | return |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 131 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 132 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 133 | // Delete accepts a unique ID and deletes the network associated with it. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 134 | func Delete(c *gophercloud.ServiceClient, networkID string) (r DeleteResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 135 | _, r.Err = c.Delete(deleteURL(c, networkID), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 136 | return |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 137 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 138 | |
jrperritt | 376d4f7 | 2015-06-28 19:07:34 -0600 | [diff] [blame] | 139 | // IDFromName is a convenience function that returns a network's ID given its name. |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 140 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 141 | count := 0 |
| 142 | id := "" |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 143 | pages, err := List(client, nil).AllPages() |
| 144 | if err != nil { |
| 145 | return "", err |
| 146 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 147 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 148 | all, err := ExtractNetworks(pages) |
| 149 | if err != nil { |
| 150 | return "", err |
| 151 | } |
| 152 | |
| 153 | for _, s := range all { |
| 154 | if s.Name == name { |
| 155 | count++ |
| 156 | id = s.ID |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | switch count { |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 161 | case 0: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 162 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 163 | case 1: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 164 | return id, nil |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 165 | default: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 166 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 167 | } |
| 168 | } |