github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/constraints.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model 5 6 import ( 7 "fmt" 8 "io" 9 10 "github.com/juju/cmd" 11 "github.com/juju/gnuflag" 12 13 jujucmd "github.com/juju/juju/cmd" 14 "github.com/juju/juju/cmd/juju/block" 15 "github.com/juju/juju/cmd/modelcmd" 16 "github.com/juju/juju/core/constraints" 17 ) 18 19 // getConstraintsDoc is multi-line since we need to use ` to denote 20 // commands for ease in markdown. 21 const getConstraintsDoc = "" + 22 "Shows machine constraints that have been set on the model with\n" + 23 "`juju set-model-constraints.`\n" + 24 "By default, the model is the current model.\n" + 25 "Model constraints are combined with constraints set on an application\n" + 26 "with `juju set-constraints` for commands (such as 'deploy') that provision\n" + 27 "machines for applications. Where model and application constraints overlap, the\n" + 28 "application constraints take precedence.\n" + 29 "Constraints for a specific application can be viewed with `juju get-constraints`.\n" + getConstraintsDocExamples 30 31 const getConstraintsDocExamples = ` 32 Examples: 33 34 juju get-model-constraints 35 juju get-model-constraints -m mymodel 36 37 See also: 38 models 39 get-constraints 40 set-constraints 41 set-model-constraints 42 ` 43 44 // setConstraintsDoc is multi-line since we need to use ` to denote 45 // commands for ease in markdown. 46 const setConstraintsDoc = "" + 47 "Sets machine constraints on the model that can be viewed with\n" + 48 "`juju get-model-constraints`. By default, the model is the current model.\n" + 49 "Model constraints are combined with constraints set for an application with\n" + 50 "`juju set-constraints` for commands (such as 'deploy') that provision\n" + 51 "machines for applications. Where model and application constraints overlap, the\n" + 52 "application constraints take precedence.\n" + 53 "Constraints for a specific application can be viewed with `juju get-constraints`.\n" + setConstraintsDocExamples 54 55 const setConstraintsDocExamples = ` 56 Examples: 57 58 juju set-model-constraints cores=8 mem=16G 59 juju set-model-constraints -m mymodel root-disk=64G 60 61 See also: 62 models 63 get-model-constraints 64 get-constraints 65 set-constraints 66 ` 67 68 // ConstraintsAPI defines methods on the client API that 69 // the get-constraints and set-constraints commands call 70 type ConstraintsAPI interface { 71 Close() error 72 GetModelConstraints() (constraints.Value, error) 73 SetModelConstraints(constraints.Value) error 74 } 75 76 // NewModelGetConstraintsCommand returns a command to get model constraints. 77 func NewModelGetConstraintsCommand() cmd.Command { 78 return modelcmd.Wrap(&modelGetConstraintsCommand{}) 79 } 80 81 // modelGetConstraintsCommand shows the constraints for a model. 82 type modelGetConstraintsCommand struct { 83 modelcmd.ModelCommandBase 84 modelcmd.IAASOnlyCommand 85 out cmd.Output 86 api ConstraintsAPI 87 } 88 89 func (c *modelGetConstraintsCommand) Info() *cmd.Info { 90 return jujucmd.Info(&cmd.Info{ 91 Name: "get-model-constraints", 92 Purpose: "Displays machine constraints for a model.", 93 Doc: getConstraintsDoc, 94 }) 95 } 96 97 func (c *modelGetConstraintsCommand) Init(args []string) error { 98 return cmd.CheckEmpty(args) 99 } 100 101 func (c *modelGetConstraintsCommand) getAPI() (ConstraintsAPI, error) { 102 if c.api != nil { 103 return c.api, nil 104 } 105 return c.NewAPIClient() 106 } 107 108 func formatConstraints(writer io.Writer, value interface{}) error { 109 fmt.Fprint(writer, value.(constraints.Value).String()) 110 return nil 111 } 112 113 func (c *modelGetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) { 114 c.ModelCommandBase.SetFlags(f) 115 c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{ 116 "constraints": formatConstraints, 117 "yaml": cmd.FormatYaml, 118 "json": cmd.FormatJson, 119 }) 120 } 121 122 func (c *modelGetConstraintsCommand) Run(ctx *cmd.Context) error { 123 apiclient, err := c.getAPI() 124 if err != nil { 125 return err 126 } 127 defer apiclient.Close() 128 129 cons, err := apiclient.GetModelConstraints() 130 if err != nil { 131 return err 132 } 133 return c.out.Write(ctx, cons) 134 } 135 136 // NewModelSetConstraintsCommand returns a command to set model constraints. 137 func NewModelSetConstraintsCommand() cmd.Command { 138 return modelcmd.Wrap(&modelSetConstraintsCommand{}) 139 } 140 141 // modelSetConstraintsCommand sets the constraints for a model. 142 type modelSetConstraintsCommand struct { 143 modelcmd.ModelCommandBase 144 modelcmd.IAASOnlyCommand 145 api ConstraintsAPI 146 Constraints constraints.Value 147 } 148 149 func (c *modelSetConstraintsCommand) Info() *cmd.Info { 150 return jujucmd.Info(&cmd.Info{ 151 Name: "set-model-constraints", 152 Args: "<constraint>=<value> ...", 153 Purpose: "Sets machine constraints on a model.", 154 Doc: setConstraintsDoc, 155 }) 156 } 157 158 func (c *modelSetConstraintsCommand) Init(args []string) (err error) { 159 c.Constraints, err = constraints.Parse(args...) 160 return err 161 } 162 163 func (c *modelSetConstraintsCommand) getAPI() (ConstraintsAPI, error) { 164 if c.api != nil { 165 return c.api, nil 166 } 167 return c.NewAPIClient() 168 } 169 170 func (c *modelSetConstraintsCommand) Run(_ *cmd.Context) (err error) { 171 apiclient, err := c.getAPI() 172 if err != nil { 173 return err 174 } 175 defer apiclient.Close() 176 177 err = apiclient.SetModelConstraints(c.Constraints) 178 return block.ProcessBlockedError(err, block.BlockChange) 179 }