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