Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 1 | package identity |
| 2 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 3 | // Client contains information that defines a generic Openstack Client. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 4 | type Client struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 5 | // Endpoint is the URL against which to authenticate. |
| 6 | Endpoint string |
| 7 | // Authority holds the results of authenticating against the Endpoint. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 8 | Authority AuthResults |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 9 | // Options holds the authentication options. Useful for auto-reauthentication. |
| 10 | Options AuthOptions |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 11 | } |
| 12 | |
Jon Perritt | 6e89878 | 2014-08-19 15:58:11 -0500 | [diff] [blame] | 13 | // EndpointOpts contains options for finding an endpoint for an Openstack Client. |
| 14 | type EndpointOpts struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 15 | // 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 Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 23 | 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 Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 26 | URLType string |
| 27 | } |
| 28 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 29 | // 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 Perritt | 6e89878 | 2014-08-19 15:58:11 -0500 | [diff] [blame] | 33 | // c, err := ao.NewClient(identity.EndpointOpts{ |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 34 | // Type: "compute", |
| 35 | // Name: "nova", |
| 36 | // }) |
| 37 | // serversClient := servers.NewClient(c.Endpoint, c.Authority, c.Options) |
Jon Perritt | 6e89878 | 2014-08-19 15:58:11 -0500 | [diff] [blame] | 38 | func (ao AuthOptions) NewClient(opts EndpointOpts) (Client, error) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 39 | 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 Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 76 | var rep string |
| 77 | for _, ep := range eps { |
Jon Perritt | a8c3b81 | 2014-08-19 22:02:31 -0500 | [diff] [blame^] | 78 | if ep.Region == opts.Region { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 79 | 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 | } |