blob: 30ceacfb046a351b13e4e7808e5ad874401268b3 [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 Perritt3860b512016-03-29 12:01:48 -050027func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060028 b, err := opts.ToEndpointCreateMap()
29 if err != nil {
30 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050031 return
Ash Wilson989ce542014-09-04 10:52:49 -040032 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060033 _, r.Err = client.Post(listURL(client), &b, &r.Body, nil)
Jon Perrittdb0ae142016-03-13 00:33:41 -060034}
Ash Wilson989ce542014-09-04 10:52:49 -040035
Jon Perrittdb0ae142016-03-13 00:33:41 -060036type ListOptsBuilder interface {
37 ToEndpointListParams() (string, error)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040038}
39
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070040// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040041// All fields are optional.
42type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050043 Availability gophercloud.Availability `q:"interface"`
44 ServiceID string `q:"service_id"`
45 Page int `q:"page"`
46 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040047}
48
Jon Perrittdb0ae142016-03-13 00:33:41 -060049func (opts ListOpts) ToEndpointListParams() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050050 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 return q.String(), err
52}
Ash Wilsonab6be612014-09-15 15:51:22 -040053
Jon Perrittdb0ae142016-03-13 00:33:41 -060054// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
55func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
56 u := listURL(client)
57 if opts != nil {
58 q, err := gophercloud.BuildQueryString(opts)
59 if err != nil {
60 return pagination.Pager{Err: err}
61 }
62 u += q.String()
63 }
64 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
65 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
66 })
67}
68
69type UpdateOptsBuilder interface {
70 ToEndpointUpdateMap() (map[string]interface{}, error)
71}
72
73// UpdateOpts contains the subset of Endpoint attributes that should be used to update an Endpoint.
74type UpdateOpts struct {
75 Availability gophercloud.Availability `json:"interface,omitempty"`
76 Name string `json:"name,omitempty"`
77 Region string `json:"region,omitempty"`
78 URL string `json:"url,omitempty"`
79 ServiceID string `json:"service_id,omitempty"`
80}
81
82func (opts UpdateOpts) ToEndpointUpdateMap() (map[string]interface{}, error) {
83 return gophercloud.BuildRequestBody(opts, "endpoint")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040084}
85
86// Update changes an existing endpoint with new data.
Jon Perritt3860b512016-03-29 12:01:48 -050087func Update(client *gophercloud.ServiceClient, endpointID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060088 b, err := opts.ToEndpointUpdateMap()
89 if err != nil {
90 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050091 return
Ash Wilsonf04a74c2014-09-04 11:16:20 -040092 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060093 _, r.Err = client.Patch(endpointURL(client, endpointID), &b, &r.Body, nil)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040094}
95
96// Delete removes an endpoint from the service catalog.
Jon Perritt3860b512016-03-29 12:01:48 -050097func Delete(client *gophercloud.ServiceClient, endpointID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060098 _, r.Err = client.Delete(endpointURL(client, endpointID), nil)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040099}