func (c *addCredentialCommand) Run(ctxt *cmd.Context) error { // Check that the supplied cloud is valid. var err error if c.cloud, err = common.CloudOrProvider(c.CloudName, c.cloudByNameFunc); err != nil { if !errors.IsNotFound(err) { return err } } if len(c.cloud.AuthTypes) == 0 { return errors.Errorf("cloud %q does not require credentials", c.CloudName) } if c.CredentialsFile == "" { credentialsProvider, err := environs.Provider(c.cloud.Type) if err != nil { return errors.Annotate(err, "getting provider for cloud") } return c.interactiveAddCredential(ctxt, credentialsProvider.CredentialSchemas()) } data, err := ioutil.ReadFile(c.CredentialsFile) if err != nil { return errors.Annotate(err, "reading credentials file") } specifiedCredentials, err := jujucloud.ParseCredentials(data) if err != nil { return errors.Annotate(err, "parsing credentials file") } credentials, ok := specifiedCredentials[c.CloudName] if !ok { return errors.Errorf("no credentials for cloud %s exist in file %s", c.CloudName, c.CredentialsFile) } existingCredentials, err := c.existingCredentialsForCloud() if err != nil { return errors.Trace(err) } // If there are *any* credentials already for the cloud, we'll ask for the --replace flag. if !c.Replace && len(existingCredentials.AuthCredentials) > 0 && len(credentials.AuthCredentials) > 0 { return errors.Errorf("credentials for cloud %s already exist; use --replace to overwrite / merge", c.CloudName) } for name, cred := range credentials.AuthCredentials { existingCredentials.AuthCredentials[name] = cred } err = c.store.UpdateCredential(c.CloudName, *existingCredentials) if err != nil { return err } fmt.Fprintf(ctxt.Stdout, "credentials updated for cloud %s\n", c.CloudName) return nil }
// ReadCredentialsFile loads all credentials defined in a given file. // If the file is not found, it is not an error. func ReadCredentialsFile(file string) (map[string]cloud.CloudCredential, error) { data, err := ioutil.ReadFile(file) if err != nil { if os.IsNotExist(err) { return nil, nil } return nil, err } credentials, err := cloud.ParseCredentials(data) if err != nil { return nil, err } return credentials, nil }
func (s *credentialsSuite) testParseCredentialsError(c *gc.C, input []byte, expect string) { _, err := cloud.ParseCredentials(input) c.Assert(err, gc.ErrorMatches, expect) }
func (s *credentialsSuite) testParseCredentials(c *gc.C, input []byte, expect map[string]cloud.CloudCredential) { output, err := cloud.ParseCredentials(input) c.Assert(err, jc.ErrorIsNil) c.Assert(output, jc.DeepEquals, expect) }
func parseCredentials(c *gc.C) map[string]cloud.CloudCredential { credentials, err := cloud.ParseCredentials([]byte(testCredentialsYAML)) c.Assert(err, jc.ErrorIsNil) return credentials }