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