blob: cc0949fd08915d09d95103317d95564aa9edb2d5 [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
12type ListOpts struct {
13 Status string
14 Name string
15 AdminStateUp *bool
16 NetworkID string
17 TenantID string
18 DeviceOwner string
19 MACAddress string
20 ID string
21 SecurityGroups string
22 DeviceID string
23 BindingHostID string
24 BindingVIFType string
25 BindingVNICType string
26 Limit int
27 Page string
28 PerPage string
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020029 SortKey string
30 SortDir string
Jamie Hannaford548d3402014-09-18 15:50:08 +020031}
32
33func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
34 // Build query parameters
35 q := make(map[string]string)
36 if opts.Status != "" {
37 q["status"] = opts.Status
38 }
39 if opts.Name != "" {
40 q["name"] = opts.Name
41 }
42 if opts.AdminStateUp != nil {
43 q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp)
44 }
45 if opts.NetworkID != "" {
46 q["network_id"] = opts.NetworkID
47 }
48 if opts.TenantID != "" {
49 q["tenant_id"] = opts.TenantID
50 }
51 if opts.DeviceOwner != "" {
52 q["device_owner"] = opts.DeviceOwner
53 }
54 if opts.MACAddress != "" {
55 q["mac_address"] = opts.MACAddress
56 }
57 if opts.ID != "" {
58 q["id"] = opts.ID
59 }
60 if opts.SecurityGroups != "" {
61 q["security_groups"] = opts.SecurityGroups
62 }
63 if opts.DeviceID != "" {
64 q["device_id"] = opts.DeviceID
65 }
66 if opts.BindingHostID != "" {
67 q["binding:host_id"] = opts.BindingHostID
68 }
69 if opts.BindingVIFType != "" {
70 q["binding:vif_type"] = opts.BindingVIFType
71 }
72 if opts.BindingVNICType != "" {
73 q["binding:vnic_type"] = opts.BindingVNICType
74 }
75 if opts.NetworkID != "" {
76 q["network_id"] = opts.NetworkID
77 }
78 if opts.Limit != 0 {
79 q["limit"] = strconv.Itoa(opts.Limit)
80 }
81 if opts.Page != "" {
82 q["page"] = opts.Page
83 }
84 if opts.PerPage != "" {
85 q["per_page"] = opts.PerPage
86 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020087 if opts.SortKey != "" {
88 q["sort_key"] = opts.SortKey
89 }
90 if opts.SortDir != "" {
91 q["sort_dir"] = opts.SortDir
92 }
Jamie Hannaford548d3402014-09-18 15:50:08 +020093
Jamie Hannaford965ae702014-09-22 14:58:19 +020094 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannaford548d3402014-09-18 15:50:08 +020095 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
96 return PortPage{pagination.LinkedPageBase(r)}
97 })
98}
Jamie Hannaforda311f182014-09-19 11:19:10 +020099
100func Get(c *gophercloud.ServiceClient, id string) (*Port, error) {
101 var p Port
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102 _, err := perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaforda311f182014-09-19 11:19:10 +0200103 MoreHeaders: c.Provider.AuthenticatedHeaders(),
104 Results: &struct {
105 Port *Port `json:"port"`
106 }{&p},
107 OkCodes: []int{200},
108 })
109 if err != nil {
110 return nil, err
111 }
112 return &p, nil
113}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200114
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200115func maybeString(original string) *string {
116 if original != "" {
117 return &original
118 }
119 return nil
120}
121
Jamie Hannaford965ae702014-09-22 14:58:19 +0200122type CreateOpts struct {
123 NetworkID string
124 Name string
125 AdminStateUp *bool
126 MACAddress string
127 FixedIPs interface{}
128 DeviceID string
129 DeviceOwner string
130 TenantID string
131 SecurityGroups []string
132}
133
134func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Port, error) {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200135 type port struct {
136 NetworkID string `json:"network_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200137 Name *string `json:"name,omitempty"`
138 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200139 MACAddress *string `json:"mac_address,omitempty"`
140 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200141 DeviceID *string `json:"device_id,omitempty"`
142 DeviceOwner *string `json:"device_owner,omitempty"`
143 TenantID *string `json:"tenant_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200144 SecurityGroups []string `json:"security_groups,omitempty"`
145 }
146 type request struct {
147 Port port `json:"port"`
148 }
149
150 // Validate
151 if opts.NetworkID == "" {
152 return nil, ErrNetworkIDRequired
153 }
154
155 // Populate request body
156 reqBody := request{Port: port{
157 NetworkID: opts.NetworkID,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200158 Name: maybeString(opts.Name),
159 AdminStateUp: opts.AdminStateUp,
160 TenantID: maybeString(opts.TenantID),
161 MACAddress: maybeString(opts.MACAddress),
Jamie Hannaford965ae702014-09-22 14:58:19 +0200162 DeviceID: maybeString(opts.DeviceID),
163 DeviceOwner: 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
175 type response struct {
176 Port *Port `json:"port"`
177 }
178 var res response
Jamie Hannaford965ae702014-09-22 14:58:19 +0200179 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200180 MoreHeaders: c.Provider.AuthenticatedHeaders(),
181 ReqBody: &reqBody,
182 Results: &res,
183 OkCodes: []int{201},
Jamie Hannaford2a0492a2014-09-22 12:02:11 +0200184 DumpReqJson: true,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200185 })
186 if err != nil {
187 return nil, err
188 }
189
190 return res.Port, nil
191}
192
Jamie Hannaford965ae702014-09-22 14:58:19 +0200193type UpdateOpts struct {
194 Name string
195 AdminStateUp *bool
196 FixedIPs interface{}
197 DeviceID string
198 DeviceOwner string
199 SecurityGroups []string
200}
201
202func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Port, error) {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200203 type port struct {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200204 Name *string `json:"name,omitempty"`
205 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200206 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200207 DeviceID *string `json:"device_id,omitempty"`
208 DeviceOwner *string `json:"device_owner,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200209 SecurityGroups []string `json:"security_groups,omitempty"`
210 }
211 type request struct {
212 Port port `json:"port"`
213 }
214
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200215 // Populate request body
216 reqBody := request{Port: port{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200217 Name: maybeString(opts.Name),
218 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford965ae702014-09-22 14:58:19 +0200219 DeviceID: maybeString(opts.DeviceID),
220 DeviceOwner: maybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200221 }}
222
223 if opts.FixedIPs != nil {
224 reqBody.Port.FixedIPs = opts.FixedIPs
225 }
226
227 if opts.SecurityGroups != nil {
228 reqBody.Port.SecurityGroups = opts.SecurityGroups
229 }
230
231 // Response
232 type response struct {
233 Port *Port `json:"port"`
234 }
235 var res response
Jamie Hannaford965ae702014-09-22 14:58:19 +0200236 _, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200237 MoreHeaders: c.Provider.AuthenticatedHeaders(),
238 ReqBody: &reqBody,
239 Results: &res,
240 OkCodes: []int{200, 201},
241 })
242 if err != nil {
243 return nil, err
244 }
245
246 return res.Port, nil
247}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200248
249func Delete(c *gophercloud.ServiceClient, id string) error {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200250 _, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200251 MoreHeaders: c.Provider.AuthenticatedHeaders(),
252 OkCodes: []int{204},
253 })
254 return err
255}