blob: 781a3c3e745aedcc0aacdb7ae18f5f0247302b9a [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
Jamie Hannaford059e1502015-03-24 16:20:32 +010082 _, res.Err = c.Get(getURL(c, id), &res.Body, nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +020083 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +020084}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020085
Jon Perritt04851d32014-10-14 02:07:13 -050086// CreateOptsBuilder is the interface options structs have to satisfy in order
87// to be used in the main Create operation in this package. Since many
88// extensions decorate or modify the common logic, it is useful for them to
89// satisfy a basic interface in order for them to be used.
90type CreateOptsBuilder interface {
91 ToPortCreateMap() (map[string]interface{}, error)
92}
93
Jamie Hannaford686c4962014-09-23 10:46:20 +020094// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020095type CreateOpts struct {
96 NetworkID string
97 Name string
98 AdminStateUp *bool
99 MACAddress string
100 FixedIPs interface{}
101 DeviceID string
102 DeviceOwner string
103 TenantID string
104 SecurityGroups []string
105}
106
Jon Perritt04851d32014-10-14 02:07:13 -0500107// ToPortCreateMap casts a CreateOpts struct to a map.
108func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
109 p := make(map[string]interface{})
110
111 if opts.NetworkID == "" {
112 return nil, errNetworkIDRequired
113 }
114 p["network_id"] = opts.NetworkID
115
116 if opts.DeviceID != "" {
117 p["device_id"] = opts.DeviceID
118 }
119 if opts.DeviceOwner != "" {
120 p["device_owner"] = opts.DeviceOwner
121 }
122 if opts.FixedIPs != nil {
123 p["fixed_ips"] = opts.FixedIPs
124 }
125 if opts.SecurityGroups != nil {
126 p["security_groups"] = opts.SecurityGroups
127 }
128 if opts.TenantID != "" {
129 p["tenant_id"] = opts.TenantID
130 }
131 if opts.AdminStateUp != nil {
132 p["admin_state_up"] = &opts.AdminStateUp
133 }
134 if opts.Name != "" {
135 p["name"] = opts.Name
136 }
137 if opts.MACAddress != "" {
138 p["mac_address"] = opts.MACAddress
139 }
140
141 return map[string]interface{}{"port": p}, nil
142}
143
Jamie Hannaford686c4962014-09-23 10:46:20 +0200144// Create accepts a CreateOpts struct and creates a new network using the values
145// provided. You must remember to provide a NetworkID value.
Jon Perritt04851d32014-10-14 02:07:13 -0500146func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200147 var res CreateResult
148
Jon Perritt04851d32014-10-14 02:07:13 -0500149 reqBody, err := opts.ToPortCreateMap()
150 if err != nil {
151 res.Err = err
Jamie Hannafordd9036422014-09-23 17:50:24 +0200152 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200153 }
154
Jamie Hannaford059e1502015-03-24 16:20:32 +0100155 _, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +0200156 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200157}
158
Jon Perritt04851d32014-10-14 02:07:13 -0500159// UpdateOptsBuilder is the interface options structs have to satisfy in order
160// to be used in the main Update operation in this package. Since many
161// extensions decorate or modify the common logic, it is useful for them to
162// satisfy a basic interface in order for them to be used.
163type UpdateOptsBuilder interface {
164 ToPortUpdateMap() (map[string]interface{}, error)
165}
166
Jamie Hannaford686c4962014-09-23 10:46:20 +0200167// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200168type UpdateOpts struct {
169 Name string
170 AdminStateUp *bool
171 FixedIPs interface{}
172 DeviceID string
173 DeviceOwner string
174 SecurityGroups []string
175}
176
Jon Perritt04851d32014-10-14 02:07:13 -0500177// ToPortUpdateMap casts an UpdateOpts struct to a map.
178func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
179 p := make(map[string]interface{})
180
181 if opts.DeviceID != "" {
182 p["device_id"] = opts.DeviceID
183 }
184 if opts.DeviceOwner != "" {
185 p["device_owner"] = opts.DeviceOwner
186 }
187 if opts.FixedIPs != nil {
188 p["fixed_ips"] = opts.FixedIPs
189 }
190 if opts.SecurityGroups != nil {
191 p["security_groups"] = opts.SecurityGroups
192 }
193 if opts.AdminStateUp != nil {
194 p["admin_state_up"] = &opts.AdminStateUp
195 }
196 if opts.Name != "" {
197 p["name"] = opts.Name
198 }
199
200 return map[string]interface{}{"port": p}, nil
201}
202
Jamie Hannaford686c4962014-09-23 10:46:20 +0200203// Update accepts a UpdateOpts struct and updates an existing port using the
204// values provided.
Jon Perritt04851d32014-10-14 02:07:13 -0500205func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200206 var res UpdateResult
Jon Perritt04851d32014-10-14 02:07:13 -0500207
208 reqBody, err := opts.ToPortUpdateMap()
209 if err != nil {
210 res.Err = err
211 return res
212 }
213
Jamie Hannaford059e1502015-03-24 16:20:32 +0100214 _, res.Err = c.Put(updateURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
215 OkCodes: []int{200, 201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200216 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200217 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200218}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200219
Jamie Hannaford686c4962014-09-23 10:46:20 +0200220// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200221func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
222 var res DeleteResult
Jamie Hannaford059e1502015-03-24 16:20:32 +0100223 _, res.Err = c.Delete(deleteURL(c, id), nil)
Jamie Hannafordd9036422014-09-23 17:50:24 +0200224 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200225}