~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/modelcmd/credentials.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "io/ioutil"
8
8
 
 
9
        "github.com/juju/cmd"
9
10
        "github.com/juju/errors"
10
11
        "github.com/juju/utils"
11
12
 
20
21
        ErrMultipleCredentials = errors.New("more than one credential detected")
21
22
)
22
23
 
 
24
// GetCredentialsParams contains parameters for the GetCredentials function.
 
25
type GetCredentialsParams struct {
 
26
        // Cloud is the cloud definition.
 
27
        Cloud cloud.Cloud
 
28
 
 
29
        // CloudName is the name of the cloud for which credentials are being
 
30
        // obtained.
 
31
        CloudName string
 
32
 
 
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.
 
38
        CloudRegion string
 
39
 
 
40
        // CredentialName is the name of the credential to get.
 
41
        CredentialName string
 
42
}
 
43
 
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,
 
49
        ctx *cmd.Context,
 
50
        store jujuclient.CredentialGetter,
 
51
        args GetCredentialsParams,
29
52
) (_ *cloud.Credential, chosenCredentialName, regionName string, _ error) {
30
53
 
31
54
        credential, credentialName, defaultRegion, err := credentialByName(
32
 
                store, cloudName, credentialName,
 
55
                store, args.CloudName, args.CredentialName,
33
56
        )
34
57
        if err != nil {
35
58
                return nil, "", "", errors.Trace(err)
36
59
        }
37
60
 
38
 
        regionName = region
 
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
 
66
                        // in the list.
 
67
                        regionName = args.Cloud.Regions[0].Name
 
68
                }
 
69
        }
 
70
 
 
71
        cloudEndpoint := args.Cloud.Endpoint
 
72
        cloudIdentityEndpoint := args.Cloud.IdentityEndpoint
 
73
        if regionName != "" {
 
74
                region, err := cloud.RegionByName(args.Cloud.Regions, regionName)
 
75
                if err != nil {
 
76
                        return nil, "", "", errors.Trace(err)
 
77
                }
 
78
                cloudEndpoint = region.Endpoint
 
79
                cloudIdentityEndpoint = region.IdentityEndpoint
41
80
        }
42
81
 
43
82
        readFile := func(f string) ([]byte, error) {
49
88
        }
50
89
 
51
90
        // Finalize credential against schemas supported by the provider.
52
 
        provider, err := environs.Provider(cloudType)
 
91
        provider, err := environs.Provider(args.Cloud.Type)
53
92
        if err != nil {
54
93
                return nil, "", "", errors.Trace(err)
55
94
        }
59
98
        )
60
99
        if err != nil {
61
100
                return nil, "", "", errors.Annotatef(
62
 
                        err, "validating %q credential for cloud %q",
63
 
                        credentialName, cloudName,
64
 
                )
65
 
        }
 
101
                        err, "finalizing %q credential for cloud %q",
 
102
                        credentialName, args.CloudName,
 
103
                )
 
104
        }
 
105
 
 
106
        credential, err = provider.FinalizeCredential(
 
107
                ctx, environs.FinalizeCredentialParams{
 
108
                        Credential:            *credential,
 
109
                        CloudEndpoint:         cloudEndpoint,
 
110
                        CloudIdentityEndpoint: cloudIdentityEndpoint,
 
111
                },
 
112
        )
 
113
        if err != nil {
 
114
                return nil, "", "", errors.Annotatef(
 
115
                        err, "finalizing %q credential for cloud %q",
 
116
                        credentialName, args.CloudName,
 
117
                )
 
118
        }
 
119
 
66
120
        return credential, credentialName, regionName, nil
67
121
}
68
122