blob: 7fed58efc1342997a396fb2fdd3f4fe45b6fdc83 [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 Hannaford686c4962014-09-23 10:46:20 +020092// CreateOpts represents the attributes used when creating a new network.
Jamie Hannaford965ae702014-09-22 14:58:19 +020093type CreateOpts networkOpts
94
Jamie Hannaford686c4962014-09-23 10:46:20 +020095// Create accepts a CreateOpts struct and creates a new network using the values
96// provided. This operation does not actually require a request body, i.e. the
97// CreateOpts struct argument can be empty.
98//
99// The tenant ID that is contained in the URI is the tenant that creates the
100// network. An admin user, however, has the option of specifying another tenant
101// ID in the CreateOpts struct.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200102func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200103 // Define structures
104 type network struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200105 AdminStateUp bool `json:"admin_state_up,omitempty"`
106 Name string `json:"name,omitempty"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200107 Shared *bool `json:"shared,omitempty"`
108 TenantID *string `json:"tenant_id,omitempty"`
109 }
110 type request struct {
111 Network network `json:"network"`
112 }
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200113
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200114 // Populate request body
115 reqBody := request{Network: network{
116 AdminStateUp: opts.AdminStateUp,
117 Name: opts.Name,
118 Shared: opts.Shared,
119 }}
120
121 if opts.TenantID != "" {
122 reqBody.Network.TenantID = &opts.TenantID
123 }
124
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200125 // Send request to API
Jamie Hannafordd9036422014-09-23 17:50:24 +0200126 var res CreateResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200127 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200128 MoreHeaders: c.Provider.AuthenticatedHeaders(),
129 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200130 Results: &res.Resp,
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200131 OkCodes: []int{201},
132 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200133 res.Err = err
134 return res
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200135}
Jamie Hannaford79475052014-09-15 17:08:06 +0200136
Jamie Hannaford686c4962014-09-23 10:46:20 +0200137// UpdateOpts represents the attributes used when updating an existing network.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200138type UpdateOpts networkOpts
139
Jamie Hannaford686c4962014-09-23 10:46:20 +0200140// Update accepts a UpdateOpts struct and updates an existing network using the
141// values provided. For more information, see the Create function.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200142func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) UpdateResult {
Jamie Hannaford79475052014-09-15 17:08:06 +0200143 // Define structures
144 type network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200145 AdminStateUp bool `json:"admin_state_up"`
146 Name string `json:"name"`
147 Shared *bool `json:"shared,omitempty"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200148 }
149
150 type request struct {
151 Network network `json:"network"`
152 }
Jamie Hannaford79475052014-09-15 17:08:06 +0200153
154 // Populate request body
155 reqBody := request{Network: network{
156 AdminStateUp: opts.AdminStateUp,
157 Name: opts.Name,
158 Shared: opts.Shared,
159 }}
160
Jamie Hannaford79475052014-09-15 17:08:06 +0200161 // Send request to API
Jamie Hannafordd9036422014-09-23 17:50:24 +0200162 var res UpdateResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200163 _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200164 MoreHeaders: c.Provider.AuthenticatedHeaders(),
165 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200166 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200167 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200168 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200169 res.Err = err
170 return res
Jamie Hannaford79475052014-09-15 17:08:06 +0200171}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200172
Jamie Hannaford686c4962014-09-23 10:46:20 +0200173// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200174func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
175 var res DeleteResult
Jamie Hannaford686c4962014-09-23 10:46:20 +0200176 _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200177 MoreHeaders: c.Provider.AuthenticatedHeaders(),
178 OkCodes: []int{204},
179 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200180 res.Err = err
181 return res
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200182}