blob: 261e8ad66b4537baa68314c4b436a8c8a1343094 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford548d3402014-09-18 15:50:08 +02006)
7
Jon Perritt04851d32014-10-14 02:07:13 -05008// ListOptsBuilder allows extensions to add additional parameters to the
9// List request.
10type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050011 ToPortListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050012}
13
Jamie Hannaford686c4962014-09-23 10:46:20 +020014// ListOpts allows the filtering and sorting of paginated collections through
15// the API. Filtering is achieved by passing in struct field values that map to
16// the port attributes you want to see returned. SortKey allows you to sort
17// by a particular port attribute. SortDir sets the direction, and is either
18// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford548d3402014-09-18 15:50:08 +020019type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020020 Status string `q:"status"`
21 Name string `q:"name"`
22 AdminStateUp *bool `q:"admin_state_up"`
23 NetworkID string `q:"network_id"`
24 TenantID string `q:"tenant_id"`
25 DeviceOwner string `q:"device_owner"`
26 MACAddress string `q:"mac_address"`
27 ID string `q:"id"`
28 DeviceID string `q:"device_id"`
29 Limit int `q:"limit"`
30 Marker string `q:"marker"`
31 SortKey string `q:"sort_key"`
32 SortDir string `q:"sort_dir"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020033}
34
Jon Perritt26780d52014-10-14 11:35:58 -050035// ToPortListQuery formats a ListOpts into a query string.
36func (opts ListOpts) ToPortListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050037 q, err := gophercloud.BuildQueryString(opts)
Jon Perritte1c6ceb2016-03-14 12:09:36 -050038 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050039}
40
Jamie Hannaford686c4962014-09-23 10:46:20 +020041// List returns a Pager which allows you to iterate over a collection of
42// ports. It accepts a ListOpts struct, which allows you to filter and sort
43// the returned collection for greater efficiency.
44//
45// Default policy settings return only those ports that are owned by the tenant
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070046// who submits the request, unless the request is submitted by a user with
Jamie Hannaford686c4962014-09-23 10:46:20 +020047// administrative rights.
Jon Perritt04851d32014-10-14 02:07:13 -050048func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
49 url := listURL(c)
50 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050051 query, err := opts.ToPortListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050052 if err != nil {
53 return pagination.Pager{Err: err}
54 }
55 url += query
Jamie Hannaford548d3402014-09-18 15:50:08 +020056 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040057 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
58 return PortPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford548d3402014-09-18 15:50:08 +020059 })
60}
Jamie Hannaforda311f182014-09-19 11:19:10 +020061
Jamie Hannaford686c4962014-09-23 10:46:20 +020062// Get retrieves a specific port based on its unique ID.
Jon Perritt3860b512016-03-29 12:01:48 -050063func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050064 _, r.Err = c.Get(getURL(c, id), &r.Body, nil)
Jamie Hannaforda311f182014-09-19 11:19:10 +020065}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020066
Jon Perritt04851d32014-10-14 02:07:13 -050067// CreateOptsBuilder is the interface options structs have to satisfy in order
68// to be used in the main Create operation in this package. Since many
69// extensions decorate or modify the common logic, it is useful for them to
70// satisfy a basic interface in order for them to be used.
71type CreateOptsBuilder interface {
72 ToPortCreateMap() (map[string]interface{}, error)
73}
74
Jamie Hannaford686c4962014-09-23 10:46:20 +020075// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020076type CreateOpts struct {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050077 NetworkID string `json:"network_id" required:"true"`
78 Name string `json:"name,omitempty"`
79 AdminStateUp *bool `json:"admin_state_up,omitempty"`
80 MACAddress string `json:"mac_address,omitempty"`
81 FixedIPs interface{} `json:"fixed_ips,omitempty"`
82 DeviceID string `json:"device_id,omitempty"`
83 DeviceOwner string `json:"device_owner,omitempty"`
84 TenantID string `json:"tenant_id,omitempty"`
85 SecurityGroups []string `json:"security_groups,omitempty"`
86 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020087}
88
Jon Perritt04851d32014-10-14 02:07:13 -050089// ToPortCreateMap casts a CreateOpts struct to a map.
90func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050091 return gophercloud.BuildRequestBody(opts, "port")
Jon Perritt04851d32014-10-14 02:07:13 -050092}
93
Jamie Hannaford686c4962014-09-23 10:46:20 +020094// Create accepts a CreateOpts struct and creates a new network using the values
95// provided. You must remember to provide a NetworkID value.
Jon Perritt3860b512016-03-29 12:01:48 -050096func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050097 b, err := opts.ToPortCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -050098 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050099 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500100 return
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200101 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500102 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200103}
104
Jon Perritt04851d32014-10-14 02:07:13 -0500105// UpdateOptsBuilder is the interface options structs have to satisfy in order
106// to be used in the main Update operation in this package. Since many
107// extensions decorate or modify the common logic, it is useful for them to
108// satisfy a basic interface in order for them to be used.
109type UpdateOptsBuilder interface {
110 ToPortUpdateMap() (map[string]interface{}, error)
111}
112
Jamie Hannaford686c4962014-09-23 10:46:20 +0200113// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200114type UpdateOpts struct {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500115 Name string `json:"name,omitempty"`
116 AdminStateUp *bool `json:"admin_state_up,omitempty"`
117 FixedIPs interface{} `json:"fixed_ips,omitempty"`
118 DeviceID string `json:"device_id,omitempty"`
119 DeviceOwner string `json:"device_owner,omitempty"`
120 SecurityGroups []string `json:"security_groups,omitempty"`
121 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200122}
123
Jon Perritt04851d32014-10-14 02:07:13 -0500124// ToPortUpdateMap casts an UpdateOpts struct to a map.
125func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500126 return gophercloud.BuildRequestBody(opts, "port")
Jon Perritt04851d32014-10-14 02:07:13 -0500127}
128
Jamie Hannaford686c4962014-09-23 10:46:20 +0200129// Update accepts a UpdateOpts struct and updates an existing port using the
130// values provided.
Jon Perritt3860b512016-03-29 12:01:48 -0500131func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500132 b, err := opts.ToPortUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500133 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500134 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500135 return
Jon Perritt04851d32014-10-14 02:07:13 -0500136 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500137 _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100138 OkCodes: []int{200, 201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200139 })
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200140}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200141
Jamie Hannaford686c4962014-09-23 10:46:20 +0200142// Delete accepts a unique ID and deletes the port associated with it.
Jon Perritt3860b512016-03-29 12:01:48 -0500143func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500144 _, r.Err = c.Delete(deleteURL(c, id), nil)
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200145}
Jon Perritt7ab13282015-06-28 18:47:19 -0600146
jrperritt14f716b2015-06-28 19:07:52 -0600147// IDFromName is a convenience function that returns a port's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600148func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600149 count := 0
150 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600151 pages, err := List(client, nil).AllPages()
152 if err != nil {
153 return "", err
154 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600155
Jon Perritte3cb7e42016-03-07 06:24:11 -0600156 all, err := ExtractPorts(pages)
157 if err != nil {
158 return "", err
159 }
160
161 for _, s := range all {
162 if s.Name == name {
163 count++
164 id = s.ID
165 }
166 }
167
168 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600169 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500170 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600171 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600172 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600173 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500174 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600175 }
176}