1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"launchpad.net/gnuflag"
10
"github.com/juju/juju/cmd/juju/block"
11
"github.com/juju/juju/cmd/modelcmd"
12
"github.com/juju/juju/constraints"
15
// getConstraintsDoc is multi-line since we need to use ` to denote
16
// commands for ease in markdown.
17
const getConstraintsDoc = "" +
18
"Shows machine constraints that have been set on the model with\n" +
19
"`juju set-model-constraints.`\n" +
20
"By default, the model is the current model.\n" +
21
"Model constraints are combined with constraints set on an application\n" +
22
"with `juju set-constraints` for commands (such as 'deploy') that provision\n" +
23
"machines for applications. Where model and application constraints overlap, the\n" +
24
"application constraints take precedence.\n" +
25
"Constraints for a specific application can be viewed with `juju get-constraints`.\n" + getConstraintsDocExamples
27
const getConstraintsDocExamples = `
30
juju get-model-constraints
31
juju get-model-constraints -m mymodel
40
// setConstraintsDoc is multi-line since we need to use ` to denote
41
// commands for ease in markdown.
42
const setConstraintsDoc = "" +
43
"Sets machine constraints on the model that can be viewed with\n" +
44
"`juju get-model-constraints`. By default, the model is the current model.\n" +
45
"Model constraints are combined with constraints set for an application with\n" +
46
"`juju set-constraints` for commands (such as 'deploy') that provision\n" +
47
"machines for applications. Where model and application constraints overlap, the\n" +
48
"application constraints take precedence.\n" +
49
"Constraints for a specific application can be viewed with `juju get-constraints`.\n" + setConstraintsDocExamples
51
const setConstraintsDocExamples = `
54
juju set-model-constraints cpu-cores=8 mem=16G
55
juju set-model-constraints -m mymodel root-disk=64G
64
// ConstraintsAPI defines methods on the client API that
65
// the get-constraints and set-constraints commands call
66
type ConstraintsAPI interface {
68
GetModelConstraints() (constraints.Value, error)
69
SetModelConstraints(constraints.Value) error
72
// NewModelGetConstraintsCommand returns a command to get model constraints.
73
func NewModelGetConstraintsCommand() cmd.Command {
74
return modelcmd.Wrap(&modelGetConstraintsCommand{})
77
// modelGetConstraintsCommand shows the constraints for a model.
78
type modelGetConstraintsCommand struct {
79
modelcmd.ModelCommandBase
84
func (c *modelGetConstraintsCommand) Info() *cmd.Info {
86
Name: "get-model-constraints",
87
Purpose: "Displays machine constraints for a model.",
88
Doc: getConstraintsDoc,
92
func (c *modelGetConstraintsCommand) Init(args []string) error {
93
return cmd.CheckEmpty(args)
96
func (c *modelGetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
100
return c.NewAPIClient()
103
func formatConstraints(value interface{}) ([]byte, error) {
104
return []byte(value.(constraints.Value).String()), nil
107
func (c *modelGetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
108
c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{
109
"constraints": formatConstraints,
110
"yaml": cmd.FormatYaml,
111
"json": cmd.FormatJson,
115
func (c *modelGetConstraintsCommand) Run(ctx *cmd.Context) error {
116
apiclient, err := c.getAPI()
120
defer apiclient.Close()
122
cons, err := apiclient.GetModelConstraints()
126
return c.out.Write(ctx, cons)
129
// NewModelSetConstraintsCommand returns a command to set model constraints.
130
func NewModelSetConstraintsCommand() cmd.Command {
131
return modelcmd.Wrap(&modelSetConstraintsCommand{})
134
// modelSetConstraintsCommand sets the constraints for a model.
135
type modelSetConstraintsCommand struct {
136
modelcmd.ModelCommandBase
138
Constraints constraints.Value
141
func (c *modelSetConstraintsCommand) Info() *cmd.Info {
143
Name: "set-model-constraints",
144
Args: "<constraint>=<value> ...",
145
Purpose: "Sets machine constraints on a model.",
146
Doc: setConstraintsDoc,
150
func (c *modelSetConstraintsCommand) Init(args []string) (err error) {
151
c.Constraints, err = constraints.Parse(args...)
155
func (c *modelSetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
159
return c.NewAPIClient()
162
func (c *modelSetConstraintsCommand) Run(_ *cmd.Context) (err error) {
163
apiclient, err := c.getAPI()
167
defer apiclient.Close()
169
err = apiclient.SetModelConstraints(c.Constraints)
170
return block.ProcessBlockedError(err, block.BlockChange)