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