blob: 2caf1cace44b30236694ce471e27782fb130d6ea [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jon Perritt7ab13282015-06-28 18:47:19 -06004 "fmt"
5
Jamie Hannaford548d3402014-09-18 15:50:08 +02006 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02007 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannafordc98f59b2014-10-09 10:32:50 +020010// AdminState gives users a solid type to work with for create and update
11// operations. It is recommended that users use the `Up` and `Down` enums.
12type AdminState *bool
13
14// Convenience vars for AdminStateUp values.
15var (
16 iTrue = true
17 iFalse = false
18
19 Up AdminState = &iTrue
20 Down AdminState = &iFalse
21)
22
Jon Perritt04851d32014-10-14 02:07:13 -050023// ListOptsBuilder allows extensions to add additional parameters to the
24// List request.
25type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050026 ToPortListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050027}
28
Jamie Hannaford686c4962014-09-23 10:46:20 +020029// ListOpts allows the filtering and sorting of paginated collections through
30// the API. Filtering is achieved by passing in struct field values that map to
31// the port attributes you want to see returned. SortKey allows you to sort
32// by a particular port attribute. SortDir sets the direction, and is either
33// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford548d3402014-09-18 15:50:08 +020034type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020035 Status string `q:"status"`
36 Name string `q:"name"`
37 AdminStateUp *bool `q:"admin_state_up"`
38 NetworkID string `q:"network_id"`
39 TenantID string `q:"tenant_id"`
40 DeviceOwner string `q:"device_owner"`
41 MACAddress string `q:"mac_address"`
42 ID string `q:"id"`
43 DeviceID string `q:"device_id"`
44 Limit int `q:"limit"`
45 Marker string `q:"marker"`
46 SortKey string `q:"sort_key"`
47 SortDir string `q:"sort_dir"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020048}
49
Jon Perritt26780d52014-10-14 11:35:58 -050050// ToPortListQuery formats a ListOpts into a query string.
51func (opts ListOpts) ToPortListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050052 q, err := gophercloud.BuildQueryString(opts)
53 if err != nil {
54 return "", err
55 }
56 return q.String(), nil
57}
58
Jamie Hannaford686c4962014-09-23 10:46:20 +020059// List returns a Pager which allows you to iterate over a collection of
60// ports. It accepts a ListOpts struct, which allows you to filter and sort
61// the returned collection for greater efficiency.
62//
63// Default policy settings return only those ports that are owned by the tenant
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070064// who submits the request, unless the request is submitted by a user with
Jamie Hannaford686c4962014-09-23 10:46:20 +020065// administrative rights.
Jon Perritt04851d32014-10-14 02:07:13 -050066func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
67 url := listURL(c)
68 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050069 query, err := opts.ToPortListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050070 if err != nil {
71 return pagination.Pager{Err: err}
72 }
73 url += query
Jamie Hannaford548d3402014-09-18 15:50:08 +020074 }
Jamie Hannaford548d3402014-09-18 15:50:08 +020075
Ash Wilsonb8b16f82014-10-20 10:19:49 -040076 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
77 return PortPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +020078 })
79}
Jamie Hannaforda311f182014-09-19 11:19:10 +020080
Jamie Hannaford686c4962014-09-23 10:46:20 +020081// Get retrieves a specific port based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020082func Get(c *gophercloud.ServiceClient, id string) GetResult {
83 var res GetResult
Jamie Hannaford059e1502015-03-24 16:20:32 +010084 _, res.Err = c.Get(getURL(c, id), &res.Body, nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +020085 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +020086}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020087
Jon Perritt04851d32014-10-14 02:07:13 -050088// CreateOptsBuilder is the interface options structs have to satisfy in order
89// to be used in the main Create operation in this package. Since many
90// extensions decorate or modify the common logic, it is useful for them to
91// satisfy a basic interface in order for them to be used.
92type CreateOptsBuilder interface {
93 ToPortCreateMap() (map[string]interface{}, error)
94}
95
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020097type CreateOpts struct {
98 NetworkID string
99 Name string
100 AdminStateUp *bool
101 MACAddress string
102 FixedIPs interface{}
103 DeviceID string
104 DeviceOwner string
105 TenantID string
106 SecurityGroups []string
107}
108
Jon Perritt04851d32014-10-14 02:07:13 -0500109// ToPortCreateMap casts a CreateOpts struct to a map.
110func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
111 p := make(map[string]interface{})
112
113 if opts.NetworkID == "" {
114 return nil, errNetworkIDRequired
115 }
116 p["network_id"] = opts.NetworkID
117
118 if opts.DeviceID != "" {
119 p["device_id"] = opts.DeviceID
120 }
121 if opts.DeviceOwner != "" {
122 p["device_owner"] = opts.DeviceOwner
123 }
124 if opts.FixedIPs != nil {
125 p["fixed_ips"] = opts.FixedIPs
126 }
127 if opts.SecurityGroups != nil {
128 p["security_groups"] = opts.SecurityGroups
129 }
130 if opts.TenantID != "" {
131 p["tenant_id"] = opts.TenantID
132 }
133 if opts.AdminStateUp != nil {
134 p["admin_state_up"] = &opts.AdminStateUp
135 }
136 if opts.Name != "" {
137 p["name"] = opts.Name
138 }
139 if opts.MACAddress != "" {
140 p["mac_address"] = opts.MACAddress
141 }
142
143 return map[string]interface{}{"port": p}, nil
144}
145
Jamie Hannaford686c4962014-09-23 10:46:20 +0200146// Create accepts a CreateOpts struct and creates a new network using the values
147// provided. You must remember to provide a NetworkID value.
Jon Perritt04851d32014-10-14 02:07:13 -0500148func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200149 var res CreateResult
150
Jon Perritt04851d32014-10-14 02:07:13 -0500151 reqBody, err := opts.ToPortCreateMap()
152 if err != nil {
153 res.Err = err
Jamie Hannafordd9036422014-09-23 17:50:24 +0200154 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200155 }
156
Jamie Hannaford059e1502015-03-24 16:20:32 +0100157 _, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +0200158 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200159}
160
Jon Perritt04851d32014-10-14 02:07:13 -0500161// UpdateOptsBuilder is the interface options structs have to satisfy in order
162// to be used in the main Update operation in this package. Since many
163// extensions decorate or modify the common logic, it is useful for them to
164// satisfy a basic interface in order for them to be used.
165type UpdateOptsBuilder interface {
166 ToPortUpdateMap() (map[string]interface{}, error)
167}
168
Jamie Hannaford686c4962014-09-23 10:46:20 +0200169// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200170type UpdateOpts struct {
171 Name string
172 AdminStateUp *bool
173 FixedIPs interface{}
174 DeviceID string
175 DeviceOwner string
176 SecurityGroups []string
177}
178
Jon Perritt04851d32014-10-14 02:07:13 -0500179// ToPortUpdateMap casts an UpdateOpts struct to a map.
180func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
181 p := make(map[string]interface{})
182
183 if opts.DeviceID != "" {
184 p["device_id"] = opts.DeviceID
185 }
186 if opts.DeviceOwner != "" {
187 p["device_owner"] = opts.DeviceOwner
188 }
189 if opts.FixedIPs != nil {
190 p["fixed_ips"] = opts.FixedIPs
191 }
192 if opts.SecurityGroups != nil {
193 p["security_groups"] = opts.SecurityGroups
194 }
195 if opts.AdminStateUp != nil {
196 p["admin_state_up"] = &opts.AdminStateUp
197 }
198 if opts.Name != "" {
199 p["name"] = opts.Name
200 }
201
202 return map[string]interface{}{"port": p}, nil
203}
204
Jamie Hannaford686c4962014-09-23 10:46:20 +0200205// Update accepts a UpdateOpts struct and updates an existing port using the
206// values provided.
Jon Perritt04851d32014-10-14 02:07:13 -0500207func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200208 var res UpdateResult
Jon Perritt04851d32014-10-14 02:07:13 -0500209
210 reqBody, err := opts.ToPortUpdateMap()
211 if err != nil {
212 res.Err = err
213 return res
214 }
215
Jamie Hannaford059e1502015-03-24 16:20:32 +0100216 _, res.Err = c.Put(updateURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
217 OkCodes: []int{200, 201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200218 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200219 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200220}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200221
Jamie Hannaford686c4962014-09-23 10:46:20 +0200222// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200223func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
224 var res DeleteResult
Jamie Hannaford059e1502015-03-24 16:20:32 +0100225 _, res.Err = c.Delete(deleteURL(c, id), nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +0200226 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200227}
Jon Perritt7ab13282015-06-28 18:47:19 -0600228
jrperritt14f716b2015-06-28 19:07:52 -0600229// IDFromName is a convenience function that returns a port's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600230func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
231 portCount := 0
232 portID := ""
233 if name == "" {
jrperritt00399b42015-06-28 19:00:45 -0600234 return "", fmt.Errorf("A port name must be provided.")
Jon Perritt7ab13282015-06-28 18:47:19 -0600235 }
236 pager := List(client, nil)
237 pager.EachPage(func(page pagination.Page) (bool, error) {
238 portList, err := ExtractPorts(page)
239 if err != nil {
240 return false, err
241 }
242
243 for _, p := range portList {
244 if p.Name == name {
245 portCount++
246 portID = p.ID
247 }
248 }
249 return true, nil
250 })
251
252 switch portCount {
253 case 0:
jrperritt00399b42015-06-28 19:00:45 -0600254 return "", fmt.Errorf("Unable to find port: %s", name)
Jon Perritt7ab13282015-06-28 18:47:19 -0600255 case 1:
256 return portID, nil
257 default:
jrperritt00399b42015-06-28 19:00:45 -0600258 return "", fmt.Errorf("Found %d ports matching %s", portCount, name)
Jon Perritt7ab13282015-06-28 18:47:19 -0600259 }
260}