20
21
ErrMultipleCredentials = errors.New("more than one credential detected")
24
// GetCredentialsParams contains parameters for the GetCredentials function.
25
type GetCredentialsParams struct {
26
// Cloud is the cloud definition.
29
// CloudName is the name of the cloud for which credentials are being
33
// CloudRegion is the name of the region that the user has specified.
34
// If this is empty, then GetCredentials will determine the default
35
// region, and return that. The default region is the one set by the
36
// user in credentials.yaml, or if there is none set, the first region
37
// in the cloud's list.
40
// CredentialName is the name of the credential to get.
23
44
// GetCredentials returns a curated set of credential values for a given cloud.
24
45
// The credential key values are read from the credentials store and the provider
25
46
// finalises the values to resolve things like json files.
26
47
// If region is not specified, the default credential region is used.
27
48
func GetCredentials(
28
store jujuclient.CredentialGetter, region, credentialName, cloudName, cloudType string,
50
store jujuclient.CredentialGetter,
51
args GetCredentialsParams,
29
52
) (_ *cloud.Credential, chosenCredentialName, regionName string, _ error) {
31
54
credential, credentialName, defaultRegion, err := credentialByName(
32
store, cloudName, credentialName,
55
store, args.CloudName, args.CredentialName,
35
58
return nil, "", "", errors.Trace(err)
61
regionName = args.CloudRegion
39
62
if regionName == "" {
40
63
regionName = defaultRegion
64
if regionName == "" && len(args.Cloud.Regions) > 0 {
65
// No region was specified, use the first region
67
regionName = args.Cloud.Regions[0].Name
71
cloudEndpoint := args.Cloud.Endpoint
72
cloudIdentityEndpoint := args.Cloud.IdentityEndpoint
74
region, err := cloud.RegionByName(args.Cloud.Regions, regionName)
76
return nil, "", "", errors.Trace(err)
78
cloudEndpoint = region.Endpoint
79
cloudIdentityEndpoint = region.IdentityEndpoint
43
82
readFile := func(f string) ([]byte, error) {
61
100
return nil, "", "", errors.Annotatef(
62
err, "validating %q credential for cloud %q",
63
credentialName, cloudName,
101
err, "finalizing %q credential for cloud %q",
102
credentialName, args.CloudName,
106
credential, err = provider.FinalizeCredential(
107
ctx, environs.FinalizeCredentialParams{
108
Credential: *credential,
109
CloudEndpoint: cloudEndpoint,
110
CloudIdentityEndpoint: cloudIdentityEndpoint,
114
return nil, "", "", errors.Annotatef(
115
err, "finalizing %q credential for cloud %q",
116
credentialName, args.CloudName,
66
120
return credential, credentialName, regionName, nil