-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
64 lines (57 loc) · 1.87 KB
/
container.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/mgo/v3/bson"
"github.com/juju/mgo/v3/txn"
"github.com/juju/juju/core/container"
)
// machineContainers holds the machine ids of all the containers belonging to a parent machine.
// All machines have an associated container ref doc, regardless of whether they host any containers.
type machineContainers struct {
DocID string `bson:"_id"`
Id string `bson:"machineid"`
ModelUUID string `bson:"model-uuid"`
Children []string `bson:",omitempty"`
}
func addChildToContainerRefOp(mb modelBackend, parentId string, childId string) txn.Op {
return txn.Op{
C: containerRefsC,
Id: mb.docID(parentId),
Assert: txn.DocExists,
Update: bson.D{{"$addToSet", bson.D{{"children", childId}}}},
}
}
func insertNewContainerRefOp(mb modelBackend, machineId string, children ...string) txn.Op {
return txn.Op{
C: containerRefsC,
Id: mb.docID(machineId),
Assert: txn.DocMissing,
Insert: &machineContainers{
Id: machineId,
Children: children,
},
}
}
// removeContainerRefOps returns the txn.Op's necessary to remove a machine container record.
// These include removing the record itself and updating the host machine's children property.
func removeContainerRefOps(mb modelBackend, machineId string) []txn.Op {
removeRefOp := txn.Op{
C: containerRefsC,
Id: mb.docID(machineId),
Assert: txn.DocExists,
Remove: true,
}
// If the machine is a container, figure out its parent host.
parentId := container.ParentId(machineId)
if parentId == "" {
return []txn.Op{removeRefOp}
}
removeParentRefOp := txn.Op{
C: containerRefsC,
Id: mb.docID(parentId),
Assert: txn.DocExists,
Update: bson.D{{"$pull", bson.D{{"children", machineId}}}},
}
return []txn.Op{removeRefOp, removeParentRefOp}
}