~rogpeppe/juju-core/256-more-status

« back to all changes in this revision

Viewing changes to state/constraints.go

  • Committer: Benji York
  • Date: 2013-03-20 20:05:34 UTC
  • mfrom: (1010.3.15 1130172)
  • Revision ID: benji.york@canonical.com-20130320200534-ho97u66shmicxkkf
Add SetServiceConstraints to API

This also involved moving the Constraints struct into its own package in order
to break an import cycle.

R=dfc, bac, dimitern, fwereade
CC=
https://codereview.appspot.com/7600044

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package state
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "labix.org/v2/mgo"
 
6
        "labix.org/v2/mgo/txn"
 
7
        "launchpad.net/juju-core/constraints"
 
8
)
 
9
 
 
10
// constraintsDoc is the mongodb representation of a constraints.Value.
 
11
type constraintsDoc struct {
 
12
        Arch     *string
 
13
        CpuCores *uint64
 
14
        CpuPower *uint64
 
15
        Mem      *uint64
 
16
}
 
17
 
 
18
func newConstraintsDoc(cons constraints.Value) constraintsDoc {
 
19
        return constraintsDoc{
 
20
                Arch:     cons.Arch,
 
21
                CpuCores: cons.CpuCores,
 
22
                CpuPower: cons.CpuPower,
 
23
                Mem:      cons.Mem,
 
24
        }
 
25
}
 
26
 
 
27
func createConstraintsOp(st *State, id string, cons constraints.Value) txn.Op {
 
28
        return txn.Op{
 
29
                C:      st.constraints.Name,
 
30
                Id:     id,
 
31
                Assert: txn.DocMissing,
 
32
                Insert: newConstraintsDoc(cons),
 
33
        }
 
34
}
 
35
 
 
36
func readConstraints(st *State, id string) (constraints.Value, error) {
 
37
        doc := constraintsDoc{}
 
38
        if err := st.constraints.FindId(id).One(&doc); err == mgo.ErrNotFound {
 
39
                return constraints.Value{}, NotFoundf("constraints")
 
40
        } else if err != nil {
 
41
                return constraints.Value{}, err
 
42
        }
 
43
        return constraints.Value{
 
44
                Arch:     doc.Arch,
 
45
                CpuCores: doc.CpuCores,
 
46
                CpuPower: doc.CpuPower,
 
47
                Mem:      doc.Mem,
 
48
        }, nil
 
49
}
 
50
 
 
51
func writeConstraints(st *State, id string, cons constraints.Value) error {
 
52
        ops := []txn.Op{{
 
53
                C:      st.constraints.Name,
 
54
                Id:     id,
 
55
                Assert: txn.DocExists,
 
56
                Update: D{{"$set", newConstraintsDoc(cons)}},
 
57
        }}
 
58
        if err := st.runner.Run(ops, "", nil); err != nil {
 
59
                return fmt.Errorf("cannot set constraints: %v", err)
 
60
        }
 
61
        return nil
 
62
}