Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 9 | // ListOpts allows the filtering and sorting of paginated collections through |
| 10 | // the API. Filtering is achieved by passing in struct field values that map to |
| 11 | // the subnet attributes you want to see returned. SortKey allows you to sort |
| 12 | // by a particular subnet attribute. SortDir sets the direction, and is either |
| 13 | // `asc' or `desc'. Marker and Limit are used for pagination. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 14 | type ListOpts struct { |
Jamie Hannaford | 5c6e996 | 2014-10-02 10:34:14 +0200 | [diff] [blame] | 15 | Name string `q:"name"` |
| 16 | EnableDHCP *bool `q:"enable_dhcp"` |
| 17 | NetworkID string `q:"network_id"` |
| 18 | TenantID string `q:"tenant_id"` |
| 19 | IPVersion int `q:"ip_version"` |
| 20 | GatewayIP string `q:"gateway_ip"` |
| 21 | CIDR string `q:"cidr"` |
| 22 | ID string `q:"id"` |
| 23 | Limit int `q:"limit"` |
| 24 | Marker string `q:"marker"` |
| 25 | SortKey string `q:"sort_key"` |
| 26 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 29 | // List returns a Pager which allows you to iterate over a collection of |
| 30 | // subnets. It accepts a ListOpts struct, which allows you to filter and sort |
| 31 | // the returned collection for greater efficiency. |
| 32 | // |
| 33 | // Default policy settings return only those subnets that are owned by the tenant |
| 34 | // who submits the request, unless the request is submitted by an user with |
| 35 | // administrative rights. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 36 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 37 | // Build query parameters |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 38 | query, err := gophercloud.BuildQueryString(&opts) |
| 39 | if err != nil { |
| 40 | return pagination.Pager{Err: err} |
| 41 | } |
| 42 | url := listURL(c) + query.String() |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 43 | |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 44 | return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 45 | return SubnetPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 46 | }) |
| 47 | } |
| 48 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 49 | // Get retrieves a specific subnet based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 50 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 51 | var res GetResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 52 | _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 53 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 54 | Results: &res.Resp, |
| 55 | OkCodes: []int{200}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 56 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 57 | return res |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 58 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 59 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 60 | // Valid IP types |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 61 | const ( |
| 62 | IPv4 = 4 |
| 63 | IPv6 = 6 |
| 64 | ) |
| 65 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 66 | // CreateOpts represents the attributes used when creating a new subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 67 | type CreateOpts struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 68 | // Required |
| 69 | NetworkID string |
| 70 | CIDR string |
| 71 | // Optional |
| 72 | Name string |
| 73 | TenantID string |
| 74 | AllocationPools []AllocationPool |
| 75 | GatewayIP string |
| 76 | IPVersion int |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 77 | EnableDHCP *bool |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 78 | DNSNameservers []string |
| 79 | HostRoutes []interface{} |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 82 | // Create accepts a CreateOpts struct and creates a new subnet using the values |
| 83 | // provided. You must remember to provide a valid NetworkID, CIDR and IP version. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 84 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 85 | var res CreateResult |
| 86 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 87 | // Validate required options |
| 88 | if opts.NetworkID == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 89 | res.Err = errNetworkIDRequired |
| 90 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 91 | } |
| 92 | if opts.CIDR == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 93 | res.Err = errCIDRRequired |
| 94 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 95 | } |
| 96 | if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 97 | res.Err = errInvalidIPType |
| 98 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | type subnet struct { |
| 102 | NetworkID string `json:"network_id"` |
| 103 | CIDR string `json:"cidr"` |
| 104 | Name *string `json:"name,omitempty"` |
| 105 | TenantID *string `json:"tenant_id,omitempty"` |
| 106 | AllocationPools []AllocationPool `json:"allocation_pools,omitempty"` |
| 107 | GatewayIP *string `json:"gateway_ip,omitempty"` |
| 108 | IPVersion int `json:"ip_version,omitempty"` |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 109 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 110 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 111 | HostRoutes []interface{} `json:"host_routes,omitempty"` |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 112 | } |
| 113 | type request struct { |
| 114 | Subnet subnet `json:"subnet"` |
| 115 | } |
| 116 | |
| 117 | reqBody := request{Subnet: subnet{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 118 | NetworkID: opts.NetworkID, |
| 119 | CIDR: opts.CIDR, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 120 | Name: gophercloud.MaybeString(opts.Name), |
| 121 | TenantID: gophercloud.MaybeString(opts.TenantID), |
| 122 | GatewayIP: gophercloud.MaybeString(opts.GatewayIP), |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 123 | EnableDHCP: opts.EnableDHCP, |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 124 | }} |
| 125 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 126 | if opts.IPVersion != 0 { |
| 127 | reqBody.Subnet.IPVersion = opts.IPVersion |
| 128 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 129 | if len(opts.AllocationPools) != 0 { |
| 130 | reqBody.Subnet.AllocationPools = opts.AllocationPools |
| 131 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 132 | if len(opts.DNSNameservers) != 0 { |
| 133 | reqBody.Subnet.DNSNameservers = opts.DNSNameservers |
| 134 | } |
| 135 | if len(opts.HostRoutes) != 0 { |
| 136 | reqBody.Subnet.HostRoutes = opts.HostRoutes |
| 137 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 138 | |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 139 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 140 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 141 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 142 | Results: &res.Resp, |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 143 | OkCodes: []int{201}, |
| 144 | }) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 145 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 146 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 147 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 148 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 149 | // UpdateOpts represents the attributes used when updating an existing subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 150 | type UpdateOpts struct { |
| 151 | Name string |
| 152 | GatewayIP string |
| 153 | DNSNameservers []string |
| 154 | HostRoutes []interface{} |
| 155 | EnableDHCP *bool |
| 156 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 157 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 158 | // Update accepts a UpdateOpts struct and updates an existing subnet using the |
| 159 | // values provided. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 160 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 161 | type subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 162 | Name *string `json:"name,omitempty"` |
| 163 | GatewayIP *string `json:"gateway_ip,omitempty"` |
| 164 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 165 | HostRoutes []interface{} `json:"host_routes,omitempty"` |
| 166 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 167 | } |
| 168 | type request struct { |
| 169 | Subnet subnet `json:"subnet"` |
| 170 | } |
| 171 | |
| 172 | reqBody := request{Subnet: subnet{ |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 173 | Name: gophercloud.MaybeString(opts.Name), |
| 174 | GatewayIP: gophercloud.MaybeString(opts.GatewayIP), |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 175 | EnableDHCP: opts.EnableDHCP, |
| 176 | }} |
| 177 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 178 | if len(opts.DNSNameservers) != 0 { |
| 179 | reqBody.Subnet.DNSNameservers = opts.DNSNameservers |
| 180 | } |
| 181 | |
| 182 | if len(opts.HostRoutes) != 0 { |
| 183 | reqBody.Subnet.HostRoutes = opts.HostRoutes |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 186 | var res UpdateResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 187 | _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 188 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 189 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 190 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 191 | OkCodes: []int{200, 201}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 192 | }) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 193 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 194 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 197 | // Delete accepts a unique ID and deletes the subnet associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 198 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 199 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 200 | _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 201 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 202 | OkCodes: []int{204}, |
| 203 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 204 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 205 | } |