Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 4 | "errors" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 5 | "fmt" |
| 6 | "net/url" |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame^] | 7 | "strings" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 8 | |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 10 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 14 | const ( |
| 15 | v20 = "v2.0" |
| 16 | v30 = "v3.0" |
| 17 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 18 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 19 | // NewClient prepares an unauthenticated ProviderClient instance. |
| 20 | // Most users will probably prefer using the AuthenticatedClient function instead. |
| 21 | // This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly, |
| 22 | // for example. |
| 23 | func NewClient(endpoint string) (*gophercloud.ProviderClient, error) { |
| 24 | // Normalize the identity endpoint that's provided by trimming any path, query or fragment from the URL. |
| 25 | u, err := url.Parse(endpoint) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 30 | normalized := u.String() |
| 31 | |
| 32 | return &gophercloud.ProviderClient{ |
| 33 | IdentityEndpoint: normalized, |
| 34 | Reauthenticate: func() error { |
| 35 | return errors.New("Unable to reauthenticate before authenticating the first time.") |
| 36 | }, |
| 37 | }, nil |
| 38 | } |
| 39 | |
| 40 | // AuthenticatedClient logs in to an OpenStack cloud found at the identity endpoint specified by options, acquires a token, and |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 41 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 42 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 43 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 44 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 45 | client, err := NewClient(options.IdentityEndpoint) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | return client, nil |
| 55 | } |
| 56 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 57 | // Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 58 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 59 | versions := []*utils.Version{ |
| 60 | &utils.Version{ID: v20, Priority: 20}, |
| 61 | &utils.Version{ID: v30, Priority: 30}, |
| 62 | } |
| 63 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 64 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 65 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 66 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame^] | 69 | if !strings.HasSuffix(endpoint, "/") { |
| 70 | endpoint = endpoint + "/" |
| 71 | } |
| 72 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 73 | switch chosen.ID { |
| 74 | case v20: |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 75 | return errors.New("Not implemented yet.") |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 76 | case v30: |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 77 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 78 | v3Client := NewIdentityV3(client) |
| 79 | v3Client.Endpoint = endpoint |
| 80 | |
| 81 | result, err := tokens3.Create(v3Client, options, nil) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | client.TokenID, err = result.TokenID() |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | return nil |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 92 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 93 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 94 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 98 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 99 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame^] | 100 | v2Endpoint := client.IdentityEndpoint + "/v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 101 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 102 | return &gophercloud.ServiceClient{ |
| 103 | Provider: client, |
| 104 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 105 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 108 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 109 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame^] | 110 | v3Endpoint := client.IdentityEndpoint + "/v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 111 | |
| 112 | return &gophercloud.ServiceClient{ |
| 113 | Provider: client, |
| 114 | Endpoint: v3Endpoint, |
| 115 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 116 | } |