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