github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/defaultcredential.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  	jujucloud "github.com/juju/juju/cloud"
    11  	"github.com/juju/juju/cmd/juju/common"
    12  	"github.com/juju/juju/jujuclient"
    13  )
    14  
    15  var usageSetDefaultCredentialSummary = `
    16  Sets the default credentials for a cloud.`[1:]
    17  
    18  var usageSetDefaultCredentialDetails = `
    19  The default credentials are specified with a "credential name". A 
    20  credential name is created during the process of adding credentials either 
    21  via `[1:] + "`juju add-credential` or `juju autoload-credentials`" + `. Credential names 
    22  can be listed with ` + "`juju list-credentials`" + `.
    23  Default credentials avoid the need to specify a particular set of 
    24  credentials when more than one are available for a given cloud.
    25  
    26  Examples:
    27      juju set-default-credential google credential_name
    28  
    29  See also: 
    30      add-credential
    31      remove-credential
    32      list-credentials
    33      autoload-credentials`
    34  
    35  type setDefaultCredentialCommand struct {
    36  	cmd.CommandBase
    37  
    38  	store      jujuclient.CredentialStore
    39  	cloud      string
    40  	credential string
    41  }
    42  
    43  // NewSetDefaultCredentialCommand returns a command to set the default credential for a cloud.
    44  func NewSetDefaultCredentialCommand() cmd.Command {
    45  	return &setDefaultCredentialCommand{
    46  		store: jujuclient.NewFileCredentialStore(),
    47  	}
    48  }
    49  
    50  func (c *setDefaultCredentialCommand) Info() *cmd.Info {
    51  	return &cmd.Info{
    52  		Name:    "set-default-credential",
    53  		Args:    "<cloud name> <credential name>",
    54  		Purpose: usageSetDefaultCredentialSummary,
    55  		Doc:     usageSetDefaultCredentialDetails,
    56  	}
    57  }
    58  
    59  func (c *setDefaultCredentialCommand) Init(args []string) (err error) {
    60  	if len(args) < 2 {
    61  		return errors.New("Usage: juju set-default-credential <cloud-name> <credential-name>")
    62  	}
    63  	c.cloud = args[0]
    64  	c.credential = args[1]
    65  	return cmd.CheckEmpty(args[2:])
    66  }
    67  
    68  func hasCredential(credential string, credentials map[string]jujucloud.Credential) bool {
    69  	for c := range credentials {
    70  		if c == credential {
    71  			return true
    72  		}
    73  	}
    74  	return false
    75  }
    76  
    77  func (c *setDefaultCredentialCommand) Run(ctxt *cmd.Context) error {
    78  	if _, err := common.CloudOrProvider(c.cloud, jujucloud.CloudByName); err != nil {
    79  		return err
    80  	}
    81  	cred, err := c.store.CredentialForCloud(c.cloud)
    82  	if errors.IsNotFound(err) {
    83  		cred = &jujucloud.CloudCredential{}
    84  	} else if err != nil {
    85  		return err
    86  	}
    87  	if !hasCredential(c.credential, cred.AuthCredentials) {
    88  		return errors.NotValidf("credential %q for cloud %s", c.credential, c.cloud)
    89  	}
    90  
    91  	cred.DefaultCredential = c.credential
    92  	if err := c.store.UpdateCredential(c.cloud, *cred); err != nil {
    93  		return err
    94  	}
    95  	ctxt.Infof("Default credential for %s set to %q.", c.cloud, c.credential)
    96  	return nil
    97  }