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 | |
Jamie Hannaford | c98f59b | 2014-10-09 10:32:50 +0200 | [diff] [blame] | 8 | // AdminState gives users a solid type to work with for create and update |
| 9 | // operations. It is recommended that users use the `Up` and `Down` enums. |
| 10 | type AdminState *bool |
| 11 | |
| 12 | // Convenience vars for AdminStateUp values. |
| 13 | var ( |
| 14 | iTrue = true |
| 15 | iFalse = false |
| 16 | |
| 17 | Up AdminState = &iTrue |
| 18 | Down AdminState = &iFalse |
| 19 | ) |
| 20 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 21 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 22 | // List request. |
| 23 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 24 | ToPortListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 27 | // ListOpts allows the filtering and sorting of paginated collections through |
| 28 | // the API. Filtering is achieved by passing in struct field values that map to |
| 29 | // the port attributes you want to see returned. SortKey allows you to sort |
| 30 | // by a particular port attribute. SortDir sets the direction, and is either |
| 31 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 32 | type ListOpts struct { |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 33 | Status string `q:"status"` |
| 34 | Name string `q:"name"` |
| 35 | AdminStateUp *bool `q:"admin_state_up"` |
| 36 | NetworkID string `q:"network_id"` |
| 37 | TenantID string `q:"tenant_id"` |
| 38 | DeviceOwner string `q:"device_owner"` |
| 39 | MACAddress string `q:"mac_address"` |
| 40 | ID string `q:"id"` |
| 41 | DeviceID string `q:"device_id"` |
| 42 | Limit int `q:"limit"` |
| 43 | Marker string `q:"marker"` |
| 44 | SortKey string `q:"sort_key"` |
| 45 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 48 | // ToPortListQuery formats a ListOpts into a query string. |
| 49 | func (opts ListOpts) ToPortListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 50 | q, err := gophercloud.BuildQueryString(opts) |
| 51 | if err != nil { |
| 52 | return "", err |
| 53 | } |
| 54 | return q.String(), nil |
| 55 | } |
| 56 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 57 | // List returns a Pager which allows you to iterate over a collection of |
| 58 | // ports. It accepts a ListOpts struct, which allows you to filter and sort |
| 59 | // the returned collection for greater efficiency. |
| 60 | // |
| 61 | // 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] | 62 | // 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] | 63 | // administrative rights. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 64 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 65 | url := listURL(c) |
| 66 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 67 | query, err := opts.ToPortListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 68 | if err != nil { |
| 69 | return pagination.Pager{Err: err} |
| 70 | } |
| 71 | url += query |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 72 | } |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 73 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 74 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 75 | return PortPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 76 | }) |
| 77 | } |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 78 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 79 | // Get retrieves a specific port based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 80 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 81 | var res GetResult |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 82 | _, res.Err = c.Get(getURL(c, id), &res.Body, nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 83 | return res |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 84 | } |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 85 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 86 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 87 | // to be used in the main Create operation in this package. Since many |
| 88 | // extensions decorate or modify the common logic, it is useful for them to |
| 89 | // satisfy a basic interface in order for them to be used. |
| 90 | type CreateOptsBuilder interface { |
| 91 | ToPortCreateMap() (map[string]interface{}, error) |
| 92 | } |
| 93 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 94 | // CreateOpts represents the attributes used when creating a new port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 95 | type CreateOpts struct { |
Travis Truman | a371c0e | 2015-10-01 10:32:20 -0400 | [diff] [blame] | 96 | NetworkID string |
| 97 | Name string |
| 98 | AdminStateUp *bool |
| 99 | MACAddress string |
| 100 | FixedIPs interface{} |
| 101 | DeviceID string |
| 102 | DeviceOwner string |
| 103 | TenantID string |
| 104 | SecurityGroups []string |
Travis Truman | 0c24665 | 2016-02-01 09:50:38 -0500 | [diff] [blame] | 105 | AllowedAddressPairs []AddressPair |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 108 | // ToPortCreateMap casts a CreateOpts struct to a map. |
| 109 | func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) { |
| 110 | p := make(map[string]interface{}) |
| 111 | |
| 112 | if opts.NetworkID == "" { |
| 113 | return nil, errNetworkIDRequired |
| 114 | } |
| 115 | p["network_id"] = opts.NetworkID |
| 116 | |
| 117 | if opts.DeviceID != "" { |
| 118 | p["device_id"] = opts.DeviceID |
| 119 | } |
| 120 | if opts.DeviceOwner != "" { |
| 121 | p["device_owner"] = opts.DeviceOwner |
| 122 | } |
| 123 | if opts.FixedIPs != nil { |
| 124 | p["fixed_ips"] = opts.FixedIPs |
| 125 | } |
| 126 | if opts.SecurityGroups != nil { |
| 127 | p["security_groups"] = opts.SecurityGroups |
| 128 | } |
| 129 | if opts.TenantID != "" { |
| 130 | p["tenant_id"] = opts.TenantID |
| 131 | } |
| 132 | if opts.AdminStateUp != nil { |
| 133 | p["admin_state_up"] = &opts.AdminStateUp |
| 134 | } |
| 135 | if opts.Name != "" { |
| 136 | p["name"] = opts.Name |
| 137 | } |
| 138 | if opts.MACAddress != "" { |
| 139 | p["mac_address"] = opts.MACAddress |
| 140 | } |
Travis Truman | a371c0e | 2015-10-01 10:32:20 -0400 | [diff] [blame] | 141 | if opts.AllowedAddressPairs != nil { |
| 142 | p["allowed_address_pairs"] = opts.AllowedAddressPairs |
| 143 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 144 | |
| 145 | return map[string]interface{}{"port": p}, nil |
| 146 | } |
| 147 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 148 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 149 | // provided. You must remember to provide a NetworkID value. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 150 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 151 | var res CreateResult |
| 152 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 153 | reqBody, err := opts.ToPortCreateMap() |
| 154 | if err != nil { |
| 155 | res.Err = err |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 156 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 159 | _, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 160 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 163 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 164 | // to be used in the main Update operation in this package. Since many |
| 165 | // extensions decorate or modify the common logic, it is useful for them to |
| 166 | // satisfy a basic interface in order for them to be used. |
| 167 | type UpdateOptsBuilder interface { |
| 168 | ToPortUpdateMap() (map[string]interface{}, error) |
| 169 | } |
| 170 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 171 | // UpdateOpts represents the attributes used when updating an existing port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 172 | type UpdateOpts struct { |
Travis Truman | 0447aca | 2015-09-15 16:09:24 -0400 | [diff] [blame] | 173 | Name string |
| 174 | AdminStateUp *bool |
| 175 | FixedIPs interface{} |
| 176 | DeviceID string |
| 177 | DeviceOwner string |
| 178 | SecurityGroups []string |
Travis Truman | 0c24665 | 2016-02-01 09:50:38 -0500 | [diff] [blame] | 179 | AllowedAddressPairs []AddressPair |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 182 | // ToPortUpdateMap casts an UpdateOpts struct to a map. |
| 183 | func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) { |
| 184 | p := make(map[string]interface{}) |
| 185 | |
| 186 | if opts.DeviceID != "" { |
| 187 | p["device_id"] = opts.DeviceID |
| 188 | } |
| 189 | if opts.DeviceOwner != "" { |
| 190 | p["device_owner"] = opts.DeviceOwner |
| 191 | } |
| 192 | if opts.FixedIPs != nil { |
| 193 | p["fixed_ips"] = opts.FixedIPs |
| 194 | } |
| 195 | if opts.SecurityGroups != nil { |
| 196 | p["security_groups"] = opts.SecurityGroups |
| 197 | } |
| 198 | if opts.AdminStateUp != nil { |
| 199 | p["admin_state_up"] = &opts.AdminStateUp |
| 200 | } |
| 201 | if opts.Name != "" { |
| 202 | p["name"] = opts.Name |
| 203 | } |
Travis Truman | 0447aca | 2015-09-15 16:09:24 -0400 | [diff] [blame] | 204 | if opts.AllowedAddressPairs != nil { |
| 205 | p["allowed_address_pairs"] = opts.AllowedAddressPairs |
| 206 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 207 | |
| 208 | return map[string]interface{}{"port": p}, nil |
| 209 | } |
| 210 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 211 | // Update accepts a UpdateOpts struct and updates an existing port using the |
| 212 | // values provided. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 213 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 214 | var res UpdateResult |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 215 | |
| 216 | reqBody, err := opts.ToPortUpdateMap() |
| 217 | if err != nil { |
| 218 | res.Err = err |
| 219 | return res |
| 220 | } |
| 221 | |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 222 | _, res.Err = c.Put(updateURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 223 | OkCodes: []int{200, 201}, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 224 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 225 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 226 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 227 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 228 | // Delete accepts a unique ID and deletes the port associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 229 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 230 | var res DeleteResult |
Jamie Hannaford | 059e150 | 2015-03-24 16:20:32 +0100 | [diff] [blame] | 231 | _, res.Err = c.Delete(deleteURL(c, id), nil) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 232 | return res |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 233 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 234 | |
jrperritt | 14f716b | 2015-06-28 19:07:52 -0600 | [diff] [blame] | 235 | // 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] | 236 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 237 | count := 0 |
| 238 | id := "" |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 239 | if name == "" { |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 240 | err := &gophercloud.ErrMissingInput{} |
| 241 | err.Function = "ports.IDFromName" |
| 242 | err.Argument = "name" |
| 243 | return "", err |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 244 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 245 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 246 | pages, err := List(client, nil).AllPages() |
| 247 | if err != nil { |
| 248 | return "", err |
| 249 | } |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 250 | |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 251 | all, err := ExtractPorts(pages) |
| 252 | if err != nil { |
| 253 | return "", err |
| 254 | } |
| 255 | |
| 256 | for _, s := range all { |
| 257 | if s.Name == name { |
| 258 | count++ |
| 259 | id = s.ID |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | switch count { |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 264 | case 0: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 265 | err := &gophercloud.ErrResourceNotFound{} |
| 266 | err.Name = name |
| 267 | err.ResourceType = "port" |
| 268 | err.Function = "ports.IDFromName" |
| 269 | return "", err |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 270 | case 1: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 271 | return id, nil |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 272 | default: |
Jon Perritt | e3cb7e4 | 2016-03-07 06:24:11 -0600 | [diff] [blame] | 273 | err := &gophercloud.ErrMultipleResourcesFound{} |
| 274 | err.Count = count |
| 275 | err.Name = name |
| 276 | err.ResourceType = "port" |
| 277 | err.Function = "ports.IDFromName" |
| 278 | return "", err |
Jon Perritt | 7ab1328 | 2015-06-28 18:47:19 -0600 | [diff] [blame] | 279 | } |
| 280 | } |