blob: da738d216ed74d3112ba6e81f637e918bd33dd17 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
4 "strconv"
5
Jamie Hannaforda311f182014-09-19 11:19:10 +02006 "github.com/racker/perigee"
Jamie Hannaford548d3402014-09-18 15:50:08 +02007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
Jamie Hannaford686c4962014-09-23 10:46:20 +020012// 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 Hannaford548d3402014-09-18 15:50:08 +020017type 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 Hannaford548d3402014-09-18 15:50:08 +020026 DeviceID string
27 BindingHostID string
28 BindingVIFType string
29 BindingVNICType string
30 Limit int
Jamie Hannaford686c4962014-09-23 10:46:20 +020031 Marker string
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020032 SortKey string
33 SortDir string
Jamie Hannaford548d3402014-09-18 15:50:08 +020034}
35
Jamie Hannaford686c4962014-09-23 10:46:20 +020036// 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 Hannaford548d3402014-09-18 15:50:08 +020043func 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 Hannaford548d3402014-09-18 15:50:08 +020070 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 Hannaford686c4962014-09-23 10:46:20 +020088 if opts.Marker != "" {
89 q["marker"] = opts.Marker
Jamie Hannaford548d3402014-09-18 15:50:08 +020090 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020091 if opts.SortKey != "" {
92 q["sort_key"] = opts.SortKey
93 }
94 if opts.SortDir != "" {
95 q["sort_dir"] = opts.SortDir
96 }
Jamie Hannaford548d3402014-09-18 15:50:08 +020097
Jamie Hannaford965ae702014-09-22 14:58:19 +020098 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannaford548d3402014-09-18 15:50:08 +020099 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -0400100 return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +0200101 })
102}
Jamie Hannaforda311f182014-09-19 11:19:10 +0200103
Jamie Hannaford686c4962014-09-23 10:46:20 +0200104// Get retrieves a specific port based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200105func Get(c *gophercloud.ServiceClient, id string) GetResult {
106 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200107 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaforda311f182014-09-19 11:19:10 +0200108 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +0200109 Results: &res.Resp,
110 OkCodes: []int{200},
Jamie Hannaforda311f182014-09-19 11:19:10 +0200111 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200112 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +0200113}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200114
Jamie Hannaford686c4962014-09-23 10:46:20 +0200115// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200116type 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 Hannaford686c4962014-09-23 10:46:20 +0200128// Create accepts a CreateOpts struct and creates a new network using the values
129// provided. You must remember to provide a NetworkID value.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200130func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
131 var res CreateResult
132
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200133 type port struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200134 NetworkID string `json:"network_id"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200135 Name *string `json:"name,omitempty"`
136 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200137 MACAddress *string `json:"mac_address,omitempty"`
138 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200139 DeviceID *string `json:"device_id,omitempty"`
140 DeviceOwner *string `json:"device_owner,omitempty"`
141 TenantID *string `json:"tenant_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200142 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 Hannafordd9036422014-09-23 17:50:24 +0200150 res.Err = errNetworkIDRequired
151 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200152 }
153
154 // Populate request body
155 reqBody := request{Port: port{
156 NetworkID: opts.NetworkID,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200157 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200158 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200159 TenantID: gophercloud.MaybeString(opts.TenantID),
160 MACAddress: gophercloud.MaybeString(opts.MACAddress),
161 DeviceID: gophercloud.MaybeString(opts.DeviceID),
162 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200163 }}
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 Hannaford6f57e9e2014-10-02 10:27:28 +0200174 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200175 MoreHeaders: c.Provider.AuthenticatedHeaders(),
176 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200177 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200178 OkCodes: []int{201},
Jamie Hannaford2a0492a2014-09-22 12:02:11 +0200179 DumpReqJson: true,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200180 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200181
Jamie Hannafordd9036422014-09-23 17:50:24 +0200182 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200183}
184
Jamie Hannaford686c4962014-09-23 10:46:20 +0200185// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200186type UpdateOpts struct {
187 Name string
188 AdminStateUp *bool
189 FixedIPs interface{}
190 DeviceID string
191 DeviceOwner string
192 SecurityGroups []string
193}
194
Jamie Hannaford686c4962014-09-23 10:46:20 +0200195// Update accepts a UpdateOpts struct and updates an existing port using the
196// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200197func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200198 type port struct {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200199 Name *string `json:"name,omitempty"`
200 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200201 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200202 DeviceID *string `json:"device_id,omitempty"`
203 DeviceOwner *string `json:"device_owner,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200204 SecurityGroups []string `json:"security_groups,omitempty"`
205 }
206 type request struct {
207 Port port `json:"port"`
208 }
209
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200210 // Populate request body
211 reqBody := request{Port: port{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200212 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200213 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200214 DeviceID: gophercloud.MaybeString(opts.DeviceID),
215 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200216 }}
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 Hannafordd9036422014-09-23 17:50:24 +0200227 var res UpdateResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200228 _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200229 MoreHeaders: c.Provider.AuthenticatedHeaders(),
230 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200231 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200232 OkCodes: []int{200, 201},
233 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200234 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200235}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200236
Jamie Hannaford686c4962014-09-23 10:46:20 +0200237// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200238func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
239 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200240 _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200241 MoreHeaders: c.Provider.AuthenticatedHeaders(),
242 OkCodes: []int{204},
243 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200244 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200245}