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