-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.go
101 lines (89 loc) · 2.45 KB
/
constraints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/errors"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"labix.org/v2/mgo/txn"
"github.com/juju/juju/constraints"
"github.com/juju/juju/instance"
)
// constraintsDoc is the mongodb representation of a constraints.Value.
type constraintsDoc struct {
Arch *string
CpuCores *uint64
CpuPower *uint64
Mem *uint64
RootDisk *uint64
InstanceType *string
Container *instance.ContainerType
Tags *[]string `bson:",omitempty"`
Networks *[]string `bson:",omitempty"`
}
func (doc constraintsDoc) value() constraints.Value {
return constraints.Value{
Arch: doc.Arch,
CpuCores: doc.CpuCores,
CpuPower: doc.CpuPower,
Mem: doc.Mem,
RootDisk: doc.RootDisk,
InstanceType: doc.InstanceType,
Container: doc.Container,
Tags: doc.Tags,
Networks: doc.Networks,
}
}
func newConstraintsDoc(cons constraints.Value) constraintsDoc {
return constraintsDoc{
Arch: cons.Arch,
CpuCores: cons.CpuCores,
CpuPower: cons.CpuPower,
Mem: cons.Mem,
RootDisk: cons.RootDisk,
InstanceType: cons.InstanceType,
Container: cons.Container,
Tags: cons.Tags,
Networks: cons.Networks,
}
}
func createConstraintsOp(st *State, id string, cons constraints.Value) txn.Op {
return txn.Op{
C: st.constraints.Name,
Id: id,
Assert: txn.DocMissing,
Insert: newConstraintsDoc(cons),
}
}
func setConstraintsOp(st *State, id string, cons constraints.Value) txn.Op {
return txn.Op{
C: st.constraints.Name,
Id: id,
Assert: txn.DocExists,
Update: bson.D{{"$set", newConstraintsDoc(cons)}},
}
}
func removeConstraintsOp(st *State, id string) txn.Op {
return txn.Op{
C: st.constraints.Name,
Id: id,
Remove: true,
}
}
func readConstraints(st *State, id string) (constraints.Value, error) {
doc := constraintsDoc{}
if err := st.constraints.FindId(id).One(&doc); err == mgo.ErrNotFound {
return constraints.Value{}, errors.NotFoundf("constraints")
} else if err != nil {
return constraints.Value{}, err
}
return doc.value(), nil
}
func writeConstraints(st *State, id string, cons constraints.Value) error {
ops := []txn.Op{setConstraintsOp(st, id, cons)}
if err := st.runTransaction(ops); err != nil {
return fmt.Errorf("cannot set constraints: %v", err)
}
return nil
}