/
database.go
74 lines (59 loc) · 1.63 KB
/
database.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
package database
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"crypto/tls"
"net/http"
"time"
"github.com/ugorji/go/codec"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/env"
)
// Database represents a database
type Database struct {
AsyncOperations AsyncOperations
OpenShiftClusters OpenShiftClusters
Subscriptions Subscriptions
}
// NewDatabase returns a new Database
func NewDatabase(ctx context.Context, env env.Interface, uuid string) (db *Database, err error) {
databaseAccount, masterKey := env.CosmosDB()
h := &codec.JsonHandle{
BasicHandle: codec.BasicHandle{
DecodeOptions: codec.DecodeOptions{
ErrorIfNoField: true,
},
},
}
err = api.AddExtensions(&h.BasicHandle)
if err != nil {
return nil, err
}
c := &http.Client{
Transport: &http.Transport{
// disable HTTP/2 for now: https://github.com/golang/go/issues/36026
TLSNextProto: map[string]func(string, *tls.Conn) http.RoundTripper{},
},
Timeout: 30 * time.Second,
}
dbc, err := cosmosdb.NewDatabaseClient(c, h, databaseAccount, masterKey)
if err != nil {
return nil, err
}
db = &Database{}
db.AsyncOperations, err = NewAsyncOperations(uuid, dbc, env.DatabaseName(), "AsyncOperations")
if err != nil {
return nil, err
}
db.OpenShiftClusters, err = NewOpenShiftClusters(ctx, uuid, dbc, env.DatabaseName(), "OpenShiftClusters")
if err != nil {
return nil, err
}
db.Subscriptions, err = NewSubscriptions(ctx, uuid, dbc, env.DatabaseName(), "Subscriptions")
if err != nil {
return nil, err
}
return db, nil
}