blob: f4fbe97b8c91673c4f3e03b6e3748f1019e7a7d8 [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 Hannaford4721abc2014-09-16 16:29:04 +020019func ptrToStr(val *bool) string {
20 if *val == true {
21 return "true"
22 } else if *val == false {
23 return "false"
24 } else {
25 return ""
26 }
27}
28
Jamie Hannaford686c4962014-09-23 10:46:20 +020029// ListOpts allows the filtering and sorting of paginated collections through
30// the API. Filtering is achieved by passing in struct field values that map to
31// the network attributes you want to see returned. SortKey allows you to sort
32// by a particular network attribute. SortDir sets the direction, and is either
33// `asc' or `desc'. Marker and Limit are used for pagination.
34type ListOpts struct {
35 Status string
36 Name string
37 AdminStateUp *bool
38 TenantID string
39 Shared *bool
40 ID string
41 Marker string
42 Limit int
43 SortKey string
44 SortDir string
45}
46
47// List returns a Pager which allows you to iterate over a collection of
48// networks. It accepts a ListOpts struct, which allows you to filter and sort
49// the returned collection for greater efficiency.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020050func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020051 // Build query parameters
52 q := make(map[string]string)
53 if opts.Status != "" {
54 q["status"] = opts.Status
55 }
56 if opts.Name != "" {
57 q["name"] = opts.Name
58 }
59 if opts.AdminStateUp != nil {
60 q["admin_state_up"] = ptrToStr(opts.AdminStateUp)
61 }
62 if opts.TenantID != "" {
63 q["tenant_id"] = opts.TenantID
64 }
65 if opts.Shared != nil {
66 q["shared"] = ptrToStr(opts.Shared)
67 }
68 if opts.ID != "" {
69 q["id"] = opts.ID
70 }
Jamie Hannaford686c4962014-09-23 10:46:20 +020071 if opts.Marker != "" {
72 q["marker"] = opts.Marker
Jamie Hannaford4721abc2014-09-16 16:29:04 +020073 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020074 if opts.Limit != 0 {
75 q["limit"] = strconv.Itoa(opts.Limit)
76 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020077 if opts.SortKey != "" {
78 q["sort_key"] = opts.SortKey
79 }
80 if opts.SortDir != "" {
81 q["sort_dir"] = opts.SortDir
82 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020083
Jamie Hannaford686c4962014-09-23 10:46:20 +020084 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020085 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
86 return NetworkPage{pagination.LinkedPageBase(r)}
87 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020088}
89
Jamie Hannaford686c4962014-09-23 10:46:20 +020090// Get retrieves a specific network based on its unique ID.
Jamie Hannaford4721abc2014-09-16 16:29:04 +020091func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
92 var n Network
Jamie Hannaford686c4962014-09-23 10:46:20 +020093 _, err := perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020094 MoreHeaders: c.Provider.AuthenticatedHeaders(),
95 Results: &struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020096 Network *Network `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020097 }{&n},
98 OkCodes: []int{200},
99 })
100 if err != nil {
101 return nil, err
102 }
103 return &n, nil
104}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200105
Jamie Hannaford686c4962014-09-23 10:46:20 +0200106// CreateOpts represents the attributes used when creating a new network.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200107type CreateOpts networkOpts
108
Jamie Hannaford686c4962014-09-23 10:46:20 +0200109// Create accepts a CreateOpts struct and creates a new network using the values
110// provided. This operation does not actually require a request body, i.e. the
111// CreateOpts struct argument can be empty.
112//
113// The tenant ID that is contained in the URI is the tenant that creates the
114// network. An admin user, however, has the option of specifying another tenant
115// ID in the CreateOpts struct.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200116func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200117 // Define structures
118 type network struct {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200119 AdminStateUp bool `json:"admin_state_up,omitempty"`
120 Name string `json:"name,omitempty"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200121 Shared *bool `json:"shared,omitempty"`
122 TenantID *string `json:"tenant_id,omitempty"`
123 }
124 type request struct {
125 Network network `json:"network"`
126 }
127 type response struct {
Jamie Hannaford79475052014-09-15 17:08:06 +0200128 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200129 }
130
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200131 // Populate request body
132 reqBody := request{Network: network{
133 AdminStateUp: opts.AdminStateUp,
134 Name: opts.Name,
135 Shared: opts.Shared,
136 }}
137
138 if opts.TenantID != "" {
139 reqBody.Network.TenantID = &opts.TenantID
140 }
141
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200142 // Send request to API
143 var res response
Jamie Hannaford686c4962014-09-23 10:46:20 +0200144 _, err := perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200145 MoreHeaders: c.Provider.AuthenticatedHeaders(),
146 ReqBody: &reqBody,
147 Results: &res,
148 OkCodes: []int{201},
149 })
150 if err != nil {
151 return nil, err
152 }
153
154 return res.Network, nil
155}
Jamie Hannaford79475052014-09-15 17:08:06 +0200156
Jamie Hannaford686c4962014-09-23 10:46:20 +0200157// UpdateOpts represents the attributes used when updating an existing network.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200158type UpdateOpts networkOpts
159
Jamie Hannaford686c4962014-09-23 10:46:20 +0200160// Update accepts a UpdateOpts struct and updates an existing network using the
161// values provided. For more information, see the Create function.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200162func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) (*Network, error) {
Jamie Hannaford79475052014-09-15 17:08:06 +0200163 // Define structures
164 type network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200165 AdminStateUp bool `json:"admin_state_up"`
166 Name string `json:"name"`
167 Shared *bool `json:"shared,omitempty"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200168 }
169
170 type request struct {
171 Network network `json:"network"`
172 }
173 type response struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200174 Network *Network `json:"network"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200175 }
176
177 // Populate request body
178 reqBody := request{Network: network{
179 AdminStateUp: opts.AdminStateUp,
180 Name: opts.Name,
181 Shared: opts.Shared,
182 }}
183
Jamie Hannaford79475052014-09-15 17:08:06 +0200184 // Send request to API
185 var res response
Jamie Hannaford686c4962014-09-23 10:46:20 +0200186 _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200187 MoreHeaders: c.Provider.AuthenticatedHeaders(),
188 ReqBody: &reqBody,
189 Results: &res,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200190 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200191 })
192 if err != nil {
193 return nil, err
194 }
195
196 return res.Network, nil
197}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200198
Jamie Hannaford686c4962014-09-23 10:46:20 +0200199// Delete accepts a unique ID and deletes the network associated with it.
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200200func Delete(c *gophercloud.ServiceClient, networkID string) error {
Jamie Hannaford686c4962014-09-23 10:46:20 +0200201 _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200202 MoreHeaders: c.Provider.AuthenticatedHeaders(),
203 OkCodes: []int{204},
204 })
205 return err
206}