blob: 7165fb493d05ef60391acc0ef5219e2b27aaa228 [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
Jon Perrittdb0ae142016-03-13 00:33:41 -06008type CreateOptsBuilder interface {
9 ToEndpointCreateMap() (map[string]interface{}, error)
10}
11
12// CreateOpts contains the subset of Endpoint attributes that should be used to create an Endpoint.
13type CreateOpts struct {
14 Availability gophercloud.Availability `json:"interface" required:"true"`
15 Name string `json:"name" required:"true"`
16 Region string `json:"region,omitempty"`
17 URL string `json:"url" required:"true"`
18 ServiceID string `json:"service_id" required:"true"`
19}
20
21func (opts CreateOpts) ToEndpointCreateMap() (map[string]interface{}, error) {
22 return gophercloud.BuildRequestBody(opts, "endpoint")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040023}
24
25// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040026// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Jon Perrittdb0ae142016-03-13 00:33:41 -060027func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
28 var r CreateResult
29 b, err := opts.ToEndpointCreateMap()
30 if err != nil {
31 r.Err = err
32 return r
Ash Wilson989ce542014-09-04 10:52:49 -040033 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060034 _, r.Err = client.Post(listURL(client), &b, &r.Body, nil)
35 return r
36}
Ash Wilson989ce542014-09-04 10:52:49 -040037
Jon Perrittdb0ae142016-03-13 00:33:41 -060038type ListOptsBuilder interface {
39 ToEndpointListParams() (string, error)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040040}
41
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070042// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040043// All fields are optional.
44type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050045 Availability gophercloud.Availability `q:"interface"`
46 ServiceID string `q:"service_id"`
47 Page int `q:"page"`
48 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040049}
50
Jon Perrittdb0ae142016-03-13 00:33:41 -060051func (opts ListOpts) ToEndpointListParams() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050052 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 return q.String(), err
54}
Ash Wilsonab6be612014-09-15 15:51:22 -040055
Jon Perrittdb0ae142016-03-13 00:33:41 -060056// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
57func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
58 u := listURL(client)
59 if opts != nil {
60 q, err := gophercloud.BuildQueryString(opts)
61 if err != nil {
62 return pagination.Pager{Err: err}
63 }
64 u += q.String()
65 }
66 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
67 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
68 })
69}
70
71type UpdateOptsBuilder interface {
72 ToEndpointUpdateMap() (map[string]interface{}, error)
73}
74
75// UpdateOpts contains the subset of Endpoint attributes that should be used to update an Endpoint.
76type UpdateOpts struct {
77 Availability gophercloud.Availability `json:"interface,omitempty"`
78 Name string `json:"name,omitempty"`
79 Region string `json:"region,omitempty"`
80 URL string `json:"url,omitempty"`
81 ServiceID string `json:"service_id,omitempty"`
82}
83
84func (opts UpdateOpts) ToEndpointUpdateMap() (map[string]interface{}, error) {
85 return gophercloud.BuildRequestBody(opts, "endpoint")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040086}
87
88// Update changes an existing endpoint with new data.
Jon Perrittdb0ae142016-03-13 00:33:41 -060089func Update(client *gophercloud.ServiceClient, endpointID string, opts UpdateOptsBuilder) UpdateResult {
90 var r UpdateResult
91 b, err := opts.ToEndpointUpdateMap()
92 if err != nil {
93 r.Err = err
94 return r
Ash Wilsonf04a74c2014-09-04 11:16:20 -040095 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060096 _, r.Err = client.Patch(endpointURL(client, endpointID), &b, &r.Body, nil)
97 return r
Ash Wilsonbdfc3302014-09-04 10:16:28 -040098}
99
100// Delete removes an endpoint from the service catalog.
Jamie Hannaford3c086742014-10-27 11:32:16 +0100101func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600102 var r DeleteResult
103 _, r.Err = client.Delete(endpointURL(client, endpointID), nil)
104 return r
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400105}