blob: 193901bf43ab2a231429937968d4fbbf60da15ac [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02004 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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 {
Krzysztof Szukiełojć718e5c42017-05-09 11:34:47 +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"`
33 Fields []string `q:"fields"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020034}
35
Jon Perritt26780d52014-10-14 11:35:58 -050036// ToPortListQuery formats a ListOpts into a query string.
37func (opts ListOpts) ToPortListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050038 q, err := gophercloud.BuildQueryString(opts)
Jon Perritte1c6ceb2016-03-14 12:09:36 -050039 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050040}
41
Jamie Hannaford686c4962014-09-23 10:46:20 +020042// List returns a Pager which allows you to iterate over a collection of
43// ports. It accepts a ListOpts struct, which allows you to filter and sort
44// the returned collection for greater efficiency.
45//
46// Default policy settings return only those ports that are owned by the tenant
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070047// who submits the request, unless the request is submitted by a user with
Jamie Hannaford686c4962014-09-23 10:46:20 +020048// administrative rights.
Jon Perritt04851d32014-10-14 02:07:13 -050049func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
50 url := listURL(c)
51 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050052 query, err := opts.ToPortListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050053 if err != nil {
54 return pagination.Pager{Err: err}
55 }
56 url += query
Jamie Hannaford548d3402014-09-18 15:50:08 +020057 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040058 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
59 return PortPage{pagination.LinkedPageBase{PageResult: 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.
Jon Perritt3860b512016-03-29 12:01:48 -050064func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050065 _, r.Err = c.Get(getURL(c, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050066 return
Jamie Hannaforda311f182014-09-19 11:19:10 +020067}
Jamie Hannaforda5fb7822014-09-19 15:07:02 +020068
Jon Perritt04851d32014-10-14 02:07:13 -050069// CreateOptsBuilder is the interface options structs have to satisfy in order
70// to be used in the main Create operation in this package. Since many
71// extensions decorate or modify the common logic, it is useful for them to
72// satisfy a basic interface in order for them to be used.
73type CreateOptsBuilder interface {
74 ToPortCreateMap() (map[string]interface{}, error)
75}
76
Jamie Hannaford686c4962014-09-23 10:46:20 +020077// CreateOpts represents the attributes used when creating a new port.
Jamie Hannaford965ae702014-09-22 14:58:19 +020078type CreateOpts struct {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050079 NetworkID string `json:"network_id" required:"true"`
80 Name string `json:"name,omitempty"`
81 AdminStateUp *bool `json:"admin_state_up,omitempty"`
82 MACAddress string `json:"mac_address,omitempty"`
83 FixedIPs interface{} `json:"fixed_ips,omitempty"`
84 DeviceID string `json:"device_id,omitempty"`
85 DeviceOwner string `json:"device_owner,omitempty"`
86 TenantID string `json:"tenant_id,omitempty"`
87 SecurityGroups []string `json:"security_groups,omitempty"`
88 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020089}
90
Jon Perritt04851d32014-10-14 02:07:13 -050091// ToPortCreateMap casts a CreateOpts struct to a map.
92func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050093 return gophercloud.BuildRequestBody(opts, "port")
Jon Perritt04851d32014-10-14 02:07:13 -050094}
95
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// Create accepts a CreateOpts struct and creates a new network using the values
97// provided. You must remember to provide a NetworkID value.
Jon Perritt3860b512016-03-29 12:01:48 -050098func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050099 b, err := opts.ToPortCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500100 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500101 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500102 return
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200103 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500104 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500105 return
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200106}
107
Jon Perritt04851d32014-10-14 02:07:13 -0500108// UpdateOptsBuilder is the interface options structs have to satisfy in order
109// to be used in the main Update operation in this package. Since many
110// extensions decorate or modify the common logic, it is useful for them to
111// satisfy a basic interface in order for them to be used.
112type UpdateOptsBuilder interface {
113 ToPortUpdateMap() (map[string]interface{}, error)
114}
115
Jamie Hannaford686c4962014-09-23 10:46:20 +0200116// UpdateOpts represents the attributes used when updating an existing port.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200117type UpdateOpts struct {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500118 Name string `json:"name,omitempty"`
119 AdminStateUp *bool `json:"admin_state_up,omitempty"`
120 FixedIPs interface{} `json:"fixed_ips,omitempty"`
121 DeviceID string `json:"device_id,omitempty"`
122 DeviceOwner string `json:"device_owner,omitempty"`
Joe Topjian6d2ec962017-02-13 21:23:55 -0700123 SecurityGroups []string `json:"security_groups"`
124 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200125}
126
Jon Perritt04851d32014-10-14 02:07:13 -0500127// ToPortUpdateMap casts an UpdateOpts struct to a map.
128func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500129 return gophercloud.BuildRequestBody(opts, "port")
Jon Perritt04851d32014-10-14 02:07:13 -0500130}
131
Jamie Hannaford686c4962014-09-23 10:46:20 +0200132// Update accepts a UpdateOpts struct and updates an existing port using the
133// values provided.
Jon Perritt3860b512016-03-29 12:01:48 -0500134func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500135 b, err := opts.ToPortUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500136 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500137 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500138 return
Jon Perritt04851d32014-10-14 02:07:13 -0500139 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500140 _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100141 OkCodes: []int{200, 201},
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200142 })
jrperritt29ae6b32016-04-13 12:59:37 -0500143 return
Jamie Hannaforda5fb7822014-09-19 15:07:02 +0200144}
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200145
Jamie Hannaford686c4962014-09-23 10:46:20 +0200146// Delete accepts a unique ID and deletes the port associated with it.
Jon Perritt3860b512016-03-29 12:01:48 -0500147func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500148 _, r.Err = c.Delete(deleteURL(c, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500149 return
Jamie Hannafordd444b7a2014-09-19 15:08:27 +0200150}
Jon Perritt7ab13282015-06-28 18:47:19 -0600151
jrperritt14f716b2015-06-28 19:07:52 -0600152// IDFromName is a convenience function that returns a port's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600153func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600154 count := 0
155 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600156 pages, err := List(client, nil).AllPages()
157 if err != nil {
158 return "", err
159 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600160
Jon Perritte3cb7e42016-03-07 06:24:11 -0600161 all, err := ExtractPorts(pages)
162 if err != nil {
163 return "", err
164 }
165
166 for _, s := range all {
167 if s.Name == name {
168 count++
169 id = s.ID
170 }
171 }
172
173 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600174 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500175 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600176 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600177 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600178 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500179 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600180 }
181}