blob: c9b8cf5662b499ed16f41b3fab03f9f7d02330c2 [file] [log] [blame]
Ash Wilson0a997f82014-09-03 15:50:52 -04001package endpoints
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/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
Jamie Hannaford562a7d52015-03-24 16:20:16 +010059 _, result.Err = client.Post(listURL(client), reqBody, &result.Body, nil)
Ash Wilson74e2bb82014-09-30 17:08:48 -040060 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -040061}
62
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070063// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040064// All fields are optional.
65type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050066 Availability gophercloud.Availability `q:"interface"`
67 ServiceID string `q:"service_id"`
68 Page int `q:"page"`
69 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040070}
71
72// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040073func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050074 u := listURL(client)
75 q, err := gophercloud.BuildQueryString(opts)
76 if err != nil {
77 return pagination.Pager{Err: err}
Ash Wilson32c0e8d2014-09-04 10:53:08 -040078 }
Jon Perritt19803442014-10-28 12:11:10 -050079 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040080 createPage := func(r pagination.PageResult) pagination.Page {
81 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040082 }
83
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040084 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040085}
86
87// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -040088// All fields are optional in the provided EndpointOpts.
Ash Wilson74e2bb82014-09-30 17:08:48 -040089func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
Ash Wilsonf04a74c2014-09-04 11:16:20 -040090 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 Wilsonf04a74c2014-09-04 11:16:20 -0400102 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400103 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 Wilsonf04a74c2014-09-04 11:16:20 -0400108
Ash Wilson74e2bb82014-09-30 17:08:48 -0400109 var result UpdateResult
Jon Perritta33da232016-03-02 04:43:08 -0600110 _, result.Err = client.Request("PATCH", endpointURL(client, endpointID), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500111 JSONBody: &reqBody,
112 JSONResponse: &result.Body,
113 OkCodes: []int{200},
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400114 })
Ash Wilson74e2bb82014-09-30 17:08:48 -0400115 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400116}
117
118// Delete removes an endpoint from the service catalog.
Jamie Hannaford3c086742014-10-27 11:32:16 +0100119func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult {
120 var res DeleteResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +0100121 _, res.Err = client.Delete(endpointURL(client, endpointID), nil)
Jamie Hannaford3c086742014-10-27 11:32:16 +0100122 return res
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400123}