~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/constraints.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package model
 
5
 
 
6
import (
 
7
        "github.com/juju/cmd"
 
8
        "launchpad.net/gnuflag"
 
9
 
 
10
        "github.com/juju/juju/cmd/juju/block"
 
11
        "github.com/juju/juju/cmd/modelcmd"
 
12
        "github.com/juju/juju/constraints"
 
13
)
 
14
 
 
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
 
26
 
 
27
const getConstraintsDocExamples = `
 
28
Examples:
 
29
 
 
30
    juju get-model-constraints
 
31
    juju get-model-constraints -m mymodel
 
32
 
 
33
See also:
 
34
    models
 
35
    get-constraints
 
36
    set-constraints
 
37
    set-model-constraints
 
38
`
 
39
 
 
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
 
50
 
 
51
const setConstraintsDocExamples = `
 
52
Examples:
 
53
 
 
54
    juju set-model-constraints cpu-cores=8 mem=16G
 
55
    juju set-model-constraints -m mymodel root-disk=64G
 
56
 
 
57
See also:
 
58
    models
 
59
    get-model-constraints
 
60
    get-constraints
 
61
    set-constraints
 
62
`
 
63
 
 
64
// ConstraintsAPI defines methods on the client API that
 
65
// the get-constraints and set-constraints commands call
 
66
type ConstraintsAPI interface {
 
67
        Close() error
 
68
        GetModelConstraints() (constraints.Value, error)
 
69
        SetModelConstraints(constraints.Value) error
 
70
}
 
71
 
 
72
// NewModelGetConstraintsCommand returns a command to get model constraints.
 
73
func NewModelGetConstraintsCommand() cmd.Command {
 
74
        return modelcmd.Wrap(&modelGetConstraintsCommand{})
 
75
}
 
76
 
 
77
// modelGetConstraintsCommand shows the constraints for a model.
 
78
type modelGetConstraintsCommand struct {
 
79
        modelcmd.ModelCommandBase
 
80
        out cmd.Output
 
81
        api ConstraintsAPI
 
82
}
 
83
 
 
84
func (c *modelGetConstraintsCommand) Info() *cmd.Info {
 
85
        return &cmd.Info{
 
86
                Name:    "get-model-constraints",
 
87
                Purpose: "Displays machine constraints for a model.",
 
88
                Doc:     getConstraintsDoc,
 
89
        }
 
90
}
 
91
 
 
92
func (c *modelGetConstraintsCommand) Init(args []string) error {
 
93
        return cmd.CheckEmpty(args)
 
94
}
 
95
 
 
96
func (c *modelGetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
 
97
        if c.api != nil {
 
98
                return c.api, nil
 
99
        }
 
100
        return c.NewAPIClient()
 
101
}
 
102
 
 
103
func formatConstraints(value interface{}) ([]byte, error) {
 
104
        return []byte(value.(constraints.Value).String()), nil
 
105
}
 
106
 
 
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,
 
112
        })
 
113
}
 
114
 
 
115
func (c *modelGetConstraintsCommand) Run(ctx *cmd.Context) error {
 
116
        apiclient, err := c.getAPI()
 
117
        if err != nil {
 
118
                return err
 
119
        }
 
120
        defer apiclient.Close()
 
121
 
 
122
        cons, err := apiclient.GetModelConstraints()
 
123
        if err != nil {
 
124
                return err
 
125
        }
 
126
        return c.out.Write(ctx, cons)
 
127
}
 
128
 
 
129
// NewModelSetConstraintsCommand returns a command to set model constraints.
 
130
func NewModelSetConstraintsCommand() cmd.Command {
 
131
        return modelcmd.Wrap(&modelSetConstraintsCommand{})
 
132
}
 
133
 
 
134
// modelSetConstraintsCommand sets the constraints for a model.
 
135
type modelSetConstraintsCommand struct {
 
136
        modelcmd.ModelCommandBase
 
137
        api         ConstraintsAPI
 
138
        Constraints constraints.Value
 
139
}
 
140
 
 
141
func (c *modelSetConstraintsCommand) Info() *cmd.Info {
 
142
        return &cmd.Info{
 
143
                Name:    "set-model-constraints",
 
144
                Args:    "<constraint>=<value> ...",
 
145
                Purpose: "Sets machine constraints on a model.",
 
146
                Doc:     setConstraintsDoc,
 
147
        }
 
148
}
 
149
 
 
150
func (c *modelSetConstraintsCommand) Init(args []string) (err error) {
 
151
        c.Constraints, err = constraints.Parse(args...)
 
152
        return err
 
153
}
 
154
 
 
155
func (c *modelSetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
 
156
        if c.api != nil {
 
157
                return c.api, nil
 
158
        }
 
159
        return c.NewAPIClient()
 
160
}
 
161
 
 
162
func (c *modelSetConstraintsCommand) Run(_ *cmd.Context) (err error) {
 
163
        apiclient, err := c.getAPI()
 
164
        if err != nil {
 
165
                return err
 
166
        }
 
167
        defer apiclient.Close()
 
168
 
 
169
        err = apiclient.SetModelConstraints(c.Constraints)
 
170
        return block.ProcessBlockedError(err, block.BlockChange)
 
171
}