blob: ad0413afe30a293916e1c786bf58d8db5960f31d [file] [log] [blame]
Jon Perritt5eb55b12014-08-18 14:48:23 -05001package identity
2
Jon Perritt8cff5cf2014-08-19 15:44:39 -05003// Client contains information that defines a generic Openstack Client.
Jon Perritt5eb55b12014-08-18 14:48:23 -05004type Client struct {
Jon Perritt8cff5cf2014-08-19 15:44:39 -05005 // Endpoint is the URL against which to authenticate.
6 Endpoint string
7 // Authority holds the results of authenticating against the Endpoint.
Jon Perritt5eb55b12014-08-18 14:48:23 -05008 Authority AuthResults
Jon Perritt8cff5cf2014-08-19 15:44:39 -05009 // Options holds the authentication options. Useful for auto-reauthentication.
10 Options AuthOptions
Jon Perritt5eb55b12014-08-18 14:48:23 -050011}
12
Jon Perritt6e898782014-08-19 15:58:11 -050013// EndpointOpts contains options for finding an endpoint for an Openstack Client.
14type EndpointOpts struct {
Jon Perritt8cff5cf2014-08-19 15:44:39 -050015 // Type is the service type for the client (e.g., "compute", "object-store").
16 // Type is a required field.
17 Type string
18 // Name is the service name for the client (e.g., "nova").
19 // Name is not a required field, but it is used if present. Services can have the
20 // same Type but different Name, which is one example of when both Type and Name are needed.
21 Name string
22 // Region is the region in which the service resides.
Jon Perritt8cff5cf2014-08-19 15:44:39 -050023 Region string
24 // URLType is they type of endpoint to be returned (e.g., "public", "private").
25 // URLType is not required, and defaults to "public".
Jon Perritt5eb55b12014-08-18 14:48:23 -050026 URLType string
27}
28
Jon Perritt8cff5cf2014-08-19 15:44:39 -050029// NewClient returns a generic Openstack Client of type identity.Client. This is a helper function
30// to create a client for the various Openstack services.
31// Example (error checking omitted for brevity):
32// ao, err := utils.AuthOptions()
Jon Perritt6e898782014-08-19 15:58:11 -050033// c, err := ao.NewClient(identity.EndpointOpts{
Jon Perritt8cff5cf2014-08-19 15:44:39 -050034// Type: "compute",
35// Name: "nova",
36// })
37// serversClient := servers.NewClient(c.Endpoint, c.Authority, c.Options)
Jon Perritt6e898782014-08-19 15:58:11 -050038func (ao AuthOptions) NewClient(opts EndpointOpts) (Client, error) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050039 client := Client{
40 Options: ao,
41 }
42
43 ar, err := Authenticate(ao)
44 if err != nil {
45 return client, err
46 }
47
48 client.Authority = ar
49
50 sc, err := GetServiceCatalog(ar)
51 if err != nil {
52 return client, err
53 }
54
55 ces, err := sc.CatalogEntries()
56 if err != nil {
57 return client, err
58 }
59
60 var eps []Endpoint
61
62 if opts.Name != "" {
63 for _, ce := range ces {
64 if ce.Type == opts.Type && ce.Name == opts.Name {
65 eps = ce.Endpoints
66 }
67 }
68 } else {
69 for _, ce := range ces {
70 if ce.Type == opts.Type {
71 eps = ce.Endpoints
72 }
73 }
74 }
75
Jon Perritt5eb55b12014-08-18 14:48:23 -050076 var rep string
77 for _, ep := range eps {
Jon Perritta8c3b812014-08-19 22:02:31 -050078 if ep.Region == opts.Region {
Jon Perritt5eb55b12014-08-18 14:48:23 -050079 switch opts.URLType {
80 case "public":
81 rep = ep.PublicURL
82 case "private":
83 rep = ep.InternalURL
84 default:
85 rep = ep.PublicURL
86 }
87 }
88 }
89
90 client.Endpoint = rep
91
92 return client, nil
93}