def test_mgmt_rule_update_dict_success(self, servicebus_namespace_connection_string, **kwargs):
        mgmt_service = ServiceBusAdministrationClient.from_connection_string(servicebus_namespace_connection_string)
        clear_topics(mgmt_service)
        topic_name = "fjruid"
        subscription_name = "eqkovcd"
        rule_name = 'rule'
        sql_filter = SqlRuleFilter("Priority = 'low'")

        try:
            topic_description = mgmt_service.create_topic(topic_name)
            subscription_description = mgmt_service.create_subscription(topic_description.name, subscription_name)
            mgmt_service.create_rule(topic_name, subscription_name, rule_name, filter=sql_filter)

            rule_desc = mgmt_service.get_rule(topic_name, subscription_name, rule_name)

            assert type(rule_desc.filter) == SqlRuleFilter
            assert rule_desc.filter.sql_expression == "Priority = 'low'"

            correlation_fitler = CorrelationRuleFilter(correlation_id='testcid')
            sql_rule_action = SqlRuleAction(sql_expression="SET Priority = 'low'")

            rule_desc.filter = correlation_fitler
            rule_desc.action = sql_rule_action
            rule_desc_dict = dict(rule_desc)
            mgmt_service.update_rule(topic_description.name, subscription_description.name, rule_desc_dict)

            rule_desc = mgmt_service.get_rule(topic_name, subscription_name, rule_name)
            assert type(rule_desc.filter) == CorrelationRuleFilter
            assert rule_desc.filter.correlation_id == 'testcid'
            assert rule_desc.action.sql_expression == "SET Priority = 'low'"

        finally:
            mgmt_service.delete_rule(topic_name, subscription_name, rule_name)
            mgmt_service.delete_subscription(topic_name, subscription_name)
            mgmt_service.delete_topic(topic_name)
    async def test_async_mgmt_rule_update_success(self, servicebus_namespace_connection_string, **kwargs):
        mgmt_service = ServiceBusManagementClient.from_connection_string(servicebus_namespace_connection_string)
        await clear_topics(mgmt_service)
        topic_name = "fjrui"
        subscription_name = "eqkovc"
        rule_name = 'rule'
        sql_filter = SqlRuleFilter("Priority = 'low'")
        rule = RuleDescription(name=rule_name, filter=sql_filter)

        try:
            topic_description = await mgmt_service.create_topic(topic_name)
            subscription_description = await mgmt_service.create_subscription(topic_description, subscription_name)
            await mgmt_service.create_rule(topic_name, subscription_name, rule)

            rule_desc = await mgmt_service.get_rule(topic_name, subscription_name, rule_name)

            assert type(rule_desc.filter) == SqlRuleFilter
            assert rule_desc.filter.sql_expression == "Priority = 'low'"

            correlation_fitler = CorrelationRuleFilter(correlation_id='testcid')
            sql_rule_action = SqlRuleAction(sql_expression="SET Priority = 'low'")

            rule_desc.filter = correlation_fitler
            rule_desc.action = sql_rule_action
            await mgmt_service.update_rule(topic_description, subscription_description, rule_desc)

            rule_desc = await mgmt_service.get_rule(topic_name, subscription_name, rule_name)
            assert type(rule_desc.filter) == CorrelationRuleFilter
            assert rule_desc.filter.correlation_id == 'testcid'
            assert rule_desc.action.sql_expression == "SET Priority = 'low'"

        finally:
            await mgmt_service.delete_rule(topic_name, subscription_name, rule_name)
            await mgmt_service.delete_subscription(topic_name, subscription_name)
            await mgmt_service.delete_topic(topic_name)
def create_rules_with_filter(servicebus_mgmt_client):
    # First subscription is already created with default rule. Leave as is.
    print("SubscriptionName: {}, Removing and re-adding Default Rule".format(
        ALL_MSGS_SUBSCRIPTION_NAME))
    create_rule_with_filter(servicebus_mgmt_client,
                            ALL_MSGS_SUBSCRIPTION_NAME,
                            "$Default",
                            filter=TrueRuleFilter())

    # Second subscription: Add required SqlRuleFilter Rule.
    print(
        "SubscriptionName: {}, Removing Default Rule and Adding SqlRuleFilter."
        .format(SQL_FILTER_ONLY_SUBSCRIPTION_NAME))
    create_rule_with_filter(servicebus_mgmt_client,
                            SQL_FILTER_ONLY_SUBSCRIPTION_NAME,
                            "RedSqlRule",
                            filter=SqlRuleFilter("Color = 'Red'"))

    # Third subscription: Add SqlRuleFilter and SqlRuleAction.
    print(
        "SubscriptionName: {}, Removing Default Rule and Adding SqlRuleFilter and SqlRuleAction"
        .format(SQL_FILTER_WITH_ACTION_SUBSCRIPTION_NAME))
    create_rule_with_filter(
        servicebus_mgmt_client,
        SQL_FILTER_WITH_ACTION_SUBSCRIPTION_NAME,
        "BlueSqlRule",
        filter=SqlRuleFilter("Color = 'Blue'"),
        action=SqlRuleAction("SET Color = 'BlueProcessed'"))

    # Fourth subscription: Add CorrelationRuleFilter.
    print(
        "SubscriptionName: {}, Removing Default Rule and Adding CorrelationRuleFilter"
        .format(CORRELATION_FILTER_SUBSCRIPTION_NAME))
    create_rule_with_filter(servicebus_mgmt_client,
                            CORRELATION_FILTER_SUBSCRIPTION_NAME,
                            "ImportantCorrelationRule",
                            filter=CorrelationRuleFilter(
                                correlation_id='important', label="Red"))

    # Get rules on subscription, called here only for one subscription as an example.
    print("SubscriptionName: {}".format(CORRELATION_FILTER_SUBSCRIPTION_NAME))
    for rule in servicebus_mgmt_client.list_rules(
            TOPIC_NAME, CORRELATION_FILTER_SUBSCRIPTION_NAME):
        print("Rule {}; Filter: {}".format(rule.name,
                                           type(rule.filter).__name__))
    async def test_async_mgmt_rule_create(self, servicebus_namespace_connection_string, **kwargs):
        mgmt_service = ServiceBusManagementClient.from_connection_string(servicebus_namespace_connection_string)
        await clear_topics(mgmt_service)
        topic_name = "topic_testaddf"
        subscription_name = "sub_testkkk"
        rule_name_1 = 'test_rule_1'
        rule_name_2 = 'test_rule_2'
        rule_name_3 = 'test_rule_3'

        correlation_fitler = CorrelationRuleFilter(correlation_id='testcid')
        sql_rule_action = SqlRuleAction(sql_expression="SET Priority = 'low'")
        rule_1 = RuleDescription(name=rule_name_1, filter=correlation_fitler, action=sql_rule_action)

        sql_filter = SqlRuleFilter("Priority = 'low'")
        rule_2 = RuleDescription(name=rule_name_2, filter=sql_filter)

        bool_filter = TrueRuleFilter()
        rule_3 = RuleDescription(name=rule_name_3, filter=bool_filter)

        try:
            await mgmt_service.create_topic(topic_name)
            await mgmt_service.create_subscription(topic_name, subscription_name)

            await mgmt_service.create_rule(topic_name, subscription_name, rule_1)
            rule_desc = await mgmt_service.get_rule(topic_name, subscription_name, rule_name_1)
            assert type(rule_desc.filter) == CorrelationRuleFilter
            assert rule_desc.filter.correlation_id == 'testcid'
            assert rule_desc.action.sql_expression == "SET Priority = 'low'"

            await mgmt_service.create_rule(topic_name, subscription_name, rule_2)
            rule_desc = await mgmt_service.get_rule(topic_name, subscription_name, rule_name_2)
            assert type(rule_desc.filter) == SqlRuleFilter
            assert rule_desc.filter.sql_expression == "Priority = 'low'"

            await mgmt_service.create_rule(topic_name, subscription_name, rule_3)
            rule_desc = await mgmt_service.get_rule(topic_name, subscription_name, rule_name_3)
            assert type(rule_desc.filter) == TrueRuleFilter

        finally:
            await mgmt_service.delete_rule(topic_name, subscription_name, rule_name_1)
            await mgmt_service.delete_rule(topic_name, subscription_name, rule_name_2)
            await mgmt_service.delete_rule(topic_name, subscription_name, rule_name_3)
            await mgmt_service.delete_subscription(topic_name, subscription_name)
            await mgmt_service.delete_topic(topic_name)
    def test_mgmt_rule_create(self, servicebus_namespace_connection_string,
                              **kwargs):
        mgmt_service = ServiceBusAdministrationClient.from_connection_string(
            servicebus_namespace_connection_string)
        clear_topics(mgmt_service)
        topic_name = "topic_testaddf"
        subscription_name = "sub_testkkk"
        rule_name_1 = 'test_rule_1'
        rule_name_2 = 'test_rule_2'
        rule_name_3 = 'test_rule_3'
        rule_name_4 = 'test_rule_4'

        correlation_fitler = CorrelationRuleFilter(correlation_id='testcid',
                                                   properties={
                                                       "key_string":
                                                       "str1",
                                                       "key_int":
                                                       2,
                                                       "key_long":
                                                       INT32_MAX_VALUE + 3,
                                                       "key_bool":
                                                       False,
                                                       "key_datetime":
                                                       datetime(
                                                           2020, 7, 5, 11, 12,
                                                           13),
                                                       "key_duration":
                                                       timedelta(days=1,
                                                                 hours=2,
                                                                 minutes=3)
                                                   })
        sql_rule_action = SqlRuleAction(sql_expression="SET Priority = @param",
                                        parameters={
                                            "@param":
                                            datetime(2020, 7, 5, 11, 12, 13),
                                        })

        sql_filter = SqlRuleFilter("Priority = @param1",
                                   parameters={
                                       "@param1": "str1",
                                   })

        bool_filter = TrueRuleFilter()

        try:
            mgmt_service.create_topic(topic_name)
            mgmt_service.create_subscription(topic_name, subscription_name)

            mgmt_service.create_rule(topic_name,
                                     subscription_name,
                                     rule_name_1,
                                     filter=correlation_fitler,
                                     action=sql_rule_action)
            rule_desc = mgmt_service.get_rule(topic_name, subscription_name,
                                              rule_name_1)
            rule_properties = rule_desc.filter.properties
            assert type(rule_desc.filter) == CorrelationRuleFilter
            assert rule_desc.filter.correlation_id == 'testcid'
            assert rule_desc.action.sql_expression == "SET Priority = @param"
            assert rule_desc.action.parameters["@param"] == datetime(
                2020, 7, 5, 11, 12, 13)
            assert rule_properties["key_string"] == "str1"
            assert rule_properties["key_int"] == 2
            assert rule_properties["key_long"] == INT32_MAX_VALUE + 3
            assert rule_properties["key_bool"] is False
            assert rule_properties["key_datetime"] == datetime(
                2020, 7, 5, 11, 12, 13)
            assert rule_properties["key_duration"] == timedelta(days=1,
                                                                hours=2,
                                                                minutes=3)

            mgmt_service.create_rule(topic_name,
                                     subscription_name,
                                     rule_name_2,
                                     filter=sql_filter)
            rule_desc = mgmt_service.get_rule(topic_name, subscription_name,
                                              rule_name_2)
            assert type(rule_desc.filter) == SqlRuleFilter
            assert rule_desc.filter.sql_expression == "Priority = @param1"
            assert rule_desc.filter.parameters["@param1"] == "str1"

            mgmt_service.create_rule(topic_name,
                                     subscription_name,
                                     rule_name_3,
                                     filter=bool_filter)
            rule_desc = mgmt_service.get_rule(topic_name, subscription_name,
                                              rule_name_3)
            assert type(rule_desc.filter) == TrueRuleFilter

            mgmt_service.create_rule(topic_name, subscription_name,
                                     rule_name_4)
            rule_desc = mgmt_service.get_rule(topic_name, subscription_name,
                                              rule_name_4)
            assert type(rule_desc.filter) == TrueRuleFilter

        finally:
            try:
                mgmt_service.delete_rule(topic_name, subscription_name,
                                         rule_name_1)
            except:
                pass
            try:
                mgmt_service.delete_rule(topic_name, subscription_name,
                                         rule_name_2)
            except:
                pass
            try:
                mgmt_service.delete_rule(topic_name, subscription_name,
                                         rule_name_3)
            except:
                pass
            try:
                mgmt_service.delete_rule(topic_name, subscription_name,
                                         rule_name_4)
            except:
                pass
            try:
                mgmt_service.delete_subscription(topic_name, subscription_name)
            except:
                pass
            try:
                mgmt_service.delete_topic(topic_name)
            except:
                pass