blob: 3b7e6055b41a932988c87600e7418c01d7c4463d [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 {
100 return PortPage{pagination.LinkedPageBase(r)}
101 })
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 Hannaford965ae702014-09-22 14:58:19 +0200107 _, 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 res.Err = err
113 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +0200114}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200115
Jamie Hannaford686c4962014-09-23 10:46:20 +0200116// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200117type 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 Hannaford686c4962014-09-23 10:46:20 +0200129// Create accepts a CreateOpts struct and creates a new network using the values
130// provided. You must remember to provide a NetworkID value.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200131func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
132 var res CreateResult
133
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200134 type port struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200135 NetworkID string `json:"network_id"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200136 Name *string `json:"name,omitempty"`
137 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200138 MACAddress *string `json:"mac_address,omitempty"`
139 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200140 DeviceID *string `json:"device_id,omitempty"`
141 DeviceOwner *string `json:"device_owner,omitempty"`
142 TenantID *string `json:"tenant_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200143 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 Hannafordd9036422014-09-23 17:50:24 +0200151 res.Err = errNetworkIDRequired
152 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200153 }
154
155 // Populate request body
156 reqBody := request{Port: port{
157 NetworkID: opts.NetworkID,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200158 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200159 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200160 TenantID: gophercloud.MaybeString(opts.TenantID),
161 MACAddress: gophercloud.MaybeString(opts.MACAddress),
162 DeviceID: gophercloud.MaybeString(opts.DeviceID),
163 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200164 }}
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 Hannaford965ae702014-09-22 14:58:19 +0200175 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200176 MoreHeaders: c.Provider.AuthenticatedHeaders(),
177 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200178 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200179 OkCodes: []int{201},
Jamie Hannaford2a0492a2014-09-22 12:02:11 +0200180 DumpReqJson: true,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200181 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200182 res.Err = err
183 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200184}
185
Jamie Hannaford686c4962014-09-23 10:46:20 +0200186// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200187type UpdateOpts struct {
188 Name string
189 AdminStateUp *bool
190 FixedIPs interface{}
191 DeviceID string
192 DeviceOwner string
193 SecurityGroups []string
194}
195
Jamie Hannaford686c4962014-09-23 10:46:20 +0200196// Update accepts a UpdateOpts struct and updates an existing port using the
197// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200198func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200199 type port struct {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200200 Name *string `json:"name,omitempty"`
201 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200202 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200203 DeviceID *string `json:"device_id,omitempty"`
204 DeviceOwner *string `json:"device_owner,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200205 SecurityGroups []string `json:"security_groups,omitempty"`
206 }
207 type request struct {
208 Port port `json:"port"`
209 }
210
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200211 // Populate request body
212 reqBody := request{Port: port{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200213 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200214 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200215 DeviceID: gophercloud.MaybeString(opts.DeviceID),
216 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200217 }}
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 Hannafordd9036422014-09-23 17:50:24 +0200228 var res UpdateResult
Jamie Hannaford965ae702014-09-22 14:58:19 +0200229 _, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200230 MoreHeaders: c.Provider.AuthenticatedHeaders(),
231 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200232 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200233 OkCodes: []int{200, 201},
234 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200235 res.Err = err
236 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200237}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200238
Jamie Hannaford686c4962014-09-23 10:46:20 +0200239// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200240func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
241 var res DeleteResult
Jamie Hannaford965ae702014-09-22 14:58:19 +0200242 _, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200243 MoreHeaders: c.Provider.AuthenticatedHeaders(),
244 OkCodes: []int{204},
245 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200246 res.Err = err
247 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200248}