blob: 3e09b2aef0dc016ddd1cce4109ec151616a4dffb [file] [log] [blame]
Ash Wilson0a997f82014-09-03 15:50:52 -04001package endpoints
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
Ash Wilsonbdfc3302014-09-04 10:16:28 -04004 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04005 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04006)
7
Ash Wilsonbdfc3302014-09-04 10:16:28 -04008// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
9type EndpointOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040010 Availability gophercloud.Availability
11 Name string
12 Region string
13 URL string
14 ServiceID string
Ash Wilsonbdfc3302014-09-04 10:16:28 -040015}
16
17// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040018// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Ash Wilson74e2bb82014-09-30 17:08:48 -040019func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
Ash Wilson989ce542014-09-04 10:52:49 -040020 // 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 Wilson989ce542014-09-04 10:52:49 -040033 // Ensure that EndpointOpts is fully populated.
Ash Wilsonefac18b2014-09-10 14:44:42 -040034 if opts.Availability == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040035 return createErr(ErrAvailabilityRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040036 }
37 if opts.Name == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040038 return createErr(ErrNameRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040039 }
40 if opts.URL == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040041 return createErr(ErrURLRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040042 }
43 if opts.ServiceID == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040044 return createErr(ErrServiceIDRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040045 }
46
47 // Populate the request body.
48 reqBody := request{
49 Endpoint: endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040050 Interface: string(opts.Availability),
Ash Wilson989ce542014-09-04 10:52:49 -040051 Name: opts.Name,
52 URL: opts.URL,
53 ServiceID: opts.ServiceID,
54 },
55 }
Ash Wilsonb18fc102014-09-30 15:26:01 -040056 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
Ash Wilson989ce542014-09-04 10:52:49 -040057
Ash Wilson74e2bb82014-09-30 17:08:48 -040058 var result CreateResult
Ash Wilson4bf41a32015-02-12 15:52:44 -050059 _, result.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{
60 JSONBody: &reqBody,
61 JSONResponse: &result.Body,
62 OkCodes: []int{201},
Ash Wilson989ce542014-09-04 10:52:49 -040063 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040064 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -040065}
66
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070067// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040068// All fields are optional.
69type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050070 Availability gophercloud.Availability `q:"interface"`
71 ServiceID string `q:"service_id"`
72 Page int `q:"page"`
73 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040074}
75
76// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040077func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050078 u := listURL(client)
79 q, err := gophercloud.BuildQueryString(opts)
80 if err != nil {
81 return pagination.Pager{Err: err}
Ash Wilson32c0e8d2014-09-04 10:53:08 -040082 }
Jon Perritt19803442014-10-28 12:11:10 -050083 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040084 createPage := func(r pagination.PageResult) pagination.Page {
85 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040086 }
87
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040088 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040089}
90
91// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -040092// All fields are optional in the provided EndpointOpts.
Ash Wilson74e2bb82014-09-30 17:08:48 -040093func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
Ash Wilsonf04a74c2014-09-04 11:16:20 -040094 type endpoint struct {
95 Interface *string `json:"interface,omitempty"`
96 Name *string `json:"name,omitempty"`
97 Region *string `json:"region,omitempty"`
98 URL *string `json:"url,omitempty"`
99 ServiceID *string `json:"service_id,omitempty"`
100 }
101
102 type request struct {
103 Endpoint endpoint `json:"endpoint"`
104 }
105
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400106 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400107 reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
108 reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
109 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
110 reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
111 reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400112
Ash Wilson74e2bb82014-09-30 17:08:48 -0400113 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500114 _, result.Err = client.Request("PATCH", endpointURL(client, endpointID), gophercloud.RequestOpts{
115 JSONBody: &reqBody,
116 JSONResponse: &result.Body,
117 OkCodes: []int{200},
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400118 })
Ash Wilson74e2bb82014-09-30 17:08:48 -0400119 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400120}
121
122// Delete removes an endpoint from the service catalog.
Jamie Hannaford3c086742014-10-27 11:32:16 +0100123func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult {
124 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500125 _, res.Err = client.Request("DELETE", endpointURL(client, endpointID), gophercloud.RequestOpts{
126 OkCodes: []int{204},
Ash Wilson70db2ab2014-09-04 11:18:32 -0400127 })
Jamie Hannaford3c086742014-10-27 11:32:16 +0100128 return res
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400129}