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