~natefinch/juju-core/jameinel-1.18-panic-parsing-jenv-1312136

« back to all changes in this revision

Viewing changes to constraints/constraints.go

  • Committer: Benji York
  • Date: 2013-03-18 19:33:51 UTC
  • mto: This revision was merged to the branch mainline in revision 1040.
  • Revision ID: benji.york@canonical.com-20130318193351-dnz4jtdb4gn2k8ts
checkpoint: tests pass; major refactoring to avoid import cycle completed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package state
 
1
package constraints
2
2
 
3
3
import (
4
4
        "fmt"
5
 
        "labix.org/v2/mgo"
6
 
        "labix.org/v2/mgo/txn"
7
5
        "math"
8
6
        "strconv"
9
7
        "strings"
10
8
)
11
9
 
12
 
// Constraints describes a user's requirements of the hardware on which units
 
10
// Value describes a user's requirements of the hardware on which units
13
11
// of a service will run. Constraints are used to choose an existing machine
14
12
// onto which a unit will be deployed, or to provision a new machine if no
15
13
// existing one satisfies the requirements.
16
 
type Constraints struct {
 
14
type Value struct {
17
15
 
18
16
        // Arch, if not nil or empty, indicates that a machine must run the named
19
17
        // architecture.
33
31
        Mem *uint64 `json:"mem,omitempty" yaml:"mem,omitempty"`
34
32
}
35
33
 
36
 
// String expresses a Constraints in the language in which it was specified.
37
 
func (c Constraints) String() string {
 
34
// String expresses a constraints.Value in the language in which it was specified.
 
35
func (c Value) String() string {
38
36
        var strs []string
39
37
        if c.Arch != nil {
40
38
                strs = append(strs, "arch="+*c.Arch)
62
60
        return fmt.Sprintf("%d", i)
63
61
}
64
62
 
65
 
// ParseConstraints constructs a Constraints from the supplied arguments,
 
63
// ParseConstraints constructs a constraint.Value from the supplied arguments,
66
64
// each of which must contain only spaces and name=value pairs. If any
67
65
// name is specified more than once, an error is returned.
68
 
func ParseConstraints(args ...string) (Constraints, error) {
69
 
        cons := Constraints{}
 
66
func ParseConstraints(args ...string) (Value, error) {
 
67
        cons := Value{}
70
68
        for _, arg := range args {
71
69
                raws := strings.Split(strings.TrimSpace(arg), " ")
72
70
                for _, raw := range raws {
74
72
                                continue
75
73
                        }
76
74
                        if err := cons.setRaw(raw); err != nil {
77
 
                                return Constraints{}, err
 
75
                                return Value{}, err
78
76
                        }
79
77
                }
80
78
        }
82
80
}
83
81
 
84
82
// setRaw interprets a name=value string and sets the supplied value.
85
 
func (c *Constraints) setRaw(raw string) error {
 
83
func (c *Value) setRaw(raw string) error {
86
84
        eq := strings.Index(raw, "=")
87
85
        if eq <= 0 {
88
86
                return fmt.Errorf("malformed constraint %q", raw)
107
105
        return nil
108
106
}
109
107
 
110
 
func (c *Constraints) setArch(str string) error {
 
108
func (c *Value) setArch(str string) error {
111
109
        if c.Arch != nil {
112
110
                return fmt.Errorf("already set")
113
111
        }
121
119
        return nil
122
120
}
123
121
 
124
 
func (c *Constraints) setCpuCores(str string) (err error) {
 
122
func (c *Value) setCpuCores(str string) (err error) {
125
123
        if c.CpuCores != nil {
126
124
                return fmt.Errorf("already set")
127
125
        }
129
127
        return
130
128
}
131
129
 
132
 
func (c *Constraints) setCpuPower(str string) (err error) {
 
130
func (c *Value) setCpuPower(str string) (err error) {
133
131
        if c.CpuPower != nil {
134
132
                return fmt.Errorf("already set")
135
133
        }
137
135
        return
138
136
}
139
137
 
140
 
func (c *Constraints) setMem(str string) error {
 
138
func (c *Value) setMem(str string) error {
141
139
        if c.Mem != nil {
142
140
                return fmt.Errorf("already set")
143
141
        }
177
175
        "T": 1024 * 1024,
178
176
        "P": 1024 * 1024 * 1024,
179
177
}
180
 
 
181
 
// constraintsDoc is the mongodb representation of a Constraints.
182
 
type constraintsDoc struct {
183
 
        Arch     *string
184
 
        CpuCores *uint64
185
 
        CpuPower *uint64
186
 
        Mem      *uint64
187
 
}
188
 
 
189
 
func newConstraintsDoc(cons Constraints) constraintsDoc {
190
 
        return constraintsDoc{
191
 
                Arch:     cons.Arch,
192
 
                CpuCores: cons.CpuCores,
193
 
                CpuPower: cons.CpuPower,
194
 
                Mem:      cons.Mem,
195
 
        }
196
 
}
197
 
 
198
 
func createConstraintsOp(st *State, id string, cons Constraints) txn.Op {
199
 
        return txn.Op{
200
 
                C:      st.constraints.Name,
201
 
                Id:     id,
202
 
                Assert: txn.DocMissing,
203
 
                Insert: newConstraintsDoc(cons),
204
 
        }
205
 
}
206
 
 
207
 
func readConstraints(st *State, id string) (Constraints, error) {
208
 
        doc := constraintsDoc{}
209
 
        if err := st.constraints.FindId(id).One(&doc); err == mgo.ErrNotFound {
210
 
                return Constraints{}, NotFoundf("constraints")
211
 
        } else if err != nil {
212
 
                return Constraints{}, err
213
 
        }
214
 
        return Constraints{
215
 
                Arch:     doc.Arch,
216
 
                CpuCores: doc.CpuCores,
217
 
                CpuPower: doc.CpuPower,
218
 
                Mem:      doc.Mem,
219
 
        }, nil
220
 
}
221
 
 
222
 
func writeConstraints(st *State, id string, cons Constraints) error {
223
 
        ops := []txn.Op{{
224
 
                C:      st.constraints.Name,
225
 
                Id:     id,
226
 
                Assert: txn.DocExists,
227
 
                Update: D{{"$set", newConstraintsDoc(cons)}},
228
 
        }}
229
 
        if err := st.runner.Run(ops, "", nil); err != nil {
230
 
                return fmt.Errorf("cannot set constraints: %v", err)
231
 
        }
232
 
        return nil
233
 
}