blob: 9d23e56d831089f9014ec02f1a6863a9045d3e25 [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}