Ejemplo n.º 1
0
 def __init__(self, arguments):
     super().__init__(arguments)
     connection_string = self.get_from_env("AZURE_TABLES_CONNECTION_STRING")
     if self.args.no_client_share:
         self.service_client = SyncTableServiceClient.from_connection_string(connection_string)
         self.async_service_client = AsyncTableServiceClient.from_connection_string(connection_string)
     else:
         if not _ServiceTest.service_client:
             _ServiceTest.service_client = SyncTableServiceClient.from_connection_string(connection_string)
             _ServiceTest.async_service_client = AsyncTableServiceClient.from_connection_string(connection_string)
         self.service_client = _ServiceTest.service_client
         self.async_service_client =_ServiceTest.async_service_client
    async def tables_in_account(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables.aio import TableServiceClient
        table_service = TableServiceClient.from_connection_string(
            conn_str=self.connection_string)

        async with table_service:
            await table_service.create_table("mytableasync1")
            await table_service.create_table("mytableasync2")

            try:
                # [START tsc_list_tables]
                # List all the tables in the service
                print("Listing tables:")
                async for table in table_service.list_tables():
                    print("\t{}".format(table.name))
                # [END tsc_list_tables]

                # [START tsc_query_tables]
                # Query for "table1" in the tables created
                table_name = "mytableasync1"
                name_filter = "TableName eq '{}'".format(table_name)
                print("Queried_tables")
                async for table in table_service.query_tables(name_filter):
                    print("\t{}".format(table.name))
                # [END tsc_query_tables]

            finally:
                await self.delete_tables()
    async def create_if_not_exists(self):
        from azure.data.tables.aio import TableServiceClient

        # [START create_if_not_exists]
        async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            table_item = await table_service_client.create_table_if_not_exists(table_name=self.table_name)
            print("Table name: {}".format(table_item.table_name))
    async def clean_up(self):
        from azure.data.tables.aio import TableServiceClient
        tsc = TableServiceClient.from_connection_string(self.connection_string)
        async with tsc:
            async for table in tsc.list_tables():
                await tsc.delete_table(table.name)

            print("Cleaned up")
Ejemplo n.º 5
0
    async def authentication_by_shared_key(self):
        # Instantiate a TableServiceClient using a shared access key
        # [START auth_from_shared_key]
        from azure.data.tables.aio import TableServiceClient

        async with TableServiceClient.from_connection_string(conn_str=self.connection_string) as table_service:
            properties = await table_service.get_service_properties()
            print("Shared Key: {}".format(properties))
Ejemplo n.º 6
0
    async def authentication_by_connection_string(self):
        # Instantiate a TableServiceClient using a connection string
        # [START auth_from_connection_string]
        from azure.data.tables.aio import TableServiceClient

        async with TableServiceClient.from_connection_string(conn_str=self.connection_string) as table_service:
            properties = await table_service.get_service_properties()
            print("Connection String: {}".format(properties))
Ejemplo n.º 7
0
    async def delete_table(self):
        from azure.data.tables.aio import TableServiceClient
        from azure.core.exceptions import HttpResponseError

        # [START delete_table]
        async with TableServiceClient.from_connection_string(
            self.connection_string) as table_service_client:
            await table_service_client.delete_table(table_name=self.table_name)
            print("Deleted table {}!".format(self.table_name))
 async def delete_tables(self):
     from azure.data.tables.aio import TableServiceClient
     ts = TableServiceClient.from_connection_string(conn_str=self.connection_string)
     tables = ["mytable1", "mytable2"]
     for table in tables:
         try:
             await ts.delete_table(table_name=table)
         except:
             pass
    async def delete_table(self):
        from azure.data.tables.aio import TableServiceClient
        from azure.core.exceptions import ResourceNotFoundError

        # [START delete_table]
        async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            try:
                await table_service_client.delete_table(table_name=self.table_name)
                print("Deleted table {}!".format(self.table_name))
            except ResourceNotFoundError:
                print("Table could not be found")
    async def create_table(self):
        from azure.data.tables.aio import TableServiceClient
        from azure.core.exceptions import ResourceExistsError

        # [START create_table]
        async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            try:
                table_item = await table_service_client.create_table(table_name=self.table_name)
                print("Created table {}!".format(table_item.table_name))
            except ResourceExistsError:
                print("Table already exists")
Ejemplo n.º 11
0
    def test_create_client_for_cosmos_emulator(self):
        emulator_credential = AzureNamedKeyCredential(
            'localhost', self.tables_primary_cosmos_account_key)
        emulator_connstr = "DefaultEndpointsProtocol=http;AccountName=localhost;AccountKey={};TableEndpoint=http://localhost:8902/;".format(
            self.tables_primary_cosmos_account_key)

        client = TableServiceClient.from_connection_string(emulator_connstr)
        assert client.url == "http://localhost:8902"
        assert client.account_name == 'localhost'
        assert client.credential.named_key.name == 'localhost'
        assert client.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert client._cosmos_endpoint

        client = TableServiceClient("http://localhost:8902/",
                                    credential=emulator_credential)
        assert client.url == "http://localhost:8902"
        assert client.account_name == 'localhost'
        assert client.credential.named_key.name == 'localhost'
        assert client.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert client._cosmos_endpoint

        table = TableClient.from_connection_string(emulator_connstr,
                                                   'tablename')
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint

        table = TableClient("http://localhost:8902/",
                            "tablename",
                            credential=emulator_credential)
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint

        table = TableClient.from_table_url(
            "http://localhost:8902/Tables('tablename')",
            credential=emulator_credential)
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint
Ejemplo n.º 12
0
    def __init__(self, table_name: str):
        logging.info("Reading connection string from environment variable")
        connection_string = os.getenv(TABLE_STORAGE_CONNECTION_STRING_ENV_VAR)
        logging.info(
            f"Read connection string from environment variable. Length: {len(connection_string)}"
        )

        logging.info(f"Connecting to table storage account")
        self.table_service_client = TableServiceClient.from_connection_string(
            conn_str=connection_string, logging_enable=True)
        logging.info(f"Connected to table storage account")
        logging.info(f"Connecting to table {table_name}")
        self.predicted_text_client = self.table_service_client.get_table_client(
            table_name)
        logging.info(f"Connected to table {table_name}")
    async def authentication_by_shared_access_signature(self):
        # Instantiate a TableServiceClient using a connection string
        # [START auth_by_sas]
        from azure.data.tables.aio import TableServiceClient
        table_service = TableServiceClient.from_connection_string(
            conn_str=self.connection_string)

        # Create a SAS token to use for authentication of a client
        from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions
        print(self.account_name)
        sas_token = generate_account_sas(
            self.account_name,
            self.access_key,
            resource_types=ResourceTypes(service=True),
            permission=AccountSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1))

        token_auth_table_service = TableServiceClient(
            account_url=self.account_url, credential=sas_token)
        # [END auth_by_sas]

        properties = await table_service.get_service_properties()
        print("Shared Access Signature: {}".format(properties))
Ejemplo n.º 14
0
    async def test_create_client_for_azurite(self):
        azurite_credential = AzureNamedKeyCredential(
            "myaccount", self.tables_primary_storage_account_key)
        http_connstr = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey={};TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;".format(
            self.tables_primary_storage_account_key)
        https_connstr = "DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey={};TableEndpoint=https://127.0.0.1:10002/devstoreaccount1;".format(
            self.tables_primary_storage_account_key)
        account_url = "https://127.0.0.1:10002/myaccount"
        client = TableServiceClient(account_url, credential=azurite_credential)
        assert client.account_name == "myaccount"
        assert client.url == "https://127.0.0.1:10002/myaccount"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert client.credential.named_key.key == azurite_credential.named_key.key
        assert client.credential.named_key.name == azurite_credential.named_key.name
        assert not client._cosmos_endpoint

        client = TableServiceClient.from_connection_string(http_connstr)
        assert client.account_name == "devstoreaccount1"
        assert client.url == "http://127.0.0.1:10002/devstoreaccount1"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "http://127.0.0.1:10002/devstoreaccount1-secondary"
        assert client.credential.named_key.key == self.tables_primary_storage_account_key
        assert client.credential.named_key.name == "devstoreaccount1"
        assert not client._cosmos_endpoint

        client = TableServiceClient.from_connection_string(https_connstr)
        assert client.account_name == "devstoreaccount1"
        assert client.url == "https://127.0.0.1:10002/devstoreaccount1"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "https://127.0.0.1:10002/devstoreaccount1-secondary"
        assert client.credential.named_key.key == self.tables_primary_storage_account_key
        assert client.credential.named_key.name == "devstoreaccount1"
        assert not client._cosmos_endpoint

        table = TableClient(account_url,
                            "tablename",
                            credential=azurite_credential)
        assert table.account_name == "myaccount"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/myaccount"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert table.credential.named_key.key == azurite_credential.named_key.key
        assert table.credential.named_key.name == azurite_credential.named_key.name
        assert not table._cosmos_endpoint

        table = TableClient.from_connection_string(http_connstr, "tablename")
        assert table.account_name == "devstoreaccount1"
        assert table.table_name == "tablename"
        assert table.url == "http://127.0.0.1:10002/devstoreaccount1"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "http://127.0.0.1:10002/devstoreaccount1-secondary"
        assert table.credential.named_key.key == self.tables_primary_storage_account_key
        assert table.credential.named_key.name == "devstoreaccount1"
        assert not table._cosmos_endpoint

        table = TableClient.from_connection_string(https_connstr, "tablename")
        assert table.account_name == "devstoreaccount1"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/devstoreaccount1"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/devstoreaccount1-secondary"
        assert table.credential.named_key.key == self.tables_primary_storage_account_key
        assert table.credential.named_key.name == "devstoreaccount1"
        assert not table._cosmos_endpoint

        table_url = "https://127.0.0.1:10002/myaccount/Tables('tablename')"
        table = TableClient.from_table_url(table_url,
                                           credential=azurite_credential)
        assert table.account_name == "myaccount"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/myaccount"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert table.credential.named_key.key == azurite_credential.named_key.key
        assert table.credential.named_key.name == azurite_credential.named_key.name
        assert not table._cosmos_endpoint