github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cloud/removecredential.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/jujuclient"
    11  )
    12  
    13  type removeCredentialCommand struct {
    14  	cmd.CommandBase
    15  
    16  	store      jujuclient.CredentialStore
    17  	cloud      string
    18  	credential string
    19  }
    20  
    21  var usageRemoveCredentialSummary = `
    22  Removes credentials for a cloud.`[1:]
    23  
    24  var usageRemoveCredentialDetails = `
    25  The credentials to be removed are specified by a "credential name".
    26  Credential names, and optionally the corresponding authentication
    27  material, can be listed with `[1:] + "`juju credentials`" + `.
    28  
    29  Examples:
    30      juju remove-credential rackspace credential_name
    31  
    32  See also: 
    33      credentials
    34      add-credential
    35      set-default-credential
    36      autoload-credentials`
    37  
    38  // NewremoveCredentialCommand returns a command to remove a named credential for a cloud.
    39  func NewRemoveCredentialCommand() cmd.Command {
    40  	return &removeCredentialCommand{
    41  		store: jujuclient.NewFileCredentialStore(),
    42  	}
    43  }
    44  
    45  func (c *removeCredentialCommand) Info() *cmd.Info {
    46  	return &cmd.Info{
    47  		Name:    "remove-credential",
    48  		Args:    "<cloud name> <credential name>",
    49  		Purpose: usageRemoveCredentialSummary,
    50  		Doc:     usageRemoveCredentialDetails,
    51  	}
    52  }
    53  
    54  func (c *removeCredentialCommand) Init(args []string) (err error) {
    55  	if len(args) < 2 {
    56  		return errors.New("Usage: juju remove-credential <cloud-name> <credential-name>")
    57  	}
    58  	c.cloud = args[0]
    59  	c.credential = args[1]
    60  	return cmd.CheckEmpty(args[2:])
    61  }
    62  
    63  func (c *removeCredentialCommand) Run(ctxt *cmd.Context) error {
    64  	cred, err := c.store.CredentialForCloud(c.cloud)
    65  	if errors.IsNotFound(err) {
    66  		ctxt.Infof("No credentials exist for cloud %q", c.cloud)
    67  		return nil
    68  	} else if err != nil {
    69  		return err
    70  	}
    71  	if _, ok := cred.AuthCredentials[c.credential]; !ok {
    72  		ctxt.Infof("No credential called %q exists for cloud %q", c.credential, c.cloud)
    73  		return nil
    74  	}
    75  	delete(cred.AuthCredentials, c.credential)
    76  	if err := c.store.UpdateCredential(c.cloud, *cred); err != nil {
    77  		return err
    78  	}
    79  	ctxt.Infof("Credential %q for cloud %q has been deleted.", c.credential, c.cloud)
    80  	return nil
    81  }