blob: b0db67e7d0faaec196d4211af4b64c707f1abd59 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannaford01e14922014-09-11 15:23:49 +02004 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02005 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +02006)
7
Jamie Hannaford32979b62014-10-09 10:32:39 +02008// AdminState gives users a solid type to work with for create and update
9// operations. It is recommended that users use the `Up` and `Down` enums.
10type AdminState *bool
11
12// Convenience vars for AdminStateUp values.
13var (
14 iTrue = true
15 iFalse = false
16
17 Up AdminState = &iTrue
18 Down AdminState = &iFalse
19)
20
Jamie Hannaford965ae702014-09-22 14:58:19 +020021type networkOpts struct {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020022 AdminStateUp *bool
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020023 Name string
24 Shared *bool
25 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020026}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020027
Jon Perritt04851d32014-10-14 02:07:13 -050028// ListOptsBuilder allows extensions to add additional parameters to the
29// List request.
30type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050031 ToNetworkListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050032}
33
Jamie Hannaford686c4962014-09-23 10:46:20 +020034// ListOpts allows the filtering and sorting of paginated collections through
35// the API. Filtering is achieved by passing in struct field values that map to
36// the network attributes you want to see returned. SortKey allows you to sort
37// by a particular network attribute. SortDir sets the direction, and is either
38// `asc' or `desc'. Marker and Limit are used for pagination.
39type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020040 Status string `q:"status"`
41 Name string `q:"name"`
42 AdminStateUp *bool `q:"admin_state_up"`
43 TenantID string `q:"tenant_id"`
44 Shared *bool `q:"shared"`
45 ID string `q:"id"`
46 Marker string `q:"marker"`
47 Limit int `q:"limit"`
48 SortKey string `q:"sort_key"`
49 SortDir string `q:"sort_dir"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020050}
51
Jon Perritt26780d52014-10-14 11:35:58 -050052// ToNetworkListQuery formats a ListOpts into a query string.
53func (opts ListOpts) ToNetworkListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050054 q, err := gophercloud.BuildQueryString(opts)
55 if err != nil {
56 return "", err
57 }
58 return q.String(), nil
59}
60
Jamie Hannaford686c4962014-09-23 10:46:20 +020061// List returns a Pager which allows you to iterate over a collection of
62// networks. It accepts a ListOpts struct, which allows you to filter and sort
63// the returned collection for greater efficiency.
Jon Perritt04851d32014-10-14 02:07:13 -050064func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
65 url := listURL(c)
66 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050067 query, err := opts.ToNetworkListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050068 if err != nil {
69 return pagination.Pager{Err: err}
70 }
71 url += query
Jamie Hannaford4721abc2014-09-16 16:29:04 +020072 }
Jon Perritt04851d32014-10-14 02:07:13 -050073
Ash Wilsonb8b16f82014-10-20 10:19:49 -040074 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
75 return NetworkPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020076 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020077}
78
Jamie Hannaford686c4962014-09-23 10:46:20 +020079// Get retrieves a specific network based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020080func Get(c *gophercloud.ServiceClient, id string) GetResult {
81 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050082 _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{
83 JSONResponse: &res.Body,
84 OkCodes: []int{200},
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020085 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020086 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020087}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020088
Jamie Hannaford35c91a62014-10-06 15:50:08 +020089// CreateOptsBuilder is the interface options structs have to satisfy in order
90// to be used in the main Create operation in this package. Since many
91// extensions decorate or modify the common logic, it is useful for them to
92// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020093type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050094 ToNetworkCreateMap() (map[string]interface{}, error)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020095}
96
Jamie Hannaford35c91a62014-10-06 15:50:08 +020097// CreateOpts is the common options struct used in this package's Create
98// operation.
Jamie Hannaford965ae702014-09-22 14:58:19 +020099type CreateOpts networkOpts
100
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200101// ToNetworkCreateMap casts a CreateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500102func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
103 n := make(map[string]interface{})
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200104
Jon Perritt04851d32014-10-14 02:07:13 -0500105 if opts.AdminStateUp != nil {
106 n["admin_state_up"] = &opts.AdminStateUp
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200107 }
Jon Perritt04851d32014-10-14 02:07:13 -0500108 if opts.Name != "" {
109 n["name"] = opts.Name
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200110 }
Jon Perritt04851d32014-10-14 02:07:13 -0500111 if opts.Shared != nil {
112 n["shared"] = &opts.Shared
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200113 }
Jon Perritt04851d32014-10-14 02:07:13 -0500114 if opts.TenantID != "" {
115 n["tenant_id"] = opts.TenantID
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200116 }
117
Jon Perritt04851d32014-10-14 02:07:13 -0500118 return map[string]interface{}{"network": n}, nil
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200119}
120
Jamie Hannaford686c4962014-09-23 10:46:20 +0200121// Create accepts a CreateOpts struct and creates a new network using the values
122// provided. This operation does not actually require a request body, i.e. the
123// CreateOpts struct argument can be empty.
124//
125// The tenant ID that is contained in the URI is the tenant that creates the
126// network. An admin user, however, has the option of specifying another tenant
127// ID in the CreateOpts struct.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200128func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200129 var res CreateResult
130
Jon Perritt04851d32014-10-14 02:07:13 -0500131 reqBody, err := opts.ToNetworkCreateMap()
132 if err != nil {
133 res.Err = err
134 return res
135 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200136
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200137 // Send request to API
Ash Wilson4bf41a32015-02-12 15:52:44 -0500138 _, res.Err = c.Request("POST", createURL(c), gophercloud.RequestOpts{
139 JSONBody: &reqBody,
140 JSONResponse: &res.Body,
141 OkCodes: []int{201},
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200142 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200143 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200144}
Jamie Hannaford79475052014-09-15 17:08:06 +0200145
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200146// UpdateOptsBuilder is the interface options structs have to satisfy in order
147// to be used in the main Update operation in this package. Since many
148// extensions decorate or modify the common logic, it is useful for them to
149// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200150type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500151 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200152}
153
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200154// UpdateOpts is the common options struct used in this package's Update
155// operation.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200156type UpdateOpts networkOpts
157
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200158// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500159func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
160 n := make(map[string]interface{})
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200161
Jon Perritt04851d32014-10-14 02:07:13 -0500162 if opts.AdminStateUp != nil {
163 n["admin_state_up"] = &opts.AdminStateUp
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200164 }
Jon Perritt04851d32014-10-14 02:07:13 -0500165 if opts.Name != "" {
166 n["name"] = opts.Name
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200167 }
Jon Perritt04851d32014-10-14 02:07:13 -0500168 if opts.Shared != nil {
169 n["shared"] = &opts.Shared
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200170 }
171
Jon Perritt04851d32014-10-14 02:07:13 -0500172 return map[string]interface{}{"network": n}, nil
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200173}
174
Jamie Hannaford686c4962014-09-23 10:46:20 +0200175// Update accepts a UpdateOpts struct and updates an existing network using the
176// values provided. For more information, see the Create function.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200177func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200178 var res UpdateResult
179
Jon Perritt04851d32014-10-14 02:07:13 -0500180 reqBody, err := opts.ToNetworkUpdateMap()
181 if err != nil {
182 res.Err = err
183 return res
184 }
Jamie Hannaford79475052014-09-15 17:08:06 +0200185
Jamie Hannaford79475052014-09-15 17:08:06 +0200186 // Send request to API
Ash Wilson59fb6c42015-02-12 16:21:13 -0500187 _, res.Err = c.Request("PUT", updateURL(c, networkID), gophercloud.RequestOpts{
188 JSONBody: &reqBody,
189 JSONResponse: &res.Body,
190 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200191 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200192
Jamie Hannafordd9036422014-09-23 17:50:24 +0200193 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200194}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200195
Jamie Hannaford686c4962014-09-23 10:46:20 +0200196// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200197func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
198 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500199 _, res.Err = c.Request("DELETE", deleteURL(c, networkID), gophercloud.RequestOpts{
200 OkCodes: []int{204},
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200201 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200202 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200203}