/
clustermanager.go
138 lines (112 loc) · 4.63 KB
/
clustermanager.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
package database
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/util/uuid"
)
const (
ClusterManagerConfigurationsGetQuery = `SELECT * FROM ClusterManagerConfigurations doc WHERE doc.key = @key`
)
type clusterManagerConfiguration struct {
c cosmosdb.ClusterManagerConfigurationDocumentClient
collc cosmosdb.CollectionClient
uuid string
uuidGenerator uuid.Generator
}
type ClusterManagerConfigurations interface {
Create(context.Context, *api.ClusterManagerConfigurationDocument) (*api.ClusterManagerConfigurationDocument, error)
Get(context.Context, string) (*api.ClusterManagerConfigurationDocument, error)
Update(context.Context, *api.ClusterManagerConfigurationDocument) (*api.ClusterManagerConfigurationDocument, error)
Delete(context.Context, *api.ClusterManagerConfigurationDocument) error
ChangeFeed() cosmosdb.ClusterManagerConfigurationDocumentIterator
NewUUID() string
}
func NewClusterManagerConfigurations(ctx context.Context, dbc cosmosdb.DatabaseClient, dbName string) (ClusterManagerConfigurations, error) {
collc := cosmosdb.NewCollectionClient(dbc, dbName)
documentClient := cosmosdb.NewClusterManagerConfigurationDocumentClient(collc, collClusterManager)
return NewClusterManagerConfigurationsWithProvidedClient(documentClient, collc, uuid.DefaultGenerator.Generate(), uuid.DefaultGenerator), nil
}
func NewClusterManagerConfigurationsWithProvidedClient(client cosmosdb.ClusterManagerConfigurationDocumentClient, collectionClient cosmosdb.CollectionClient, uuid string, uuidGenerator uuid.Generator) ClusterManagerConfigurations {
return &clusterManagerConfiguration{
c: client,
collc: collectionClient,
uuid: uuid,
uuidGenerator: uuidGenerator,
}
}
func (c *clusterManagerConfiguration) NewUUID() string {
return c.uuidGenerator.Generate()
}
func (c *clusterManagerConfiguration) Create(ctx context.Context, doc *api.ClusterManagerConfigurationDocument) (*api.ClusterManagerConfigurationDocument, error) {
if doc.ID != strings.ToLower(doc.ID) {
return nil, fmt.Errorf("id %q is not lower case", doc.ID)
}
var err error
doc.PartitionKey, err = c.partitionKey(doc.Key)
if err != nil {
return nil, err
}
doc, err = c.c.Create(ctx, doc.PartitionKey, doc, nil)
if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict {
err.StatusCode = http.StatusPreconditionFailed
}
return doc, err
}
func (c *clusterManagerConfiguration) Get(ctx context.Context, id string) (*api.ClusterManagerConfigurationDocument, error) {
if id != strings.ToLower(id) {
return nil, fmt.Errorf("id %q is not lower case", id)
}
partitionKey, err := c.partitionKey(id)
if err != nil {
return nil, err
}
docs, err := c.c.QueryAll(ctx, partitionKey, &cosmosdb.Query{
Query: ClusterManagerConfigurationsGetQuery,
Parameters: []cosmosdb.Parameter{
{
Name: "@key",
Value: id,
},
},
}, nil)
if err != nil {
return nil, err
}
switch {
case len(docs.ClusterManagerConfigurationDocuments) > 1:
return nil, fmt.Errorf("read %d documents, expected <= 1", len(docs.ClusterManagerConfigurationDocuments))
case len(docs.ClusterManagerConfigurationDocuments) == 1:
return docs.ClusterManagerConfigurationDocuments[0], nil
default:
return nil, &cosmosdb.Error{StatusCode: http.StatusNotFound}
}
}
func (c *clusterManagerConfiguration) Update(ctx context.Context, doc *api.ClusterManagerConfigurationDocument) (*api.ClusterManagerConfigurationDocument, error) {
return c.update(ctx, doc, nil)
}
func (c *clusterManagerConfiguration) update(ctx context.Context, doc *api.ClusterManagerConfigurationDocument, options *cosmosdb.Options) (*api.ClusterManagerConfigurationDocument, error) {
if doc.ID != strings.ToLower(doc.ID) {
return nil, fmt.Errorf("id %q is not lower case", doc.ID)
}
return c.c.Replace(ctx, doc.PartitionKey, doc, options)
}
func (c *clusterManagerConfiguration) Delete(ctx context.Context, doc *api.ClusterManagerConfigurationDocument) error {
if doc.ID != strings.ToLower(doc.ID) {
return fmt.Errorf("id %q is not lower case", doc.ID)
}
return c.c.Delete(ctx, doc.PartitionKey, doc, &cosmosdb.Options{NoETag: true})
}
func (c *clusterManagerConfiguration) ChangeFeed() cosmosdb.ClusterManagerConfigurationDocumentIterator {
return c.c.ChangeFeed(nil)
}
func (c *clusterManagerConfiguration) partitionKey(key string) (string, error) {
r, err := azure.ParseResourceID(key)
return r.SubscriptionID, err
}