github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/add.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 "launchpad.net/gnuflag" 10 11 "github.com/juju/juju/cloud" 12 ) 13 14 var usageAddCloudSummary = ` 15 Adds a user-defined cloud to Juju from among known cloud types.`[1:] 16 17 var usageAddCloudDetails = ` 18 A cloud definition file has the following YAML format: 19 20 clouds: 21 mycloud: 22 type: openstack 23 auth-types: [ userpass ] 24 regions: 25 london: 26 endpoint: https://london.mycloud.com:35574/v3.0/ 27 28 If the named cloud already exists, the `[1:] + "`--replace`" + ` option is required to 29 overwrite its configuration. 30 Known cloud types: azure, cloudsigma, ec2, gce, joyent, lxd, maas, manual, 31 openstack, rackspace 32 33 Examples: 34 juju add-cloud mycloud ~/mycloud.yaml 35 36 See also: 37 list-clouds` 38 39 type addCloudCommand struct { 40 cmd.CommandBase 41 42 // Replace, if true, existing cloud information is overwritten. 43 Replace bool 44 45 // Cloud is the name fo the cloud to add. 46 Cloud string 47 48 // CloudFile is the name of the cloud YAML file. 49 CloudFile string 50 } 51 52 // NewAddCloudCommand returns a command to add cloud information. 53 func NewAddCloudCommand() cmd.Command { 54 return &addCloudCommand{} 55 } 56 57 func (c *addCloudCommand) Info() *cmd.Info { 58 return &cmd.Info{ 59 Name: "add-cloud", 60 Args: "<cloud name> <cloud definition file>", 61 Purpose: usageAddCloudSummary, 62 Doc: usageAddCloudDetails, 63 } 64 } 65 66 func (c *addCloudCommand) SetFlags(f *gnuflag.FlagSet) { 67 f.BoolVar(&c.Replace, "replace", false, "Overwrite any existing cloud information") 68 } 69 70 func (c *addCloudCommand) Init(args []string) (err error) { 71 if len(args) < 2 { 72 return errors.New("Usage: juju add-cloud <cloud name> <cloud definition file>") 73 } 74 c.Cloud = args[0] 75 c.CloudFile = args[1] 76 return cmd.CheckEmpty(args[2:]) 77 } 78 79 func (c *addCloudCommand) Run(ctxt *cmd.Context) error { 80 specifiedClouds, err := cloud.ParseCloudMetadataFile(c.CloudFile) 81 if err != nil { 82 return err 83 } 84 if specifiedClouds == nil { 85 return errors.New("no personal clouds are defined") 86 } 87 newCloud, ok := specifiedClouds[c.Cloud] 88 if !ok { 89 return errors.Errorf("cloud %q not found in file %q", c.Cloud, c.CloudFile) 90 } 91 personalClouds, err := cloud.PersonalCloudMetadata() 92 if err != nil { 93 return err 94 } 95 if _, ok = personalClouds[c.Cloud]; ok && !c.Replace { 96 return errors.Errorf("cloud called %q already exists; use --replace to replace this existing cloud", c.Cloud) 97 } 98 if personalClouds == nil { 99 personalClouds = make(map[string]cloud.Cloud) 100 } 101 personalClouds[c.Cloud] = newCloud 102 return cloud.WritePersonalCloudMetadata(personalClouds) 103 }