Esempio n. 1
0
def main(req: func.HttpRequest,
         todoItems: func.DocumentList) -> func.HttpResponse:
    # Read client settings from environment
    database_name = os.environ['DB_NAME']
    collection_name = os.environ['COLLECTION_NAME']
    # Read itemId from route param
    itemId = req.route_params.get('itemId')
    # Read tenantId from route param
    tenantId = req.route_params.get('tenantId')
    # If item doesn't exist, return error
    if not todoItems:
        return func.HttpResponse(f"Invalid item id {itemId}", status_code=400)
    # Delete item
    else:
        try:
            client = CosmosClient.from_connection_string(os.environ['DB_CSTR'])
            database = client.get_database_client(database_name)
            container = database.get_container_client(collection_name)
            container.delete_item(itemId, tenantId)
            return func.HttpResponse(status_code=200)
        except Exception as inst:
            return func.HttpResponse(
                f"Error deleting item: {str(type(inst))}: {str(inst)} ",
                status_code=400)
Esempio n. 2
0
import logging
import os
import json
import uuid
from azure.cosmos import exceptions, CosmosClient, PartitionKey
import azure.functions as func
from typing import Dict

logging.info("bootstrapping COSMOS DB connection...")
cosmosclient = CosmosClient.from_connection_string(
    os.environ["COSMOSDB_STRING"])
databaseName = 'day11'
logging.info("bootstrapping databse...")
database = cosmosclient.create_database_if_not_exists(id=databaseName)
containerName = 'wishes'
logging.info("bootstrapping databse container...")
CONTAINER = database.create_container_if_not_exists(
    id=containerName,
    partition_key=PartitionKey(path="/type"),
    offer_throughput=400)


def getAllItems():
    global CONTAINER
    items = list(
        CONTAINER.query_items(
            'SELECT d.childname, d.description, d.type, d.address FROM wishes as d',
            enable_cross_partition_query=True))
    return items

Esempio n. 3
0
 def __init__(self, connection_string, database_name, container_name):
     self.connection_string = connection_string
     self.database_name = database_name
     self.container_name = container_name
     self.client = CosmosClient.from_connection_string(
         self.connection_string, credential=None)
import json
import sys
import os

cosmosdb_cstr = os.environ.get('COSMOSDB_CONNECTION_STRING', None)
cosmosdb_db_name = os.environ.get('COSMOSDB_DATABASE', None)
cosmosdb_container_name = os.environ.get('COSMOSDB_CONTAINER', None)

if (cosmosdb_cstr is None or cosmosdb_db_name is None
        or cosmosdb_container_name is None):
    print(
        "Please set COSMOSDB_CONNECTION_STRING, COSMOSDB_DATABASE and COSMOSDB_CONTAINER appropriately",
        file=sys.stderr)
    sys.exit(-1)

cosmosdb_client = CosmosClient.from_connection_string(cosmosdb_cstr)
cosmosdb_db = cosmosdb_client.get_database_client(cosmosdb_db_name)
cosmosdb_container = cosmosdb_db.get_container_client(cosmosdb_container_name)


def print_status(headers, properties):
    print(
        json.dumps({
            'headers:': headers,
            'properties': properties
        },
                   sort_keys=True,
                   indent=4))


cosmosdb_container.read(populate_quota_info=True,
Esempio n. 5
0
def get_client() -> CosmosClient:
    conn_str = os.environ['AZURE_COSMOS_CONN_STRING']
    client = CosmosClient.from_connection_string(conn_str=conn_str)
    database = client.get_database_client('database1')
    container = database.get_container_client('container2')
    return container