blob: eb52573d25d569df62320144509627399bcdfd35 [file] [log] [blame]
Jamie Hannaford8c072a32014-10-16 14:33:32 +02001package endpoints
2
3import (
4 "strconv"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
12// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
13type EndpointOpts struct {
14 Availability gophercloud.Availability
15 Name string
16 Region string
17 URL string
18 ServiceID string
19}
20
21// Create inserts a new Endpoint into the service catalog.
22// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
23func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
24 // 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 // Ensure that EndpointOpts is fully populated.
38 if opts.Availability == "" {
39 return createErr(ErrAvailabilityRequired)
40 }
41 if opts.Name == "" {
42 return createErr(ErrNameRequired)
43 }
44 if opts.URL == "" {
45 return createErr(ErrURLRequired)
46 }
47 if opts.ServiceID == "" {
48 return createErr(ErrServiceIDRequired)
49 }
50
51 // Populate the request body.
52 reqBody := request{
53 Endpoint: endpoint{
54 Interface: string(opts.Availability),
55 Name: opts.Name,
56 URL: opts.URL,
57 ServiceID: opts.ServiceID,
58 },
59 }
60 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
61
62 var result CreateResult
63 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
64 MoreHeaders: client.Provider.AuthenticatedHeaders(),
65 ReqBody: &reqBody,
66 Results: &result.Resp,
67 OkCodes: []int{201},
68 })
69 return result
70}
71
72// ListOpts allows finer control over the the endpoints returned by a List call.
73// All fields are optional.
74type ListOpts struct {
75 Availability gophercloud.Availability
76 ServiceID string
77 Page int
78 PerPage int
79}
80
81// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
82func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
83 q := make(map[string]string)
84 if opts.Availability != "" {
85 q["interface"] = string(opts.Availability)
86 }
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
97 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
98 return EndpointPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
99 }
100
101 u := listURL(client) + utils.BuildQuery(q)
102 return pagination.NewPager(client, u, createPage)
103}
104
105// Update changes an existing endpoint with new data.
106// All fields are optional in the provided EndpointOpts.
107func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
108 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
120 reqBody := request{Endpoint: endpoint{}}
121 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)
126
127 var result UpdateResult
128 _, result.Err = perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{
129 MoreHeaders: client.Provider.AuthenticatedHeaders(),
130 ReqBody: &reqBody,
131 Results: &result.Resp,
132 OkCodes: []int{200},
133 })
134 return result
135}
136
137// Delete removes an endpoint from the service catalog.
138func Delete(client *gophercloud.ServiceClient, endpointID string) error {
139 _, err := perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{
140 MoreHeaders: client.Provider.AuthenticatedHeaders(),
141 OkCodes: []int{204},
142 })
143 return err
144}