-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmongo.go
170 lines (133 loc) · 3.07 KB
/
mongo.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"gopkg.in/mgo.v2"
)
type MongoDB struct {
conf *dbConf
}
type dbConf struct {
Mongo struct {
Host string
Database string
Username string
Password string
}
}
type tfState struct {
Version uint32
Serial uint32
Modules []map[string]interface{}
}
// Dumb wrapper around conf.
func (s *MongoDB) getConfig() *dbConf {
return s.conf
}
// Get a new Session to MongoDB. Do not need to cache it.
// mgo has an internal pool.
func (s *MongoDB) getSession() (*mgo.Session, error) {
cfg := s.getConfig()
session, err := mgo.Dial(cfg.Mongo.Host)
if err != nil {
return nil, err
}
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
return session, nil
}
// Try to Get the MongoDB and yeah save it also.
func (md *MongoDB) getDb(s *mgo.Session) (*mgo.Database, error) {
cfg := md.getConfig()
db := s.DB(cfg.Mongo.Database)
if cfg.Mongo.Username == "" && cfg.Mongo.Password == "" {
return db, nil
}
if err := db.Login(cfg.Mongo.Username, cfg.Mongo.Password); err != nil {
return nil, err
}
return db, nil
}
var defaultYaml = `
mongo:
host: "127.0.0.1:27017"
database: terraform
`
// Do not do anything in MongoDB Setup.
func (s *MongoDB) Setup(cfgpath string) error {
t := &dbConf{}
if cfgpath == "" {
content := []byte(defaultYaml)
tmpfile, err := ioutil.TempFile("", "default_tfstate.yaml")
if err != nil {
return err
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(content); err != nil {
return err
}
if err := tmpfile.Close(); err != nil {
return err
}
cfgpath = tmpfile.Name()
}
if err := parseConfig(cfgpath, t); err != nil {
return err
}
s.conf = t
return nil
}
// Get the latest state for a ident saved in the database.
func (s *MongoDB) Get(ident string) ([]byte, error) {
log.Println("Getting ident", ident)
session, err := s.getSession()
if err != nil {
return nil, err
}
defer session.Close()
db, err := s.getDb(session)
if err != nil {
return nil, err
}
coll := db.C(ident)
state := &tfState{}
if err := coll.Find(nil).Sort("-$natural").One(&state); err != nil {
if err.Error() != "not found" {
return nil, err
}
}
if out, err := json.Marshal(state); err != nil {
return nil, err
} else {
return out, nil
}
}
// Write the state to a database, even if the same version/serial exists, new
// entry will be created. This will help maintain versions and never loose
// history.
func (s *MongoDB) Save(ident string, data []byte) error {
log.Println("Saving ident", ident)
m := &tfState{}
if err := json.Unmarshal(data, m); err != nil {
return err
}
session, err := s.getSession()
if err != nil {
return err
}
defer session.Close()
db, err := s.getDb(session)
if err != nil {
return err
}
coll := db.C(ident)
return coll.Insert(m)
}
// Delete the state from database? How do we care. Just archive the collection
// by renaming it to <coll>-archive-<timestamp>.
func (s *MongoDB) Delete(ident string) error {
log.Println("Deleting ident", ident)
return nil
}