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 | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 9 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 13 | const ( |
| 14 | v20 = "v2.0" |
| 15 | v30 = "v3.0" |
| 16 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 17 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 18 | // NewClient prepares an unauthenticated ProviderClient instance. |
| 19 | // Most users will probably prefer using the AuthenticatedClient function instead. |
| 20 | // This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly, |
| 21 | // for example. |
| 22 | func NewClient(endpoint string) (*gophercloud.ProviderClient, error) { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 23 | u, err := url.Parse(endpoint) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | hadPath := u.Path != "" |
| 28 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 29 | base := u.String() |
| 30 | |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 31 | endpoint = gophercloud.NormalizeURL(endpoint) |
| 32 | base = gophercloud.NormalizeURL(base) |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 33 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 34 | if hadPath { |
| 35 | return &gophercloud.ProviderClient{ |
| 36 | IdentityBase: base, |
| 37 | IdentityEndpoint: endpoint, |
| 38 | }, nil |
| 39 | } |
| 40 | |
| 41 | return &gophercloud.ProviderClient{ |
| 42 | IdentityBase: base, |
| 43 | IdentityEndpoint: "", |
| 44 | }, nil |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // 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] | 48 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 49 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 50 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 51 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 52 | client, err := NewClient(options.IdentityEndpoint) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
| 57 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | return client, nil |
| 62 | } |
| 63 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 64 | // 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] | 65 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 66 | versions := []*utils.Version{ |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 67 | &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"}, |
| 68 | &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"}, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 71 | chosen, endpoint, err := utils.ChooseVersion(client, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 72 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 73 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | switch chosen.ID { |
| 77 | case v20: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 78 | return v2auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 79 | case v30: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 80 | return v3auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 81 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 82 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 83 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 87 | // AuthenticateV2 explicitly authenticates against the identity v2 endpoint. |
| 88 | func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 89 | return v2auth(client, "", options) |
| 90 | } |
| 91 | |
| 92 | func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 93 | v2Client := NewIdentityV2(client) |
| 94 | if endpoint != "" { |
| 95 | v2Client.Endpoint = endpoint |
| 96 | } |
| 97 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 98 | result := tokens2.Create(v2Client, tokens2.AuthOptions{AuthOptions: options}) |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 99 | |
| 100 | token, err := result.ExtractToken() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 105 | catalog, err := result.ExtractServiceCatalog() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 110 | if options.AllowReauth { |
Jon Perritt | 6fe7c40 | 2015-02-17 12:24:53 -0700 | [diff] [blame] | 111 | client.ReauthFunc = func() error { |
Masahiro Sano | 1b2bafe | 2015-03-06 23:26:54 +0900 | [diff] [blame] | 112 | client.TokenID = "" |
Jon Perritt | 6fe7c40 | 2015-02-17 12:24:53 -0700 | [diff] [blame] | 113 | return AuthenticateV2(client, options) |
| 114 | } |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 115 | } |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 116 | client.TokenID = token.ID |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 117 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 118 | return V2EndpointURL(catalog, opts) |
| 119 | } |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 120 | |
| 121 | return nil |
| 122 | } |
| 123 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 124 | // AuthenticateV3 explicitly authenticates against the identity v3 service. |
| 125 | func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 126 | return v3auth(client, "", options) |
| 127 | } |
| 128 | |
| 129 | func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 130 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 131 | v3Client := NewIdentityV3(client) |
| 132 | if endpoint != "" { |
| 133 | v3Client.Endpoint = endpoint |
| 134 | } |
| 135 | |
Guillaume Giamarchi | 08b33d5 | 2015-04-01 01:34:17 +0200 | [diff] [blame] | 136 | var scope *tokens3.Scope |
| 137 | if options.TenantID != "" { |
| 138 | scope = &tokens3.Scope{ |
| 139 | ProjectID: options.TenantID, |
| 140 | } |
| 141 | options.TenantID = "" |
| 142 | options.TenantName = "" |
| 143 | } else { |
| 144 | if options.TenantName != "" { |
| 145 | scope = &tokens3.Scope{ |
| 146 | ProjectName: options.TenantName, |
| 147 | DomainID: options.DomainID, |
| 148 | DomainName: options.DomainName, |
| 149 | } |
| 150 | options.TenantName = "" |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | result := tokens3.Create(v3Client, options, scope) |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 155 | |
| 156 | token, err := result.ExtractToken() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 157 | if err != nil { |
| 158 | return err |
| 159 | } |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 160 | |
| 161 | catalog, err := result.ExtractServiceCatalog() |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 166 | client.TokenID = token.ID |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 167 | |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 168 | if options.AllowReauth { |
Jon Perritt | 6fe7c40 | 2015-02-17 12:24:53 -0700 | [diff] [blame] | 169 | client.ReauthFunc = func() error { |
| 170 | return AuthenticateV3(client, options) |
| 171 | } |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 172 | } |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 173 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 174 | return V3EndpointURL(catalog, opts) |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | return nil |
| 178 | } |
| 179 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 180 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 181 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 182 | v2Endpoint := client.IdentityBase + "v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 183 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 184 | return &gophercloud.ServiceClient{ |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 185 | ProviderClient: client, |
| 186 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 187 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 190 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 191 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 192 | v3Endpoint := client.IdentityBase + "v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 193 | |
| 194 | return &gophercloud.ServiceClient{ |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 195 | ProviderClient: client, |
| 196 | Endpoint: v3Endpoint, |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 197 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 198 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 199 | |
Jon Perritt | bb5e981 | 2014-10-15 17:53:44 -0500 | [diff] [blame] | 200 | // NewObjectStorageV1 creates a ServiceClient that may be used with the v1 object storage package. |
| 201 | func NewObjectStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
Jon Perritt | 509fbb6 | 2014-09-10 13:29:56 -0500 | [diff] [blame] | 202 | eo.ApplyDefaults("object-store") |
| 203 | url, err := client.EndpointLocator(eo) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 204 | if err != nil { |
| 205 | return nil, err |
| 206 | } |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 207 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 208 | } |
Ash Wilson | 5e57c1b | 2014-09-17 09:24:46 -0400 | [diff] [blame] | 209 | |
| 210 | // NewComputeV2 creates a ServiceClient that may be used with the v2 compute package. |
| 211 | func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 212 | eo.ApplyDefaults("compute") |
| 213 | url, err := client.EndpointLocator(eo) |
| 214 | if err != nil { |
| 215 | return nil, err |
| 216 | } |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 217 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
Ash Wilson | 5e57c1b | 2014-09-17 09:24:46 -0400 | [diff] [blame] | 218 | } |
Ash Wilson | ebc3d12 | 2014-09-24 13:44:05 -0400 | [diff] [blame] | 219 | |
| 220 | // 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] | 221 | func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 222 | eo.ApplyDefaults("network") |
| 223 | url, err := client.EndpointLocator(eo) |
| 224 | if err != nil { |
| 225 | return nil, err |
| 226 | } |
Ash Wilson | 99541ab | 2014-10-06 17:32:39 -0400 | [diff] [blame] | 227 | return &gophercloud.ServiceClient{ |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 228 | ProviderClient: client, |
| 229 | Endpoint: url, |
| 230 | ResourceBase: url + "v2.0/", |
Ash Wilson | 99541ab | 2014-10-06 17:32:39 -0400 | [diff] [blame] | 231 | }, nil |
Jamie Hannaford | 7ea2958 | 2014-09-11 15:49:46 +0200 | [diff] [blame] | 232 | } |
Jon Perritt | c5ee85e | 2014-09-17 00:53:19 -0500 | [diff] [blame] | 233 | |
| 234 | // NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service. |
| 235 | func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 236 | eo.ApplyDefaults("volume") |
| 237 | url, err := client.EndpointLocator(eo) |
| 238 | if err != nil { |
| 239 | return nil, err |
| 240 | } |
Ash Wilson | 92c380c | 2014-10-22 09:14:53 -0400 | [diff] [blame] | 241 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
Jon Perritt | c5ee85e | 2014-09-17 00:53:19 -0500 | [diff] [blame] | 242 | } |
Jon Perritt | ebd18ec | 2015-01-16 09:13:31 -0700 | [diff] [blame] | 243 | |
| 244 | // NewCDNV1 creates a ServiceClient that may be used to access the OpenStack v1 |
| 245 | // CDN service. |
| 246 | func NewCDNV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 247 | eo.ApplyDefaults("cdn") |
| 248 | url, err := client.EndpointLocator(eo) |
| 249 | if err != nil { |
| 250 | return nil, err |
| 251 | } |
| 252 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
| 253 | } |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 254 | |
| 255 | // NewOrchestrationV1 creates a ServiceClient that may be used to access the v1 orchestration service. |
| 256 | func NewOrchestrationV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 257 | eo.ApplyDefaults("orchestration") |
| 258 | url, err := client.EndpointLocator(eo) |
| 259 | if err != nil { |
| 260 | return nil, err |
| 261 | } |
| 262 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
| 263 | } |
Jamie Hannaford | 05d200d | 2015-02-20 14:49:05 +0100 | [diff] [blame] | 264 | |
| 265 | func NewDBV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 266 | eo.ApplyDefaults("database") |
| 267 | url, err := client.EndpointLocator(eo) |
| 268 | if err != nil { |
| 269 | return nil, err |
| 270 | } |
| 271 | return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil |
| 272 | } |