This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase_client.go
157 lines (143 loc) · 5.46 KB
/
database_client.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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright (c) 2019-present, Jet.com, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License."
package interstellar
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
)
// DatabaseResource represents a Database in Cosmos DB
// Documentation adapted from adapted from docs.microsoft.com
// See https://docs.microsoft.com/en-us/rest/api/cosmos-db/databases for the latest documentation
type DatabaseResource struct {
// ID is the unique user generated name for the database.
ID string `json:"id"`
// ResourceID is a unique identifier that is also hierarchical per the resource stack on the resource model. It is used internally for placement of and navigation to the database resource.
ResourceID string `json:"_rid,omitempty"`
// Timestamp is a system generated property. It denotes the last updated timestamp of the resource.
Timestamp int64 `json:"_ts,omitempty"`
// Self is the unique addressable URI for the resource.
Self string `json:"_self,omitempty"`
// ETag value required for optimistic concurrency control.
ETag string `json:"_etag,omitempty"`
// Colls specifies the addressable path of the collections resource.
Colls string `json:"_colls,omitempty"`
// Users specifies the addressable path of the users resource.
Users string `json:"_users,omitempty"`
}
// CreateDatabaseRaw creates a new database with the given ID and returns the raw response
func (c *Client) CreateDatabaseRaw(ctx context.Context, id string, opts RequestOptions) ([]byte, *ResponseMetadata, error) {
body, err := json.Marshal(DatabaseResource{ID: id})
if err != nil {
return nil, nil, err
}
return c.CreateOrReplaceResource(ctx, ClientRequest{
Path: "/dbs",
ResourceType: ResourceDatabases,
ResourceLink: "",
Options: opts,
Body: bytes.NewBuffer(body),
})
}
// CreateDatabase creates a new database with the given ID
func (c *Client) CreateDatabase(ctx context.Context, id string, opts RequestOptions) (*DatabaseResource, *ResponseMetadata, error) {
body, meta, err := c.CreateDatabaseRaw(ctx, id, opts)
if err != nil {
return nil, meta, err
}
var db DatabaseResource
if err = json.Unmarshal(body, &db); err != nil {
return nil, meta, err
}
return &db, meta, err
}
// ListDatabasesRaw lists each database in the CosmosDB Account as raw JSON objects given to the pagination function
func (c *Client) ListDatabasesRaw(ctx context.Context, opts RequestOptions, fn PaginateRawResources) error {
return c.ListResources(ctx, "Databases", ClientRequest{
Path: "/dbs",
ResourceType: ResourceDatabases,
ResourceLink: "",
Options: opts,
}, fn)
}
// PaginateDatabaseResource pagination function for a list of DatabaseResources
type PaginateDatabaseResource func(resList []DatabaseResource, meta ResponseMetadata) (bool, error)
// DatabaseClient is a client scoped to a single database
// Used to perform API calls within the scope of the Database resource
type DatabaseClient struct {
Client *Client
DatabaseID string
}
// ListDatabases lists each database in the CosmosDB Account given to the pagination function
func (c *Client) ListDatabases(ctx context.Context, opts RequestOptions, fn PaginateDatabaseResource) error {
return c.ListDatabasesRaw(ctx, opts, func(resList []json.RawMessage, meta ResponseMetadata) (bool, error) {
databases := make([]DatabaseResource, len(resList))
for i, res := range resList {
var db DatabaseResource
if err := json.Unmarshal(res, &db); err != nil {
return false, err
}
databases[i] = db
}
return fn(databases, meta)
})
}
// WithDatabase creates a DatabaseClient for the given Database ID
func (c *Client) WithDatabase(id string) *DatabaseClient {
return &DatabaseClient{
Client: c,
DatabaseID: id,
}
}
// ResourceLink gets the resource link for the database
func (c *DatabaseClient) ResourceLink() string {
return fmt.Sprintf("dbs/%s", url.PathEscape(c.DatabaseID))
}
// GetRaw retrieves the raw database resource
func (c *DatabaseClient) GetRaw(ctx context.Context, opts RequestOptions) ([]byte, *ResponseMetadata, error) {
rl := c.ResourceLink()
return c.Client.GetResource(ctx, ClientRequest{
Path: fmt.Sprintf("/%s", rl),
ResourceLink: rl,
ResourceType: ResourceDatabases,
Options: opts,
})
}
// Get retrieves the DatabaseResource
func (c *DatabaseClient) Get(ctx context.Context, opts RequestOptions) (*DatabaseResource, *ResponseMetadata, error) {
body, meta, err := c.GetRaw(ctx, opts)
if err != nil {
return nil, meta, err
}
var db DatabaseResource
if err = json.Unmarshal(body, &db); err != nil {
return nil, meta, err
}
return &db, meta, err
}
// Delete will delete the database
// See Client.DeleteResource for more information
func (c *DatabaseClient) Delete(ctx context.Context, opts RequestOptions) (bool, *ResponseMetadata, error) {
rl := c.ResourceLink()
return c.Client.DeleteResource(ctx, ClientRequest{
Path: fmt.Sprintf("/%s", rl),
ResourceLink: rl,
ResourceType: ResourceDatabases,
Options: opts,
})
}