/
environ.go
78 lines (67 loc) · 1.87 KB
/
environ.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/txn"
"launchpad.net/juju-core/errors"
)
// environGlobalKey is the key for the environment, its
// settings and constraints.
const environGlobalKey = "e"
// Environment represents the state of an environment.
type Environment struct {
st *State
doc environmentDoc
annotator
}
// environmentDoc represents the internal state of the environment in MongoDB.
type environmentDoc struct {
UUID string `bson:"_id"`
Name string
}
// Environment returns the environment entity.
func (st *State) Environment() (*Environment, error) {
doc := environmentDoc{}
err := st.environments.Find(D{{"uuid", D{{"$ne", ""}}}}).One(&doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("environment")
} else if err != nil {
return nil, err
}
env := &Environment{
st: st,
doc: doc,
}
env.annotator = annotator{
globalKey: env.globalKey(),
tag: env.Tag(),
st: st,
}
return env, nil
}
// Tag returns a name identifying the environment.
// The returned name will be different from other Tag values returned
// by any other entities from the same state.
func (e Environment) Tag() string {
return "environment-" + e.doc.Name
}
// UUID returns the universally unique identifier of the environment.
func (e Environment) UUID() string {
return e.doc.UUID
}
// globalKey returns the global database key for the environment.
func (e *Environment) globalKey() string {
return environGlobalKey
}
// createEnvironmentOp returns the operation needed to create
// an environment document with the given name and UUID.
func createEnvironmentOp(st *State, name, uuid string) txn.Op {
doc := &environmentDoc{uuid, name}
return txn.Op{
C: st.environments.Name,
Id: uuid,
Assert: txn.DocMissing,
Insert: doc,
}
}