blob: 180914fa6a95f9af9f243a62f73b81bb7b5f4278 [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 "strconv"
6
Jamie Hannaford01e14922014-09-11 15:23:49 +02007 "github.com/racker/perigee"
8 "github.com/rackspace/gophercloud"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02009 "github.com/rackspace/gophercloud/openstack/utils"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020010 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +020011)
12
Jamie Hannaford965ae702014-09-22 14:58:19 +020013type networkOpts struct {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020014 AdminStateUp *bool
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020015 Name string
16 Shared *bool
17 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020018}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020019
Jamie Hannaford686c4962014-09-23 10:46:20 +020020// ListOpts allows the filtering and sorting of paginated collections through
21// the API. Filtering is achieved by passing in struct field values that map to
22// the network attributes you want to see returned. SortKey allows you to sort
23// by a particular network attribute. SortDir sets the direction, and is either
24// `asc' or `desc'. Marker and Limit are used for pagination.
25type ListOpts struct {
26 Status string
27 Name string
28 AdminStateUp *bool
29 TenantID string
30 Shared *bool
31 ID string
32 Marker string
33 Limit int
34 SortKey string
35 SortDir string
36}
37
38// 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.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020041func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042 // Build query parameters
43 q := make(map[string]string)
44 if opts.Status != "" {
45 q["status"] = opts.Status
46 }
47 if opts.Name != "" {
48 q["name"] = opts.Name
49 }
50 if opts.AdminStateUp != nil {
Jamie Hannaford6abf9282014-09-24 10:54:13 +020051 q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020052 }
53 if opts.TenantID != "" {
54 q["tenant_id"] = opts.TenantID
55 }
56 if opts.Shared != nil {
Jamie Hannaford6abf9282014-09-24 10:54:13 +020057 q["shared"] = strconv.FormatBool(*opts.Shared)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020058 }
59 if opts.ID != "" {
60 q["id"] = opts.ID
61 }
Jamie Hannaford686c4962014-09-23 10:46:20 +020062 if opts.Marker != "" {
63 q["marker"] = opts.Marker
Jamie Hannaford4721abc2014-09-16 16:29:04 +020064 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020065 if opts.Limit != 0 {
66 q["limit"] = strconv.Itoa(opts.Limit)
67 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020068 if opts.SortKey != "" {
69 q["sort_key"] = opts.SortKey
70 }
71 if opts.SortDir != "" {
72 q["sort_dir"] = opts.SortDir
73 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020074
Jamie Hannaford686c4962014-09-23 10:46:20 +020075 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020076 return pagination.NewPager(c, u, 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 Hannaford686c4962014-09-23 10:46:20 +020084 _, 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 res.Err = err
90 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020091}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020092
Jamie Hannaford7db63f22014-09-29 11:18:45 +020093type CreateOptsInt interface {
94 ToMap() map[string]map[string]interface{}
95 IsCreateOpts() bool
Jamie Hannaford9823bb62014-09-26 17:06:36 +020096}
97
Jamie Hannaford965ae702014-09-22 14:58:19 +020098type CreateOpts networkOpts
99
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200100func (o CreateOpts) ToMap() map[string]map[string]interface{} {
101 inner := make(map[string]interface{})
102
103 if o.AdminStateUp != nil {
104 inner["admin_state_up"] = &o.AdminStateUp
105 }
106 if o.Name != "" {
107 inner["name"] = o.Name
108 }
109 if o.Shared != nil {
110 inner["shared"] = &o.Shared
111 }
112 if o.TenantID != "" {
113 inner["tenant_id"] = o.TenantID
114 }
115
116 outer := make(map[string]map[string]interface{})
117 outer["network"] = inner
118
119 return outer
120}
121
122func (o CreateOpts) IsCreateOpts() bool { return true }
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 Hannaford7db63f22014-09-29 11:18:45 +0200131func Create(c *gophercloud.ServiceClient, opts CreateOptsInt) CreateResult {
132 var res CreateResult
133
134 if opts.IsCreateOpts() != true {
135 res.Err = fmt.Errorf("Must provide valid create opts")
136 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200137 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200138
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200139 reqBody := opts.ToMap()
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200140
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200141 // Send request to API
Jamie Hannaford686c4962014-09-23 10:46:20 +0200142 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200143 MoreHeaders: c.Provider.AuthenticatedHeaders(),
144 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200145 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200146 OkCodes: []int{201},
147 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200148 res.Err = err
149 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200150}
Jamie Hannaford79475052014-09-15 17:08:06 +0200151
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200152type UpdateOptsInt interface {
153 ToMap() map[string]map[string]interface{}
154 IsUpdateOpts() bool
155}
156
Jamie Hannaford965ae702014-09-22 14:58:19 +0200157type UpdateOpts networkOpts
158
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200159func (o UpdateOpts) ToMap() map[string]map[string]interface{} {
160 inner := make(map[string]interface{})
161
162 if o.AdminStateUp != nil {
163 inner["admin_state_up"] = &o.AdminStateUp
164 }
165 if o.Name != "" {
166 inner["name"] = o.Name
167 }
168 if o.Shared != nil {
169 inner["shared"] = &o.Shared
170 }
171
172 outer := make(map[string]map[string]interface{})
173 outer["network"] = inner
174
175 return outer
176}
177
178func (o UpdateOpts) IsUpdateOpts() bool { return true }
179
Jamie Hannaford686c4962014-09-23 10:46:20 +0200180// Update accepts a UpdateOpts struct and updates an existing network using the
181// values provided. For more information, see the Create function.
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200182func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsInt) UpdateResult {
183 var res UpdateResult
184
185 if opts.IsUpdateOpts() != true {
186 res.Err = fmt.Errorf("Must provide valid update opts")
187 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200188 }
189
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200190 reqBody := opts.ToMap()
Jamie Hannaford79475052014-09-15 17:08:06 +0200191
Jamie Hannaford79475052014-09-15 17:08:06 +0200192 // Send request to API
Jamie Hannaford686c4962014-09-23 10:46:20 +0200193 _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200194 MoreHeaders: c.Provider.AuthenticatedHeaders(),
195 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200196 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200197 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200198 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200199 res.Err = err
200 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200201}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200202
Jamie Hannaford686c4962014-09-23 10:46:20 +0200203// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200204func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
205 var res DeleteResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200206 _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200207 MoreHeaders: c.Provider.AuthenticatedHeaders(),
208 OkCodes: []int{204},
209 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200210 res.Err = err
211 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200212}