blob: 9b52c8dc090a5b43ee503bda0ebe33ef71ec809f [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 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 Hannaford7db63f22014-09-29 11:18:45 +020092type CreateOptsInt interface {
93 ToMap() map[string]map[string]interface{}
94 IsCreateOpts() bool
Jamie Hannaford9823bb62014-09-26 17:06:36 +020095}
96
Jamie Hannaford965ae702014-09-22 14:58:19 +020097type CreateOpts networkOpts
98
Jamie Hannaford7db63f22014-09-29 11:18:45 +020099func (o CreateOpts) ToMap() map[string]map[string]interface{} {
100 inner := make(map[string]interface{})
101
102 if o.AdminStateUp != nil {
103 inner["admin_state_up"] = &o.AdminStateUp
104 }
105 if o.Name != "" {
106 inner["name"] = o.Name
107 }
108 if o.Shared != nil {
109 inner["shared"] = &o.Shared
110 }
111 if o.TenantID != "" {
112 inner["tenant_id"] = o.TenantID
113 }
114
115 outer := make(map[string]map[string]interface{})
116 outer["network"] = inner
117
118 return outer
119}
120
121func (o CreateOpts) IsCreateOpts() bool { return true }
122
Jamie Hannaford686c4962014-09-23 10:46:20 +0200123// Create accepts a CreateOpts struct and creates a new network using the values
124// provided. This operation does not actually require a request body, i.e. the
125// CreateOpts struct argument can be empty.
126//
127// The tenant ID that is contained in the URI is the tenant that creates the
128// network. An admin user, however, has the option of specifying another tenant
129// ID in the CreateOpts struct.
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200130func Create(c *gophercloud.ServiceClient, opts CreateOptsInt) CreateResult {
131 var res CreateResult
132
133 if opts.IsCreateOpts() != true {
134 res.Err = fmt.Errorf("Must provide valid create opts")
135 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200136 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200137
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200138 reqBody := opts.ToMap()
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 Hannaford7db63f22014-09-29 11:18:45 +0200150type UpdateOptsInt interface {
151 ToMap() map[string]map[string]interface{}
152 IsUpdateOpts() bool
153}
154
Jamie Hannaford965ae702014-09-22 14:58:19 +0200155type UpdateOpts networkOpts
156
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200157func (o UpdateOpts) ToMap() map[string]map[string]interface{} {
158 inner := make(map[string]interface{})
159
160 if o.AdminStateUp != nil {
161 inner["admin_state_up"] = &o.AdminStateUp
162 }
163 if o.Name != "" {
164 inner["name"] = o.Name
165 }
166 if o.Shared != nil {
167 inner["shared"] = &o.Shared
168 }
169
170 outer := make(map[string]map[string]interface{})
171 outer["network"] = inner
172
173 return outer
174}
175
176func (o UpdateOpts) IsUpdateOpts() bool { return true }
177
Jamie Hannaford686c4962014-09-23 10:46:20 +0200178// Update accepts a UpdateOpts struct and updates an existing network using the
179// values provided. For more information, see the Create function.
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200180func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsInt) UpdateResult {
181 var res UpdateResult
182
183 if opts.IsUpdateOpts() != true {
184 res.Err = fmt.Errorf("Must provide valid update opts")
185 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200186 }
187
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200188 reqBody := opts.ToMap()
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}