-
Notifications
You must be signed in to change notification settings - Fork 132
/
manage_topic.py
90 lines (76 loc) · 2.25 KB
/
manage_topic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.servicebus import ServiceBusManagementClient
from azure.mgmt.resource import ResourceManagementClient
def main():
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
GROUP_NAME = "testgroupx"
NAMESPACE = "namespacexxyyzz"
TOPIC = "topicxxyyzz"
# Create client
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
servicebus_client = ServiceBusManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# Create resource group
resource_client.resource_groups.create_or_update(
GROUP_NAME,
{"location": "eastus"}
)
# - init depended resources -
namespace = servicebus_client.namespaces.begin_create_or_update(
GROUP_NAME,
NAMESPACE,
{
"sku": {
"name": "Standard",
"tier": "Standard"
},
"location": "eastus",
"tags": {
"tag1": "value1",
"tag2": "value2"
}
}
).result()
# - end -
# Create topic
topic = servicebus_client.topics.create_or_update(
GROUP_NAME,
NAMESPACE,
TOPIC,
{
"enable_express": True
}
)
print("Create topic:\n{}".format(topic))
# Get topic
topic = servicebus_client.topics.get(
GROUP_NAME,
NAMESPACE,
TOPIC
)
print("Get topic:\n{}".format(topic))
# Delete topic
topic = servicebus_client.topics.delete(
GROUP_NAME,
NAMESPACE,
TOPIC
)
print("Delete topic.\n")
# Delete Group
resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()
if __name__ == "__main__":
main()