blob: 93641d9fd3ead2173414cd032d91fa41472fdf80 [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{
127 NetworkID: opts.NetworkID,
128 CIDR: opts.CIDR,
129 }}
130
131 reqBody.Subnet.Name = maybeString(opts.Name)
132 reqBody.Subnet.TenantID = maybeString(opts.TenantID)
133 reqBody.Subnet.GatewayIP = maybeString(opts.GatewayIP)
134 reqBody.Subnet.ID = maybeString(opts.ID)
135 reqBody.Subnet.EnableDHCP = opts.EnableDHCP
136
137 if opts.IPVersion != 0 {
138 reqBody.Subnet.IPVersion = opts.IPVersion
139 }
140
141 if len(opts.AllocationPools) != 0 {
142 reqBody.Subnet.AllocationPools = opts.AllocationPools
143 }
144
145 type response struct {
146 Subnet *Subnet `json:"subnet"`
147 }
148
149 var res response
150 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
151 MoreHeaders: c.Provider.AuthenticatedHeaders(),
152 ReqBody: &reqBody,
153 Results: &res,
154 OkCodes: []int{201},
155 })
156 if err != nil {
157 return nil, err
158 }
159
160 return res.Subnet, nil
161}