blob: 01d550fc1afb4b5089e8cf5464e8d57ac5ddf31d [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jamie Hannaford548d3402014-09-18 15:50:08 +02004 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02005 "github.com/rackspace/gophercloud/pagination"
6)
7
Jamie Hannafordc98f59b2014-10-09 10:32:50 +02008// AdminState gives users a solid type to work with for create and update
9// operations. It is recommended that users use the `Up` and `Down` enums.
10type AdminState *bool
11
12// Convenience vars for AdminStateUp values.
13var (
14 iTrue = true
15 iFalse = false
16
17 Up AdminState = &iTrue
18 Down AdminState = &iFalse
19)
20
Jon Perritt04851d32014-10-14 02:07:13 -050021// ListOptsBuilder allows extensions to add additional parameters to the
22// List request.
23type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050024 ToPortListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050025}
26
Jamie Hannaford686c4962014-09-23 10:46:20 +020027// ListOpts allows the filtering and sorting of paginated collections through
28// the API. Filtering is achieved by passing in struct field values that map to
29// the port attributes you want to see returned. SortKey allows you to sort
30// by a particular port attribute. SortDir sets the direction, and is either
31// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford548d3402014-09-18 15:50:08 +020032type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020033 Status string `q:"status"`
34 Name string `q:"name"`
35 AdminStateUp *bool `q:"admin_state_up"`
36 NetworkID string `q:"network_id"`
37 TenantID string `q:"tenant_id"`
38 DeviceOwner string `q:"device_owner"`
39 MACAddress string `q:"mac_address"`
40 ID string `q:"id"`
41 DeviceID string `q:"device_id"`
42 Limit int `q:"limit"`
43 Marker string `q:"marker"`
44 SortKey string `q:"sort_key"`
45 SortDir string `q:"sort_dir"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020046}
47
Jon Perritt26780d52014-10-14 11:35:58 -050048// ToPortListQuery formats a ListOpts into a query string.
49func (opts ListOpts) ToPortListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050050 q, err := gophercloud.BuildQueryString(opts)
51 if err != nil {
52 return "", err
53 }
54 return q.String(), nil
55}
56
Jamie Hannaford686c4962014-09-23 10:46:20 +020057// List returns a Pager which allows you to iterate over a collection of
58// ports. It accepts a ListOpts struct, which allows you to filter and sort
59// the returned collection for greater efficiency.
60//
61// Default policy settings return only those ports that are owned by the tenant
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070062// who submits the request, unless the request is submitted by a user with
Jamie Hannaford686c4962014-09-23 10:46:20 +020063// administrative rights.
Jon Perritt04851d32014-10-14 02:07:13 -050064func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
65 url := listURL(c)
66 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050067 query, err := opts.ToPortListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050068 if err != nil {
69 return pagination.Pager{Err: err}
70 }
71 url += query
Jamie Hannaford548d3402014-09-18 15:50:08 +020072 }
Jamie Hannaford548d3402014-09-18 15:50:08 +020073
Ash Wilsonb8b16f82014-10-20 10:19:49 -040074 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
75 return PortPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +020076 })
77}
Jamie Hannaforda311f182014-09-19 11:19:10 +020078
Jamie Hannaford686c4962014-09-23 10:46:20 +020079// Get retrieves a specific port based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020080func Get(c *gophercloud.ServiceClient, id string) GetResult {
81 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050082 _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{
83 JSONResponse: &res.Body,
84 OkCodes: []int{200},
Jamie Hannaforda311f182014-09-19 11:19:10 +020085 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020086 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +020087}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020088
Jon Perritt04851d32014-10-14 02:07:13 -050089// CreateOptsBuilder is the interface options structs have to satisfy in order
90// to be used in the main Create operation in this package. Since many
91// extensions decorate or modify the common logic, it is useful for them to
92// satisfy a basic interface in order for them to be used.
93type CreateOptsBuilder interface {
94 ToPortCreateMap() (map[string]interface{}, error)
95}
96
Jamie Hannaford686c4962014-09-23 10:46:20 +020097// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020098type CreateOpts struct {
99 NetworkID string
100 Name string
101 AdminStateUp *bool
102 MACAddress string
103 FixedIPs interface{}
104 DeviceID string
105 DeviceOwner string
106 TenantID string
107 SecurityGroups []string
108}
109
Jon Perritt04851d32014-10-14 02:07:13 -0500110// ToPortCreateMap casts a CreateOpts struct to a map.
111func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
112 p := make(map[string]interface{})
113
114 if opts.NetworkID == "" {
115 return nil, errNetworkIDRequired
116 }
117 p["network_id"] = opts.NetworkID
118
119 if opts.DeviceID != "" {
120 p["device_id"] = opts.DeviceID
121 }
122 if opts.DeviceOwner != "" {
123 p["device_owner"] = opts.DeviceOwner
124 }
125 if opts.FixedIPs != nil {
126 p["fixed_ips"] = opts.FixedIPs
127 }
128 if opts.SecurityGroups != nil {
129 p["security_groups"] = opts.SecurityGroups
130 }
131 if opts.TenantID != "" {
132 p["tenant_id"] = opts.TenantID
133 }
134 if opts.AdminStateUp != nil {
135 p["admin_state_up"] = &opts.AdminStateUp
136 }
137 if opts.Name != "" {
138 p["name"] = opts.Name
139 }
140 if opts.MACAddress != "" {
141 p["mac_address"] = opts.MACAddress
142 }
143
144 return map[string]interface{}{"port": p}, nil
145}
146
Jamie Hannaford686c4962014-09-23 10:46:20 +0200147// Create accepts a CreateOpts struct and creates a new network using the values
148// provided. You must remember to provide a NetworkID value.
Jon Perritt04851d32014-10-14 02:07:13 -0500149func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200150 var res CreateResult
151
Jon Perritt04851d32014-10-14 02:07:13 -0500152 reqBody, err := opts.ToPortCreateMap()
153 if err != nil {
154 res.Err = err
Jamie Hannafordd9036422014-09-23 17:50:24 +0200155 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200156 }
157
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200158 // Response
Ash Wilson4bf41a32015-02-12 15:52:44 -0500159 _, res.Err = c.Request("POST", createURL(c), gophercloud.RequestOpts{
160 JSONBody: &reqBody,
161 JSONResponse: &res.Body,
162 OkCodes: []int{201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200163 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200164
Jamie Hannafordd9036422014-09-23 17:50:24 +0200165 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200166}
167
Jon Perritt04851d32014-10-14 02:07:13 -0500168// UpdateOptsBuilder is the interface options structs have to satisfy in order
169// to be used in the main Update operation in this package. Since many
170// extensions decorate or modify the common logic, it is useful for them to
171// satisfy a basic interface in order for them to be used.
172type UpdateOptsBuilder interface {
173 ToPortUpdateMap() (map[string]interface{}, error)
174}
175
Jamie Hannaford686c4962014-09-23 10:46:20 +0200176// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200177type UpdateOpts struct {
178 Name string
179 AdminStateUp *bool
180 FixedIPs interface{}
181 DeviceID string
182 DeviceOwner string
183 SecurityGroups []string
184}
185
Jon Perritt04851d32014-10-14 02:07:13 -0500186// ToPortUpdateMap casts an UpdateOpts struct to a map.
187func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
188 p := make(map[string]interface{})
189
190 if opts.DeviceID != "" {
191 p["device_id"] = opts.DeviceID
192 }
193 if opts.DeviceOwner != "" {
194 p["device_owner"] = opts.DeviceOwner
195 }
196 if opts.FixedIPs != nil {
197 p["fixed_ips"] = opts.FixedIPs
198 }
199 if opts.SecurityGroups != nil {
200 p["security_groups"] = opts.SecurityGroups
201 }
202 if opts.AdminStateUp != nil {
203 p["admin_state_up"] = &opts.AdminStateUp
204 }
205 if opts.Name != "" {
206 p["name"] = opts.Name
207 }
208
209 return map[string]interface{}{"port": p}, nil
210}
211
Jamie Hannaford686c4962014-09-23 10:46:20 +0200212// Update accepts a UpdateOpts struct and updates an existing port using the
213// values provided.
Jon Perritt04851d32014-10-14 02:07:13 -0500214func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200215 var res UpdateResult
Jon Perritt04851d32014-10-14 02:07:13 -0500216
217 reqBody, err := opts.ToPortUpdateMap()
218 if err != nil {
219 res.Err = err
220 return res
221 }
222
Ash Wilson59fb6c42015-02-12 16:21:13 -0500223 _, res.Err = c.Request("PUT", updateURL(c, id), gophercloud.RequestOpts{
224 JSONBody: &reqBody,
225 JSONResponse: &res.Body,
226 OkCodes: []int{200, 201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200227 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200228 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200229}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200230
Jamie Hannaford686c4962014-09-23 10:46:20 +0200231// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200232func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
233 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500234 _, res.Err = c.Request("DELETE", deleteURL(c, id), gophercloud.RequestOpts{
235 OkCodes: []int{204},
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200236 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200237 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200238}