blob: 0fbb9f2bce600ee62a60b06e22c9b6e09dce0f92 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jamie Hannaforda311f182014-09-19 11:19:10 +02004 "github.com/racker/perigee"
Jamie Hannaford548d3402014-09-18 15:50:08 +02005 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02006 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannafordc98f59b2014-10-09 10:32:50 +02009// AdminState gives users a solid type to work with for create and update
10// operations. It is recommended that users use the `Up` and `Down` enums.
11type AdminState *bool
12
13// Convenience vars for AdminStateUp values.
14var (
15 iTrue = true
16 iFalse = false
17
18 Up AdminState = &iTrue
19 Down AdminState = &iFalse
20)
21
Jamie Hannaford686c4962014-09-23 10:46:20 +020022// ListOpts allows the filtering and sorting of paginated collections through
23// the API. Filtering is achieved by passing in struct field values that map to
24// the port attributes you want to see returned. SortKey allows you to sort
25// by a particular port attribute. SortDir sets the direction, and is either
26// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford548d3402014-09-18 15:50:08 +020027type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020028 Status string `q:"status"`
29 Name string `q:"name"`
30 AdminStateUp *bool `q:"admin_state_up"`
31 NetworkID string `q:"network_id"`
32 TenantID string `q:"tenant_id"`
33 DeviceOwner string `q:"device_owner"`
34 MACAddress string `q:"mac_address"`
35 ID string `q:"id"`
36 DeviceID string `q:"device_id"`
37 Limit int `q:"limit"`
38 Marker string `q:"marker"`
39 SortKey string `q:"sort_key"`
40 SortDir string `q:"sort_dir"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020041}
42
Jamie Hannaford686c4962014-09-23 10:46:20 +020043// List returns a Pager which allows you to iterate over a collection of
44// ports. It accepts a ListOpts struct, which allows you to filter and sort
45// the returned collection for greater efficiency.
46//
47// Default policy settings return only those ports that are owned by the tenant
48// who submits the request, unless the request is submitted by an user with
49// administrative rights.
Jamie Hannaford548d3402014-09-18 15:50:08 +020050func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
51 // Build query parameters
Jamie Hannaford92523e32014-10-02 11:08:36 +020052 q, err := gophercloud.BuildQueryString(&opts)
53 if err != nil {
54 return pagination.Pager{Err: err}
Jamie Hannaford548d3402014-09-18 15:50:08 +020055 }
Jamie Hannaford92523e32014-10-02 11:08:36 +020056 u := listURL(c) + q.String()
Jamie Hannaford548d3402014-09-18 15:50:08 +020057
Jamie Hannaford548d3402014-09-18 15:50:08 +020058 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040059 return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +020060 })
61}
Jamie Hannaforda311f182014-09-19 11:19:10 +020062
Jamie Hannaford686c4962014-09-23 10:46:20 +020063// Get retrieves a specific port based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020064func Get(c *gophercloud.ServiceClient, id string) GetResult {
65 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020066 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaforda311f182014-09-19 11:19:10 +020067 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020068 Results: &res.Resp,
69 OkCodes: []int{200},
Jamie Hannaforda311f182014-09-19 11:19:10 +020070 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020071 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +020072}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020073
Jamie Hannaford686c4962014-09-23 10:46:20 +020074// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020075type CreateOpts struct {
76 NetworkID string
77 Name string
78 AdminStateUp *bool
79 MACAddress string
80 FixedIPs interface{}
81 DeviceID string
82 DeviceOwner string
83 TenantID string
84 SecurityGroups []string
85}
86
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// Create accepts a CreateOpts struct and creates a new network using the values
88// provided. You must remember to provide a NetworkID value.
Jamie Hannafordd9036422014-09-23 17:50:24 +020089func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
90 var res CreateResult
91
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020092 type port struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +020093 NetworkID string `json:"network_id"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020094 Name *string `json:"name,omitempty"`
95 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020096 MACAddress *string `json:"mac_address,omitempty"`
97 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020098 DeviceID *string `json:"device_id,omitempty"`
99 DeviceOwner *string `json:"device_owner,omitempty"`
100 TenantID *string `json:"tenant_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200101 SecurityGroups []string `json:"security_groups,omitempty"`
102 }
103 type request struct {
104 Port port `json:"port"`
105 }
106
107 // Validate
108 if opts.NetworkID == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +0200109 res.Err = errNetworkIDRequired
110 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200111 }
112
113 // Populate request body
114 reqBody := request{Port: port{
115 NetworkID: opts.NetworkID,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200116 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200117 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200118 TenantID: gophercloud.MaybeString(opts.TenantID),
119 MACAddress: gophercloud.MaybeString(opts.MACAddress),
120 DeviceID: gophercloud.MaybeString(opts.DeviceID),
121 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200122 }}
123
124 if opts.FixedIPs != nil {
125 reqBody.Port.FixedIPs = opts.FixedIPs
126 }
127
128 if opts.SecurityGroups != nil {
129 reqBody.Port.SecurityGroups = opts.SecurityGroups
130 }
131
132 // Response
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200133 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200134 MoreHeaders: c.Provider.AuthenticatedHeaders(),
135 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200136 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200137 OkCodes: []int{201},
Jamie Hannaford2a0492a2014-09-22 12:02:11 +0200138 DumpReqJson: true,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200139 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200140
Jamie Hannafordd9036422014-09-23 17:50:24 +0200141 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200142}
143
Jamie Hannaford686c4962014-09-23 10:46:20 +0200144// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200145type UpdateOpts struct {
146 Name string
147 AdminStateUp *bool
148 FixedIPs interface{}
149 DeviceID string
150 DeviceOwner string
151 SecurityGroups []string
152}
153
Jamie Hannaford686c4962014-09-23 10:46:20 +0200154// Update accepts a UpdateOpts struct and updates an existing port using the
155// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200156func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200157 type port struct {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200158 Name *string `json:"name,omitempty"`
159 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200160 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200161 DeviceID *string `json:"device_id,omitempty"`
162 DeviceOwner *string `json:"device_owner,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200163 SecurityGroups []string `json:"security_groups,omitempty"`
164 }
165 type request struct {
166 Port port `json:"port"`
167 }
168
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200169 // Populate request body
170 reqBody := request{Port: port{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200171 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200172 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200173 DeviceID: gophercloud.MaybeString(opts.DeviceID),
174 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200175 }}
176
177 if opts.FixedIPs != nil {
178 reqBody.Port.FixedIPs = opts.FixedIPs
179 }
180
181 if opts.SecurityGroups != nil {
182 reqBody.Port.SecurityGroups = opts.SecurityGroups
183 }
184
185 // Response
Jamie Hannafordd9036422014-09-23 17:50:24 +0200186 var res UpdateResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200187 _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200188 MoreHeaders: c.Provider.AuthenticatedHeaders(),
189 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200190 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200191 OkCodes: []int{200, 201},
192 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200193 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200194}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200195
Jamie Hannaford686c4962014-09-23 10:46:20 +0200196// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200197func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
198 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200199 _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200200 MoreHeaders: c.Provider.AuthenticatedHeaders(),
201 OkCodes: []int{204},
202 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200203 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200204}