blob: 4bec42762bb2c34f5cce0b0f34fd5e197c262ff3 [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 Wilson3c8cc772014-09-16 11:40:49 -04008 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04009)
10
Ash Wilsonbdfc3302014-09-04 10:16:28 -040011// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
12type EndpointOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040013 Availability gophercloud.Availability
14 Name string
15 Region string
16 URL string
17 ServiceID string
Ash Wilsonbdfc3302014-09-04 10:16:28 -040018}
19
20// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040021// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Ash Wilson74e2bb82014-09-30 17:08:48 -040022func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
Ash Wilson989ce542014-09-04 10:52:49 -040023 // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
24 type endpoint struct {
25 Interface string `json:"interface"`
26 Name string `json:"name"`
27 Region *string `json:"region,omitempty"`
28 URL string `json:"url"`
29 ServiceID string `json:"service_id"`
30 }
31
32 type request struct {
33 Endpoint endpoint `json:"endpoint"`
34 }
35
Ash Wilson989ce542014-09-04 10:52:49 -040036 // Ensure that EndpointOpts is fully populated.
Ash Wilsonefac18b2014-09-10 14:44:42 -040037 if opts.Availability == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040038 return createErr(ErrAvailabilityRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040039 }
40 if opts.Name == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040041 return createErr(ErrNameRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040042 }
43 if opts.URL == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040044 return createErr(ErrURLRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040045 }
46 if opts.ServiceID == "" {
Ash Wilson74e2bb82014-09-30 17:08:48 -040047 return createErr(ErrServiceIDRequired)
Ash Wilson989ce542014-09-04 10:52:49 -040048 }
49
50 // Populate the request body.
51 reqBody := request{
52 Endpoint: endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040053 Interface: string(opts.Availability),
Ash Wilson989ce542014-09-04 10:52:49 -040054 Name: opts.Name,
55 URL: opts.URL,
56 ServiceID: opts.ServiceID,
57 },
58 }
Ash Wilsonb18fc102014-09-30 15:26:01 -040059 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
Ash Wilson989ce542014-09-04 10:52:49 -040060
Ash Wilson74e2bb82014-09-30 17:08:48 -040061 var result CreateResult
62 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040063 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson989ce542014-09-04 10:52:49 -040064 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -040065 Results: &result.Body,
Ash Wilson989ce542014-09-04 10:52:49 -040066 OkCodes: []int{201},
67 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040068 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -040069}
70
71// ListOpts allows finer control over the the endpoints returned by a List call.
72// All fields are optional.
73type ListOpts struct {
Ash Wilsonefac18b2014-09-10 14:44:42 -040074 Availability gophercloud.Availability
75 ServiceID string
76 Page int
77 PerPage int
Ash Wilsonbdfc3302014-09-04 10:16:28 -040078}
79
80// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
Ash Wilson3c8cc772014-09-16 11:40:49 -040081func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson32c0e8d2014-09-04 10:53:08 -040082 q := make(map[string]string)
Ash Wilsonefac18b2014-09-10 14:44:42 -040083 if opts.Availability != "" {
84 q["interface"] = string(opts.Availability)
Ash Wilson32c0e8d2014-09-04 10:53:08 -040085 }
86 if opts.ServiceID != "" {
87 q["service_id"] = opts.ServiceID
88 }
89 if opts.Page != 0 {
90 q["page"] = strconv.Itoa(opts.Page)
91 }
92 if opts.PerPage != 0 {
93 q["per_page"] = strconv.Itoa(opts.Page)
94 }
95
Ash Wilsonb8b16f82014-10-20 10:19:49 -040096 createPage := func(r pagination.PageResult) pagination.Page {
97 return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040098 }
99
Jamie Hannaford53da3162014-10-22 17:03:12 +0200100 u := listURL(client) + gophercloud.BuildQuery(q)
Ash Wilsoncd95a0c2014-09-16 13:07:31 -0400101 return pagination.NewPager(client, u, createPage)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400102}
103
104// Update changes an existing endpoint with new data.
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400105// All fields are optional in the provided EndpointOpts.
Ash Wilson74e2bb82014-09-30 17:08:48 -0400106func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400107 type endpoint struct {
108 Interface *string `json:"interface,omitempty"`
109 Name *string `json:"name,omitempty"`
110 Region *string `json:"region,omitempty"`
111 URL *string `json:"url,omitempty"`
112 ServiceID *string `json:"service_id,omitempty"`
113 }
114
115 type request struct {
116 Endpoint endpoint `json:"endpoint"`
117 }
118
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400119 reqBody := request{Endpoint: endpoint{}}
Ash Wilsonb18fc102014-09-30 15:26:01 -0400120 reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
121 reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
122 reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
123 reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
124 reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400125
Ash Wilson74e2bb82014-09-30 17:08:48 -0400126 var result UpdateResult
127 _, result.Err = perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400128 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400129 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400130 Results: &result.Body,
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400131 OkCodes: []int{200},
132 })
Ash Wilson74e2bb82014-09-30 17:08:48 -0400133 return result
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400134}
135
136// Delete removes an endpoint from the service catalog.
137func Delete(client *gophercloud.ServiceClient, endpointID string) error {
Ash Wilson1e1b7332014-09-30 16:38:11 -0400138 _, err := perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400139 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilson70db2ab2014-09-04 11:18:32 -0400140 OkCodes: []int{204},
141 })
142 return err
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400143}