github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/addimage.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 "github.com/juju/utils/series" 11 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/cmd/modelcmd" 14 ) 15 16 func newAddImageMetadataCommand() cmd.Command { 17 return modelcmd.Wrap(&addImageMetadataCommand{}) 18 } 19 20 const addImageCommandDoc = ` 21 Add image metadata to Juju model. 22 23 Image metadata properties vary between providers. Consequently, some properties 24 are optional for this command but they may still be needed by your provider. 25 26 This command takes only one positional argument - an image id. 27 28 arguments: 29 image-id 30 image identifier 31 32 options: 33 -m, --model (= "") 34 juju model to operate in 35 --region 36 cloud region (= region of current model) 37 --series (= current model preferred series) 38 image series 39 --arch (= "amd64") 40 image architectures 41 --virt-type 42 virtualisation type [provider specific], e.g. hmv 43 --storage-type 44 root storage type [provider specific], e.g. ebs 45 --storage-size 46 root storage size [provider specific] 47 --stream (= "released") 48 image stream 49 50 ` 51 52 // addImageMetadataCommand stores image metadata in Juju environment. 53 type addImageMetadataCommand struct { 54 cloudImageMetadataCommandBase 55 56 ImageId string 57 Region string 58 Series string 59 Arch string 60 VirtType string 61 RootStorageType string 62 RootStorageSize uint64 63 Stream string 64 } 65 66 // Init implements Command.Init. 67 func (c *addImageMetadataCommand) Init(args []string) (err error) { 68 if len(args) == 0 { 69 return errors.New("image id must be supplied when adding image metadata") 70 } 71 if len(args) != 1 { 72 return errors.New("only one image id can be supplied as an argument to this command") 73 } 74 c.ImageId = args[0] 75 return c.validate() 76 } 77 78 // Info implements Command.Info. 79 func (c *addImageMetadataCommand) Info() *cmd.Info { 80 return &cmd.Info{ 81 Name: "add-image", 82 Purpose: "adds image metadata to model", 83 Doc: addImageCommandDoc, 84 } 85 } 86 87 // SetFlags implements Command.SetFlags. 88 func (c *addImageMetadataCommand) SetFlags(f *gnuflag.FlagSet) { 89 c.cloudImageMetadataCommandBase.SetFlags(f) 90 91 f.StringVar(&c.Region, "region", "", "image cloud region") 92 f.StringVar(&c.Series, "series", "", "image series") 93 f.StringVar(&c.Arch, "arch", "amd64", "image architecture") 94 f.StringVar(&c.VirtType, "virt-type", "", "image metadata virtualisation type") 95 f.StringVar(&c.RootStorageType, "storage-type", "", "image metadata root storage type") 96 f.Uint64Var(&c.RootStorageSize, "storage-size", 0, "image metadata root storage size") 97 f.StringVar(&c.Stream, "stream", "released", "image metadata stream") 98 } 99 100 // Run implements Command.Run. 101 func (c *addImageMetadataCommand) Run(ctx *cmd.Context) (err error) { 102 api, err := getImageMetadataAddAPI(c) 103 if err != nil { 104 return err 105 } 106 defer api.Close() 107 108 m := c.constructMetadataParam() 109 if err := api.Save([]params.CloudImageMetadata{m}); err != nil { 110 return errors.Trace(err) 111 } 112 return nil 113 } 114 115 // MetadataAddAPI defines the API methods that add image metadata command uses. 116 type MetadataAddAPI interface { 117 Close() error 118 Save(metadata []params.CloudImageMetadata) error 119 } 120 121 var getImageMetadataAddAPI = (*addImageMetadataCommand).getImageMetadataAddAPI 122 123 func (c *addImageMetadataCommand) getImageMetadataAddAPI() (MetadataAddAPI, error) { 124 return c.NewImageMetadataAPI() 125 } 126 127 // Init implements Command.Init. 128 func (c *addImageMetadataCommand) validate() error { 129 if c.Series != "" { 130 if _, err := series.SeriesVersion(c.Series); err != nil { 131 return errors.Trace(err) 132 } 133 } 134 return nil 135 } 136 137 // constructMetadataParam returns cloud image metadata as a param. 138 func (c *addImageMetadataCommand) constructMetadataParam() params.CloudImageMetadata { 139 info := params.CloudImageMetadata{ 140 ImageId: c.ImageId, 141 Region: c.Region, 142 Series: c.Series, 143 Arch: c.Arch, 144 VirtType: c.VirtType, 145 RootStorageType: c.RootStorageType, 146 Stream: c.Stream, 147 Source: "custom", 148 } 149 if c.RootStorageSize != 0 { 150 info.RootStorageSize = &c.RootStorageSize 151 } 152 return info 153 }