blob: 8cfe4e0d6ae1e2aa928cc7e2d4e40da6b744503b [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"
Jon Perritt04851d32014-10-14 02:07:13 -05006
7 "github.com/racker/perigee"
Jamie Hannaford01e14922014-09-11 15:23:49 +02008)
9
Jamie Hannaford32979b62014-10-09 10:32:39 +020010// AdminState gives users a solid type to work with for create and update
11// operations. It is recommended that users use the `Up` and `Down` enums.
12type AdminState *bool
13
14// Convenience vars for AdminStateUp values.
15var (
16 iTrue = true
17 iFalse = false
18
19 Up AdminState = &iTrue
20 Down AdminState = &iFalse
21)
22
Jamie Hannaford965ae702014-09-22 14:58:19 +020023type networkOpts struct {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020024 AdminStateUp *bool
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020025 Name string
26 Shared *bool
27 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020028}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020029
Jon Perritt04851d32014-10-14 02:07:13 -050030// ListOptsBuilder allows extensions to add additional parameters to the
31// List request.
32type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050033 ToNetworkListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050034}
35
Jamie Hannaford686c4962014-09-23 10:46:20 +020036// ListOpts allows the filtering and sorting of paginated collections through
37// the API. Filtering is achieved by passing in struct field values that map to
38// the network attributes you want to see returned. SortKey allows you to sort
39// by a particular network attribute. SortDir sets the direction, and is either
40// `asc' or `desc'. Marker and Limit are used for pagination.
41type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020042 Status string `q:"status"`
43 Name string `q:"name"`
44 AdminStateUp *bool `q:"admin_state_up"`
45 TenantID string `q:"tenant_id"`
46 Shared *bool `q:"shared"`
47 ID string `q:"id"`
48 Marker string `q:"marker"`
49 Limit int `q:"limit"`
50 SortKey string `q:"sort_key"`
51 SortDir string `q:"sort_dir"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020052}
53
Jon Perritt26780d52014-10-14 11:35:58 -050054// ToNetworkListQuery formats a ListOpts into a query string.
55func (opts ListOpts) ToNetworkListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050056 q, err := gophercloud.BuildQueryString(opts)
57 if err != nil {
58 return "", err
59 }
60 return q.String(), nil
61}
62
Jamie Hannaford686c4962014-09-23 10:46:20 +020063// List returns a Pager which allows you to iterate over a collection of
64// networks. It accepts a ListOpts struct, which allows you to filter and sort
65// the returned collection for greater efficiency.
Jon Perritt04851d32014-10-14 02:07:13 -050066func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
67 url := listURL(c)
68 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050069 query, err := opts.ToNetworkListQuery()
Jon Perritt04851d32014-10-14 02:07:13 -050070 if err != nil {
71 return pagination.Pager{Err: err}
72 }
73 url += query
Jamie Hannaford4721abc2014-09-16 16:29:04 +020074 }
Jon Perritt04851d32014-10-14 02:07:13 -050075
76 return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040077 return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020078 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020079}
80
Jamie Hannaford686c4962014-09-23 10:46:20 +020081// Get retrieves a specific network based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020082func Get(c *gophercloud.ServiceClient, id string) GetResult {
83 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020084 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020085 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020086 Results: &res.Resp,
87 OkCodes: []int{200},
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020088 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020089 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020090}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020091
Jamie Hannaford35c91a62014-10-06 15:50:08 +020092// CreateOptsBuilder is the interface options structs have to satisfy in order
93// to be used in the main Create operation in this package. Since many
94// extensions decorate or modify the common logic, it is useful for them to
95// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020096type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050097 ToNetworkCreateMap() (map[string]interface{}, error)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020098}
99
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200100// CreateOpts is the common options struct used in this package's Create
101// operation.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102type CreateOpts networkOpts
103
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200104// ToNetworkCreateMap casts a CreateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500105func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
106 n := make(map[string]interface{})
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200107
Jon Perritt04851d32014-10-14 02:07:13 -0500108 if opts.AdminStateUp != nil {
109 n["admin_state_up"] = &opts.AdminStateUp
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200110 }
Jon Perritt04851d32014-10-14 02:07:13 -0500111 if opts.Name != "" {
112 n["name"] = opts.Name
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200113 }
Jon Perritt04851d32014-10-14 02:07:13 -0500114 if opts.Shared != nil {
115 n["shared"] = &opts.Shared
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200116 }
Jon Perritt04851d32014-10-14 02:07:13 -0500117 if opts.TenantID != "" {
118 n["tenant_id"] = opts.TenantID
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200119 }
120
Jon Perritt04851d32014-10-14 02:07:13 -0500121 return map[string]interface{}{"network": n}, nil
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200122}
123
Jamie Hannaford686c4962014-09-23 10:46:20 +0200124// Create accepts a CreateOpts struct and creates a new network using the values
125// provided. This operation does not actually require a request body, i.e. the
126// CreateOpts struct argument can be empty.
127//
128// The tenant ID that is contained in the URI is the tenant that creates the
129// network. An admin user, however, has the option of specifying another tenant
130// ID in the CreateOpts struct.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200131func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200132 var res CreateResult
133
Jon Perritt04851d32014-10-14 02:07:13 -0500134 reqBody, err := opts.ToNetworkCreateMap()
135 if err != nil {
136 res.Err = err
137 return res
138 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200139
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200140 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200141 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200142 MoreHeaders: c.Provider.AuthenticatedHeaders(),
143 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200144 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200145 OkCodes: []int{201},
146 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200147 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200148}
Jamie Hannaford79475052014-09-15 17:08:06 +0200149
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200150// UpdateOptsBuilder is the interface options structs have to satisfy in order
151// to be used in the main Update operation in this package. Since many
152// extensions decorate or modify the common logic, it is useful for them to
153// satisfy a basic interface in order for them to be used.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200154type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500155 ToNetworkUpdateMap() (map[string]interface{}, error)
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200156}
157
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200158// UpdateOpts is the common options struct used in this package's Update
159// operation.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200160type UpdateOpts networkOpts
161
Jamie Hannaford35c91a62014-10-06 15:50:08 +0200162// ToNetworkUpdateMap casts a UpdateOpts struct to a map.
Jon Perritt04851d32014-10-14 02:07:13 -0500163func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
164 n := make(map[string]interface{})
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200165
Jon Perritt04851d32014-10-14 02:07:13 -0500166 if opts.AdminStateUp != nil {
167 n["admin_state_up"] = &opts.AdminStateUp
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200168 }
Jon Perritt04851d32014-10-14 02:07:13 -0500169 if opts.Name != "" {
170 n["name"] = opts.Name
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200171 }
Jon Perritt04851d32014-10-14 02:07:13 -0500172 if opts.Shared != nil {
173 n["shared"] = &opts.Shared
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200174 }
175
Jon Perritt04851d32014-10-14 02:07:13 -0500176 return map[string]interface{}{"network": n}, nil
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200177}
178
Jamie Hannaford686c4962014-09-23 10:46:20 +0200179// Update accepts a UpdateOpts struct and updates an existing network using the
180// values provided. For more information, see the Create function.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200181func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200182 var res UpdateResult
183
Jon Perritt04851d32014-10-14 02:07:13 -0500184 reqBody, err := opts.ToNetworkUpdateMap()
185 if err != nil {
186 res.Err = err
187 return res
188 }
Jamie Hannaford79475052014-09-15 17:08:06 +0200189
Jamie Hannaford79475052014-09-15 17:08:06 +0200190 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200191 _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200192 MoreHeaders: c.Provider.AuthenticatedHeaders(),
193 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200194 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200195 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200196 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200197
Jamie Hannafordd9036422014-09-23 17:50:24 +0200198 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200199}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200200
Jamie Hannaford686c4962014-09-23 10:46:20 +0200201// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200202func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
203 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200204 _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200205 MoreHeaders: c.Provider.AuthenticatedHeaders(),
206 OkCodes: []int{204},
207 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200208 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200209}