blob: 6928d8f81bf25a7f8205047c3167d2c545c1d88f [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 Hannaford686c4962014-09-23 10:46:20 +02009// ListOpts allows the filtering and sorting of paginated collections through
10// the API. Filtering is achieved by passing in struct field values that map to
11// the port attributes you want to see returned. SortKey allows you to sort
12// by a particular port attribute. SortDir sets the direction, and is either
13// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford548d3402014-09-18 15:50:08 +020014type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020015 Status string `q:"status"`
16 Name string `q:"name"`
17 AdminStateUp *bool `q:"admin_state_up"`
18 NetworkID string `q:"network_id"`
19 TenantID string `q:"tenant_id"`
20 DeviceOwner string `q:"device_owner"`
21 MACAddress string `q:"mac_address"`
22 ID string `q:"id"`
23 DeviceID string `q:"device_id"`
24 Limit int `q:"limit"`
25 Marker string `q:"marker"`
26 SortKey string `q:"sort_key"`
27 SortDir string `q:"sort_dir"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020028}
29
Jamie Hannaford686c4962014-09-23 10:46:20 +020030// List returns a Pager which allows you to iterate over a collection of
31// ports. It accepts a ListOpts struct, which allows you to filter and sort
32// the returned collection for greater efficiency.
33//
34// Default policy settings return only those ports that are owned by the tenant
35// who submits the request, unless the request is submitted by an user with
36// administrative rights.
Jamie Hannaford548d3402014-09-18 15:50:08 +020037func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
38 // Build query parameters
Jamie Hannaford92523e32014-10-02 11:08:36 +020039 q, err := gophercloud.BuildQueryString(&opts)
40 if err != nil {
41 return pagination.Pager{Err: err}
Jamie Hannaford548d3402014-09-18 15:50:08 +020042 }
Jamie Hannaford92523e32014-10-02 11:08:36 +020043 u := listURL(c) + q.String()
Jamie Hannaford548d3402014-09-18 15:50:08 +020044
Jamie Hannaford548d3402014-09-18 15:50:08 +020045 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040046 return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +020047 })
48}
Jamie Hannaforda311f182014-09-19 11:19:10 +020049
Jamie Hannaford686c4962014-09-23 10:46:20 +020050// Get retrieves a specific port based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020051func Get(c *gophercloud.ServiceClient, id string) GetResult {
52 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020053 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaforda311f182014-09-19 11:19:10 +020054 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020055 Results: &res.Resp,
56 OkCodes: []int{200},
Jamie Hannaforda311f182014-09-19 11:19:10 +020057 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020058 return res
Jamie Hannaforda311f182014-09-19 11:19:10 +020059}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020060
Jamie Hannaford686c4962014-09-23 10:46:20 +020061// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020062type CreateOpts struct {
63 NetworkID string
64 Name string
65 AdminStateUp *bool
66 MACAddress string
67 FixedIPs interface{}
68 DeviceID string
69 DeviceOwner string
70 TenantID string
71 SecurityGroups []string
72}
73
Jamie Hannaford686c4962014-09-23 10:46:20 +020074// Create accepts a CreateOpts struct and creates a new network using the values
75// provided. You must remember to provide a NetworkID value.
Jamie Hannafordd9036422014-09-23 17:50:24 +020076func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
77 var res CreateResult
78
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020079 type port struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +020080 NetworkID string `json:"network_id"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020081 Name *string `json:"name,omitempty"`
82 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020083 MACAddress *string `json:"mac_address,omitempty"`
84 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020085 DeviceID *string `json:"device_id,omitempty"`
86 DeviceOwner *string `json:"device_owner,omitempty"`
87 TenantID *string `json:"tenant_id,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020088 SecurityGroups []string `json:"security_groups,omitempty"`
89 }
90 type request struct {
91 Port port `json:"port"`
92 }
93
94 // Validate
95 if opts.NetworkID == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +020096 res.Err = errNetworkIDRequired
97 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020098 }
99
100 // Populate request body
101 reqBody := request{Port: port{
102 NetworkID: opts.NetworkID,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200103 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200104 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200105 TenantID: gophercloud.MaybeString(opts.TenantID),
106 MACAddress: gophercloud.MaybeString(opts.MACAddress),
107 DeviceID: gophercloud.MaybeString(opts.DeviceID),
108 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200109 }}
110
111 if opts.FixedIPs != nil {
112 reqBody.Port.FixedIPs = opts.FixedIPs
113 }
114
115 if opts.SecurityGroups != nil {
116 reqBody.Port.SecurityGroups = opts.SecurityGroups
117 }
118
119 // Response
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200120 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200121 MoreHeaders: c.Provider.AuthenticatedHeaders(),
122 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200123 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200124 OkCodes: []int{201},
Jamie Hannaford2a0492a2014-09-22 12:02:11 +0200125 DumpReqJson: true,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200126 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200127
Jamie Hannafordd9036422014-09-23 17:50:24 +0200128 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200129}
130
Jamie Hannaford686c4962014-09-23 10:46:20 +0200131// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200132type UpdateOpts struct {
133 Name string
134 AdminStateUp *bool
135 FixedIPs interface{}
136 DeviceID string
137 DeviceOwner string
138 SecurityGroups []string
139}
140
Jamie Hannaford686c4962014-09-23 10:46:20 +0200141// Update accepts a UpdateOpts struct and updates an existing port using the
142// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200143func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200144 type port struct {
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200145 Name *string `json:"name,omitempty"`
146 AdminStateUp *bool `json:"admin_state_up,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200147 FixedIPs interface{} `json:"fixed_ips,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200148 DeviceID *string `json:"device_id,omitempty"`
149 DeviceOwner *string `json:"device_owner,omitempty"`
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200150 SecurityGroups []string `json:"security_groups,omitempty"`
151 }
152 type request struct {
153 Port port `json:"port"`
154 }
155
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200156 // Populate request body
157 reqBody := request{Port: port{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200158 Name: gophercloud.MaybeString(opts.Name),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200159 AdminStateUp: opts.AdminStateUp,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200160 DeviceID: gophercloud.MaybeString(opts.DeviceID),
161 DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200162 }}
163
164 if opts.FixedIPs != nil {
165 reqBody.Port.FixedIPs = opts.FixedIPs
166 }
167
168 if opts.SecurityGroups != nil {
169 reqBody.Port.SecurityGroups = opts.SecurityGroups
170 }
171
172 // Response
Jamie Hannafordd9036422014-09-23 17:50:24 +0200173 var res UpdateResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200174 _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200175 MoreHeaders: c.Provider.AuthenticatedHeaders(),
176 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200177 Results: &res.Resp,
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200178 OkCodes: []int{200, 201},
179 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200180 return res
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200181}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200182
Jamie Hannaford686c4962014-09-23 10:46:20 +0200183// Delete accepts a unique ID and deletes the port associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200184func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
185 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200186 _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200187 MoreHeaders: c.Provider.AuthenticatedHeaders(),
188 OkCodes: []int{204},
189 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200190 return res
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200191}