コード例 #1
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Starting clean table.')
    ret = dict()
    name = req.headers.get('name')

    if not name:  #If name wasnt added as header, search for it in the parameters
        name = req.params.get('name')

    if name:
        retrieved_secret = getConnectionString()

        table_service = TableService(connection_string=retrieved_secret.value)

        table_service.delete_table(name)
        time.sleep(1)
        existe = False
        while (not existe):
            logging.info("Intentando crearla...")
            time.sleep(5)
            existe = table_service.create_table(name)

        logging.info("Done!!")

        ret['result'] = 'Done!'
        return func.HttpResponse(json.dumps(ret), status_code=200)
    else:
        ret['result'] = 'Please pass a name on the query string or in the request body!'
        return func.HttpResponse(json.dumps(ret), status_code=400)
コード例 #2
0
def table_service():
    table_name = "apmagentpythonci" + str(uuid.uuid4().hex)
    table_service = TableService(connection_string=CONNECTION_STRING)
    table_service.create_table(table_name)
    table_service.table_name = table_name

    yield table_service

    table_service.delete_table(table_name)
コード例 #3
0
def create_table(table_service: TableService, table_name: str):
    if table_service.exists(table_name):
        """
        When a table is successfully deleted,
        it is immediately marked for deletion and is no longer accessible to clients.
        The table is later removed from the Table service during garbage collection.
        Note that deleting a table is likely to take at least 40 seconds to complete.
        """
        table_service.delete_table(table_name)
        print(f"Deleted table '{table_name}'")
    _wait_for_table_to_be_created(table_service, table_name)
コード例 #4
0
def test_table_create(instrument, elasticapm_client):
    table_name = "apmagentpythonci" + str(uuid.uuid4().hex)
    table_service = TableService(connection_string=CONNECTION_STRING)

    elasticapm_client.begin_transaction("transaction.test")
    table_service.create_table(table_name)
    table_service.delete_table(table_name)
    elasticapm_client.end_transaction("MyView")

    span = elasticapm_client.events[constants.SPAN][0]

    assert span["name"] == "AzureTable Create {}".format(table_name)
    assert span["type"] == "storage"
    assert span["subtype"] == "azuretable"
    assert span["action"] == "Create"
コード例 #5
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Starting delete table.')
    ret = dict()
    name = req.headers.get('name')

    if not name:  #If name wasnt added as header, search for it in the parameters
        name = req.params.get('name')

    if name:
        retrieved_secret = getConnectionString()

        table_service = TableService(connection_string=retrieved_secret.value)
        table_service.delete_table(name)

        ret['result'] = 'Success'
        return func.HttpResponse(json.dumps(ret), status_code=200)
    else:
        ret['result'] = 'Please pass a name on the query string or in the request body!'
        return func.HttpResponse(json.dumps(ret), status_code=400)
コード例 #6
0
def delete_table(account_name: str, account_key: str, table_name: str):
    """ Delete an Azure Table.

    :param account_name: Azure Storage account name.
    :param account_key: Azure Storage account key.
    :param table_name: name of the table to delete.
    :return: whether the table was deleted or not.
    """

    service = TableService(account_name=account_name, account_key=account_key)
    return service.delete_table(table_name)
コード例 #7
0
class AzureTable():
    def __init__(self, account_name, account_key):
        self.table_service = TableService(account_name=account_name,
                                          account_key=account_key)

    def create_table(self, table_name):
        return self.table_service.create_table(table_name)

    def exists_table(self, table_name):
        return self.table_service.exists(table_name)

    def insert_or_replace_entity(self, table_name, partition_key, row_key,
                                 **kwargs):
        try:
            entity = self.table_service.get_entity(table_name, partition_key,
                                                   row_key)
        except Exception:
            # Insert a new entity
            entity = {'PartitionKey': partition_key, 'RowKey': row_key}

        for (k, v) in kwargs.items():
            entity[k] = v

        return self.table_service.insert_or_replace_entity(table_name, entity)

    def insert_or_replace_entity2(self, table_name, entity):
        return self.table_service.insert_or_replace_entity(table_name, entity)

    def insert_entity(self, table_name, entity):
        return self.table_service.insert_entity(table_name, entity)

    def update_entity(self, table_name, entity):
        return self.table_service.update_entity(table_name, entity)

    def get_entity(self, table_name, partition_key, row_key):
        return self.table_service.get_entity(table_name, partition_key,
                                             row_key)

    def delete_entity(self, table_name, partition_key, row_key):
        self.table_service.delete_entity(table_name, partition_key, row_key)

    def delete_table(self, table_name):
        return self.table_service.delete_table(table_name)

    def get_entities(self, table_name, partition_key):
        filter = "PartitionKey eq '{0}'".format(partition_key)
        return self.table_service.query_entities(table_name, filter)
コード例 #8
0
print(task.description)
print(task.priority)

# Query a set of entities
print("Query set of entities...")
tasks = table_service.query_entities(
    'tasktable', filter="PartitionKey eq 'tasksSeattle'")
for task in tasks:
    print(task.description)
    print(task.priority)
    try:
       print(task.newCol)
    except AttributeError:
       print("No newCol.")

# Query a subset of entity properties
print("Query a subset of entity properties...")
tasks = table_service.query_entities(
    'tasktable', filter="PartitionKey eq 'tasksSeattle'", select='description')
for task in tasks:
    print(task.description)

# Delete an entity
print("Delete an entity...")
table_service.delete_entity('tasktable', 'tasksSeattle', '001')

# Delete a table
print("Delete a table...")
table_service.delete_table('tasktable')

コード例 #9
0
pizzas = table_service.query_entities('pizzatable',
                                      filter="PartitionKey eq 'pizzamenu'",
                                      select='description,cost')
for pizza in pizzas:
    print('Name: ' + pizza.description)
    print('Cost: ' + str(pizza.cost) + '\n')

time.sleep(1)

###
# This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created, it's good to clean up resources when you're done
###
print(
    '\nThis is a basic example of how Azure Storage Tables behave like a database.\nTo keep things tidy, let\'s clean up the Azure Storage resources we created.'
)
raw_input('Press Enter to continue...')

response = table_service.delete_table('pizzatable')
if response == True:
    print('Storage table: pizzatable deleted successfully.')
else:
    print('Error deleting Storage Table')

response = azurerm.delete_resource_group(auth_token, subscription_id,
                                         resourcegroup_name)
if response.status_code == 202:
    print('Resource group: ' + resourcegroup_name + ' deleted successfully.')
else:
    print('Error deleting resource group.')