This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Program.py
280 lines (218 loc) 路 10.5 KB
/
Program.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import pydocumentdb.documents as documents
import pydocumentdb.document_client as document_client
import pydocumentdb.errors as errors
import config as cfg
# ----------------------------------------------------------------------------------------------------------
# Prerequistes -
#
# 1. An Azure DocumentDB account -
# https://azure.microsoft.com/en-us/documentation/articles/documentdb-create-account/
#
# 2. Microsoft Azure DocumentDB PyPi package -
# https://pypi.python.org/pypi/pydocumentdb/
# ----------------------------------------------------------------------------------------------------------
# Sample - demonstrates the basic CRUD operations on a Collection resource for Azure DocumentDB
#
# 1. Query for Collection
#
# 2. Create Collection
# 2.1 - Basic Create
# 2.2 - Create collection with custom IndexPolicy
# 2.3 - Create collection with offerType set
#
# 3. Manage Collection OfferType
# 3.1 - Get Collection performance tier
# 3.2 - Change performance tier
#
# 4. Get a Collection by its Id property
#
# 5. List all Collection resources in a Database
#
# 6. Delete Collection
# ----------------------------------------------------------------------------------------------------------
# Note -
#
# Running this sample will create (and delete) multiple DocumentCollections on your account.
# Each time a DocumentCollection is created the account will be billed for 1 hour of usage based on
# the performance tier of that account.
# ----------------------------------------------------------------------------------------------------------
HOST = cfg.settings['host']
MASTER_KEY = cfg.settings['master_key']
DATABASE_ID = cfg.settings['database_id']
COLLECTION_ID = cfg.settings['collection_id']
database_link = 'dbs/' + DATABASE_ID
class IDisposable:
""" A context manager to automatically close an object with a close method
in a with statement. """
def __init__(self, obj):
self.obj = obj
def __enter__(self):
return self.obj # bound to target
def __exit__(self, exception_type, exception_val, trace):
# extra cleanup in here
self = None
class CollectionManagement:
@staticmethod
def find_collection(client, id):
print('1. Query for Collection')
collections = list(client.QueryCollections(
database_link,
{
"query": "SELECT * FROM r WHERE r.id=@id",
"parameters": [
{ "name":"@id", "value": id }
]
}
))
if len(collections) > 0:
print('Collection with id \'{0}\' was found'.format(id))
else:
print('No collection with id \'{0}\' was found'. format(id))
@staticmethod
def create_collection(client, id):
""" Execute the most basic Create of collection.
This will create a collection with OfferType = S1 and default indexing policy """
print("\n2.1 Create Collection - Basic")
try:
client.CreateCollection(database_link, {"id": id})
print('Collection with id \'{0}\' created'.format(id))
except errors.DocumentDBError as e:
if e.status_code == 409:
print('A collection with id \'{0}\' already exists'.format(id))
else:
raise errors.HTTPFailure(e.status_code)
print("\n2.2 Create Collection - With custom index policy")
try:
coll = {
"id": "collection_custom_index_policy",
"indexingPolicy": {
"indexingMode": "lazy",
"automatic": False
}
}
collection = client.CreateCollection(database_link, coll)
print('Collection with id \'{0}\' created'.format(collection['id']))
print('IndexPolicy Mode - \'{0}\''.format(collection['indexingPolicy']['indexingMode']))
print('IndexPolicy Automatic - \'{0}\''.format(collection['indexingPolicy']['automatic']))
except errors.DocumentDBError as e:
if e.status_code == 409:
print('A collection with id \'{0}\' already exists'.format(collection['id']))
else:
raise errors.HTTPFailure(e.status_code)
print("\n2.3 Create Collection - With custom offerType")
try:
coll = {"id": "collection_custom_offertype"}
collection = client.CreateCollection(database_link, coll, {'offerType': 'S2'} )
print('Collection with id \'{0}\' created'.format(collection['id']))
except errors.DocumentDBError as e:
if e.status_code == 409:
print('A collection with id \'{0}\' already exists'.format(collection['id']))
else:
raise errors.HTTPFailure(e.status_code)
@staticmethod
def manage_offertype(client, id):
print("\n3.1 Get Collection Performance tier")
#Collections have offers which are of type S1, S2, or S3.
#Each of these determine the performance throughput of a collection.
#A Collection is loosely coupled to Offer through the Offer's offerResourceId
#Offer.offerResourceId == Collection._rid
#Offer.resource == Collection._self
try:
# read the collection, so we can get its _self
collection_link = database_link + '/colls/{0}'.format(id)
collection = client.ReadCollection(collection_link)
# now use its _self to query for Offers
offer = list(client.QueryOffers('SELECT * FROM c WHERE c.resource = \'{0}\''.format(collection['_self'])))[0]
print('Found Offer \'{0}\' for Collection \'{1}\' and its offerType is \'{2}\''.format(offer['id'], collection['_self'], offer['offerType']))
except errors.DocumentDBError as e:
if e.status_code == 404:
print('A collection with id \'{0}\' does not exist'.format(id))
else:
raise errors.HTTPFailure(e.status_code)
print("\n3.2 Change OfferType of Collection")
#The OfferType of a collection controls the throughput allocated to the Collection
#To increase (or decrease) the throughput of any Collection you need to adjust the Offer.offerType
#of the Offer record linked to the Collection
#setting it S1, doesn't do anything because by default Collections are created with S1 Offer.offerType
#just doing it in the sample to demonstrate how to do it and not create resources (and incur costs) we don't need
#valid values for offerType are (currently) S1, S2 or S3
offer['offerType'] = 'S2'
offer = client.ReplaceOffer(offer['_self'], offer)
print('Replaced Offer. OfferType is now \'{0}\''.format(offer['offerType']))
@staticmethod
def read_collection(client, id):
print("\n4. Get a Collection by id")
try:
# All DocumentDB resources are addressable via a link
# This link is constructed from a combination of resource hierachy and
# the resource id.
# Eg. The link for collection with an id of Bar in database Foo would be dbs/Foo/colls/Bar
collection_link = database_link + '/colls/{0}'.format(id)
collection = client.ReadCollection(collection_link)
print('Collection with id \'{0}\' was found, it\'s _self is {1}'.format(collection['id'], collection['_self']))
except errors.DocumentDBError as e:
if e.status_code == 404:
print('A collection with id \'{0}\' does not exist'.format(id))
else:
raise errors.HTTPFailure(e.status_code)
@staticmethod
def list_collections(client):
print("\n5. List all Collection in a Database")
print('Collections:')
collections = list(client.ReadCollections(database_link))
if not collections:
return
for collection in collections:
print(collection['id'])
@staticmethod
def delete_collection(client, id):
print("\n6. Delete Collection")
try:
collection_link = database_link + '/colls/{0}'.format(id)
client.DeleteCollection(collection_link)
print('Collection with id \'{0}\' was deleted'.format(id))
except errors.DocumentDBError as e:
if e.status_code == 404:
print('A collection with id \'{0}\' does not exist'.format(id))
else:
raise errors.HTTPFailure(e.status_code)
def run_sample():
with IDisposable(document_client.DocumentClient(HOST, {'masterKey': MASTER_KEY} )) as client:
try:
# setup database for this sample
try:
client.CreateDatabase({"id": DATABASE_ID})
except errors.DocumentDBError as e:
if e.status_code == 409:
pass
else:
raise errors.HTTPFailure(e.status_code)
# query for a collection
CollectionManagement.find_collection(client, COLLECTION_ID)
# create a collection
CollectionManagement.create_collection(client, COLLECTION_ID)
# get & change OfferType of collection
CollectionManagement.manage_offertype(client, COLLECTION_ID)
# get a collection using its id
CollectionManagement.read_collection(client, COLLECTION_ID)
# list all collection on an account
CollectionManagement.list_collections(client)
# delete collection by id
CollectionManagement.delete_collection(client, COLLECTION_ID)
# cleanup database after sample
try:
client.DeleteDatabase(database_link)
except errors.DocumentDBError as e:
if e.status_code == 404:
pass
else:
raise errors.HTTPFailure(e.status_code)
except errors.HTTPFailure as e:
print('\nrun_sample has caught an error. {0}'.format(e.message))
finally:
print("\nrun_sample done")
if __name__ == '__main__':
try:
run_sample()
except Exception as e:
print("Top level Error: args:{0}, message:{1}".format(e.args,e.message))