Back to snippets
azure_notification_hubs_namespace_and_hub_creation_quickstart.py
pythonAuthenticates with Azure and creates a new Notification Hubs
Agent Votes
1
0
100% positive
azure_notification_hubs_namespace_and_hub_creation_quickstart.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.notificationhubs import NotificationHubsManagementClient
4from azure.mgmt.notificationhubs.models import CheckAvailabilityParameters, SharedAccessAuthorizationRuleProperties
5
6def main():
7 # Set your subscription ID and resource group details
8 SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "your-subscription-id")
9 RESOURCE_GROUP = "sample-resource-group"
10 NAMESPACE_NAME = "sample-nh-namespace"
11 NOTIFICATION_HUB_NAME = "sample-hub"
12 LOCATION = "eastus"
13
14 # 1. Acquire a credential object using managed identity or environment variables
15 credential = DefaultAzureCredential()
16
17 # 2. Initialize the management client
18 client = NotificationHubsManagementClient(credential, SUBSCRIPTION_ID)
19
20 # 3. Create a Notification Hubs Namespace
21 print(f"Creating namespace: {NAMESPACE_NAME}...")
22 namespace = client.namespaces.create_or_update(
23 RESOURCE_GROUP,
24 NAMESPACE_NAME,
25 {
26 "location": LOCATION,
27 "sku": {"name": "Standard"},
28 }
29 )
30 print(f"Namespace created: {namespace.name}")
31
32 # 4. Create a Notification Hub
33 print(f"Creating notification hub: {NOTIFICATION_HUB_NAME}...")
34 hub = client.notification_hubs.create_or_update(
35 RESOURCE_GROUP,
36 NAMESPACE_NAME,
37 NOTIFICATION_HUB_NAME,
38 {
39 "location": LOCATION
40 }
41 )
42 print(f"Notification Hub created: {hub.name}")
43
44 # 5. List Connection Strings (Primary/Secondary)
45 print("Fetching connection strings...")
46 keys = client.namespaces.list_keys(
47 RESOURCE_GROUP,
48 NAMESPACE_NAME,
49 "RootManageSharedAccessKey"
50 )
51 print(f"Primary Connection String: {keys.primary_connection_string}")
52
53if __name__ == "__main__":
54 main()