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