blob: f59273938209e89bd3c43514c68befa4c606cd75 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
4 "strconv"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
12type ListOpts struct {
13 Name string
14 EnableDHCP *bool
15 NetworkID string
16 TenantID string
17 IPVersion int
18 GatewayIP string
19 CIDR string
20 ID string
Jamie Hannafordf84171d2014-09-18 14:00:01 +020021 Limit int
22 Page string
23 PerPage string
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020024 SortKey string
25 SortDir string
Jamie Hannaford0708c002014-09-17 16:08:49 +020026}
27
28func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
29 // Build query parameters
30 q := make(map[string]string)
31 if opts.Name != "" {
32 q["name"] = opts.Name
33 }
34 if opts.EnableDHCP != nil {
35 q["enable_dhcp"] = strconv.FormatBool(*opts.EnableDHCP)
36 }
37 if opts.NetworkID != "" {
38 q["network_id"] = opts.NetworkID
39 }
40 if opts.TenantID != "" {
41 q["tenant_id"] = opts.TenantID
42 }
43 if opts.IPVersion != 0 {
44 q["ip_version"] = strconv.Itoa(opts.IPVersion)
45 }
46 if opts.GatewayIP != "" {
47 q["gateway_ip"] = opts.GatewayIP
48 }
49 if opts.CIDR != "" {
50 q["cidr"] = opts.CIDR
51 }
52 if opts.ID != "" {
53 q["id"] = opts.ID
54 }
Jamie Hannafordf84171d2014-09-18 14:00:01 +020055 if opts.Limit != 0 {
56 q["limit"] = strconv.Itoa(opts.Limit)
57 }
58 if opts.Page != "" {
59 q["page"] = opts.Page
60 }
61 if opts.PerPage != "" {
62 q["per_page"] = opts.PerPage
63 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020064 if opts.SortKey != "" {
65 q["sort_key"] = opts.SortKey
66 }
67 if opts.SortDir != "" {
68 q["sort_dir"] = opts.SortDir
69 }
Jamie Hannaford0708c002014-09-17 16:08:49 +020070
71 u := ListURL(c) + utils.BuildQuery(q)
72 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
73 return SubnetPage{pagination.LinkedPageBase(r)}
74 })
75}
76
77func Get(c *gophercloud.ServiceClient, id string) (*Subnet, error) {
78 var s Subnet
79 _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
80 MoreHeaders: c.Provider.AuthenticatedHeaders(),
81 Results: &struct {
82 Subnet *Subnet `json:"subnet"`
83 }{&s},
84 OkCodes: []int{200},
85 })
86 if err != nil {
87 return nil, err
88 }
89 return &s, nil
90}
Jamie Hannaford63631432014-09-18 11:40:09 +020091
92const (
93 IPv4 = 4
94 IPv6 = 6
95)
96
97type SubnetOpts struct {
98 // Required
99 NetworkID string
100 CIDR string
101 // Optional
102 Name string
103 TenantID string
104 AllocationPools []AllocationPool
105 GatewayIP string
106 IPVersion int
107 ID string
108 EnableDHCP *bool
109}
110
111// maybeString returns nil for empty strings and nil for empty.
112func maybeString(original string) *string {
113 if original != "" {
114 return &original
115 }
116 return nil
117}
118
119func Create(c *gophercloud.ServiceClient, opts SubnetOpts) (*Subnet, error) {
120 // Validate required options
121 if opts.NetworkID == "" {
122 return nil, ErrNetworkIDRequired
123 }
124 if opts.CIDR == "" {
125 return nil, ErrCIDRRequired
126 }
127 if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
128 return nil, ErrInvalidIPType
129 }
130
131 type subnet struct {
132 NetworkID string `json:"network_id"`
133 CIDR string `json:"cidr"`
134 Name *string `json:"name,omitempty"`
135 TenantID *string `json:"tenant_id,omitempty"`
136 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
137 GatewayIP *string `json:"gateway_ip,omitempty"`
138 IPVersion int `json:"ip_version,omitempty"`
139 ID *string `json:"id,omitempty"`
140 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
141 }
142 type request struct {
143 Subnet subnet `json:"subnet"`
144 }
145
146 reqBody := request{Subnet: subnet{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200147 NetworkID: opts.NetworkID,
148 CIDR: opts.CIDR,
149 Name: maybeString(opts.Name),
150 TenantID: maybeString(opts.TenantID),
151 GatewayIP: maybeString(opts.GatewayIP),
152 ID: maybeString(opts.ID),
153 EnableDHCP: opts.EnableDHCP,
Jamie Hannaford63631432014-09-18 11:40:09 +0200154 }}
155
Jamie Hannaford63631432014-09-18 11:40:09 +0200156 if opts.IPVersion != 0 {
157 reqBody.Subnet.IPVersion = opts.IPVersion
158 }
159
160 if len(opts.AllocationPools) != 0 {
161 reqBody.Subnet.AllocationPools = opts.AllocationPools
162 }
163
164 type response struct {
165 Subnet *Subnet `json:"subnet"`
166 }
167
168 var res response
169 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
170 MoreHeaders: c.Provider.AuthenticatedHeaders(),
171 ReqBody: &reqBody,
172 Results: &res,
173 OkCodes: []int{201},
174 })
175 if err != nil {
176 return nil, err
177 }
178
179 return res.Subnet, nil
180}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200181
182func Update(c *gophercloud.ServiceClient, id string, opts SubnetOpts) (*Subnet, error) {
183 if opts.CIDR != "" {
184 return nil, ErrCIDRNotUpdatable
185 }
186 if opts.IPVersion != 0 {
187 return nil, ErrIPVersionNotUpdatable
188 }
189
190 type subnet struct {
191 NetworkID string `json:"network_id,omitempty"`
192 Name *string `json:"name,omitempty"`
193 TenantID *string `json:"tenant_id,omitempty"`
194 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
195 GatewayIP *string `json:"gateway_ip,omitempty"`
196 ID *string `json:"id,omitempty"`
197 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
198 }
199 type request struct {
200 Subnet subnet `json:"subnet"`
201 }
202
203 reqBody := request{Subnet: subnet{
204 NetworkID: opts.NetworkID,
205 Name: maybeString(opts.Name),
206 TenantID: maybeString(opts.TenantID),
207 GatewayIP: maybeString(opts.GatewayIP),
208 ID: maybeString(opts.ID),
209 EnableDHCP: opts.EnableDHCP,
210 }}
211
212 if len(opts.AllocationPools) != 0 {
213 reqBody.Subnet.AllocationPools = opts.AllocationPools
214 }
215
216 type response struct {
217 Subnet *Subnet `json:"subnet"`
218 }
219
220 var res response
221 _, err := perigee.Request("PUT", UpdateURL(c, id), perigee.Options{
222 MoreHeaders: c.Provider.AuthenticatedHeaders(),
223 ReqBody: &reqBody,
224 Results: &res,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200225 OkCodes: []int{200, 201},
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200226 })
227 if err != nil {
228 return nil, err
229 }
230
231 return res.Subnet, nil
232}
233
234func Delete(c *gophercloud.ServiceClient, id string) error {
235 _, err := perigee.Request("DELETE", DeleteURL(c, id), perigee.Options{
236 MoreHeaders: c.Provider.AuthenticatedHeaders(),
237 OkCodes: []int{204},
238 })
239 return err
240}