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 | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame^] | 88 | _, res.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 | return res |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 94 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 95 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 96 | // Valid IP types |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 97 | const ( |
| 98 | IPv4 = 4 |
| 99 | IPv6 = 6 |
| 100 | ) |
| 101 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 102 | // CreateOpts represents the attributes used when creating a new subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 103 | type CreateOpts struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 104 | // Required |
| 105 | NetworkID string |
| 106 | CIDR string |
| 107 | // Optional |
| 108 | Name string |
| 109 | TenantID string |
| 110 | AllocationPools []AllocationPool |
| 111 | GatewayIP string |
| 112 | IPVersion int |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 113 | EnableDHCP *bool |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 114 | DNSNameservers []string |
| 115 | HostRoutes []interface{} |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 118 | // Create accepts a CreateOpts struct and creates a new subnet using the values |
| 119 | // 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] | 120 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 121 | var res CreateResult |
| 122 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 123 | // Validate required options |
| 124 | if opts.NetworkID == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 125 | res.Err = errNetworkIDRequired |
| 126 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 127 | } |
| 128 | if opts.CIDR == "" { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 129 | res.Err = errCIDRRequired |
| 130 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 131 | } |
| 132 | if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 133 | res.Err = errInvalidIPType |
| 134 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | type subnet struct { |
| 138 | NetworkID string `json:"network_id"` |
| 139 | CIDR string `json:"cidr"` |
| 140 | Name *string `json:"name,omitempty"` |
| 141 | TenantID *string `json:"tenant_id,omitempty"` |
| 142 | AllocationPools []AllocationPool `json:"allocation_pools,omitempty"` |
| 143 | GatewayIP *string `json:"gateway_ip,omitempty"` |
| 144 | IPVersion int `json:"ip_version,omitempty"` |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 145 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 146 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 147 | HostRoutes []interface{} `json:"host_routes,omitempty"` |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 148 | } |
| 149 | type request struct { |
| 150 | Subnet subnet `json:"subnet"` |
| 151 | } |
| 152 | |
| 153 | reqBody := request{Subnet: subnet{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 154 | NetworkID: opts.NetworkID, |
| 155 | CIDR: opts.CIDR, |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 156 | Name: gophercloud.MaybeString(opts.Name), |
| 157 | TenantID: gophercloud.MaybeString(opts.TenantID), |
| 158 | GatewayIP: gophercloud.MaybeString(opts.GatewayIP), |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 159 | EnableDHCP: opts.EnableDHCP, |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 160 | }} |
| 161 | |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 162 | if opts.IPVersion != 0 { |
| 163 | reqBody.Subnet.IPVersion = opts.IPVersion |
| 164 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 165 | if len(opts.AllocationPools) != 0 { |
| 166 | reqBody.Subnet.AllocationPools = opts.AllocationPools |
| 167 | } |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 168 | if len(opts.DNSNameservers) != 0 { |
| 169 | reqBody.Subnet.DNSNameservers = opts.DNSNameservers |
| 170 | } |
| 171 | if len(opts.HostRoutes) != 0 { |
| 172 | reqBody.Subnet.HostRoutes = opts.HostRoutes |
| 173 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 174 | |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame^] | 175 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 176 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 177 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 178 | Results: &res.Resp, |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 179 | OkCodes: []int{201}, |
| 180 | }) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 181 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 182 | return res |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 183 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 184 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 185 | // UpdateOpts represents the attributes used when updating an existing subnet. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 186 | type UpdateOpts struct { |
| 187 | Name string |
| 188 | GatewayIP string |
| 189 | DNSNameservers []string |
| 190 | HostRoutes []interface{} |
| 191 | EnableDHCP *bool |
| 192 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 193 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 194 | // Update accepts a UpdateOpts struct and updates an existing subnet using the |
| 195 | // values provided. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 196 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 197 | type subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 198 | Name *string `json:"name,omitempty"` |
| 199 | GatewayIP *string `json:"gateway_ip,omitempty"` |
| 200 | DNSNameservers []string `json:"dns_nameservers,omitempty"` |
| 201 | HostRoutes []interface{} `json:"host_routes,omitempty"` |
| 202 | EnableDHCP *bool `json:"enable_dhcp,omitempty"` |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 203 | } |
| 204 | type request struct { |
| 205 | Subnet subnet `json:"subnet"` |
| 206 | } |
| 207 | |
| 208 | reqBody := request{Subnet: subnet{ |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 209 | Name: gophercloud.MaybeString(opts.Name), |
| 210 | GatewayIP: gophercloud.MaybeString(opts.GatewayIP), |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 211 | EnableDHCP: opts.EnableDHCP, |
| 212 | }} |
| 213 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 214 | if len(opts.DNSNameservers) != 0 { |
| 215 | reqBody.Subnet.DNSNameservers = opts.DNSNameservers |
| 216 | } |
| 217 | |
| 218 | if len(opts.HostRoutes) != 0 { |
| 219 | reqBody.Subnet.HostRoutes = opts.HostRoutes |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 222 | var res UpdateResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame^] | 223 | _, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 224 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 225 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 226 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 227 | OkCodes: []int{200, 201}, |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 228 | }) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 229 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 230 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 233 | // Delete accepts a unique ID and deletes the subnet associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 234 | func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 235 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame^] | 236 | _, res.Err = perigee.Request("DELETE", deleteURL(c, id), perigee.Options{ |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 237 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 238 | OkCodes: []int{204}, |
| 239 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 240 | return res |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 241 | } |