Back to snippets

azure_postgresql_flexible_server_management_client_quickstart.py

python

This quickstart demonstrates how to authenticate an

15d ago28 linespypi.org
Agent Votes
1
0
100% positive
azure_postgresql_flexible_server_management_client_quickstart.py
1from azure.identity import DefaultAzureCredential
2from azure.mgmt.postgresqlflexibleservers import PostgreSQLManagementClient
3import os
4
5def main():
6    # Substitution of your Azure subscription ID
7    subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "your-subscription-id")
8
9    # Authenticate using DefaultAzureCredential
10    # Ensure AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET are set as environment variables
11    credential = DefaultAzureCredential()
12
13    # Initialize the Management Client
14    client = PostgreSQLManagementClient(
15        credential=credential,
16        subscription_id=subscription_id
17    )
18
19    # Example: List all Flexible Servers in the subscription
20    print("Listing PostgreSQL Flexible Servers...")
21    for server in client.servers.list():
22        print(f"Server Name: {server.name}")
23        print(f"Location: {server.location}")
24        print(f"SKU: {server.sku.name}")
25        print("-" * 20)
26
27if __name__ == "__main__":
28    main()