blob: ee5631de5fbf50ee63107c323eb34fac0e81738e [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
Jamie Hannaford0708c002014-09-17 16:08:49 +02004 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/openstack/utils"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford686c4962014-09-23 10:46:20 +020010// ListOpts allows the filtering and sorting of paginated collections through
11// the API. Filtering is achieved by passing in struct field values that map to
12// the subnet attributes you want to see returned. SortKey allows you to sort
13// by a particular subnet attribute. SortDir sets the direction, and is either
14// `asc' or `desc'. Marker and Limit are used for pagination.
Jamie Hannaford0708c002014-09-17 16:08:49 +020015type ListOpts struct {
Jamie Hannaford5c6e9962014-10-02 10:34:14 +020016 Name string `q:"name"`
17 EnableDHCP *bool `q:"enable_dhcp"`
18 NetworkID string `q:"network_id"`
19 TenantID string `q:"tenant_id"`
20 IPVersion int `q:"ip_version"`
21 GatewayIP string `q:"gateway_ip"`
22 CIDR string `q:"cidr"`
23 ID string `q:"id"`
24 Limit int `q:"limit"`
25 Marker string `q:"marker"`
26 SortKey string `q:"sort_key"`
27 SortDir string `q:"sort_dir"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020028}
29
Jamie Hannaford686c4962014-09-23 10:46:20 +020030// List returns a Pager which allows you to iterate over a collection of
31// subnets. It accepts a ListOpts struct, which allows you to filter and sort
32// the returned collection for greater efficiency.
33//
34// Default policy settings return only those subnets that are owned by the tenant
35// who submits the request, unless the request is submitted by an user with
36// administrative rights.
Jamie Hannaford0708c002014-09-17 16:08:49 +020037func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
38 // Build query parameters
Jamie Hannaford0708c002014-09-17 16:08:49 +020039
Jamie Hannaford686c4962014-09-23 10:46:20 +020040 u := listURL(c) + utils.BuildQuery(q)
Jamie Hannaford0708c002014-09-17 16:08:49 +020041 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040042 return SubnetPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Jamie Hannaford0708c002014-09-17 16:08:49 +020043 })
44}
45
Jamie Hannaford686c4962014-09-23 10:46:20 +020046// Get retrieves a specific subnet based on its unique ID.
Jamie Hannafordd9036422014-09-23 17:50:24 +020047func Get(c *gophercloud.ServiceClient, id string) GetResult {
48 var res GetResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +020049 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jamie Hannaford0708c002014-09-17 16:08:49 +020050 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jamie Hannafordd9036422014-09-23 17:50:24 +020051 Results: &res.Resp,
52 OkCodes: []int{200},
Jamie Hannaford0708c002014-09-17 16:08:49 +020053 })
Jamie Hannafordd9036422014-09-23 17:50:24 +020054 return res
Jamie Hannaford0708c002014-09-17 16:08:49 +020055}
Jamie Hannaford63631432014-09-18 11:40:09 +020056
Jamie Hannaford686c4962014-09-23 10:46:20 +020057// Valid IP types
Jamie Hannaford63631432014-09-18 11:40:09 +020058const (
59 IPv4 = 4
60 IPv6 = 6
61)
62
Jamie Hannaford686c4962014-09-23 10:46:20 +020063// CreateOpts represents the attributes used when creating a new subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +020064type CreateOpts struct {
Jamie Hannaford63631432014-09-18 11:40:09 +020065 // Required
66 NetworkID string
67 CIDR string
68 // Optional
69 Name string
70 TenantID string
71 AllocationPools []AllocationPool
72 GatewayIP string
73 IPVersion int
Jamie Hannaford63631432014-09-18 11:40:09 +020074 EnableDHCP *bool
Jamie Hannaford965ae702014-09-22 14:58:19 +020075 DNSNameservers []string
76 HostRoutes []interface{}
Jamie Hannaford63631432014-09-18 11:40:09 +020077}
78
Jamie Hannaford686c4962014-09-23 10:46:20 +020079// Create accepts a CreateOpts struct and creates a new subnet using the values
80// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
Jamie Hannafordd9036422014-09-23 17:50:24 +020081func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
82 var res CreateResult
83
Jamie Hannaford63631432014-09-18 11:40:09 +020084 // Validate required options
85 if opts.NetworkID == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +020086 res.Err = errNetworkIDRequired
87 return res
Jamie Hannaford63631432014-09-18 11:40:09 +020088 }
89 if opts.CIDR == "" {
Jamie Hannafordd9036422014-09-23 17:50:24 +020090 res.Err = errCIDRRequired
91 return res
Jamie Hannaford63631432014-09-18 11:40:09 +020092 }
93 if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
Jamie Hannafordd9036422014-09-23 17:50:24 +020094 res.Err = errInvalidIPType
95 return res
Jamie Hannaford63631432014-09-18 11:40:09 +020096 }
97
98 type subnet struct {
99 NetworkID string `json:"network_id"`
100 CIDR string `json:"cidr"`
101 Name *string `json:"name,omitempty"`
102 TenantID *string `json:"tenant_id,omitempty"`
103 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"`
104 GatewayIP *string `json:"gateway_ip,omitempty"`
105 IPVersion int `json:"ip_version,omitempty"`
Jamie Hannaford63631432014-09-18 11:40:09 +0200106 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
Jamie Hannaford965ae702014-09-22 14:58:19 +0200107 DNSNameservers []string `json:"dns_nameservers,omitempty"`
108 HostRoutes []interface{} `json:"host_routes,omitempty"`
Jamie Hannaford63631432014-09-18 11:40:09 +0200109 }
110 type request struct {
111 Subnet subnet `json:"subnet"`
112 }
113
114 reqBody := request{Subnet: subnet{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200115 NetworkID: opts.NetworkID,
116 CIDR: opts.CIDR,
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200117 Name: gophercloud.MaybeString(opts.Name),
118 TenantID: gophercloud.MaybeString(opts.TenantID),
119 GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200120 EnableDHCP: opts.EnableDHCP,
Jamie Hannaford63631432014-09-18 11:40:09 +0200121 }}
122
Jamie Hannaford63631432014-09-18 11:40:09 +0200123 if opts.IPVersion != 0 {
124 reqBody.Subnet.IPVersion = opts.IPVersion
125 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200126 if len(opts.AllocationPools) != 0 {
127 reqBody.Subnet.AllocationPools = opts.AllocationPools
128 }
Jamie Hannaford965ae702014-09-22 14:58:19 +0200129 if len(opts.DNSNameservers) != 0 {
130 reqBody.Subnet.DNSNameservers = opts.DNSNameservers
131 }
132 if len(opts.HostRoutes) != 0 {
133 reqBody.Subnet.HostRoutes = opts.HostRoutes
134 }
Jamie Hannaford63631432014-09-18 11:40:09 +0200135
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200136 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jamie Hannaford63631432014-09-18 11:40:09 +0200137 MoreHeaders: c.Provider.AuthenticatedHeaders(),
138 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200139 Results: &res.Resp,
Jamie Hannaford63631432014-09-18 11:40:09 +0200140 OkCodes: []int{201},
141 })
Jamie Hannaford63631432014-09-18 11:40:09 +0200142
Jamie Hannafordd9036422014-09-23 17:50:24 +0200143 return res
Jamie Hannaford63631432014-09-18 11:40:09 +0200144}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200145
Jamie Hannaford686c4962014-09-23 10:46:20 +0200146// UpdateOpts represents the attributes used when updating an existing subnet.
Jamie Hannaford965ae702014-09-22 14:58:19 +0200147type UpdateOpts struct {
148 Name string
149 GatewayIP string
150 DNSNameservers []string
151 HostRoutes []interface{}
152 EnableDHCP *bool
153}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200154
Jamie Hannaford686c4962014-09-23 10:46:20 +0200155// Update accepts a UpdateOpts struct and updates an existing subnet using the
156// values provided.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200157func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200158 type subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200159 Name *string `json:"name,omitempty"`
160 GatewayIP *string `json:"gateway_ip,omitempty"`
161 DNSNameservers []string `json:"dns_nameservers,omitempty"`
162 HostRoutes []interface{} `json:"host_routes,omitempty"`
163 EnableDHCP *bool `json:"enable_dhcp,omitempty"`
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200164 }
165 type request struct {
166 Subnet subnet `json:"subnet"`
167 }
168
169 reqBody := request{Subnet: subnet{
Jamie Hannaford6abf9282014-09-24 10:54:13 +0200170 Name: gophercloud.MaybeString(opts.Name),
171 GatewayIP: gophercloud.MaybeString(opts.GatewayIP),
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200172 EnableDHCP: opts.EnableDHCP,
173 }}
174
Jamie Hannaford965ae702014-09-22 14:58:19 +0200175 if len(opts.DNSNameservers) != 0 {
176 reqBody.Subnet.DNSNameservers = opts.DNSNameservers
177 }
178
179 if len(opts.HostRoutes) != 0 {
180 reqBody.Subnet.HostRoutes = opts.HostRoutes
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200181 }
182
Jamie Hannafordd9036422014-09-23 17:50:24 +0200183 var res UpdateResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200184 _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200185 MoreHeaders: c.Provider.AuthenticatedHeaders(),
186 ReqBody: &reqBody,
Jamie Hannafordd9036422014-09-23 17:50:24 +0200187 Results: &res.Resp,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200188 OkCodes: []int{200, 201},
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200189 })
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200190
Jamie Hannafordd9036422014-09-23 17:50:24 +0200191 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200192}
193
Jamie Hannaford686c4962014-09-23 10:46:20 +0200194// Delete accepts a unique ID and deletes the subnet associated with it.
Jamie Hannafordd9036422014-09-23 17:50:24 +0200195func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
196 var res DeleteResult
Jamie Hannaford6f57e9e2014-10-02 10:27:28 +0200197 _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200198 MoreHeaders: c.Provider.AuthenticatedHeaders(),
199 OkCodes: []int{204},
200 })
Jamie Hannafordd9036422014-09-23 17:50:24 +0200201 return res
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200202}