blob: 8b2829111d2cededa9834c92614519b34d3c795b [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannaford7db63f22014-09-29 11:18:45 +02004 "fmt"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02005
Jamie Hannaford01e14922014-09-11 15:23:49 +02006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +02009)
10
Jamie Hannaford965ae702014-09-22 14:58:19 +020011type networkOpts struct {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020012 AdminStateUp *bool
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020013 Name string
14 Shared *bool
15 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020016}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020017
Jamie Hannaford686c4962014-09-23 10:46:20 +020018// ListOpts allows the filtering and sorting of paginated collections through
19// the API. Filtering is achieved by passing in struct field values that map to
20// the network attributes you want to see returned. SortKey allows you to sort
21// by a particular network attribute. SortDir sets the direction, and is either
22// `asc' or `desc'. Marker and Limit are used for pagination.
23type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020024 Status string `q:"status"`
25 Name string `q:"name"`
26 AdminStateUp *bool `q:"admin_state_up"`
27 TenantID string `q:"tenant_id"`
28 Shared *bool `q:"shared"`
29 ID string `q:"id"`
30 Marker string `q:"marker"`
31 Limit int `q:"limit"`
32 SortKey string `q:"sort_key"`
33 SortDir string `q:"sort_dir"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020034}
35
36// List returns a Pager which allows you to iterate over a collection of
37// networks. It accepts a ListOpts struct, which allows you to filter and sort
38// the returned collection for greater efficiency.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020039func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020040 // Build query parameters
Jamie Hannaford92523e32014-10-02 11:08:36 +020041 q, err := gophercloud.BuildQueryString(&opts)
42 if err != nil {
43 return pagination.Pager{Err: err}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020044 }
Jamie Hannaford92523e32014-10-02 11:08:36 +020045 u := listURL(c) + q.String()
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020046 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040047 return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020048 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020049}
50
Jamie Hannaford686c4962014-09-23 10:46:20 +020051// Get retrieves a specific network based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020052func Get(c *gophercloud.ServiceClient, id string) GetResult {
53 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020054 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020055 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020056 Results: &res.Resp,
57 OkCodes: []int{200},
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020058 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020059 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020060}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020061
Jamie Hannaford7db63f22014-09-29 11:18:45 +020062type CreateOptsInt interface {
63 ToMap() map[string]map[string]interface{}
64 IsCreateOpts() bool
Jamie Hannaford9823bb62014-09-26 17:06:36 +020065}
66
Jamie Hannaford965ae702014-09-22 14:58:19 +020067type CreateOpts networkOpts
68
Jamie Hannaford7db63f22014-09-29 11:18:45 +020069func (o CreateOpts) ToMap() map[string]map[string]interface{} {
70 inner := make(map[string]interface{})
71
72 if o.AdminStateUp != nil {
73 inner["admin_state_up"] = &o.AdminStateUp
74 }
75 if o.Name != "" {
76 inner["name"] = o.Name
77 }
78 if o.Shared != nil {
79 inner["shared"] = &o.Shared
80 }
81 if o.TenantID != "" {
82 inner["tenant_id"] = o.TenantID
83 }
84
85 outer := make(map[string]map[string]interface{})
86 outer["network"] = inner
87
88 return outer
89}
90
91func (o CreateOpts) IsCreateOpts() bool { return true }
92
Jamie Hannaford686c4962014-09-23 10:46:20 +020093// Create accepts a CreateOpts struct and creates a new network using the values
94// provided. This operation does not actually require a request body, i.e. the
95// CreateOpts struct argument can be empty.
96//
97// The tenant ID that is contained in the URI is the tenant that creates the
98// network. An admin user, however, has the option of specifying another tenant
99// ID in the CreateOpts struct.
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200100func Create(c *gophercloud.ServiceClient, opts CreateOptsInt) CreateResult {
101 var res CreateResult
102
103 if opts.IsCreateOpts() != true {
104 res.Err = fmt.Errorf("Must provide valid create opts")
105 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200106 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200107
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200108 reqBody := opts.ToMap()
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200109
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200110 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200111 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200112 MoreHeaders: c.Provider.AuthenticatedHeaders(),
113 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200114 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200115 OkCodes: []int{201},
116 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200117 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200118}
Jamie Hannaford79475052014-09-15 17:08:06 +0200119
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200120type UpdateOptsInt interface {
121 ToMap() map[string]map[string]interface{}
122 IsUpdateOpts() bool
123}
124
Jamie Hannaford965ae702014-09-22 14:58:19 +0200125type UpdateOpts networkOpts
126
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200127func (o UpdateOpts) ToMap() map[string]map[string]interface{} {
128 inner := make(map[string]interface{})
129
130 if o.AdminStateUp != nil {
131 inner["admin_state_up"] = &o.AdminStateUp
132 }
133 if o.Name != "" {
134 inner["name"] = o.Name
135 }
136 if o.Shared != nil {
137 inner["shared"] = &o.Shared
138 }
139
140 outer := make(map[string]map[string]interface{})
141 outer["network"] = inner
142
143 return outer
144}
145
146func (o UpdateOpts) IsUpdateOpts() bool { return true }
147
Jamie Hannaford686c4962014-09-23 10:46:20 +0200148// Update accepts a UpdateOpts struct and updates an existing network using the
149// values provided. For more information, see the Create function.
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200150func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsInt) UpdateResult {
151 var res UpdateResult
152
153 if opts.IsUpdateOpts() != true {
154 res.Err = fmt.Errorf("Must provide valid update opts")
155 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200156 }
157
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200158 reqBody := opts.ToMap()
Jamie Hannaford79475052014-09-15 17:08:06 +0200159
Jamie Hannaford79475052014-09-15 17:08:06 +0200160 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200161 _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200162 MoreHeaders: c.Provider.AuthenticatedHeaders(),
163 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200164 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200165 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200166 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200167
Jamie Hannafordd9036422014-09-23 17:50:24 +0200168 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200169}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200170
Jamie Hannaford686c4962014-09-23 10:46:20 +0200171// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200172func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
173 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200174 _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200175 MoreHeaders: c.Provider.AuthenticatedHeaders(),
176 OkCodes: []int{204},
177 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200178 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200179}