Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +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 | ToPortListQuery() (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 port attributes you want to see returned. SortKey allows you to sort |
| 17 | // by a particular port attribute. SortDir sets the direction, and is either |
| 18 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 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 | NetworkID string `q:"network_id"` |
| 24 | TenantID string `q:"tenant_id"` |
| 25 | DeviceOwner string `q:"device_owner"` |
| 26 | MACAddress string `q:"mac_address"` |
| 27 | ID string `q:"id"` |
| 28 | DeviceID string `q:"device_id"` |
| 29 | Limit int `q:"limit"` |
| 30 | Marker string `q:"marker"` |
| 31 | SortKey string `q:"sort_key"` |
| 32 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 35 | // ToPortListQuery formats a ListOpts into a query string. |
| 36 | func (opts ListOpts) ToPortListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 37 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 38 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 41 | // List returns a Pager which allows you to iterate over a collection of |
| 42 | // ports. It accepts a ListOpts struct, which allows you to filter and sort |
| 43 | // the returned collection for greater efficiency. |
| 44 | // |
| 45 | // Default policy settings return only those ports that are owned by the tenant |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 46 | // 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] | 47 | // administrative rights. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 48 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 49 | url := listURL(c) |
| 50 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 51 | query, err := opts.ToPortListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 52 | if err != nil { |
| 53 | return pagination.Pager{Err: err} |
| 54 | } |
| 55 | url += query |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 56 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 57 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 58 | return PortPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 59 | }) |
| 60 | } |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 61 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 62 | // Get retrieves a specific port based on its unique ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 63 | func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 64 | _, r.Err = c.Get(getURL(c, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 65 | return |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 66 | } |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 67 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 68 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 69 | // to be used in the main Create operation in this package. Since many |
| 70 | // extensions decorate or modify the common logic, it is useful for them to |
| 71 | // satisfy a basic interface in order for them to be used. |
| 72 | type CreateOptsBuilder interface { |
| 73 | ToPortCreateMap() (map[string]interface{}, error) |
| 74 | } |
| 75 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 76 | // CreateOpts represents the attributes used when creating a new port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 77 | type CreateOpts struct { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 78 | NetworkID string `json:"network_id" required:"true"` |
| 79 | Name string `json:"name,omitempty"` |
| 80 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 81 | MACAddress string `json:"mac_address,omitempty"` |
| 82 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
| 83 | DeviceID string `json:"device_id,omitempty"` |
| 84 | DeviceOwner string `json:"device_owner,omitempty"` |
| 85 | TenantID string `json:"tenant_id,omitempty"` |
| 86 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 87 | AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 90 | // ToPortCreateMap casts a CreateOpts struct to a map. |
| 91 | func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 92 | return gophercloud.BuildRequestBody(opts, "port") |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 95 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 96 | // provided. You must remember to provide a NetworkID value. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 97 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 98 | b, err := opts.ToPortCreateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 99 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 100 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 101 | return |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 102 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 103 | _, r.Err = c.Post(createURL(c), b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 104 | return |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 107 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 108 | // to be used in the main Update operation in this package. Since many |
| 109 | // extensions decorate or modify the common logic, it is useful for them to |
| 110 | // satisfy a basic interface in order for them to be used. |
| 111 | type UpdateOptsBuilder interface { |
| 112 | ToPortUpdateMap() (map[string]interface{}, error) |
| 113 | } |
| 114 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 115 | // UpdateOpts represents the attributes used when updating an existing port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 116 | type UpdateOpts struct { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 117 | Name string `json:"name,omitempty"` |
| 118 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 119 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
| 120 | DeviceID string `json:"device_id,omitempty"` |
| 121 | DeviceOwner string `json:"device_owner,omitempty"` |
Joe Topjian | 6d2ec96 | 2017-02-13 21:23:55 -0700 | [diff] [blame] | 122 | SecurityGroups []string `json:"security_groups"` |
| 123 | AllowedAddressPairs []AddressPair `json:"allowed_address_pairs"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 126 | // ToPortUpdateMap casts an UpdateOpts struct to a map. |
| 127 | func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 128 | return gophercloud.BuildRequestBody(opts, "port") |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 131 | // Update accepts a UpdateOpts struct and updates an existing port using the |
| 132 | // values provided. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 133 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 134 | b, err := opts.ToPortUpdateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 135 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 136 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 137 | return |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 138 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 139 | _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 140 | OkCodes: []int{200, 201}, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 141 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 142 | return |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 143 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 144 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 145 | // Delete accepts a unique ID and deletes the port associated with it. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 146 | func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 147 | _, r.Err = c.Delete(deleteURL(c, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 148 | return |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 149 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 150 | |
jrperritt | 14f716b | 2015-06-28 19:07:52 -0600 | [diff] [blame] | 151 | // IDFromName is a convenience function that returns a port's ID given its name. |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 152 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 153 | count := 0 |
| 154 | id := "" |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 155 | pages, err := List(client, nil).AllPages() |
| 156 | if err != nil { |
| 157 | return "", err |
| 158 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 159 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 160 | all, err := ExtractPorts(pages) |
| 161 | if err != nil { |
| 162 | return "", err |
| 163 | } |
| 164 | |
| 165 | for _, s := range all { |
| 166 | if s.Name == name { |
| 167 | count++ |
| 168 | id = s.ID |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | switch count { |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 173 | case 0: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 174 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 175 | case 1: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 176 | return id, nil |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 177 | default: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame] | 178 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 179 | } |
| 180 | } |