| Ash Wilson | 0a997f8 | 2014-09-03 15:50:52 -0400 | [diff] [blame] | 1 | package endpoints | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 2 |  | 
 | 3 | import ( | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 4 | 	"github.com/rackspace/gophercloud" | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 5 | 	"github.com/rackspace/gophercloud/pagination" | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 6 | ) | 
 | 7 |  | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 8 | // EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint. | 
 | 9 | type EndpointOpts struct { | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 10 | 	Availability gophercloud.Availability | 
 | 11 | 	Name         string | 
 | 12 | 	Region       string | 
 | 13 | 	URL          string | 
 | 14 | 	ServiceID    string | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 15 | } | 
 | 16 |  | 
 | 17 | // Create inserts a new Endpoint into the service catalog. | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 18 | // Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required. | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 19 | func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult { | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 20 | 	// Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output. | 
 | 21 | 	type endpoint struct { | 
 | 22 | 		Interface string  `json:"interface"` | 
 | 23 | 		Name      string  `json:"name"` | 
 | 24 | 		Region    *string `json:"region,omitempty"` | 
 | 25 | 		URL       string  `json:"url"` | 
 | 26 | 		ServiceID string  `json:"service_id"` | 
 | 27 | 	} | 
 | 28 |  | 
 | 29 | 	type request struct { | 
 | 30 | 		Endpoint endpoint `json:"endpoint"` | 
 | 31 | 	} | 
 | 32 |  | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 33 | 	// Ensure that EndpointOpts is fully populated. | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 34 | 	if opts.Availability == "" { | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 35 | 		return createErr(ErrAvailabilityRequired) | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 36 | 	} | 
 | 37 | 	if opts.Name == "" { | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 38 | 		return createErr(ErrNameRequired) | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 39 | 	} | 
 | 40 | 	if opts.URL == "" { | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 41 | 		return createErr(ErrURLRequired) | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 42 | 	} | 
 | 43 | 	if opts.ServiceID == "" { | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 44 | 		return createErr(ErrServiceIDRequired) | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 45 | 	} | 
 | 46 |  | 
 | 47 | 	// Populate the request body. | 
 | 48 | 	reqBody := request{ | 
 | 49 | 		Endpoint: endpoint{ | 
| Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 50 | 			Interface: string(opts.Availability), | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 51 | 			Name:      opts.Name, | 
 | 52 | 			URL:       opts.URL, | 
 | 53 | 			ServiceID: opts.ServiceID, | 
 | 54 | 		}, | 
 | 55 | 	} | 
| Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 56 | 	reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 57 |  | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 58 | 	var result CreateResult | 
| Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame^] | 59 | 	_, result.Err = client.Post(listURL(client), reqBody, &result.Body, nil) | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 60 | 	return result | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 63 | // ListOpts allows finer control over the endpoints returned by a List call. | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 64 | // All fields are optional. | 
 | 65 | type ListOpts struct { | 
| Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 66 | 	Availability gophercloud.Availability `q:"interface"` | 
 | 67 | 	ServiceID    string                   `q:"service_id"` | 
 | 68 | 	Page         int                      `q:"page"` | 
 | 69 | 	PerPage      int                      `q:"per_page"` | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 70 | } | 
 | 71 |  | 
 | 72 | // List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. | 
| Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 73 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { | 
| Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 74 | 	u := listURL(client) | 
 | 75 | 	q, err := gophercloud.BuildQueryString(opts) | 
 | 76 | 	if err != nil { | 
 | 77 | 		return pagination.Pager{Err: err} | 
| Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 78 | 	} | 
| Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 79 | 	u += q.String() | 
| Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 80 | 	createPage := func(r pagination.PageResult) pagination.Page { | 
 | 81 | 		return EndpointPage{pagination.LinkedPageBase{PageResult: r}} | 
| Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 82 | 	} | 
 | 83 |  | 
| Ash Wilson | cd95a0c | 2014-09-16 13:07:31 -0400 | [diff] [blame] | 84 | 	return pagination.NewPager(client, u, createPage) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 85 | } | 
 | 86 |  | 
 | 87 | // Update changes an existing endpoint with new data. | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 88 | // All fields are optional in the provided EndpointOpts. | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 89 | func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult { | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 90 | 	type endpoint struct { | 
 | 91 | 		Interface *string `json:"interface,omitempty"` | 
 | 92 | 		Name      *string `json:"name,omitempty"` | 
 | 93 | 		Region    *string `json:"region,omitempty"` | 
 | 94 | 		URL       *string `json:"url,omitempty"` | 
 | 95 | 		ServiceID *string `json:"service_id,omitempty"` | 
 | 96 | 	} | 
 | 97 |  | 
 | 98 | 	type request struct { | 
 | 99 | 		Endpoint endpoint `json:"endpoint"` | 
 | 100 | 	} | 
 | 101 |  | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 102 | 	reqBody := request{Endpoint: endpoint{}} | 
| Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 103 | 	reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability)) | 
 | 104 | 	reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name) | 
 | 105 | 	reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) | 
 | 106 | 	reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL) | 
 | 107 | 	reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID) | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 108 |  | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 109 | 	var result UpdateResult | 
| Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 110 | 	_, result.Err = client.Request("PATCH", endpointURL(client, endpointID), gophercloud.RequestOpts{ | 
 | 111 | 		JSONBody:     &reqBody, | 
 | 112 | 		JSONResponse: &result.Body, | 
 | 113 | 		OkCodes:      []int{200}, | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 114 | 	}) | 
| Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 115 | 	return result | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 116 | } | 
 | 117 |  | 
 | 118 | // Delete removes an endpoint from the service catalog. | 
| Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 119 | func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult { | 
 | 120 | 	var res DeleteResult | 
| Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame^] | 121 | 	_, res.Err = client.Delete(endpointURL(client, endpointID), nil) | 
| Jamie Hannaford | 3c08674 | 2014-10-27 11:32:16 +0100 | [diff] [blame] | 122 | 	return res | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 123 | } |