Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 1 | package utils |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 2 | |
Jon Perritt | e1ce298 | 2014-08-19 22:25:08 -0500 | [diff] [blame] | 3 | import ( |
| 4 | "fmt" |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 5 | identity "github.com/rackspace/gophercloud/openstack/identity/v2" |
Jon Perritt | e1ce298 | 2014-08-19 22:25:08 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 8 | // Client contains information that defines a generic Openstack Client. |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 9 | type Client struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 10 | // Endpoint is the URL against which to authenticate. |
| 11 | Endpoint string |
| 12 | // Authority holds the results of authenticating against the Endpoint. |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 13 | Authority identity.AuthResults |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 14 | // Options holds the authentication options. Useful for auto-reauthentication. |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 15 | Options identity.AuthOptions |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 16 | } |
| 17 | |
Jon Perritt | 6e89878 | 2014-08-19 15:58:11 -0500 | [diff] [blame] | 18 | // EndpointOpts contains options for finding an endpoint for an Openstack Client. |
| 19 | type EndpointOpts struct { |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 20 | // Type is the service type for the client (e.g., "compute", "object-store"). |
| 21 | // Type is a required field. |
| 22 | Type string |
| 23 | // Name is the service name for the client (e.g., "nova"). |
| 24 | // Name is not a required field, but it is used if present. Services can have the |
| 25 | // same Type but different Name, which is one example of when both Type and Name are needed. |
| 26 | Name string |
| 27 | // Region is the region in which the service resides. |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 28 | Region string |
| 29 | // URLType is they type of endpoint to be returned (e.g., "public", "private"). |
| 30 | // URLType is not required, and defaults to "public". |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 31 | URLType string |
| 32 | } |
| 33 | |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 34 | // NewClient returns a generic Openstack Client of type identity.Client. This is a helper function |
| 35 | // to create a client for the various Openstack services. |
| 36 | // Example (error checking omitted for brevity): |
| 37 | // ao, err := utils.AuthOptions() |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 38 | // c, err := identity.NewClient(ao, identity.EndpointOpts{ |
Jon Perritt | 8cff5cf | 2014-08-19 15:44:39 -0500 | [diff] [blame] | 39 | // Type: "compute", |
| 40 | // Name: "nova", |
| 41 | // }) |
| 42 | // serversClient := servers.NewClient(c.Endpoint, c.Authority, c.Options) |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 43 | func NewClient(ao identity.AuthOptions, eo EndpointOpts) (Client, error) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 44 | client := Client{ |
| 45 | Options: ao, |
| 46 | } |
| 47 | |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 48 | ar, err := identity.Authenticate(ao) |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 49 | if err != nil { |
| 50 | return client, err |
| 51 | } |
| 52 | |
| 53 | client.Authority = ar |
| 54 | |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 55 | sc, err := identity.GetServiceCatalog(ar) |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 56 | if err != nil { |
| 57 | return client, err |
| 58 | } |
| 59 | |
| 60 | ces, err := sc.CatalogEntries() |
| 61 | if err != nil { |
| 62 | return client, err |
| 63 | } |
| 64 | |
Jon Perritt | 5f4b5c2 | 2014-08-27 11:44:07 -0500 | [diff] [blame] | 65 | var eps []identity.Endpoint |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 66 | |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 67 | if eo.Name != "" { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 68 | for _, ce := range ces { |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 69 | if ce.Type == eo.Type && ce.Name == eo.Name { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 70 | eps = ce.Endpoints |
| 71 | } |
| 72 | } |
| 73 | } else { |
| 74 | for _, ce := range ces { |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 75 | if ce.Type == eo.Type { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 76 | eps = ce.Endpoints |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 81 | var rep string |
| 82 | for _, ep := range eps { |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 83 | if ep.Region == eo.Region { |
| 84 | switch eo.URLType { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 85 | case "public": |
| 86 | rep = ep.PublicURL |
| 87 | case "private": |
| 88 | rep = ep.InternalURL |
| 89 | default: |
| 90 | rep = ep.PublicURL |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Jon Perritt | e1ce298 | 2014-08-19 22:25:08 -0500 | [diff] [blame] | 95 | if rep != "" { |
| 96 | client.Endpoint = rep |
| 97 | } else { |
Jon Perritt | aab1fcd | 2014-08-27 11:21:45 -0500 | [diff] [blame] | 98 | return client, fmt.Errorf("No endpoint for given service type (%s) name (%s) and region (%s)", eo.Type, eo.Name, eo.Region) |
Jon Perritt | e1ce298 | 2014-08-19 22:25:08 -0500 | [diff] [blame] | 99 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 100 | |
| 101 | return client, nil |
| 102 | } |