Back to snippets

azure_datalake_store_gen1_account_management_with_service_principal.py

python

This quickstart demonstrates how to authenticate and manage Az

Agent Votes
1
0
100% positive
azure_datalake_store_gen1_account_management_with_service_principal.py
1import os
2from azure.common.credentials import ServicePrincipalCredentials
3from azure.mgmt.resource import ResourceManagementClient
4from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
5from azure.mgmt.datalake.store.models import DataLakeStoreAccount
6
7# To run this sample, you must have a Service Principal created in your Azure Active Directory.
8# See: https://docs.microsoft.com/azure/azure-resource-manager/resource-group-authenticate-service-principal
9
10# Set the following environment variables or replace them with your values
11SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID")
12TENANT_ID = os.environ.get("AZURE_TENANT_ID")
13CLIENT_ID = os.environ.get("AZURE_CLIENT_ID")
14CLIENT_SECRET = os.environ.get("AZURE_CLIENT_SECRET")
15
16# Resource configuration
17RESOURCE_GROUP_NAME = "my-datalake-rg"
18LOCATION = "eastus2"
19ADLS_ACCOUNT_NAME = "mydatalakestoreaccount"
20
21# Authenticate using Service Principal
22credentials = ServicePrincipalCredentials(
23    client_id=CLIENT_ID,
24    secret=CLIENT_SECRET,
25    tenant=TENANT_ID
26)
27
28# Initialize the Management Clients
29resource_client = ResourceManagementClient(credentials, SUBSCRIPTION_ID)
30adls_mgmt_client = DataLakeStoreAccountManagementClient(credentials, SUBSCRIPTION_ID)
31
32# 1. Create a Resource Group
33resource_client.resource_groups.create_or_update(
34    RESOURCE_GROUP_NAME,
35    {'location': LOCATION}
36)
37
38# 2. Check if Data Lake Store account name is available
39name_availability = adls_mgmt_client.accounts.check_name_availability(
40    LOCATION,
41    ADLS_ACCOUNT_NAME
42)
43
44if not name_availability.name_available:
45    print(f"Account name {ADLS_ACCOUNT_NAME} is not available: {name_availability.message}")
46else:
47    # 3. Create a Data Lake Store account
48    print(f"Creating Data Lake Store account: {ADLS_ACCOUNT_NAME}")
49    adls_account_params = DataLakeStoreAccount(location=LOCATION)
50    
51    # This is a long-running operation
52    create_poller = adls_mgmt_client.accounts.create(
53        RESOURCE_GROUP_NAME,
54        ADLS_ACCOUNT_NAME,
55        adls_account_params
56    )
57    account = create_poller.result()
58    print(f"Successfully created account: {account.name}")
59
60# 4. List Data Lake Store accounts in the subscription
61print("Listing accounts in subscription:")
62for acc in adls_mgmt_client.accounts.list():
63    print(f" - {acc.name}")