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