File: mgmt_rule_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (106 lines) | stat: -rw-r--r-- 3,912 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

"""
Example to show managing rule entities under a ServiceBus Subscription, including
    - Create a rule
    - Create a rule with sql filter
    - Get rule properties.
    - Update a rule
    - Delete a rule
    - List rules under the given ServiceBus Namespace
"""

import os
import asyncio
import uuid
from azure.servicebus.management import SqlRuleFilter
from azure.servicebus.aio.management import ServiceBusAdministrationClient

CONNECTION_STR = os.environ['SERVICEBUS_CONNECTION_STR']
TOPIC_NAME = os.environ['SERVICEBUS_TOPIC_NAME']
SUBSCRIPTION_NAME = os.environ['SERVICEBUS_SUBSCRIPTION_NAME']
RULE_NAME = "sb_mgmt_rule" + str(uuid.uuid4())
RULE_WITH_SQL_FILTER_NAME = "sb_sql_rule" + str(uuid.uuid4())


async def create_rule(servicebus_mgmt_client):
    print("-- Create Rule")
    await servicebus_mgmt_client.create_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_NAME)
    print("Rule {} is created.".format(RULE_NAME))
    print("")

    print("-- Create Rule with SQL Filter")
    sql_filter_parametrized = SqlRuleFilter(
        "property1 = @param1 AND property2 = @param2",
        parameters={
            "@param1": "value",
            "@param2" : 1
        }
    )
    await servicebus_mgmt_client.create_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_WITH_SQL_FILTER_NAME, filter=sql_filter_parametrized)
    print("Rule {} is created.".format(RULE_WITH_SQL_FILTER_NAME))
    print("")


async def delete_rule(servicebus_mgmt_client):
    print("-- Delete Rule")
    await servicebus_mgmt_client.delete_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_NAME)
    print("Rule {} is deleted.".format(RULE_NAME))
    await servicebus_mgmt_client.delete_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_WITH_SQL_FILTER_NAME)
    print("Rule {} is deleted.".format(RULE_WITH_SQL_FILTER_NAME))
    print("")


async def list_rules(servicebus_mgmt_client):
    print("-- List Rules")
    async for rule_properties in servicebus_mgmt_client.list_rules(TOPIC_NAME, SUBSCRIPTION_NAME):
        print("Rule Name:", rule_properties.name)
    print("")


async def get_and_update_rule(servicebus_mgmt_client):
    print("-- Get and Update Rule")
    rule_properties = await servicebus_mgmt_client.get_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_NAME)
    print("Rule Name:", rule_properties.name)
    print("Please refer to RuleProperties for complete available properties.")
    print("")

    # update by updating the properties in the model
    rule_properties.filter = SqlRuleFilter(
        "property1 = @param1 AND property2 = @param2",
        parameters={
            "@param1": "value2",
            "@param2": 2
        }
    )
    await servicebus_mgmt_client.update_rule(TOPIC_NAME, SUBSCRIPTION_NAME, rule_properties)

    # update by passing keyword arguments
    rule_properties = await servicebus_mgmt_client.get_rule(TOPIC_NAME, SUBSCRIPTION_NAME, RULE_NAME)
    await servicebus_mgmt_client.update_rule(
        TOPIC_NAME,
        SUBSCRIPTION_NAME,
        rule_properties,
        filter=SqlRuleFilter(
            "property1 = @param1 AND property2 = @param2",
            parameters={
                "@param1": "value3",
                "@param2": 3
            }
        )
    )


async def main():
    async with ServiceBusAdministrationClient.from_connection_string(CONNECTION_STR) as servicebus_mgmt_client:
        await create_rule(servicebus_mgmt_client)
        await list_rules(servicebus_mgmt_client)
        await get_and_update_rule(servicebus_mgmt_client)
        await delete_rule(servicebus_mgmt_client)

asyncio.run(main())