/
cosmos.go
179 lines (145 loc) · 4.02 KB
/
cosmos.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
171
172
173
174
175
176
177
178
179
package azure
import (
"fmt"
"github.com/Azure/go-autorest/autorest"
)
// it seems the cosmos API is not returning any sort of valid ID in the main response body
// so lets grab it from the response.request.url.path
func CosmosGetIDFromResponse(resp autorest.Response) (string, error) {
if resp.Response == nil {
return "", fmt.Errorf("Error: Unable to get Cosmos ID from Response: http response is nil")
}
if resp.Response.Request == nil {
return "", fmt.Errorf("Error: Unable to get Cosmos ID from Response: Request is nil")
}
if resp.Response.Request.URL == nil {
return "", fmt.Errorf("Error: Unable to get Cosmos ID from Response: URL is nil")
}
return resp.Response.Request.URL.Path, nil
}
type CosmosAccountID struct {
ResourceID
Account string
}
func ParseCosmosAccountID(id string) (*CosmosAccountID, error) {
subid, err := ParseAzureResourceID(id)
if err != nil {
return nil, err
}
account, ok := subid.Path["databaseAccounts"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Database Resource ID: databaseAccounts is missing from: %s", id)
}
return &CosmosAccountID{
ResourceID: *subid,
Account: account,
}, nil
}
type CosmosDatabaseID struct {
CosmosAccountID
Database string
}
func ParseCosmosDatabaseID(id string) (*CosmosDatabaseID, error) {
subid, err := ParseCosmosAccountID(id)
if err != nil {
return nil, err
}
db, ok := subid.Path["databases"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Database Resource ID: databases is missing from: %s", id)
}
return &CosmosDatabaseID{
CosmosAccountID: *subid,
Database: db,
}, nil
}
type CosmosDatabaseCollectionID struct {
CosmosDatabaseID
Collection string
}
func ParseCosmosDatabaseCollectionID(id string) (*CosmosDatabaseCollectionID, error) {
subid, err := ParseCosmosDatabaseID(id)
if err != nil {
return nil, err
}
collection, ok := subid.Path["collections"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Database Resource ID: collections is missing from: %s", id)
}
return &CosmosDatabaseCollectionID{
CosmosDatabaseID: *subid,
Collection: collection,
}, nil
}
type CosmosDatabaseContainerID struct {
CosmosDatabaseID
Container string
}
func ParseCosmosDatabaseContainerID(id string) (*CosmosDatabaseContainerID, error) {
subid, err := ParseCosmosDatabaseID(id)
if err != nil {
return nil, err
}
container, ok := subid.Path["containers"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Database Container Resource ID: containers is missing from: %s", id)
}
return &CosmosDatabaseContainerID{
CosmosDatabaseID: *subid,
Container: container,
}, nil
}
type CosmosKeyspaceID struct {
CosmosAccountID
Keyspace string
}
func ParseCosmosKeyspaceID(id string) (*CosmosKeyspaceID, error) {
subid, err := ParseCosmosAccountID(id)
if err != nil {
return nil, err
}
ks, ok := subid.Path["keyspaces"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Keyspace Resource ID: keyspaces is missing from: %s", id)
}
return &CosmosKeyspaceID{
CosmosAccountID: *subid,
Keyspace: ks,
}, nil
}
type CosmosTableID struct {
CosmosAccountID
Table string
}
func ParseCosmosTableID(id string) (*CosmosTableID, error) {
subid, err := ParseCosmosAccountID(id)
if err != nil {
return nil, err
}
table, ok := subid.Path["tables"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Table Resource ID: tables is missing from: %s", id)
}
return &CosmosTableID{
CosmosAccountID: *subid,
Table: table,
}, nil
}
type CosmosGremlinGraphID struct {
CosmosDatabaseID
Graph string
}
func ParseCosmosGramlinGraphID(id string) (*CosmosGremlinGraphID, error) {
subid, err := ParseCosmosDatabaseID(id)
if err != nil {
return nil, err
}
graph, ok := subid.Path["graphs"]
if !ok {
return nil, fmt.Errorf("Error: Unable to parse Cosmos Gremlin Graph Resource ID: Graph is missing from: %s", id)
}
return &CosmosGremlinGraphID{
CosmosDatabaseID: *subid,
Graph: graph,
}, nil
}