blob: 3a1613e8f61ccbaf868e1a65e78959757edcd1e8 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannaford4721abc2014-09-16 16:29:04 +02004 "strconv"
5
Jamie Hannaford01e14922014-09-11 15:23:49 +02006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02008 "github.com/rackspace/gophercloud/openstack/utils"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +020010)
11
Jamie Hannaford965ae702014-09-22 14:58:19 +020012type networkOpts struct {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020013 AdminStateUp bool
14 Name string
15 Shared *bool
16 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020017}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020018
Jamie Hannaford686c4962014-09-23 10:46:20 +020019// ListOpts allows the filtering and sorting of paginated collections through
20// the API. Filtering is achieved by passing in struct field values that map to
21// the network attributes you want to see returned. SortKey allows you to sort
22// by a particular network attribute. SortDir sets the direction, and is either
23// `asc' or `desc'. Marker and Limit are used for pagination.
24type ListOpts struct {
25 Status string
26 Name string
27 AdminStateUp *bool
28 TenantID string
29 Shared *bool
30 ID string
31 Marker string
32 Limit int
33 SortKey string
34 SortDir string
35}
36
37// List returns a Pager which allows you to iterate over a collection of
38// networks. It accepts a ListOpts struct, which allows you to filter and sort
39// the returned collection for greater efficiency.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020040func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020041 // Build query parameters
42 q := make(map[string]string)
43 if opts.Status != "" {
44 q["status"] = opts.Status
45 }
46 if opts.Name != "" {
47 q["name"] = opts.Name
48 }
49 if opts.AdminStateUp != nil {
Jamie Hannaford6abf9282014-09-24 10:54:13 +020050 q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020051 }
52 if opts.TenantID != "" {
53 q["tenant_id"] = opts.TenantID
54 }
55 if opts.Shared != nil {
Jamie Hannaford6abf9282014-09-24 10:54:13 +020056 q["shared"] = strconv.FormatBool(*opts.Shared)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020057 }
58 if opts.ID != "" {
59 q["id"] = opts.ID
60 }
Jamie Hannaford686c4962014-09-23 10:46:20 +020061 if opts.Marker != "" {
62 q["marker"] = opts.Marker
Jamie Hannaford4721abc2014-09-16 16:29:04 +020063 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020064 if opts.Limit != 0 {
65 q["limit"] = strconv.Itoa(opts.Limit)
66 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020067 if opts.SortKey != "" {
68 q["sort_key"] = opts.SortKey
69 }
70 if opts.SortDir != "" {
71 q["sort_dir"] = opts.SortDir
72 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020073
Jamie Hannaford686c4962014-09-23 10:46:20 +020074 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020075 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040076 return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020077 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020078}
79
Jamie Hannaford686c4962014-09-23 10:46:20 +020080// Get retrieves a specific network based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020081func Get(c *gophercloud.ServiceClient, id string) GetResult {
82 var res GetResult
Jamie Hannaford686c4962014-09-23 10:46:20 +020083 _, err := perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020084 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020085 Results: &res.Resp,
86 OkCodes: []int{200},
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020087 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020088 res.Err = err
89 return res
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020090}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020091
Jamie Hannaford9823bb62014-09-26 17:06:36 +020092type CreateOpts interface {
93 ToMap() map[string]interface{}
94}
95
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// CreateOpts represents the attributes used when creating a new network.
Jamie Hannaford965ae702014-09-22 14:58:19 +020097type CreateOpts networkOpts
98
Jamie Hannaford686c4962014-09-23 10:46:20 +020099// Create accepts a CreateOpts struct and creates a new network using the values
100// provided. This operation does not actually require a request body, i.e. the
101// CreateOpts struct argument can be empty.
102//
103// The tenant ID that is contained in the URI is the tenant that creates the
104// network. An admin user, however, has the option of specifying another tenant
105// ID in the CreateOpts struct.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200106func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200107 // Define structures
108 type network struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200109 AdminStateUp bool `json:"admin_state_up,omitempty"`
110 Name string `json:"name,omitempty"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200111 Shared *bool `json:"shared,omitempty"`
112 TenantID *string `json:"tenant_id,omitempty"`
113 }
114 type request struct {
115 Network network `json:"network"`
116 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200117
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200118 // Populate request body
119 reqBody := request{Network: network{
120 AdminStateUp: opts.AdminStateUp,
121 Name: opts.Name,
122 Shared: opts.Shared,
123 }}
124
125 if opts.TenantID != "" {
126 reqBody.Network.TenantID = &opts.TenantID
127 }
128
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200129 // Send request to API
Jamie Hannafordd9036422014-09-23 17:50:24 +0200130 var res CreateResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200131 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200132 MoreHeaders: c.Provider.AuthenticatedHeaders(),
133 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200134 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200135 OkCodes: []int{201},
136 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200137 res.Err = err
138 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200139}
Jamie Hannaford79475052014-09-15 17:08:06 +0200140
Jamie Hannaford686c4962014-09-23 10:46:20 +0200141// UpdateOpts represents the attributes used when updating an existing network.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200142type UpdateOpts networkOpts
143
Jamie Hannaford686c4962014-09-23 10:46:20 +0200144// Update accepts a UpdateOpts struct and updates an existing network using the
145// values provided. For more information, see the Create function.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200146func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) UpdateResult {
Jamie Hannaford79475052014-09-15 17:08:06 +0200147 // Define structures
148 type network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200149 AdminStateUp bool `json:"admin_state_up"`
150 Name string `json:"name"`
151 Shared *bool `json:"shared,omitempty"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200152 }
153
154 type request struct {
155 Network network `json:"network"`
156 }
Jamie Hannaford79475052014-09-15 17:08:06 +0200157
158 // Populate request body
159 reqBody := request{Network: network{
160 AdminStateUp: opts.AdminStateUp,
161 Name: opts.Name,
162 Shared: opts.Shared,
163 }}
164
Jamie Hannaford79475052014-09-15 17:08:06 +0200165 // Send request to API
Jamie Hannafordd9036422014-09-23 17:50:24 +0200166 var res UpdateResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200167 _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200168 MoreHeaders: c.Provider.AuthenticatedHeaders(),
169 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200170 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200171 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200172 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200173 res.Err = err
174 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200175}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200176
Jamie Hannaford686c4962014-09-23 10:46:20 +0200177// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200178func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
179 var res DeleteResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200180 _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200181 MoreHeaders: c.Provider.AuthenticatedHeaders(),
182 OkCodes: []int{204},
183 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200184 res.Err = err
185 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200186}