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 | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame^] | 11 | endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints" |
| 12 | services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 13 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 15 | ) |
| 16 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 17 | const ( |
| 18 | v20 = "v2.0" |
| 19 | v30 = "v3.0" |
| 20 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 21 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 22 | // NewClient prepares an unauthenticated ProviderClient instance. |
| 23 | // Most users will probably prefer using the AuthenticatedClient function instead. |
| 24 | // This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly, |
| 25 | // for example. |
| 26 | func NewClient(endpoint string) (*gophercloud.ProviderClient, error) { |
| 27 | // Normalize the identity endpoint that's provided by trimming any path, query or fragment from the URL. |
| 28 | u, err := url.Parse(endpoint) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 33 | normalized := u.String() |
| 34 | |
| 35 | return &gophercloud.ProviderClient{ |
| 36 | IdentityEndpoint: normalized, |
| 37 | Reauthenticate: func() error { |
| 38 | return errors.New("Unable to reauthenticate before authenticating the first time.") |
| 39 | }, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | // 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] | 44 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 45 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 46 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 47 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 48 | client, err := NewClient(options.IdentityEndpoint) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return client, nil |
| 58 | } |
| 59 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 60 | // 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] | 61 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 62 | versions := []*utils.Version{ |
| 63 | &utils.Version{ID: v20, Priority: 20}, |
| 64 | &utils.Version{ID: v30, Priority: 30}, |
| 65 | } |
| 66 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 67 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 68 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 69 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 72 | if !strings.HasSuffix(endpoint, "/") { |
| 73 | endpoint = endpoint + "/" |
| 74 | } |
| 75 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 76 | switch chosen.ID { |
| 77 | case v20: |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 78 | v2Client := NewIdentityV2(client) |
| 79 | v2Client.Endpoint = endpoint |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 80 | |
| 81 | result, err := identity2.Authenticate(v2Client, options) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | token, err := identity2.GetToken(result) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame^] | 90 | |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 91 | client.TokenID = token.ID |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame^] | 92 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 93 | return v2endpointLocator(v2Client, opts) |
| 94 | } |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 95 | |
| 96 | return nil |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 97 | case v30: |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 98 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 99 | v3Client := NewIdentityV3(client) |
| 100 | v3Client.Endpoint = endpoint |
| 101 | |
| 102 | result, err := tokens3.Create(v3Client, options, nil) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | client.TokenID, err = result.TokenID() |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame^] | 111 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 112 | return v3endpointLocator(v3Client, opts) |
| 113 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 114 | |
| 115 | return nil |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 116 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 117 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 118 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame^] | 122 | func v2endpointLocator(v2Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
| 123 | return "", gophercloud.ErrEndpointNotFound |
| 124 | } |
| 125 | |
| 126 | func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
| 127 | // Transform URLType into an Interface. |
| 128 | var endpointInterface = endpoints3.InterfacePublic |
| 129 | switch opts.URLType { |
| 130 | case "", "public": |
| 131 | endpointInterface = endpoints3.InterfacePublic |
| 132 | case "internal": |
| 133 | endpointInterface = endpoints3.InterfaceInternal |
| 134 | case "admin": |
| 135 | endpointInterface = endpoints3.InterfaceAdmin |
| 136 | default: |
| 137 | return "", fmt.Errorf("Unrecognized URLType: %s", opts.URLType) |
| 138 | } |
| 139 | |
| 140 | // Discover the service we're interested in. |
| 141 | computeResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type}) |
| 142 | if err != nil { |
| 143 | return "", err |
| 144 | } |
| 145 | |
| 146 | serviceResults, err := gophercloud.AllPages(computeResults) |
| 147 | if err != nil { |
| 148 | return "", err |
| 149 | } |
| 150 | allServices := services3.AsServices(serviceResults) |
| 151 | |
| 152 | if opts.Name != "" { |
| 153 | filtered := make([]services3.Service, 1) |
| 154 | for _, service := range allServices { |
| 155 | if service.Name == opts.Name { |
| 156 | filtered = append(filtered, service) |
| 157 | } |
| 158 | } |
| 159 | allServices = filtered |
| 160 | } |
| 161 | |
| 162 | if len(allServices) == 0 { |
| 163 | return "", gophercloud.ErrEndpointNotFound |
| 164 | } |
| 165 | if len(allServices) > 1 { |
| 166 | return "", fmt.Errorf("Discovered %d matching services: %#v", len(allServices), allServices) |
| 167 | } |
| 168 | |
| 169 | service := allServices[0] |
| 170 | |
| 171 | // Enumerate the endpoints available for this service. |
| 172 | endpointResults, err := endpoints3.List(v3Client, endpoints3.ListOpts{ |
| 173 | Interface: endpointInterface, |
| 174 | ServiceID: service.ID, |
| 175 | }) |
| 176 | if err != nil { |
| 177 | return "", err |
| 178 | } |
| 179 | |
| 180 | allEndpoints, err := gophercloud.AllPages(endpointResults) |
| 181 | if err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | |
| 185 | endpoints := endpoints3.AsEndpoints(allEndpoints) |
| 186 | |
| 187 | if opts.Name != "" { |
| 188 | filtered := make([]endpoints3.Endpoint, 1) |
| 189 | for _, endpoint := range endpoints { |
| 190 | if endpoint.Region == opts.Region { |
| 191 | filtered = append(filtered, endpoint) |
| 192 | } |
| 193 | } |
| 194 | endpoints = filtered |
| 195 | } |
| 196 | |
| 197 | if len(endpoints) == 0 { |
| 198 | return "", gophercloud.ErrEndpointNotFound |
| 199 | } |
| 200 | if len(endpoints) > 1 { |
| 201 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 202 | } |
| 203 | |
| 204 | endpoint := endpoints[0] |
| 205 | |
| 206 | return endpoint.URL, nil |
| 207 | } |
| 208 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 209 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 210 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 211 | v2Endpoint := client.IdentityEndpoint + "/v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 212 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 213 | return &gophercloud.ServiceClient{ |
| 214 | Provider: client, |
| 215 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 216 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 219 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 220 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 221 | v3Endpoint := client.IdentityEndpoint + "/v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 222 | |
| 223 | return &gophercloud.ServiceClient{ |
| 224 | Provider: client, |
| 225 | Endpoint: v3Endpoint, |
| 226 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 227 | } |