blob: 3f7811afc92909e00dc012efb838ea4ca740f308 [file] [log] [blame]
Ash Wilson0a997f82014-09-03 15:50:52 -04001package endpoints
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
4 "errors"
Ash Wilson989ce542014-09-04 10:52:49 -04005 "strconv"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04006
Ash Wilson989ce542014-09-04 10:52:49 -04007 "github.com/racker/perigee"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04008 "github.com/rackspace/gophercloud"
Ash Wilson989ce542014-09-04 10:52:49 -04009 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040010)
11
12// Interface describes the availability of a specific service endpoint.
13type Interface string
14
15const (
16 // InterfaceAdmin makes an endpoint only available to administrators.
17 InterfaceAdmin Interface = "admin"
18
19 // InterfacePublic makes an endpoint available to everyone.
20 InterfacePublic Interface = "public"
21
22 // InterfaceInternal makes an endpoint only available within the cluster.
23 InterfaceInternal Interface = "internal"
24)
25
26// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
27type EndpointOpts struct {
28 Interface Interface
29 Name string
30 Region string
31 URL string
32 ServiceID string
33}
34
35// Create inserts a new Endpoint into the service catalog.
Ash Wilson989ce542014-09-04 10:52:49 -040036// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
Ash Wilsonbdfc3302014-09-04 10:16:28 -040037func Create(client *gophercloud.ServiceClient, opts EndpointOpts) (*Endpoint, error) {
Ash Wilson989ce542014-09-04 10:52:49 -040038 // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
39 type endpoint struct {
40 Interface string `json:"interface"`
41 Name string `json:"name"`
42 Region *string `json:"region,omitempty"`
43 URL string `json:"url"`
44 ServiceID string `json:"service_id"`
45 }
46
47 type request struct {
48 Endpoint endpoint `json:"endpoint"`
49 }
50
51 type response struct {
52 Endpoint Endpoint `json:"endpoint"`
53 }
54
55 // Ensure that EndpointOpts is fully populated.
56 if opts.Interface == "" {
57 return nil, ErrInterfaceRequired
58 }
59 if opts.Name == "" {
60 return nil, ErrNameRequired
61 }
62 if opts.URL == "" {
63 return nil, ErrURLRequired
64 }
65 if opts.ServiceID == "" {
66 return nil, ErrServiceIDRequired
67 }
68
69 // Populate the request body.
70 reqBody := request{
71 Endpoint: endpoint{
72 Interface: string(opts.Interface),
73 Name: opts.Name,
74 URL: opts.URL,
75 ServiceID: opts.ServiceID,
76 },
77 }
78
79 if opts.Region != "" {
80 reqBody.Endpoint.Region = &opts.Region
81 }
82
83 var respBody response
84 _, err := perigee.Request("POST", getListURL(client), perigee.Options{
85 MoreHeaders: client.Provider.AuthenticatedHeaders(),
86 ReqBody: &reqBody,
87 Results: &respBody,
88 OkCodes: []int{201},
89 })
90 if err != nil {
91 return nil, err
92 }
93
94 return &respBody.Endpoint, nil
Ash Wilsonbdfc3302014-09-04 10:16:28 -040095}
96
97// ListOpts allows finer control over the the endpoints returned by a List call.
98// All fields are optional.
99type ListOpts struct {
100 Interface Interface
101 ServiceID string
102 Page int
103 PerPage int
104}
105
106// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
107func List(client *gophercloud.ServiceClient, opts ListOpts) (*EndpointList, error) {
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400108 q := make(map[string]string)
109 if opts.Interface != "" {
110 q["interface"] = string(opts.Interface)
111 }
112 if opts.ServiceID != "" {
113 q["service_id"] = opts.ServiceID
114 }
115 if opts.Page != 0 {
116 q["page"] = strconv.Itoa(opts.Page)
117 }
118 if opts.PerPage != 0 {
119 q["per_page"] = strconv.Itoa(opts.Page)
120 }
121
122 u := getListURL(client) + utils.BuildQuery(q)
123
124 var respBody []Endpoint
125 _, err := perigee.Request("GET", u, perigee.Options{
126 MoreHeaders: client.Provider.AuthenticatedHeaders(),
127 Results: &respBody,
128 OkCodes: []int{200},
129 })
130 if err != nil {
131 return nil, err
132 }
133
134 return &EndpointList{Endpoints: respBody}, nil
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400135}
136
137// Update changes an existing endpoint with new data.
138func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) (*Endpoint, error) {
139 return nil, errors.New("Not implemented")
140}
141
142// Delete removes an endpoint from the service catalog.
143func Delete(client *gophercloud.ServiceClient, endpointID string) error {
144 return errors.New("Not implemented")
145}