blob: d9a5b060f78199961fdece6926c49ac4b6c36f4e [file] [log] [blame]
Jon Perritt5eb55b12014-08-18 14:48:23 -05001package identity
2
3import (
4 "os"
5)
6
Jon Perritt8cff5cf2014-08-19 15:44:39 -05007// Client contains information that defines a generic Openstack Client.
Jon Perritt5eb55b12014-08-18 14:48:23 -05008type Client struct {
Jon Perritt8cff5cf2014-08-19 15:44:39 -05009 // Endpoint is the URL against which to authenticate.
10 Endpoint string
11 // Authority holds the results of authenticating against the Endpoint.
Jon Perritt5eb55b12014-08-18 14:48:23 -050012 Authority AuthResults
Jon Perritt8cff5cf2014-08-19 15:44:39 -050013 // Options holds the authentication options. Useful for auto-reauthentication.
14 Options AuthOptions
Jon Perritt5eb55b12014-08-18 14:48:23 -050015}
16
Jon Perritt8cff5cf2014-08-19 15:44:39 -050017// ClientOpts contains options for creating a generic Openstack Client.
Jon Perritt5eb55b12014-08-18 14:48:23 -050018type ClientOpts struct {
Jon Perritt8cff5cf2014-08-19 15:44:39 -050019 // Type is the service type for the client (e.g., "compute", "object-store").
20 // Type is a required field.
21 Type string
22 // Name is the service name for the client (e.g., "nova").
23 // Name is not a required field, but it is used if present. Services can have the
24 // same Type but different Name, which is one example of when both Type and Name are needed.
25 Name string
26 // Region is the region in which the service resides.
27 // Region is not a required field. If Region is not set, then the OS_REGION_NAME enviroment
28 // variable is used.
29 Region string
30 // URLType is they type of endpoint to be returned (e.g., "public", "private").
31 // URLType is not required, and defaults to "public".
Jon Perritt5eb55b12014-08-18 14:48:23 -050032 URLType string
33}
34
Jon Perritt8cff5cf2014-08-19 15:44:39 -050035// NewClient returns a generic Openstack Client of type identity.Client. This is a helper function
36// to create a client for the various Openstack services.
37// Example (error checking omitted for brevity):
38// ao, err := utils.AuthOptions()
39// c, err := ao.NewClient(identity.ClientOpts{
40// Type: "compute",
41// Name: "nova",
42// })
43// serversClient := servers.NewClient(c.Endpoint, c.Authority, c.Options)
Jon Perritt5eb55b12014-08-18 14:48:23 -050044func (ao AuthOptions) NewClient(opts ClientOpts) (Client, error) {
45 client := Client{
46 Options: ao,
47 }
48
49 ar, err := Authenticate(ao)
50 if err != nil {
51 return client, err
52 }
53
54 client.Authority = ar
55
56 sc, err := GetServiceCatalog(ar)
57 if err != nil {
58 return client, err
59 }
60
61 ces, err := sc.CatalogEntries()
62 if err != nil {
63 return client, err
64 }
65
66 var eps []Endpoint
67
68 if opts.Name != "" {
69 for _, ce := range ces {
70 if ce.Type == opts.Type && ce.Name == opts.Name {
71 eps = ce.Endpoints
72 }
73 }
74 } else {
75 for _, ce := range ces {
76 if ce.Type == opts.Type {
77 eps = ce.Endpoints
78 }
79 }
80 }
81
82 region := os.Getenv("OS_REGION_NAME")
83 if opts.Region != "" {
84 region = opts.Region
85 }
86
87 var rep string
88 for _, ep := range eps {
89 if ep.Region == region {
90 switch opts.URLType {
91 case "public":
92 rep = ep.PublicURL
93 case "private":
94 rep = ep.InternalURL
95 default:
96 rep = ep.PublicURL
97 }
98 }
99 }
100
101 client.Endpoint = rep
102
103 return client, nil
104}