Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 5 | "strings" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 6 | |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 8 | identity2 "github.com/rackspace/gophercloud/openstack/identity/v2" |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 9 | endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints" |
| 10 | services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services" |
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) { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame^] | 25 | if !strings.HasSuffix(endpoint, "/") { |
| 26 | endpoint = endpoint + "/" |
| 27 | } |
| 28 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 29 | return &gophercloud.ProviderClient{IdentityEndpoint: endpoint}, nil |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // 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] | 33 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 34 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 35 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 36 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 37 | client, err := NewClient(options.IdentityEndpoint) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | return client, nil |
| 47 | } |
| 48 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 49 | // 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] | 50 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 51 | versions := []*utils.Version{ |
| 52 | &utils.Version{ID: v20, Priority: 20}, |
| 53 | &utils.Version{ID: v30, Priority: 30}, |
| 54 | } |
| 55 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 56 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 57 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 58 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | switch chosen.ID { |
| 62 | case v20: |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 63 | v2Client := NewIdentityV2(client) |
| 64 | v2Client.Endpoint = endpoint |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 65 | |
| 66 | result, err := identity2.Authenticate(v2Client, options) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | token, err := identity2.GetToken(result) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 75 | |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 76 | client.TokenID = token.ID |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 77 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 78 | return v2endpointLocator(result, opts) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 79 | } |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 80 | |
| 81 | return nil |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 82 | case v30: |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 83 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 84 | v3Client := NewIdentityV3(client) |
| 85 | v3Client.Endpoint = endpoint |
| 86 | |
| 87 | result, err := tokens3.Create(v3Client, options, nil) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | client.TokenID, err = result.TokenID() |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 96 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 97 | return v3endpointLocator(v3Client, opts) |
| 98 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 99 | |
| 100 | return nil |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 101 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 102 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 103 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 107 | func v2endpointLocator(authResults identity2.AuthResults, opts gophercloud.EndpointOpts) (string, error) { |
| 108 | catalog, err := identity2.GetServiceCatalog(authResults) |
| 109 | if err != nil { |
| 110 | return "", err |
| 111 | } |
| 112 | |
| 113 | entries, err := catalog.CatalogEntries() |
| 114 | if err != nil { |
| 115 | return "", err |
| 116 | } |
| 117 | |
| 118 | // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame^] | 119 | var endpoints = make([]identity2.Endpoint, 0, 1) |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 120 | for _, entry := range entries { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame^] | 121 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 122 | for _, endpoint := range entry.Endpoints { |
| 123 | if opts.Region == "" || endpoint.Region == opts.Region { |
| 124 | endpoints = append(endpoints, endpoint) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Report an error if the options were ambiguous. |
| 131 | if len(endpoints) == 0 { |
| 132 | return "", gophercloud.ErrEndpointNotFound |
| 133 | } |
| 134 | if len(endpoints) > 1 { |
| 135 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 136 | } |
| 137 | |
| 138 | // Extract the appropriate URL from the matching Endpoint. |
| 139 | for _, endpoint := range endpoints { |
| 140 | switch opts.URLType { |
| 141 | case "public", "": |
| 142 | return endpoint.PublicURL, nil |
| 143 | case "private": |
| 144 | return endpoint.InternalURL, nil |
| 145 | default: |
| 146 | return "", fmt.Errorf("Unexpected URLType in endpoint query: %s", opts.URLType) |
| 147 | } |
| 148 | } |
| 149 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 150 | return "", gophercloud.ErrEndpointNotFound |
| 151 | } |
| 152 | |
| 153 | func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
| 154 | // Transform URLType into an Interface. |
| 155 | var endpointInterface = endpoints3.InterfacePublic |
| 156 | switch opts.URLType { |
| 157 | case "", "public": |
| 158 | endpointInterface = endpoints3.InterfacePublic |
| 159 | case "internal": |
| 160 | endpointInterface = endpoints3.InterfaceInternal |
| 161 | case "admin": |
| 162 | endpointInterface = endpoints3.InterfaceAdmin |
| 163 | default: |
| 164 | return "", fmt.Errorf("Unrecognized URLType: %s", opts.URLType) |
| 165 | } |
| 166 | |
| 167 | // Discover the service we're interested in. |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 168 | serviceResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type}) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 169 | if err != nil { |
| 170 | return "", err |
| 171 | } |
| 172 | |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 173 | allServiceResults, err := gophercloud.AllPages(serviceResults) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 174 | if err != nil { |
| 175 | return "", err |
| 176 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 177 | allServices := services3.AsServices(allServiceResults) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 178 | |
| 179 | if opts.Name != "" { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 180 | filtered := make([]services3.Service, 0, 1) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 181 | for _, service := range allServices { |
| 182 | if service.Name == opts.Name { |
| 183 | filtered = append(filtered, service) |
| 184 | } |
| 185 | } |
| 186 | allServices = filtered |
| 187 | } |
| 188 | |
| 189 | if len(allServices) == 0 { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 190 | return "", gophercloud.ErrServiceNotFound |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 191 | } |
| 192 | if len(allServices) > 1 { |
| 193 | return "", fmt.Errorf("Discovered %d matching services: %#v", len(allServices), allServices) |
| 194 | } |
| 195 | |
| 196 | service := allServices[0] |
| 197 | |
| 198 | // Enumerate the endpoints available for this service. |
| 199 | endpointResults, err := endpoints3.List(v3Client, endpoints3.ListOpts{ |
| 200 | Interface: endpointInterface, |
| 201 | ServiceID: service.ID, |
| 202 | }) |
| 203 | if err != nil { |
| 204 | return "", err |
| 205 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 206 | allEndpoints, err := gophercloud.AllPages(endpointResults) |
| 207 | if err != nil { |
| 208 | return "", err |
| 209 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 210 | endpoints := endpoints3.AsEndpoints(allEndpoints) |
| 211 | |
| 212 | if opts.Name != "" { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 213 | filtered := make([]endpoints3.Endpoint, 0, 1) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 214 | for _, endpoint := range endpoints { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 215 | if opts.Region == "" || endpoint.Region == opts.Region { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 216 | filtered = append(filtered, endpoint) |
| 217 | } |
| 218 | } |
| 219 | endpoints = filtered |
| 220 | } |
| 221 | |
| 222 | if len(endpoints) == 0 { |
| 223 | return "", gophercloud.ErrEndpointNotFound |
| 224 | } |
| 225 | if len(endpoints) > 1 { |
| 226 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 227 | } |
| 228 | |
| 229 | endpoint := endpoints[0] |
| 230 | |
| 231 | return endpoint.URL, nil |
| 232 | } |
| 233 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 234 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 235 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame^] | 236 | v2Endpoint := client.IdentityEndpoint + "v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 237 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 238 | return &gophercloud.ServiceClient{ |
| 239 | Provider: client, |
| 240 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 241 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 244 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 245 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame^] | 246 | v3Endpoint := client.IdentityEndpoint + "v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 247 | |
| 248 | return &gophercloud.ServiceClient{ |
| 249 | Provider: client, |
| 250 | Endpoint: v3Endpoint, |
| 251 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 252 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 253 | |
| 254 | // NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package. |
| 255 | func NewStorageV1(client *gophercloud.ProviderClient, region string) (*gophercloud.ServiceClient, error) { |
| 256 | url, err := client.EndpointLocator(gophercloud.EndpointOpts{Type: "object-store", Name: "swift"}) |
| 257 | if err != nil { |
| 258 | return nil, err |
| 259 | } |
| 260 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 261 | } |