-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.go
110 lines (97 loc) · 2.61 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
102
103
104
105
106
107
108
109
110
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/constraints"
"github.com/juju/juju/instance"
)
// constraintsDoc is the mongodb representation of a constraints.Value.
type constraintsDoc struct {
ModelUUID string `bson:"model-uuid"`
Arch *string
CpuCores *uint64
CpuPower *uint64
Mem *uint64
RootDisk *uint64
InstanceType *string
Container *instance.ContainerType
Tags *[]string
Spaces *[]string
VirtType *string
}
func (doc constraintsDoc) value() constraints.Value {
result := 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,
Spaces: doc.Spaces,
VirtType: doc.VirtType,
}
return result
}
func newConstraintsDoc(cons constraints.Value) constraintsDoc {
result := 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,
Spaces: cons.Spaces,
VirtType: cons.VirtType,
}
return result
}
func createConstraintsOp(id string, cons constraints.Value) txn.Op {
return txn.Op{
C: constraintsC,
Id: id,
Assert: txn.DocMissing,
Insert: newConstraintsDoc(cons),
}
}
func setConstraintsOp(id string, cons constraints.Value) txn.Op {
return txn.Op{
C: constraintsC,
Id: id,
Assert: txn.DocExists,
Update: bson.D{{"$set", newConstraintsDoc(cons)}},
}
}
func removeConstraintsOp(id string) txn.Op {
return txn.Op{
C: constraintsC,
Id: id,
Remove: true,
}
}
func readConstraints(mb modelBackend, id string) (constraints.Value, error) {
constraintsCollection, closer := mb.db().GetCollection(constraintsC)
defer closer()
doc := constraintsDoc{}
if err := constraintsCollection.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(mb modelBackend, id string, cons constraints.Value) error {
ops := []txn.Op{setConstraintsOp(id, cons)}
if err := mb.db().RunTransaction(ops); err != nil {
return fmt.Errorf("cannot set constraints: %v", err)
}
return nil
}