Пример #1
0
// Run implements Command.Run
func (c *updateCredentialCommand) Run(ctx *cmd.Context) error {
	cred, err := c.ClientStore().CredentialForCloud(c.cloud)
	if errors.IsNotFound(err) {
		ctx.Infof("No credentials exist for cloud %q", c.cloud)
		return nil
	} else if err != nil {
		return err
	}
	credToUpdate, ok := cred.AuthCredentials[c.credential]
	if !ok {
		ctx.Infof("No credential called %q exists for cloud %q", c.credential, c.cloud)
		return nil
	}

	accountDetails, err := c.ClientStore().AccountDetails(c.ControllerName())
	if err != nil {
		return errors.Trace(err)
	}
	credentialTag, err := common.ResolveCloudCredentialTag(
		names.NewUserTag(accountDetails.User), names.NewCloudTag(c.cloud), c.credential,
	)

	client, err := c.getAPI()
	if err != nil {
		return err
	}
	defer client.Close()

	if err := client.UpdateCredential(credentialTag, credToUpdate); err != nil {
		return err
	}
	ctx.Infof("Updated credential %q for user %q on cloud %q.", c.credential, accountDetails.User, c.cloud)
	return nil
}
Пример #2
0
func testResolveCloudCredentialTag(
	c *gc.C,
	user names.UserTag,
	cloud names.CloudTag,
	credentialName string,
	expect string,
) {
	tag, err := common.ResolveCloudCredentialTag(user, cloud, credentialName)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(tag.Id(), gc.Equals, expect)
}
Пример #3
0
func (c *addModelCommand) maybeUploadCredential(
	ctx *cmd.Context,
	cloudClient CloudAPI,
	cloudTag names.CloudTag,
	cloudRegion string,
	cloud jujucloud.Cloud,
	modelOwner string,
) (names.CloudCredentialTag, error) {

	modelOwnerTag := names.NewUserTag(modelOwner)
	credentialTag, err := common.ResolveCloudCredentialTag(
		modelOwnerTag, cloudTag, c.CredentialName,
	)
	if err != nil {
		return names.CloudCredentialTag{}, errors.Trace(err)
	}

	// Check if the credential is already in the controller.
	//
	// TODO(axw) consider implementing a call that can check
	// that the credential exists without fetching all of the
	// names.
	credentialTags, err := cloudClient.UserCredentials(modelOwnerTag, cloudTag)
	if err != nil {
		return names.CloudCredentialTag{}, errors.Trace(err)
	}
	credentialId := credentialTag.Canonical()
	for _, tag := range credentialTags {
		if tag.Canonical() != credentialId {
			continue
		}
		ctx.Infof("Using credential '%s' cached in controller", c.CredentialName)
		return credentialTag, nil
	}

	if credentialTag.Owner().Canonical() != modelOwner {
		// Another user's credential was specified, so
		// we cannot automatically upload.
		return names.CloudCredentialTag{}, errors.NotFoundf(
			"credential '%s'", c.CredentialName,
		)
	}

	// Upload the credential from the client, if it exists locally.
	credential, _, _, err := modelcmd.GetCredentials(
		ctx, c.ClientStore(), modelcmd.GetCredentialsParams{
			Cloud:          cloud,
			CloudName:      cloudTag.Id(),
			CloudRegion:    cloudRegion,
			CredentialName: credentialTag.Name(),
		},
	)
	if err != nil {
		return names.CloudCredentialTag{}, errors.Trace(err)
	}
	ctx.Infof("Uploading credential '%s' to controller", credentialTag.Id())
	if err := cloudClient.UpdateCredential(credentialTag, *credential); err != nil {
		return names.CloudCredentialTag{}, errors.Trace(err)
	}
	return credentialTag, nil
}