blob: 635511d078e6f2156696c6f7a6531944ac1afc53 [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"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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)
jrperritt29ae6b32016-04-13 12:59:37 -050034 return
Jon Perrittdb0ae142016-03-13 00:33:41 -060035}
Ash Wilson989ce542014-09-04 10:52:49 -040036
Jon Perrittdb0ae142016-03-13 00:33:41 -060037type ListOptsBuilder interface {
38 ToEndpointListParams() (string, error)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040039}
40
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070041// ListOpts allows finer control over the endpoints returned by a List call.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040042// All fields are optional.
43type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050044 Availability gophercloud.Availability `q:"interface"`
45 ServiceID string `q:"service_id"`
46 Page int `q:"page"`
47 PerPage int `q:"per_page"`
Ash Wilsonbdfc3302014-09-04 10:16:28 -040048}
49
Jon Perrittdb0ae142016-03-13 00:33:41 -060050func (opts ListOpts) ToEndpointListParams() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050051 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 return q.String(), err
53}
Ash Wilsonab6be612014-09-15 15:51:22 -040054
Jon Perrittdb0ae142016-03-13 00:33:41 -060055// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
56func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
57 u := listURL(client)
58 if opts != nil {
59 q, err := gophercloud.BuildQueryString(opts)
60 if err != nil {
61 return pagination.Pager{Err: err}
62 }
63 u += q.String()
64 }
65 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
66 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
67 })
68}
69
70type UpdateOptsBuilder interface {
71 ToEndpointUpdateMap() (map[string]interface{}, error)
72}
73
74// UpdateOpts contains the subset of Endpoint attributes that should be used to update an Endpoint.
75type UpdateOpts struct {
76 Availability gophercloud.Availability `json:"interface,omitempty"`
77 Name string `json:"name,omitempty"`
78 Region string `json:"region,omitempty"`
79 URL string `json:"url,omitempty"`
80 ServiceID string `json:"service_id,omitempty"`
81}
82
83func (opts UpdateOpts) ToEndpointUpdateMap() (map[string]interface{}, error) {
84 return gophercloud.BuildRequestBody(opts, "endpoint")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040085}
86
87// Update changes an existing endpoint with new data.
Jon Perritt3860b512016-03-29 12:01:48 -050088func Update(client *gophercloud.ServiceClient, endpointID string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 b, err := opts.ToEndpointUpdateMap()
90 if err != nil {
91 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050092 return
Ash Wilsonf04a74c2014-09-04 11:16:20 -040093 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060094 _, r.Err = client.Patch(endpointURL(client, endpointID), &b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050095 return
Ash Wilsonbdfc3302014-09-04 10:16:28 -040096}
97
98// Delete removes an endpoint from the service catalog.
Jon Perritt3860b512016-03-29 12:01:48 -050099func Delete(client *gophercloud.ServiceClient, endpointID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600100 _, r.Err = client.Delete(endpointURL(client, endpointID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500101 return
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400102}