blob: 7bdb7cef2e8810d67d96c0de5925b78843203ec3 [file] [log] [blame]
Ash Wilson0a997f82014-09-03 15:50:52 -04001package endpoints
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
Ash Wilson989ce542014-09-04 10:52:49 -04004 "github.com/racker/perigee"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04005 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04006 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04007)
8
Ash Wilsonbdfc3302014-09-04 10:16:28 -04009// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
10type EndpointOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040011 Availability gophercloud.Availability
12 Name string
13 Region string
14 URL string
15 ServiceID string
Ash Wilsonbdfc3302014-09-04 10:16:28 -040016}
17
18// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040019// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Ash Wilson74e2bb82014-09-30 17:08:48 -040020func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
Ash Wilson989ce542014-09-04 10:52:49 -040021 // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
22 type endpoint struct {
23 Interface string `json:"interface"`
24 Name string `json:"name"`
25 Region *string `json:"region,omitempty"`
26 URL string `json:"url"`
27 ServiceID string `json:"service_id"`
28 }
29
30 type request struct {
31 Endpoint endpoint `json:"endpoint"`
32 }
33
Ash Wilson989ce542014-09-04 10:52:49 -040034 // Ensure that EndpointOpts is fully populated.
Ash Wilsonefac18b2014-09-10 14:44:42 -040035 if opts.Availability == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040036 return createErr(ErrAvailabilityRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040037 }
38 if opts.Name == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040039 return createErr(ErrNameRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040040 }
41 if opts.URL == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040042 return createErr(ErrURLRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040043 }
44 if opts.ServiceID == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040045 return createErr(ErrServiceIDRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040046 }
47
48 // Populate the request body.
49 reqBody := request{
50 Endpoint: endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040051 Interface: string(opts.Availability),
Ash Wilson989ce542014-09-04 10:52:49 -040052 Name: opts.Name,
53 URL: opts.URL,
54 ServiceID: opts.ServiceID,
55 },
56 }
Ash Wilsonb18fc102014-09-30 15:26:01 -040057 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
Ash Wilson989ce542014-09-04 10:52:49 -040058
Ash Wilson74e2bb82014-09-30 17:08:48 -040059 var result CreateResult
60 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040061 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson989ce542014-09-04 10:52:49 -040062 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -040063 Results: &result.Body,
Ash Wilson989ce542014-09-04 10:52:49 -040064 OkCodes: []int{201},
65 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040066 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -040067}
68
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070069// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040070// All fields are optional.
71type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050072 Availability gophercloud.Availability `q:"interface"`
73 ServiceID string `q:"service_id"`
74 Page int `q:"page"`
75 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040076}
77
78// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040079func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050080 u := listURL(client)
81 q, err := gophercloud.BuildQueryString(opts)
82 if err != nil {
83 return pagination.Pager{Err: err}
Ash Wilson32c0e8d2014-09-04 10:53:08 -040084 }
Jon Perritt19803442014-10-28 12:11:10 -050085 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040086 createPage := func(r pagination.PageResult) pagination.Page {
87 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040088 }
89
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040090 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040091}
92
93// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -040094// All fields are optional in the provided EndpointOpts.
Ash Wilson74e2bb82014-09-30 17:08:48 -040095func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
Ash Wilsonf04a74c2014-09-04 11:16:20 -040096 type endpoint struct {
97 Interface *string `json:"interface,omitempty"`
98 Name *string `json:"name,omitempty"`
99 Region *string `json:"region,omitempty"`
100 URL *string `json:"url,omitempty"`
101 ServiceID *string `json:"service_id,omitempty"`
102 }
103
104 type request struct {
105 Endpoint endpoint `json:"endpoint"`
106 }
107
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400108 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400109 reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
110 reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
111 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
112 reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
113 reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400114
Ash Wilson74e2bb82014-09-30 17:08:48 -0400115 var result UpdateResult
116 _, result.Err = perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400117 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400118 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400119 Results: &result.Body,
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400120 OkCodes: []int{200},
121 })
Ash Wilson74e2bb82014-09-30 17:08:48 -0400122 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400123}
124
125// Delete removes an endpoint from the service catalog.
Jamie Hannaford3c086742014-10-27 11:32:16 +0100126func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult {
127 var res DeleteResult
128 _, res.Err = perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400129 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson70db2ab2014-09-04 11:18:32 -0400130 OkCodes: []int{204},
131 })
Jamie Hannaford3c086742014-10-27 11:32:16 +0100132 return res
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400133}