blob: 6088b366e1d584383f19f8992861ca3431ea521e [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02006 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +02007)
8
Jamie Hannaford965ae702014-09-22 14:58:19 +02009type networkOpts struct {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020010 AdminStateUp *bool
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020011 Name string
12 Shared *bool
13 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020014}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020015
Jamie Hannaford686c4962014-09-23 10:46:20 +020016// ListOpts allows the filtering and sorting of paginated collections through
17// the API. Filtering is achieved by passing in struct field values that map to
18// the network attributes you want to see returned. SortKey allows you to sort
19// by a particular network attribute. SortDir sets the direction, and is either
20// `asc' or `desc'. Marker and Limit are used for pagination.
21type ListOpts struct {
Jamie Hannaford92523e32014-10-02 11:08:36 +020022 Status string `q:"status"`
23 Name string `q:"name"`
24 AdminStateUp *bool `q:"admin_state_up"`
25 TenantID string `q:"tenant_id"`
26 Shared *bool `q:"shared"`
27 ID string `q:"id"`
28 Marker string `q:"marker"`
29 Limit int `q:"limit"`
30 SortKey string `q:"sort_key"`
31 SortDir string `q:"sort_dir"`
Jamie Hannaford686c4962014-09-23 10:46:20 +020032}
33
34// List returns a Pager which allows you to iterate over a collection of
35// networks. It accepts a ListOpts struct, which allows you to filter and sort
36// the returned collection for greater efficiency.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020037func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020038 // Build query parameters
Jamie Hannaford92523e32014-10-02 11:08:36 +020039 q, err := gophercloud.BuildQueryString(&opts)
40 if err != nil {
41 return pagination.Pager{Err: err}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042 }
Jamie Hannaford92523e32014-10-02 11:08:36 +020043 u := listURL(c) + q.String()
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020044 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040045 return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020046 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020047}
48
Jamie Hannaford686c4962014-09-23 10:46:20 +020049// Get retrieves a specific network based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020050func Get(c *gophercloud.ServiceClient, id string) GetResult {
51 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020052 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020053 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020054 Results: &res.Resp,
55 OkCodes: []int{200},
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020056 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020057 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020058}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020059
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020060type CreateOptsBuilder interface {
61 ToNetworkCreateMap() map[string]map[string]interface{}
Jamie Hannaford9823bb62014-09-26 17:06:36 +020062}
63
Jamie Hannaford965ae702014-09-22 14:58:19 +020064type CreateOpts networkOpts
65
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020066func (o CreateOpts) ToNetworkCreateMap() map[string]map[string]interface{} {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020067 inner := make(map[string]interface{})
68
69 if o.AdminStateUp != nil {
70 inner["admin_state_up"] = &o.AdminStateUp
71 }
72 if o.Name != "" {
73 inner["name"] = o.Name
74 }
75 if o.Shared != nil {
76 inner["shared"] = &o.Shared
77 }
78 if o.TenantID != "" {
79 inner["tenant_id"] = o.TenantID
80 }
81
82 outer := make(map[string]map[string]interface{})
83 outer["network"] = inner
84
85 return outer
86}
87
Jamie Hannaford686c4962014-09-23 10:46:20 +020088// Create accepts a CreateOpts struct and creates a new network using the values
89// provided. This operation does not actually require a request body, i.e. the
90// CreateOpts struct argument can be empty.
91//
92// The tenant ID that is contained in the URI is the tenant that creates the
93// network. An admin user, however, has the option of specifying another tenant
94// ID in the CreateOpts struct.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020095func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +020096 var res CreateResult
97
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +020098 reqBody := opts.ToNetworkCreateMap()
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020099
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200100 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200101 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200102 MoreHeaders: c.Provider.AuthenticatedHeaders(),
103 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200104 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200105 OkCodes: []int{201},
106 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200107 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200108}
Jamie Hannaford79475052014-09-15 17:08:06 +0200109
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200110type UpdateOptsBuilder interface {
111 ToNetworkUpdateMap() map[string]map[string]interface{}
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200112}
113
Jamie Hannaford965ae702014-09-22 14:58:19 +0200114type UpdateOpts networkOpts
115
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200116func (o UpdateOpts) ToNetworkUpdateMap() map[string]map[string]interface{} {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200117 inner := make(map[string]interface{})
118
119 if o.AdminStateUp != nil {
120 inner["admin_state_up"] = &o.AdminStateUp
121 }
122 if o.Name != "" {
123 inner["name"] = o.Name
124 }
125 if o.Shared != nil {
126 inner["shared"] = &o.Shared
127 }
128
129 outer := make(map[string]map[string]interface{})
130 outer["network"] = inner
131
132 return outer
133}
134
Jamie Hannaford686c4962014-09-23 10:46:20 +0200135// Update accepts a UpdateOpts struct and updates an existing network using the
136// values provided. For more information, see the Create function.
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200137func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult {
Jamie Hannaford7db63f22014-09-29 11:18:45 +0200138 var res UpdateResult
139
Jamie Hannaforde3bb3f62014-10-06 09:40:27 +0200140 reqBody := opts.ToNetworkUpdateMap()
Jamie Hannaford79475052014-09-15 17:08:06 +0200141
Jamie Hannaford79475052014-09-15 17:08:06 +0200142 // Send request to API
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200143 _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200144 MoreHeaders: c.Provider.AuthenticatedHeaders(),
145 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200146 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200147 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200148 })
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200149
Jamie Hannafordd9036422014-09-23 17:50:24 +0200150 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200151}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200152
Jamie Hannaford686c4962014-09-23 10:46:20 +0200153// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200154func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
155 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200156 _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200157 MoreHeaders: c.Provider.AuthenticatedHeaders(),
158 OkCodes: []int{204},
159 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200160 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200161}