File: cosmos_responses_management_async.py

package info (click to toggle)
python-azure 20251104%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 770,224 kB
  • sloc: python: 6,357,217; ansic: 804; javascript: 287; makefile: 198; sh: 193; xml: 109
file content (240 lines) | stat: -rw-r--r-- 9,561 bytes parent folder | download
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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import uuid

from azure.cosmos.aio import CosmosClient
import azure.cosmos.exceptions as exceptions
from azure.cosmos.partition_key import PartitionKey
from azure.cosmos import ThroughputProperties

import asyncio
import config

# ----------------------------------------------------------------------------------------------------------
# Prerequisites -
#
# 1. An Azure Cosmos account -
#    https://azure.microsoft.com/documentation/articles/documentdb-create-account/
#
# 2. Microsoft Azure Cosmos PyPi package -
#    https://pypi.python.org/pypi/azure-cosmos/
# ----------------------------------------------------------------------------------------------------------
# Sample - demonstrates the basic CRUD operations for Control Plane responses for Azure Cosmos
#
# 1. Create Database
#    1.1 - Basic Create Database
#    1.2 - Create Database if not previously existing
#    1.3 - Create Database if not previously existing
#    1.4 - Database Read
#    1.5 - Database Replace Throughput
#
# 2. Create Container
#    2.1 - Basic Create Container
#    2.2 - Create container if not previously existing
#    2.3 - Create container if not previously existing
#    2.4 - Container Read
#    2.5 - Container Replace Throughput
#
# ----------------------------------------------------------------------------------------------------------
# Note -
#
# Running this sample will create (and delete) multiple Containers on your account.
# Each time a Container is created the account will be billed for 1 hour of usage based on
# the provisioned throughput (RU/s) of that account.
# ----------------------------------------------------------------------------------------------------------

HOST = config.settings['host']
MASTER_KEY = config.settings['master_key']
DATABASE_ID = config.settings['database_id'] + str(uuid.uuid4())
CONTAINER_ID = config.settings['container_id'] + str(uuid.uuid4())


async def create_db(client, id):
    # create database with return properties parameter set to true
    print("\n1.1 Create Database")

    try:
        response_properties = await client.create_database(id=id, return_properties=True)
        print('Database with id \'{0}\' created'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosResourceExistsError:
        print('A database with id \'{0}\' already exists'.format(id))


async def create_db_if_not_exists(client, id):
    print("\n1.2 Create Database if not exists")

    try:
        response_properties = await client.create_database_if_not_exists(id=id, return_properties=True)
        print('Database with id \'{0}\' created or retrieved'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosHttpResponseError as e:
        print('Error creating database: {0}'.format(e.message))


async def create_db_if_exists(client, id):
    print("\n1.3 Get Database if exists")

    try:
        response_properties = await client.create_database_if_not_exists(id=id, return_properties=True)
        print('Database with id \'{0}\' found'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosResourceNotFoundError:
        print('Database with id \'{0}\' does not exist'.format(id))


async def read_db(client, id):
    print("\n1.4 Read Database")

    try:
        database = client.get_database_client(id)
        properties = await database.read()
        print('Database with id \'{0}\' was found'.format(id))
    except exceptions.CosmosResourceNotFoundError:
        print('A database with id \'{0}\' does not exist'.format(id))


async def replace_throughput_db(client, id):
    print("\n1.5 Replace Database Throughput")

    try:
        database = await client.create_database(id=id+"1", offer_throughput=400)
        replace_throughput_value = 500
        properties = await database.replace_throughput(replace_throughput_value)
        print('Database throughput changed to 800 RU/s')
        print(properties.get_response_headers())
    except exceptions.CosmosResourceNotFoundError:
        print('Database with id \'{0}\' does not exist'.format(id))
    except exceptions.CosmosHttpResponseError as e:
        print('Error changing database throughput: {0}'.format(e.message))


async def create_container(db, id):
    """ Execute basic container creation.
    This will create containers with 400 RUs with different indexing, partitioning, and storage options """

    partition_key = PartitionKey(path='/id', kind='Hash')
    print("\n2.1 Create Container - Basic")

    try:
        response_properties = await db.create_container(id=id, partition_key=partition_key, return_properties=True)
        print('Container with id \'{0}\' created'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosResourceExistsError:
        print('A container with id \'{0}\' already exists'.format(id))


async def create_container_if_not_exists(db, id):
    print("\n2.2 Create Container if not exists")

    partition_key = PartitionKey(path='/id', kind='Hash')
    try:
        response_properties = await db.create_container_if_not_exists(id=id, partition_key=partition_key, return_properties=True)
        print('Container with id \'{0}\' created or retrieved'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosHttpResponseError as e:
        print('Error creating container: {0}'.format(e.message))


async def create_container_if_exists(db, id):
    print("\n2.3 Get Container if exists")

    try:
        response_properties = await db.create_container_if_not_exists(id=id, return_properties=True)
        print('Container with id \'{0}\' found'.format(id))
        print(response_properties[1].get_response_headers())
    except exceptions.CosmosResourceNotFoundError:
        print('Container with id \'{0}\' does not exist'.format(id))


async def read_container(db, id):
    print("\n2.4 Get a Container by id")

    try:
        container = db.get_container_client(id)
        properties = await container.read()
        print('Container with id \'{0}\' was found, it\'s link is {1}'.format(container.id, container.container_link))
    except exceptions.CosmosResourceNotFoundError:
        print('A container with id \'{0}\' does not exist'.format(id))


async def replace_throughput_container(db, id):
    print("\n2.5 Replace Container Throughput")

    try:
        container = await db.create_container(id=id,  partition_key=PartitionKey(path="/company"), offer_throughput=400)
        replace_throughput_value = 500
        properties = await container.replace_throughput(replace_throughput_value)
        # Set new throughput to 600 RU/s
        new_throughput = ThroughputProperties(offer_throughput=600)
        print('Container throughput changed to 600 RU/s')
        print(properties.get_response_headers())
    except exceptions.CosmosResourceNotFoundError:
        print('Container with id \'{0}\' does not exist'.format(id))
    except exceptions.CosmosHttpResponseError as e:
        print('Error changing container throughput: {0}'.format(e.message))


async def run_sample():
    async with CosmosClient(HOST, {'masterKey': MASTER_KEY}) as client:
        try:
            # setup database for this sample
            try:
                throughput_properties = ThroughputProperties(500)
                db = await client.create_database(id=DATABASE_ID, offer_throughput=throughput_properties)

            except exceptions.CosmosResourceExistsError:
                db = client.get_database_client(DATABASE_ID)

            # demonstrate database operations
            print("\n=== Database Operations ===")

            # create a database
            await create_db(client, DATABASE_ID + "_demo1")

            # create a database if it doesn't exist already
            await create_db_if_not_exists(client, DATABASE_ID + "_demo2")

            # read database information if the database already exists
            await create_db_if_exists(client, DATABASE_ID)

            # read from database
            await read_db(client, DATABASE_ID)

            # replace throughput for database
            await replace_throughput_db(client, DATABASE_ID)

            # demonstrate container operations
            print("\n=== Container Operations ===")

            # create a container
            await create_container(db, CONTAINER_ID)

            # create container if not exists
            await create_container_if_not_exists(db, CONTAINER_ID + "_demo")

            # get a container using its id
            await read_container(db, CONTAINER_ID)

            # replace container throughput
            await replace_throughput_container(db, CONTAINER_ID + "demo")

            # cleanup database after sample
            try:
                await client.delete_database(db)

            except exceptions.CosmosResourceNotFoundError:
                pass

        except exceptions.CosmosHttpResponseError as e:
            print('\nrun_sample has caught an error. {0}'.format(e.message))

        finally:
            print("\nrun_sample done")


if __name__ == '__main__':
    asyncio.run(run_sample())