Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 6 | "github.com/racker/perigee" |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/openstack/utils" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 12 | // ListOpts allows the filtering and sorting of paginated collections through |
| 13 | // the API. Filtering is achieved by passing in struct field values that map to |
| 14 | // the port attributes you want to see returned. SortKey allows you to sort |
| 15 | // by a particular port attribute. SortDir sets the direction, and is either |
| 16 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 17 | type ListOpts struct { |
| 18 | Status string |
| 19 | Name string |
| 20 | AdminStateUp *bool |
| 21 | NetworkID string |
| 22 | TenantID string |
| 23 | DeviceOwner string |
| 24 | MACAddress string |
| 25 | ID string |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 26 | DeviceID string |
| 27 | BindingHostID string |
| 28 | BindingVIFType string |
| 29 | BindingVNICType string |
| 30 | Limit int |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 31 | Marker string |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 32 | SortKey string |
| 33 | SortDir string |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 36 | // List returns a Pager which allows you to iterate over a collection of |
| 37 | // ports. It accepts a ListOpts struct, which allows you to filter and sort |
| 38 | // the returned collection for greater efficiency. |
| 39 | // |
| 40 | // Default policy settings return only those ports that are owned by the tenant |
| 41 | // who submits the request, unless the request is submitted by an user with |
| 42 | // administrative rights. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 43 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 44 | // Build query parameters |
| 45 | q := make(map[string]string) |
| 46 | if opts.Status != "" { |
| 47 | q["status"] = opts.Status |
| 48 | } |
| 49 | if opts.Name != "" { |
| 50 | q["name"] = opts.Name |
| 51 | } |
| 52 | if opts.AdminStateUp != nil { |
| 53 | q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp) |
| 54 | } |
| 55 | if opts.NetworkID != "" { |
| 56 | q["network_id"] = opts.NetworkID |
| 57 | } |
| 58 | if opts.TenantID != "" { |
| 59 | q["tenant_id"] = opts.TenantID |
| 60 | } |
| 61 | if opts.DeviceOwner != "" { |
| 62 | q["device_owner"] = opts.DeviceOwner |
| 63 | } |
| 64 | if opts.MACAddress != "" { |
| 65 | q["mac_address"] = opts.MACAddress |
| 66 | } |
| 67 | if opts.ID != "" { |
| 68 | q["id"] = opts.ID |
| 69 | } |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 70 | if opts.DeviceID != "" { |
| 71 | q["device_id"] = opts.DeviceID |
| 72 | } |
| 73 | if opts.BindingHostID != "" { |
| 74 | q["binding:host_id"] = opts.BindingHostID |
| 75 | } |
| 76 | if opts.BindingVIFType != "" { |
| 77 | q["binding:vif_type"] = opts.BindingVIFType |
| 78 | } |
| 79 | if opts.BindingVNICType != "" { |
| 80 | q["binding:vnic_type"] = opts.BindingVNICType |
| 81 | } |
| 82 | if opts.NetworkID != "" { |
| 83 | q["network_id"] = opts.NetworkID |
| 84 | } |
| 85 | if opts.Limit != 0 { |
| 86 | q["limit"] = strconv.Itoa(opts.Limit) |
| 87 | } |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 88 | if opts.Marker != "" { |
| 89 | q["marker"] = opts.Marker |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 90 | } |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 91 | if opts.SortKey != "" { |
| 92 | q["sort_key"] = opts.SortKey |
| 93 | } |
| 94 | if opts.SortDir != "" { |
| 95 | q["sort_dir"] = opts.SortDir |
| 96 | } |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 97 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 98 | u := listURL(c) + utils.BuildQuery(q) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 99 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
| 100 | return PortPage{pagination.LinkedPageBase(r)} |
| 101 | }) |
| 102 | } |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 103 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 104 | // Get retrieves a specific port based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 105 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 106 | var res GetResult |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 107 | _, err := perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 108 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 109 | Results: &res.Resp, |
| 110 | OkCodes: []int{200}, |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 111 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 112 | res.Err = err |
| 113 | return res |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 114 | } |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 115 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 116 | // CreateOpts represents the attributes used when creating a new port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 117 | type CreateOpts struct { |
| 118 | NetworkID string |
| 119 | Name string |
| 120 | AdminStateUp *bool |
| 121 | MACAddress string |
| 122 | FixedIPs interface{} |
| 123 | DeviceID string |
| 124 | DeviceOwner string |
| 125 | TenantID string |
| 126 | SecurityGroups []string |
| 127 | } |
| 128 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 129 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 130 | // provided. You must remember to provide a NetworkID value. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 131 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 132 | var res CreateResult |
| 133 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 134 | type port struct { |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 135 | NetworkID string `json:"network_id"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 136 | Name *string `json:"name,omitempty"` |
| 137 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 138 | MACAddress *string `json:"mac_address,omitempty"` |
| 139 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 140 | DeviceID *string `json:"device_id,omitempty"` |
| 141 | DeviceOwner *string `json:"device_owner,omitempty"` |
| 142 | TenantID *string `json:"tenant_id,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 143 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 144 | } |
| 145 | type request struct { |
| 146 | Port port `json:"port"` |
| 147 | } |
| 148 | |
| 149 | // Validate |
| 150 | if opts.NetworkID == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 151 | res.Err = errNetworkIDRequired |
| 152 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Populate request body |
| 156 | reqBody := request{Port: port{ |
| 157 | NetworkID: opts.NetworkID, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame^] | 158 | Name: gophercloud.MaybeString(opts.Name), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 159 | AdminStateUp: opts.AdminStateUp, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame^] | 160 | TenantID: gophercloud.MaybeString(opts.TenantID), |
| 161 | MACAddress: gophercloud.MaybeString(opts.MACAddress), |
| 162 | DeviceID: gophercloud.MaybeString(opts.DeviceID), |
| 163 | DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 164 | }} |
| 165 | |
| 166 | if opts.FixedIPs != nil { |
| 167 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 168 | } |
| 169 | |
| 170 | if opts.SecurityGroups != nil { |
| 171 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 172 | } |
| 173 | |
| 174 | // Response |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 175 | _, err := perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 176 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 177 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 178 | Results: &res.Resp, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 179 | OkCodes: []int{201}, |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 180 | DumpReqJson: true, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 181 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 182 | res.Err = err |
| 183 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 186 | // UpdateOpts represents the attributes used when updating an existing port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 187 | type UpdateOpts struct { |
| 188 | Name string |
| 189 | AdminStateUp *bool |
| 190 | FixedIPs interface{} |
| 191 | DeviceID string |
| 192 | DeviceOwner string |
| 193 | SecurityGroups []string |
| 194 | } |
| 195 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 196 | // Update accepts a UpdateOpts struct and updates an existing port using the |
| 197 | // values provided. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 198 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 199 | type port struct { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 200 | Name *string `json:"name,omitempty"` |
| 201 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 202 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 203 | DeviceID *string `json:"device_id,omitempty"` |
| 204 | DeviceOwner *string `json:"device_owner,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 205 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 206 | } |
| 207 | type request struct { |
| 208 | Port port `json:"port"` |
| 209 | } |
| 210 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 211 | // Populate request body |
| 212 | reqBody := request{Port: port{ |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame^] | 213 | Name: gophercloud.MaybeString(opts.Name), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 214 | AdminStateUp: opts.AdminStateUp, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame^] | 215 | DeviceID: gophercloud.MaybeString(opts.DeviceID), |
| 216 | DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 217 | }} |
| 218 | |
| 219 | if opts.FixedIPs != nil { |
| 220 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 221 | } |
| 222 | |
| 223 | if opts.SecurityGroups != nil { |
| 224 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 225 | } |
| 226 | |
| 227 | // Response |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 228 | var res UpdateResult |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 229 | _, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 230 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 231 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 232 | Results: &res.Resp, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 233 | OkCodes: []int{200, 201}, |
| 234 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 235 | res.Err = err |
| 236 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 237 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 238 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 239 | // Delete accepts a unique ID and deletes the port associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 240 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 241 | var res DeleteResult |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 242 | _, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{ |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 243 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 244 | OkCodes: []int{204}, |
| 245 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 246 | res.Err = err |
| 247 | return res |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 248 | } |