Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 1 | package identity |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | ) |
| 6 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 7 | // Client contains information that defines a generic Openstack Client. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 8 | type Client struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 9 | // Endpoint is the URL against which to authenticate. |
| 10 | Endpoint string |
| 11 | // Authority holds the results of authenticating against the Endpoint. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 12 | Authority AuthResults |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 13 | // Options holds the authentication options. Useful for auto-reauthentication. |
| 14 | Options AuthOptions |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 15 | } |
| 16 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 17 | // ClientOpts contains options for creating a generic Openstack Client. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 18 | type ClientOpts struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 19 | // 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 Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 32 | URLType string |
| 33 | } |
| 34 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame^] | 35 | // 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 Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 44 | func (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 | } |