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