blob: f10bf18651f76693556cfcbf954f81a96fdc8043 [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
21}
22
23func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
24 // Build query parameters
25 q := make(map[string]string)
26 if opts.Name != "" {
27 q["name"] = opts.Name
28 }
29 if opts.EnableDHCP != nil {
30 q["enable_dhcp"] = strconv.FormatBool(*opts.EnableDHCP)
31 }
32 if opts.NetworkID != "" {
33 q["network_id"] = opts.NetworkID
34 }
35 if opts.TenantID != "" {
36 q["tenant_id"] = opts.TenantID
37 }
38 if opts.IPVersion != 0 {
39 q["ip_version"] = strconv.Itoa(opts.IPVersion)
40 }
41 if opts.GatewayIP != "" {
42 q["gateway_ip"] = opts.GatewayIP
43 }
44 if opts.CIDR != "" {
45 q["cidr"] = opts.CIDR
46 }
47 if opts.ID != "" {
48 q["id"] = opts.ID
49 }
50
51 u := ListURL(c) + utils.BuildQuery(q)
52 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
53 return SubnetPage{pagination.LinkedPageBase(r)}
54 })
55}
56
57func Get(c *gophercloud.ServiceClient, id string) (*Subnet, error) {
58 var s Subnet
59 _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
60 MoreHeaders: c.Provider.AuthenticatedHeaders(),
61 Results: &struct {
62 Subnet *Subnet `json:"subnet"`
63 }{&s},
64 OkCodes: []int{200},
65 })
66 if err != nil {
67 return nil, err
68 }
69 return &s, nil
70}
Jamie Hannaford63631432014-09-18 11:40:09 +020071
72const (
73 IPv4 = 4
74 IPv6 = 6
75)
76
77type SubnetOpts struct {
78 // Required
79 NetworkID string
80 CIDR string
81 // Optional
82 Name string
83 TenantID string
84 AllocationPools []AllocationPool
85 GatewayIP string
86 IPVersion int
87 ID string
88 EnableDHCP *bool
89}
90
91// maybeString returns nil for empty strings and nil for empty.
92func maybeString(original string) *string {
93 if original != "" {
94 return &original
95 }
96 return nil
97}
98
99func Create(c *gophercloud.ServiceClient, opts SubnetOpts) (*Subnet, error) {
100 // Validate required options
101 if opts.NetworkID == "" {
102 return nil, ErrNetworkIDRequired
103 }
104 if opts.CIDR == "" {
105 return nil, ErrCIDRRequired
106 }
107 if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
108 return nil, ErrInvalidIPType
109 }
110
111 type subnet struct {
112 NetworkID string `json:"network_id"`
113 CIDR string `json:"cidr"`
114 Name *string `json:"name,omitempty"`
115 TenantID *string `json:"tenant_id,omitempty"`
116 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
117 GatewayIP *string `json:"gateway_ip,omitempty"`
118 IPVersion int `json:"ip_version,omitempty"`
119 ID *string `json:"id,omitempty"`
120 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
121 }
122 type request struct {
123 Subnet subnet `json:"subnet"`
124 }
125
126 reqBody := request{Subnet: subnet{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200127 NetworkID: opts.NetworkID,
128 CIDR: opts.CIDR,
129 Name: maybeString(opts.Name),
130 TenantID: maybeString(opts.TenantID),
131 GatewayIP: maybeString(opts.GatewayIP),
132 ID: maybeString(opts.ID),
133 EnableDHCP: opts.EnableDHCP,
Jamie Hannaford63631432014-09-18 11:40:09 +0200134 }}
135
Jamie Hannaford63631432014-09-18 11:40:09 +0200136 if opts.IPVersion != 0 {
137 reqBody.Subnet.IPVersion = opts.IPVersion
138 }
139
140 if len(opts.AllocationPools) != 0 {
141 reqBody.Subnet.AllocationPools = opts.AllocationPools
142 }
143
144 type response struct {
145 Subnet *Subnet `json:"subnet"`
146 }
147
148 var res response
149 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
150 MoreHeaders: c.Provider.AuthenticatedHeaders(),
151 ReqBody: &reqBody,
152 Results: &res,
153 OkCodes: []int{201},
154 })
155 if err != nil {
156 return nil, err
157 }
158
159 return res.Subnet, nil
160}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200161
162func Update(c *gophercloud.ServiceClient, id string, opts SubnetOpts) (*Subnet, error) {
163 if opts.CIDR != "" {
164 return nil, ErrCIDRNotUpdatable
165 }
166 if opts.IPVersion != 0 {
167 return nil, ErrIPVersionNotUpdatable
168 }
169
170 type subnet struct {
171 NetworkID string `json:"network_id,omitempty"`
172 Name *string `json:"name,omitempty"`
173 TenantID *string `json:"tenant_id,omitempty"`
174 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
175 GatewayIP *string `json:"gateway_ip,omitempty"`
176 ID *string `json:"id,omitempty"`
177 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
178 }
179 type request struct {
180 Subnet subnet `json:"subnet"`
181 }
182
183 reqBody := request{Subnet: subnet{
184 NetworkID: opts.NetworkID,
185 Name: maybeString(opts.Name),
186 TenantID: maybeString(opts.TenantID),
187 GatewayIP: maybeString(opts.GatewayIP),
188 ID: maybeString(opts.ID),
189 EnableDHCP: opts.EnableDHCP,
190 }}
191
192 if len(opts.AllocationPools) != 0 {
193 reqBody.Subnet.AllocationPools = opts.AllocationPools
194 }
195
196 type response struct {
197 Subnet *Subnet `json:"subnet"`
198 }
199
200 var res response
201 _, err := perigee.Request("PUT", UpdateURL(c, id), perigee.Options{
202 MoreHeaders: c.Provider.AuthenticatedHeaders(),
203 ReqBody: &reqBody,
204 Results: &res,
205 OkCodes: []int{201},
206 })
207 if err != nil {
208 return nil, err
209 }
210
211 return res.Subnet, nil
212}
213
214func Delete(c *gophercloud.ServiceClient, id string) error {
215 _, err := perigee.Request("DELETE", DeleteURL(c, id), perigee.Options{
216 MoreHeaders: c.Provider.AuthenticatedHeaders(),
217 OkCodes: []int{204},
218 })
219 return err
220}