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