blob: 0b4cc14ee55ad8232232b158f355c1984dbe66cc [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 Wilsonbdfc3302014-09-04 10:16:28 -040023func Create(client *gophercloud.ServiceClient, opts EndpointOpts) (*Endpoint, error) {
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
37 type response struct {
Ash Wilsonb18fc102014-09-30 15:26:01 -040038 Endpoint `json:"endpoint"`
Ash Wilson989ce542014-09-04 10:52:49 -040039 }
40
41 // Ensure that EndpointOpts is fully populated.
Ash Wilsonefac18b2014-09-10 14:44:42 -040042 if opts.Availability == "" {
43 return nil, ErrAvailabilityRequired
Ash Wilson989ce542014-09-04 10:52:49 -040044 }
45 if opts.Name == "" {
46 return nil, ErrNameRequired
47 }
48 if opts.URL == "" {
49 return nil, ErrURLRequired
50 }
51 if opts.ServiceID == "" {
52 return nil, ErrServiceIDRequired
53 }
54
55 // Populate the request body.
56 reqBody := request{
57 Endpoint: endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040058 Interface: string(opts.Availability),
Ash Wilson989ce542014-09-04 10:52:49 -040059 Name: opts.Name,
60 URL: opts.URL,
61 ServiceID: opts.ServiceID,
62 },
63 }
Ash Wilsonb18fc102014-09-30 15:26:01 -040064 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
Ash Wilson989ce542014-09-04 10:52:49 -040065
66 var respBody response
Ash Wilson1e1b7332014-09-30 16:38:11 -040067 _, err := perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson989ce542014-09-04 10:52:49 -040068 MoreHeaders: client.Provider.AuthenticatedHeaders(),
69 ReqBody: &reqBody,
70 Results: &respBody,
71 OkCodes: []int{201},
72 })
73 if err != nil {
74 return nil, err
75 }
76
77 return &respBody.Endpoint, nil
Ash Wilsonbdfc3302014-09-04 10:16:28 -040078}
79
80// ListOpts allows finer control over the the endpoints returned by a List call.
81// All fields are optional.
82type ListOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040083 Availability gophercloud.Availability
84 ServiceID string
85 Page int
86 PerPage int
Ash Wilsonbdfc3302014-09-04 10:16:28 -040087}
88
89// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040090func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson32c0e8d2014-09-04 10:53:08 -040091 q := make(map[string]string)
Ash Wilsonefac18b2014-09-10 14:44:42 -040092 if opts.Availability != "" {
93 q["interface"] = string(opts.Availability)
Ash Wilson32c0e8d2014-09-04 10:53:08 -040094 }
95 if opts.ServiceID != "" {
96 q["service_id"] = opts.ServiceID
97 }
98 if opts.Page != 0 {
99 q["page"] = strconv.Itoa(opts.Page)
100 }
101 if opts.PerPage != 0 {
102 q["per_page"] = strconv.Itoa(opts.Page)
103 }
104
Ash Wilson3c8cc772014-09-16 11:40:49 -0400105 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfc55c822014-09-25 13:18:16 -0400106 return EndpointPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -0400107 }
108
Ash Wilson1e1b7332014-09-30 16:38:11 -0400109 u := listURL(client) + utils.BuildQuery(q)
Ash Wilsoncd95a0c2014-09-16 13:07:31 -0400110 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400111}
112
113// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400114// All fields are optional in the provided EndpointOpts.
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400115func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) (*Endpoint, error) {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400116 type endpoint struct {
117 Interface *string `json:"interface,omitempty"`
118 Name *string `json:"name,omitempty"`
119 Region *string `json:"region,omitempty"`
120 URL *string `json:"url,omitempty"`
121 ServiceID *string `json:"service_id,omitempty"`
122 }
123
124 type request struct {
125 Endpoint endpoint `json:"endpoint"`
126 }
127
128 type response struct {
129 Endpoint Endpoint `json:"endpoint"`
130 }
131
132 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400133 reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
134 reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
135 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
136 reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
137 reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400138
139 var respBody response
Ash Wilson1e1b7332014-09-30 16:38:11 -0400140 _, err := perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400141 MoreHeaders: client.Provider.AuthenticatedHeaders(),
142 ReqBody: &reqBody,
143 Results: &respBody,
144 OkCodes: []int{200},
145 })
146 if err != nil {
147 return nil, err
148 }
149
150 return &respBody.Endpoint, nil
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400151}
152
153// Delete removes an endpoint from the service catalog.
154func Delete(client *gophercloud.ServiceClient, endpointID string) error {
Ash Wilson1e1b7332014-09-30 16:38:11 -0400155 _, err := perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{
Ash Wilson70db2ab2014-09-04 11:18:32 -0400156 MoreHeaders: client.Provider.AuthenticatedHeaders(),
157 OkCodes: []int{204},
158 })
159 return err
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400160}