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 | 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 | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 8 | tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
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 | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
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 | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 34 | endpoint = gophercloud.NormalizeURL(endpoint) |
| 35 | base = gophercloud.NormalizeURL(base) |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 36 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 37 | if hadPath { |
| 38 | return &gophercloud.ProviderClient{ |
| 39 | IdentityBase: base, |
| 40 | IdentityEndpoint: endpoint, |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | return &gophercloud.ProviderClient{ |
| 45 | IdentityBase: base, |
| 46 | IdentityEndpoint: "", |
| 47 | }, nil |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // 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] | 51 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 52 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 53 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 54 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 55 | client, err := NewClient(options.IdentityEndpoint) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return client, nil |
| 65 | } |
| 66 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 67 | // 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] | 68 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 69 | versions := []*utils.Version{ |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 70 | &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"}, |
| 71 | &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"}, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 74 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 75 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 76 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | switch chosen.ID { |
| 80 | case v20: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 81 | return v2auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 82 | case v30: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 83 | return v3auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 84 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 85 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 86 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 90 | // AuthenticateV2 explicitly authenticates against the identity v2 endpoint. |
| 91 | func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 92 | return v2auth(client, "", options) |
| 93 | } |
| 94 | |
| 95 | func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 96 | v2Client := NewIdentityV2(client) |
| 97 | if endpoint != "" { |
| 98 | v2Client.Endpoint = endpoint |
| 99 | } |
| 100 | |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 101 | result := tokens2.Create(v2Client, options) |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 102 | |
| 103 | token, err := result.ExtractToken() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 108 | catalog, err := result.ExtractServiceCatalog() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | client.TokenID = token.ID |
Ash Wilson | 405f310 | 2014-10-07 10:21:46 -0400 | [diff] [blame] | 114 | client.EndpointLocator = catalog.EndpointURL |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 119 | // AuthenticateV3 explicitly authenticates against the identity v3 service. |
| 120 | func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 121 | return v3auth(client, "", options) |
| 122 | } |
| 123 | |
| 124 | func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 125 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 126 | v3Client := NewIdentityV3(client) |
| 127 | if endpoint != "" { |
| 128 | v3Client.Endpoint = endpoint |
| 129 | } |
| 130 | |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 131 | token, err := tokens3.Create(v3Client, options, nil).Extract() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 132 | if err != nil { |
| 133 | return err |
| 134 | } |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 135 | client.TokenID = token.ID |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 136 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 137 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 138 | return v3endpointLocator(v3Client, opts) |
| 139 | } |
| 140 | |
| 141 | return nil |
| 142 | } |
| 143 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 144 | func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 145 | // Discover the service we're interested in. |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 146 | var services = make([]services3.Service, 0, 1) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 147 | servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type}) |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 148 | err := servicePager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 149 | part, err := services3.ExtractServices(page) |
| 150 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 151 | return false, err |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 152 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 153 | |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 154 | for _, service := range part { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 155 | if service.Name == opts.Name { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 156 | services = append(services, service) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 159 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 160 | return true, nil |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 161 | }) |
| 162 | if err != nil { |
| 163 | return "", err |
| 164 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 165 | |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 166 | if len(services) == 0 { |
| 167 | return "", gophercloud.ErrServiceNotFound |
| 168 | } |
| 169 | if len(services) > 1 { |
| 170 | return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services) |
| 171 | } |
| 172 | service := services[0] |
| 173 | |
| 174 | // Enumerate the endpoints available for this service. |
| 175 | var endpoints []endpoints3.Endpoint |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 176 | endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{ |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 177 | Availability: opts.Availability, |
| 178 | ServiceID: service.ID, |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 179 | }) |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 180 | err = endpointPager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 181 | part, err := endpoints3.ExtractEndpoints(page) |
| 182 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 183 | return false, err |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | for _, endpoint := range part { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 187 | if opts.Region == "" || endpoint.Region == opts.Region { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 188 | endpoints = append(endpoints, endpoint) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 189 | } |
| 190 | } |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 191 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 192 | return true, nil |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 193 | }) |
| 194 | if err != nil { |
| 195 | return "", err |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if len(endpoints) == 0 { |
| 199 | return "", gophercloud.ErrEndpointNotFound |
| 200 | } |
| 201 | if len(endpoints) > 1 { |
| 202 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 203 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 204 | endpoint := endpoints[0] |
| 205 | |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 206 | return gophercloud.NormalizeURL(endpoint.URL), nil |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 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 | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 211 | v2Endpoint := client.IdentityBase + "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 | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 221 | v3Endpoint := client.IdentityBase + "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 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 228 | |
| 229 | // NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package. |
Jon Perritt | 509fbb6 | 2014-09-10 13:29:56 -0500 | [diff] [blame] | 230 | func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 231 | eo.ApplyDefaults("object-store") |
| 232 | url, err := client.EndpointLocator(eo) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 237 | } |
Ash Wilson | 5e57c1b | 2014-09-17 09:24:46 -0400 | [diff] [blame] | 238 | |
| 239 | // NewComputeV2 creates a ServiceClient that may be used with the v2 compute package. |
| 240 | func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 241 | eo.ApplyDefaults("compute") |
| 242 | url, err := client.EndpointLocator(eo) |
| 243 | if err != nil { |
| 244 | return nil, err |
| 245 | } |
| 246 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 247 | } |
Ash Wilson | ebc3d12 | 2014-09-24 13:44:05 -0400 | [diff] [blame] | 248 | |
| 249 | // NewNetworkV2 creates a ServiceClient that may be used with the v2 network package. |
Jamie Hannaford | 7ea2958 | 2014-09-11 15:49:46 +0200 | [diff] [blame] | 250 | func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 251 | eo.ApplyDefaults("network") |
| 252 | url, err := client.EndpointLocator(eo) |
| 253 | if err != nil { |
| 254 | return nil, err |
| 255 | } |
Ash Wilson | 99541ab | 2014-10-06 17:32:39 -0400 | [diff] [blame] | 256 | return &gophercloud.ServiceClient{ |
| 257 | Provider: client, |
| 258 | Endpoint: url, |
| 259 | ResourceBase: url + "v2.0/", |
| 260 | }, nil |
Jamie Hannaford | 7ea2958 | 2014-09-11 15:49:46 +0200 | [diff] [blame] | 261 | } |
Jon Perritt | c5ee85e | 2014-09-17 00:53:19 -0500 | [diff] [blame] | 262 | |
| 263 | // NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service. |
| 264 | func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 265 | eo.ApplyDefaults("volume") |
| 266 | url, err := client.EndpointLocator(eo) |
| 267 | if err != nil { |
| 268 | return nil, err |
| 269 | } |
| 270 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 271 | } |