/
main.go
130 lines (106 loc) · 2.66 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/jewzaam/go-cosmosdb/example/cosmosdb"
"github.com/jewzaam/go-cosmosdb/example/types"
"github.com/sirupsen/logrus"
"github.com/ugorji/go/codec"
)
const (
dbid = "example-database"
collid = "example"
)
var (
persons = []types.Person{
types.Person{
ID: "jim",
Surname: "minter",
},
types.Person{
ID: "mangirdas",
Surname: "judeikis",
},
}
)
func main() {
ctx := context.Background()
log := logrus.NewEntry(logrus.StandardLogger())
err := run(ctx, log)
if err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context, log *logrus.Entry) error {
// read cosmosdb account
account, found := os.LookupEnv("COSMOSDB_ACCOUNT")
if !found {
return fmt.Errorf("must set COSMOSDB_ACCOUNT")
}
key, found := os.LookupEnv("COSMOSDB_KEY")
if !found {
return fmt.Errorf("must set COSMOSDB_KEY")
}
// jsonHandle enables custom encoders. In example field encryption or marshaling
jsonHandle := &codec.JsonHandle{
BasicHandle: codec.BasicHandle{
DecodeOptions: codec.DecodeOptions{
ErrorIfNoField: true,
},
},
}
// create authorizer for rest calls
keyAuthorizer, err := cosmosdb.NewMasterKeyAuthorizer(key)
if err != nil {
return err
}
// get database client
dbc := cosmosdb.NewDatabaseClient(log, http.DefaultClient, jsonHandle, account+".documents.azure.com", keyAuthorizer)
// create database "example-database"
db, err := dbc.Create(ctx, &cosmosdb.Database{ID: dbid})
if err != nil && !cosmosdb.IsErrorStatusCode(err, http.StatusConflict) {
return err
}
log.Info("database: %#v\n", db)
collc := cosmosdb.NewCollectionClient(dbc, dbid)
coll, err := collc.Create(ctx, &cosmosdb.Collection{
ID: collid,
PartitionKey: &cosmosdb.PartitionKey{
Paths: []string{
"/id",
},
},
})
if err != nil && !cosmosdb.IsErrorStatusCode(err, http.StatusConflict) {
return err
}
log.Info("collection: %#v\n", coll)
// Create persons document client in the collections above
dc := cosmosdb.NewPersonClient(collc, collid)
// HACK: if you are re-running this application we delete existing users first
// As cosmosdb do not support truncate do it old fashion way
for _, person := range persons {
err = dc.Delete(ctx, person.ID, &person, nil)
if err != nil {
return err
}
}
for _, person := range persons {
_, err := dc.Create(ctx, person.ID, &person, &cosmosdb.Options{})
if err != nil {
return err
}
}
// Read document back
docs, err := dc.ListAll(ctx, &cosmosdb.Options{})
if err != nil {
return err
}
// Print back the values
for _, person := range docs.People {
log.Info("documents: %#v\n", person)
}
return nil
}