blob: ec043b1a222aff7fd513a73942b41e3eca373108 [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.
Jamie Hannafordd9036422014-09-23 17:50:24 +020056func Get(c *gophercloud.ServiceClient, id string) GetResult {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050057 var r GetResult
58 _, r.Err = c.Get(getURL(c, id), &r.Body, nil)
59 return r
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.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020090func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050091 var r CreateResult
92 b, err := opts.ToNetworkCreateMap()
Jon Perritt04851d32014-10-14 02:07:13 -050093 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -050094 r.Err = err
95 return r
Jon Perritt04851d32014-10-14 02:07:13 -050096 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -050097 _, r.Err = c.Post(createURL(c), b, &r.Body, nil)
98 return r
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020099}
Jamie Hannaford79475052014-09-15 17:08:06 +0200100
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200101// UpdateOptsBuilder is the interface options structs have to satisfy in order
102// to be used in the main Update operation in this package. Since many
103// extensions decorate or modify the common logic, it is useful for them to
104// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200105type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500106 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200107}
108
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500109// UpdateOpts satisfies the UpdateOptsBuilder interface
110type UpdateOpts struct {
111 AdminStateUp *bool `json:"admin_state_up,omitempty"`
112 Name string `json:"name,omitempty"`
113 Shared *bool `json:"shared,omitempty"`
114}
Jamie Hannaford965ae702014-09-22 14:58:19 +0200115
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200116// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500117func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500118 return gophercloud.BuildRequestBody(opts, "network")
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200119}
120
Jamie Hannaford686c4962014-09-23 10:46:20 +0200121// Update accepts a UpdateOpts struct and updates an existing network using the
122// values provided. For more information, see the Create function.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200123func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500124 var r UpdateResult
125 b, err := opts.ToNetworkUpdateMap()
Jon Perritt04851d32014-10-14 02:07:13 -0500126 if err != nil {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500127 r.Err = err
128 return r
Jon Perritt04851d32014-10-14 02:07:13 -0500129 }
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500130 _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford059e1502015-03-24 16:20:32 +0100131 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200132 })
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500133 return r
Jamie Hannaford79475052014-09-15 17:08:06 +0200134}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200135
Jamie Hannaford686c4962014-09-23 10:46:20 +0200136// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200137func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500138 var r DeleteResult
139 _, r.Err = c.Delete(deleteURL(c, networkID), nil)
140 return r
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200141}
Jon Perritt7ab13282015-06-28 18:47:19 -0600142
jrperritt376d4f72015-06-28 19:07:34 -0600143// IDFromName is a convenience function that returns a network's ID given its name.
Jon Perritt7ab13282015-06-28 18:47:19 -0600144func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritte3cb7e42016-03-07 06:24:11 -0600145 count := 0
146 id := ""
Jon Perritte3cb7e42016-03-07 06:24:11 -0600147 pages, err := List(client, nil).AllPages()
148 if err != nil {
149 return "", err
150 }
Jon Perritt7ab13282015-06-28 18:47:19 -0600151
Jon Perritte3cb7e42016-03-07 06:24:11 -0600152 all, err := ExtractNetworks(pages)
153 if err != nil {
154 return "", err
155 }
156
157 for _, s := range all {
158 if s.Name == name {
159 count++
160 id = s.ID
161 }
162 }
163
164 switch count {
Jon Perritt7ab13282015-06-28 18:47:19 -0600165 case 0:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500166 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600167 case 1:
Jon Perritte3cb7e42016-03-07 06:24:11 -0600168 return id, nil
Jon Perritt7ab13282015-06-28 18:47:19 -0600169 default:
Jon Perritte1c6ceb2016-03-14 12:09:36 -0500170 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"}
Jon Perritt7ab13282015-06-28 18:47:19 -0600171 }
172}