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