blob: 876a00bb0beea4e18641f1b87b62339816d2631b [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/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 {
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 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"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020030}
31
Jon Perritt26780d52014-10-14 11:35:58 -050032// ToNetworkListQuery formats a ListOpts into a query string.
33func (opts ListOpts) ToNetworkListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050034 q, err := gophercloud.BuildQueryString(opts)
Jon Perritte1c6ceb2016-03-14 12:09:36 -050035 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050036}
37
Jamie Hannaford686c4962014-09-23 10:46:20 +020038// List returns a Pager which allows you to iterate over a collection of
39// networks. It accepts a ListOpts struct, which allows you to filter and sort
40// the returned collection for greater efficiency.
Jon Perritt04851d32014-10-14 02:07:13 -050041func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
42 url := listURL(c)
43 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050044 query, err := opts.ToNetworkListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050045 if err != nil {
46 return pagination.Pager{Err: err}
47 }
48 url += query
Jamie Hannaford4721abc2014-09-16 16:29:04 +020049 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040050 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
51 return NetworkPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020052 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020053}
54
Jamie Hannaford686c4962014-09-23 10:46:20 +020055// Get retrieves a specific network based on its unique ID.
Jon Perritt3860b512016-03-29 12:01:48 -050056func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050057 _, r.Err = c.Get(getURL(c, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050058 return
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020059}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020060
Jamie Hannaford35c91a62014-10-06 15:50:08 +020061// CreateOptsBuilder is the interface options structs have to satisfy in order
62// to be used in the main Create operation in this package. Since many
63// extensions decorate or modify the common logic, it is useful for them to
64// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020065type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050066 ToNetworkCreateMap() (map[string]interface{}, error)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020067}
68
Jon Perritte1c6ceb2016-03-14 12:09:36 -050069// CreateOpts satisfies the CreateOptsBuilder interface
70type CreateOpts struct {
71 AdminStateUp *bool `json:"admin_state_up,omitempty"`
72 Name string `json:"name,omitempty"`
73 Shared *bool `json:"shared,omitempty"`
74 TenantID string `json:"tenant_id,omitempty"`
75}
Jamie Hannaford965ae702014-09-22 14:58:19 +020076
Jamie Hannaford35c91a62014-10-06 15:50:08 +020077// ToNetworkCreateMap casts a CreateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -050078func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050079 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +020080}
81
Jamie Hannaford686c4962014-09-23 10:46:20 +020082// Create accepts a CreateOpts struct and creates a new network using the values
83// provided. This operation does not actually require a request body, i.e. the
84// CreateOpts struct argument can be empty.
85//
86// The tenant ID that is contained in the URI is the tenant that creates the
87// network. An admin user, however, has the option of specifying another tenant
88// ID in the CreateOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -050089func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050090 b, err := opts.ToNetworkCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -050091 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050092 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050093 return
Jon Perritt04851d32014-10-14 02:07:13 -050094 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -050095 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050096 return
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020097}
Jamie Hannaford79475052014-09-15 17:08:06 +020098
Jamie Hannaford35c91a62014-10-06 15:50:08 +020099// UpdateOptsBuilder is the interface options structs have to satisfy in order
100// to be used in the main Update operation in this package. Since many
101// extensions decorate or modify the common logic, it is useful for them to
102// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200103type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500104 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200105}
106
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500107// UpdateOpts satisfies the UpdateOptsBuilder interface
108type UpdateOpts struct {
109 AdminStateUp *bool `json:"admin_state_up,omitempty"`
110 Name string `json:"name,omitempty"`
111 Shared *bool `json:"shared,omitempty"`
112}
Jamie Hannaford965ae702014-09-22 14:58:19 +0200113
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200114// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500115func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500116 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200117}
118
Jamie Hannaford686c4962014-09-23 10:46:20 +0200119// Update accepts a UpdateOpts struct and updates an existing network using the
120// values provided. For more information, see the Create function.
Jon Perritt3860b512016-03-29 12:01:48 -0500121func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500122 b, err := opts.ToNetworkUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500123 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500124 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500125 return
Jon Perritt04851d32014-10-14 02:07:13 -0500126 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500127 _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100128 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200129 })
jrperritt29ae6b32016-04-13 12:59:37 -0500130 return
Jamie Hannaford79475052014-09-15 17:08:06 +0200131}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200132
Jamie Hannaford686c4962014-09-23 10:46:20 +0200133// Delete accepts a unique ID and deletes the network associated with it.
Jon Perritt3860b512016-03-29 12:01:48 -0500134func Delete(c *gophercloud.ServiceClient, networkID string) (r DeleteResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500135 _, r.Err = c.Delete(deleteURL(c, networkID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500136 return
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200137}
Jon Perritt7ab13282015-06-28 18:47:19 -0600138
jrperritt376d4f72015-06-28 19:07:34 -0600139// IDFromName is a convenience function that returns a network's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600140func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600141 count := 0
142 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600143 pages, err := List(client, nil).AllPages()
144 if err != nil {
145 return "", err
146 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600147
Jon Perritte3cb7e42016-03-07 06:24:11 -0600148 all, err := ExtractNetworks(pages)
149 if err != nil {
150 return "", err
151 }
152
153 for _, s := range all {
154 if s.Name == name {
155 count++
156 id = s.ID
157 }
158 }
159
160 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600161 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500162 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600163 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600164 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600165 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500166 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600167 }
168}