blob: eb52573d25d569df62320144509627399bcdfd35 [file] [log] [blame]
Ash Wilson0a997f82014-09-03 15:50:52 -04001package endpoints
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
Ash Wilson989ce542014-09-04 10:52:49 -04004 "strconv"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04005
Ash Wilson989ce542014-09-04 10:52:49 -04006 "github.com/racker/perigee"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04007 "github.com/rackspace/gophercloud"
Ash Wilson989ce542014-09-04 10:52:49 -04008 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson3c8cc772014-09-16 11:40:49 -04009 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040010)
11
Ash Wilsonbdfc3302014-09-04 10:16:28 -040012// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
13type EndpointOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040014 Availability gophercloud.Availability
15 Name string
16 Region string
17 URL string
18 ServiceID string
Ash Wilsonbdfc3302014-09-04 10:16:28 -040019}
20
21// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040022// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Ash Wilson74e2bb82014-09-30 17:08:48 -040023func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
Ash Wilson989ce542014-09-04 10:52:49 -040024 // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
25 type endpoint struct {
26 Interface string `json:"interface"`
27 Name string `json:"name"`
28 Region *string `json:"region,omitempty"`
29 URL string `json:"url"`
30 ServiceID string `json:"service_id"`
31 }
32
33 type request struct {
34 Endpoint endpoint `json:"endpoint"`
35 }
36
Ash Wilson989ce542014-09-04 10:52:49 -040037 // Ensure that EndpointOpts is fully populated.
Ash Wilsonefac18b2014-09-10 14:44:42 -040038 if opts.Availability == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040039 return createErr(ErrAvailabilityRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040040 }
41 if opts.Name == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040042 return createErr(ErrNameRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040043 }
44 if opts.URL == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040045 return createErr(ErrURLRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040046 }
47 if opts.ServiceID == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040048 return createErr(ErrServiceIDRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040049 }
50
51 // Populate the request body.
52 reqBody := request{
53 Endpoint: endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040054 Interface: string(opts.Availability),
Ash Wilson989ce542014-09-04 10:52:49 -040055 Name: opts.Name,
56 URL: opts.URL,
57 ServiceID: opts.ServiceID,
58 },
59 }
Ash Wilsonb18fc102014-09-30 15:26:01 -040060 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
Ash Wilson989ce542014-09-04 10:52:49 -040061
Ash Wilson74e2bb82014-09-30 17:08:48 -040062 var result CreateResult
63 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson989ce542014-09-04 10:52:49 -040064 MoreHeaders: client.Provider.AuthenticatedHeaders(),
65 ReqBody: &reqBody,
Ash Wilson74e2bb82014-09-30 17:08:48 -040066 Results: &result.Resp,
Ash Wilson989ce542014-09-04 10:52:49 -040067 OkCodes: []int{201},
68 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040069 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -040070}
71
72// ListOpts allows finer control over the the endpoints returned by a List call.
73// All fields are optional.
74type ListOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040075 Availability gophercloud.Availability
76 ServiceID string
77 Page int
78 PerPage int
Ash Wilsonbdfc3302014-09-04 10:16:28 -040079}
80
81// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040082func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson32c0e8d2014-09-04 10:53:08 -040083 q := make(map[string]string)
Ash Wilsonefac18b2014-09-10 14:44:42 -040084 if opts.Availability != "" {
85 q["interface"] = string(opts.Availability)
Ash Wilson32c0e8d2014-09-04 10:53:08 -040086 }
87 if opts.ServiceID != "" {
88 q["service_id"] = opts.ServiceID
89 }
90 if opts.Page != 0 {
91 q["page"] = strconv.Itoa(opts.Page)
92 }
93 if opts.PerPage != 0 {
94 q["per_page"] = strconv.Itoa(opts.Page)
95 }
96
Ash Wilson3c8cc772014-09-16 11:40:49 -040097 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -040098 return EndpointPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040099 }
100
Ash Wilson1e1b7332014-09-30 16:38:11 -0400101 u := listURL(client) + utils.BuildQuery(q)
Ash Wilsoncd95a0c2014-09-16 13:07:31 -0400102 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400103}
104
105// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400106// All fields are optional in the provided EndpointOpts.
Ash Wilson74e2bb82014-09-30 17:08:48 -0400107func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400108 type endpoint struct {
109 Interface *string `json:"interface,omitempty"`
110 Name *string `json:"name,omitempty"`
111 Region *string `json:"region,omitempty"`
112 URL *string `json:"url,omitempty"`
113 ServiceID *string `json:"service_id,omitempty"`
114 }
115
116 type request struct {
117 Endpoint endpoint `json:"endpoint"`
118 }
119
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400120 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400121 reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
122 reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
123 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
124 reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
125 reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400126
Ash Wilson74e2bb82014-09-30 17:08:48 -0400127 var result UpdateResult
128 _, result.Err = perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400129 MoreHeaders: client.Provider.AuthenticatedHeaders(),
130 ReqBody: &reqBody,
Ash Wilson74e2bb82014-09-30 17:08:48 -0400131 Results: &result.Resp,
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400132 OkCodes: []int{200},
133 })
Ash Wilson74e2bb82014-09-30 17:08:48 -0400134 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400135}
136
137// Delete removes an endpoint from the service catalog.
138func Delete(client *gophercloud.ServiceClient, endpointID string) error {
Ash Wilson1e1b7332014-09-30 16:38:11 -0400139 _, err := perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{
Ash Wilson70db2ab2014-09-04 11:18:32 -0400140 MoreHeaders: client.Provider.AuthenticatedHeaders(),
141 OkCodes: []int{204},
142 })
143 return err
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400144}