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" |
| 5 | "github.com/gophercloud/gophercloud/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. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 63 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 64 | var r GetResult |
| 65 | _, r.Err = c.Get(getURL(c, id), &r.Body, nil) |
| 66 | return r |
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 | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 98 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 99 | var r CreateResult |
| 100 | b, err := opts.ToPortCreateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 101 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 102 | r.Err = err |
| 103 | return r |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 104 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 105 | _, r.Err = c.Post(createURL(c), b, &r.Body, nil) |
| 106 | return r |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 109 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 110 | // to be used in the main Update operation in this package. Since many |
| 111 | // extensions decorate or modify the common logic, it is useful for them to |
| 112 | // satisfy a basic interface in order for them to be used. |
| 113 | type UpdateOptsBuilder interface { |
| 114 | ToPortUpdateMap() (map[string]interface{}, error) |
| 115 | } |
| 116 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 117 | // UpdateOpts represents the attributes used when updating an existing port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 118 | type UpdateOpts struct { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 119 | Name string `json:"name,omitempty"` |
| 120 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 121 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
| 122 | DeviceID string `json:"device_id,omitempty"` |
| 123 | DeviceOwner string `json:"device_owner,omitempty"` |
| 124 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 125 | AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 128 | // ToPortUpdateMap casts an UpdateOpts struct to a map. |
| 129 | func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 130 | return gophercloud.BuildRequestBody(opts, "port") |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 133 | // Update accepts a UpdateOpts struct and updates an existing port using the |
| 134 | // values provided. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 135 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 136 | var r UpdateResult |
| 137 | b, err := opts.ToPortUpdateMap() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 138 | if err != nil { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 139 | r.Err = err |
| 140 | return r |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 141 | } |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 142 | _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 143 | OkCodes: []int{200, 201}, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 144 | }) |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 145 | return r |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 146 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 147 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 148 | // Delete accepts a unique ID and deletes the port associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 149 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 150 | var r DeleteResult |
| 151 | _, r.Err = c.Delete(deleteURL(c, id), nil) |
| 152 | return r |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 153 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 154 | |
jrperritt | 14f716b | 2015-06-28 19:07:52 -0600 | [diff] [blame] | 155 | // 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] | 156 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 157 | count := 0 |
| 158 | id := "" |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 159 | pages, err := List(client, nil).AllPages() |
| 160 | if err != nil { |
| 161 | return "", err |
| 162 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 163 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 164 | all, err := ExtractPorts(pages) |
| 165 | if err != nil { |
| 166 | return "", err |
| 167 | } |
| 168 | |
| 169 | for _, s := range all { |
| 170 | if s.Name == name { |
| 171 | count++ |
| 172 | id = s.ID |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | switch count { |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 177 | case 0: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 178 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 179 | case 1: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 180 | return id, nil |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 181 | default: |
Jon Perritt | e1c6ceb | 2016-03-14 12:09:36 -0500 | [diff] [blame^] | 182 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"} |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 183 | } |
| 184 | } |