github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/model/set.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/utils/keyvalues" 12 13 "github.com/juju/juju/cmd/juju/block" 14 "github.com/juju/juju/cmd/modelcmd" 15 ) 16 17 func NewSetCommand() cmd.Command { 18 return modelcmd.Wrap(&setCommand{}) 19 } 20 21 type attributes map[string]interface{} 22 23 type setCommand struct { 24 modelcmd.ModelCommandBase 25 api SetModelAPI 26 values attributes 27 } 28 29 const setModelHelpDoc = ` 30 Model configuration consists of a collection of keys and their respective values. 31 By default, the model is the current model. 32 Consult the online documentation for a list of keys and possible values. 33 34 Examples: 35 36 juju set-model-config logging-config='<root>=WARNING;unit=INFO' 37 juju set-model-config -m mymodel api-port=17071 default-series=xenial 38 39 See also: list-models 40 get-model-config 41 unset-model-config 42 ` 43 44 func (c *setCommand) Info() *cmd.Info { 45 return &cmd.Info{ 46 Name: "set-model-config", 47 Args: "<model key>=<value> ...", 48 Purpose: "Sets configuration keys on a model.", 49 Doc: strings.TrimSpace(setModelHelpDoc), 50 } 51 } 52 53 func (c *setCommand) Init(args []string) (err error) { 54 if len(args) == 0 { 55 return fmt.Errorf("no key, value pairs specified") 56 } 57 58 options, err := keyvalues.Parse(args, true) 59 if err != nil { 60 return err 61 } 62 63 c.values = make(attributes) 64 for key, value := range options { 65 if key == "agent-version" { 66 return fmt.Errorf("agent-version must be set via upgrade-juju") 67 } 68 c.values[key] = value 69 } 70 71 return nil 72 } 73 74 type SetModelAPI interface { 75 Close() error 76 ModelGet() (map[string]interface{}, error) 77 ModelSet(config map[string]interface{}) error 78 } 79 80 func (c *setCommand) getAPI() (SetModelAPI, error) { 81 if c.api != nil { 82 return c.api, nil 83 } 84 return c.NewAPIClient() 85 } 86 87 func (c *setCommand) Run(ctx *cmd.Context) error { 88 client, err := c.getAPI() 89 if err != nil { 90 return err 91 } 92 defer client.Close() 93 94 // extra call to the API to retrieve env config 95 envAttrs, err := client.ModelGet() 96 if err != nil { 97 return err 98 } 99 for key := range c.values { 100 // check if the key exists in the existing env config 101 // and warn the user if the key is not defined in 102 // the existing config 103 if _, exists := envAttrs[key]; !exists { 104 logger.Warningf("key %q is not defined in the current model configuration: possible misspelling", key) 105 } 106 107 } 108 return block.ProcessBlockedError(client.ModelSet(c.values), block.BlockChange) 109 }