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 { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 100 | return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 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 | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 107 | _, res.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 | return res |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 113 | } |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 114 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 115 | // CreateOpts represents the attributes used when creating a new port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 116 | type CreateOpts struct { |
| 117 | NetworkID string |
| 118 | Name string |
| 119 | AdminStateUp *bool |
| 120 | MACAddress string |
| 121 | FixedIPs interface{} |
| 122 | DeviceID string |
| 123 | DeviceOwner string |
| 124 | TenantID string |
| 125 | SecurityGroups []string |
| 126 | } |
| 127 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 128 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 129 | // provided. You must remember to provide a NetworkID value. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 130 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 131 | var res CreateResult |
| 132 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 133 | type port struct { |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 134 | NetworkID string `json:"network_id"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 135 | Name *string `json:"name,omitempty"` |
| 136 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 137 | MACAddress *string `json:"mac_address,omitempty"` |
| 138 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 139 | DeviceID *string `json:"device_id,omitempty"` |
| 140 | DeviceOwner *string `json:"device_owner,omitempty"` |
| 141 | TenantID *string `json:"tenant_id,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 142 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 143 | } |
| 144 | type request struct { |
| 145 | Port port `json:"port"` |
| 146 | } |
| 147 | |
| 148 | // Validate |
| 149 | if opts.NetworkID == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 150 | res.Err = errNetworkIDRequired |
| 151 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Populate request body |
| 155 | reqBody := request{Port: port{ |
| 156 | NetworkID: opts.NetworkID, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 157 | Name: gophercloud.MaybeString(opts.Name), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 158 | AdminStateUp: opts.AdminStateUp, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 159 | TenantID: gophercloud.MaybeString(opts.TenantID), |
| 160 | MACAddress: gophercloud.MaybeString(opts.MACAddress), |
| 161 | DeviceID: gophercloud.MaybeString(opts.DeviceID), |
| 162 | DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 163 | }} |
| 164 | |
| 165 | if opts.FixedIPs != nil { |
| 166 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 167 | } |
| 168 | |
| 169 | if opts.SecurityGroups != nil { |
| 170 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 171 | } |
| 172 | |
| 173 | // Response |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 174 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 175 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 176 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 177 | Results: &res.Resp, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 178 | OkCodes: []int{201}, |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 179 | DumpReqJson: true, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 180 | }) |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 181 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 182 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 185 | // UpdateOpts represents the attributes used when updating an existing port. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 186 | type UpdateOpts struct { |
| 187 | Name string |
| 188 | AdminStateUp *bool |
| 189 | FixedIPs interface{} |
| 190 | DeviceID string |
| 191 | DeviceOwner string |
| 192 | SecurityGroups []string |
| 193 | } |
| 194 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 195 | // Update accepts a UpdateOpts struct and updates an existing port using the |
| 196 | // values provided. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 197 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 198 | type port struct { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 199 | Name *string `json:"name,omitempty"` |
| 200 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 201 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 202 | DeviceID *string `json:"device_id,omitempty"` |
| 203 | DeviceOwner *string `json:"device_owner,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 204 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 205 | } |
| 206 | type request struct { |
| 207 | Port port `json:"port"` |
| 208 | } |
| 209 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 210 | // Populate request body |
| 211 | reqBody := request{Port: port{ |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 212 | Name: gophercloud.MaybeString(opts.Name), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 213 | AdminStateUp: opts.AdminStateUp, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 214 | DeviceID: gophercloud.MaybeString(opts.DeviceID), |
| 215 | DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 216 | }} |
| 217 | |
| 218 | if opts.FixedIPs != nil { |
| 219 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 220 | } |
| 221 | |
| 222 | if opts.SecurityGroups != nil { |
| 223 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 224 | } |
| 225 | |
| 226 | // Response |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 227 | var res UpdateResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 228 | _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 229 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 230 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 231 | Results: &res.Resp, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 232 | OkCodes: []int{200, 201}, |
| 233 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 234 | return res |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 235 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 236 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 237 | // Delete accepts a unique ID and deletes the port associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 238 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 239 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 240 | _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{ |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 241 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 242 | OkCodes: []int{204}, |
| 243 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 244 | return res |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 245 | } |