blob: 31c3037ea6679e879492246943d5f440ad8470cd [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)
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020058}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020059
Jamie Hannaford35c91a62014-10-06 15:50:08 +020060// CreateOptsBuilder is the interface options structs have to satisfy in order
61// to be used in the main Create operation in this package. Since many
62// extensions decorate or modify the common logic, it is useful for them to
63// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020064type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050065 ToNetworkCreateMap() (map[string]interface{}, error)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020066}
67
Jon Perritte1c6ceb2016-03-14 12:09:36 -050068// CreateOpts satisfies the CreateOptsBuilder interface
69type CreateOpts struct {
70 AdminStateUp *bool `json:"admin_state_up,omitempty"`
71 Name string `json:"name,omitempty"`
72 Shared *bool `json:"shared,omitempty"`
73 TenantID string `json:"tenant_id,omitempty"`
74}
Jamie Hannaford965ae702014-09-22 14:58:19 +020075
Jamie Hannaford35c91a62014-10-06 15:50:08 +020076// ToNetworkCreateMap casts a CreateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -050077func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050078 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +020079}
80
Jamie Hannaford686c4962014-09-23 10:46:20 +020081// Create accepts a CreateOpts struct and creates a new network using the values
82// provided. This operation does not actually require a request body, i.e. the
83// CreateOpts struct argument can be empty.
84//
85// The tenant ID that is contained in the URI is the tenant that creates the
86// network. An admin user, however, has the option of specifying another tenant
87// ID in the CreateOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -050088func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050089 b, err := opts.ToNetworkCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -050090 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050091 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050092 return
Jon Perritt04851d32014-10-14 02:07:13 -050093 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -050094 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020095}
Jamie Hannaford79475052014-09-15 17:08:06 +020096
Jamie Hannaford35c91a62014-10-06 15:50:08 +020097// UpdateOptsBuilder is the interface options structs have to satisfy in order
98// to be used in the main Update operation in this package. Since many
99// extensions decorate or modify the common logic, it is useful for them to
100// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200101type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500102 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200103}
104
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500105// UpdateOpts satisfies the UpdateOptsBuilder interface
106type UpdateOpts struct {
107 AdminStateUp *bool `json:"admin_state_up,omitempty"`
108 Name string `json:"name,omitempty"`
109 Shared *bool `json:"shared,omitempty"`
110}
Jamie Hannaford965ae702014-09-22 14:58:19 +0200111
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200112// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500113func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500114 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200115}
116
Jamie Hannaford686c4962014-09-23 10:46:20 +0200117// Update accepts a UpdateOpts struct and updates an existing network using the
118// values provided. For more information, see the Create function.
Jon Perritt3860b512016-03-29 12:01:48 -0500119func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500120 b, err := opts.ToNetworkUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500121 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500122 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500123 return
Jon Perritt04851d32014-10-14 02:07:13 -0500124 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500125 _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100126 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200127 })
Jamie Hannaford79475052014-09-15 17:08:06 +0200128}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200129
Jamie Hannaford686c4962014-09-23 10:46:20 +0200130// Delete accepts a unique ID and deletes the network associated with it.
Jon Perritt3860b512016-03-29 12:01:48 -0500131func Delete(c *gophercloud.ServiceClient, networkID string) (r DeleteResult) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500132 _, r.Err = c.Delete(deleteURL(c, networkID), nil)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200133}
Jon Perritt7ab13282015-06-28 18:47:19 -0600134
jrperritt376d4f72015-06-28 19:07:34 -0600135// IDFromName is a convenience function that returns a network's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600136func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600137 count := 0
138 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600139 pages, err := List(client, nil).AllPages()
140 if err != nil {
141 return "", err
142 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600143
Jon Perritte3cb7e42016-03-07 06:24:11 -0600144 all, err := ExtractNetworks(pages)
145 if err != nil {
146 return "", err
147 }
148
149 for _, s := range all {
150 if s.Name == name {
151 count++
152 id = s.ID
153 }
154 }
155
156 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600157 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500158 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600159 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600160 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600161 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500162 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600163 }
164}