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