blob: a666effe5aae74e3a2fd721793d9d6d80467ed96 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
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 Hannaford01e14922014-09-11 15:23:49 +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 ToNetworkListQuery() (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 network attributes you want to see returned. SortKey allows you to sort
17// by a particular network attribute. SortDir sets the direction, and is either
18// `asc' or `desc'. Marker and Limit are used for pagination.
19type 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 TenantID string `q:"tenant_id"`
24 Shared *bool `q:"shared"`
25 ID string `q:"id"`
26 Marker string `q:"marker"`
27 Limit int `q:"limit"`
28 SortKey string `q:"sort_key"`
29 SortDir string `q:"sort_dir"`
30 Fields []string `q:"fields"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020031}
32
Jon Perritt26780d52014-10-14 11:35:58 -050033// ToNetworkListQuery formats a ListOpts into a query string.
34func (opts ListOpts) ToNetworkListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050035 q, err := gophercloud.BuildQueryString(opts)
Jon Perritte1c6ceb2016-03-14 12:09:36 -050036 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050037}
38
Jamie Hannaford686c4962014-09-23 10:46:20 +020039// List returns a Pager which allows you to iterate over a collection of
40// networks. It accepts a ListOpts struct, which allows you to filter and sort
41// the returned collection for greater efficiency.
Jon Perritt04851d32014-10-14 02:07:13 -050042func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
43 url := listURL(c)
44 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050045 query, err := opts.ToNetworkListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050046 if err != nil {
47 return pagination.Pager{Err: err}
48 }
49 url += query
Jamie Hannaford4721abc2014-09-16 16:29:04 +020050 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040051 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
52 return NetworkPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020053 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020054}
55
Jamie Hannaford686c4962014-09-23 10:46:20 +020056// Get retrieves a specific network based on its unique ID.
Jon Perritt3860b512016-03-29 12:01:48 -050057func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050058 _, r.Err = c.Get(getURL(c, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050059 return
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020060}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020061
Jamie Hannaford35c91a62014-10-06 15:50:08 +020062// CreateOptsBuilder is the interface options structs have to satisfy in order
63// to be used in the main Create operation in this package. Since many
64// extensions decorate or modify the common logic, it is useful for them to
65// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020066type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050067 ToNetworkCreateMap() (map[string]interface{}, error)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020068}
69
Jon Perritte1c6ceb2016-03-14 12:09:36 -050070// CreateOpts satisfies the CreateOptsBuilder interface
71type CreateOpts struct {
72 AdminStateUp *bool `json:"admin_state_up,omitempty"`
73 Name string `json:"name,omitempty"`
74 Shared *bool `json:"shared,omitempty"`
75 TenantID string `json:"tenant_id,omitempty"`
76}
Jamie Hannaford965ae702014-09-22 14:58:19 +020077
Jamie Hannaford35c91a62014-10-06 15:50:08 +020078// ToNetworkCreateMap casts a CreateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -050079func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050080 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +020081}
82
Jamie Hannaford686c4962014-09-23 10:46:20 +020083// Create accepts a CreateOpts struct and creates a new network using the values
84// provided. This operation does not actually require a request body, i.e. the
85// CreateOpts struct argument can be empty.
86//
87// The tenant ID that is contained in the URI is the tenant that creates the
88// network. An admin user, however, has the option of specifying another tenant
89// ID in the CreateOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -050090func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050091 b, err := opts.ToNetworkCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -050092 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050093 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050094 return
Jon Perritt04851d32014-10-14 02:07:13 -050095 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -050096 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050097 return
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020098}
Jamie Hannaford79475052014-09-15 17:08:06 +020099
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200100// UpdateOptsBuilder is the interface options structs have to satisfy in order
101// to be used in the main Update operation in this package. Since many
102// extensions decorate or modify the common logic, it is useful for them to
103// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200104type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500105 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200106}
107
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500108// UpdateOpts satisfies the UpdateOptsBuilder interface
109type UpdateOpts struct {
110 AdminStateUp *bool `json:"admin_state_up,omitempty"`
111 Name string `json:"name,omitempty"`
112 Shared *bool `json:"shared,omitempty"`
113}
Jamie Hannaford965ae702014-09-22 14:58:19 +0200114
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200115// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500116func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500117 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200118}
119
Jamie Hannaford686c4962014-09-23 10:46:20 +0200120// Update accepts a UpdateOpts struct and updates an existing network using the
121// values provided. For more information, see the Create function.
Jon Perritt3860b512016-03-29 12:01:48 -0500122func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500123 b, err := opts.ToNetworkUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500124 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500125 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500126 return
Jon Perritt04851d32014-10-14 02:07:13 -0500127 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500128 _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100129 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200130 })
jrperritt29ae6b32016-04-13 12:59:37 -0500131 return
Jamie Hannaford79475052014-09-15 17:08:06 +0200132}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200133
Jamie Hannaford686c4962014-09-23 10:46:20 +0200134// Delete accepts a unique ID and deletes the network associated with it.
Jon Perritt3860b512016-03-29 12:01:48 -0500135func Delete(c *gophercloud.ServiceClient, networkID string) (r DeleteResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500136 _, r.Err = c.Delete(deleteURL(c, networkID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500137 return
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200138}
Jon Perritt7ab13282015-06-28 18:47:19 -0600139
jrperritt376d4f72015-06-28 19:07:34 -0600140// IDFromName is a convenience function that returns a network's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600141func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600142 count := 0
143 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600144 pages, err := List(client, nil).AllPages()
145 if err != nil {
146 return "", err
147 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600148
Jon Perritte3cb7e42016-03-07 06:24:11 -0600149 all, err := ExtractNetworks(pages)
150 if err != nil {
151 return "", err
152 }
153
154 for _, s := range all {
155 if s.Name == name {
156 count++
157 id = s.ID
158 }
159 }
160
161 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600162 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500163 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600164 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600165 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600166 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500167 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600168 }
169}