Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/azure-cli/azure/cli/command_modules/cdn/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@
helps['cdn endpoint rule add'] = """
type: command
short-summary: Add a delivery rule to a CDN endpoint.
parameters:
- name: --rule-name
type: string
short-summary: >
Name of the rule, only required for Microsoft SKU.
examples:
- name: Create a global rule to disable caching.
text: >
Expand All @@ -248,6 +253,9 @@
- name: Remove the global rule.
text: >
az cdn endpoint rule remove -g group -n endpoint --profile-name profile --rule-name Global
- name: Remove the rule with the order 4.
text: >
az cdn endpoint rule remove -g group -n endpoint --profile-name profile --order 4
"""

helps['cdn endpoint rule show'] = """
Expand Down
11 changes: 8 additions & 3 deletions src/azure-cli/azure/cli/command_modules/cdn/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def load_arguments(self, _):
help='The secret name of the KeyVault certificate')
c.argument('user_cert_secret_version',
arg_group='Bring Your Own Certificate',
help='The secret version of the KeyVault certificate')
help='The secret version of the KeyVault certificate, If not specified, the "Latest" version will '
'always been used and the deployed certificate will be automatically rotated to the latest '
'version when a newer version of the certificate is available.')

# Origin #
with self.argument_context('cdn origin') as c:
Expand Down Expand Up @@ -501,8 +503,11 @@ def configure_log_analytic_common_parameters(c):
# pylint: disable=protected-access
def configure_rule_parameters(c):
c.argument('rule_name', help='Name of the rule.')
c.argument('order', help='The order of the rule. The order number must start from 0 and consecutive. '
"Rule with higher order will be applied later.")
c.argument('order', type=int,
help='The order in which the rules are applied for the endpoint. Possible values {0,1,2,3,………}. '
'A rule with a lower order will be applied before one with a higher order. '
'Rule with order 0 is a special rule. '
'It does not require any condition and actions listed in it will always be applied.')
c.argument('match_variable', arg_group="Match Condition", help='Name of the match condition.',
arg_type=get_enum_type(DeliveryRuleCondition._subtype_map["name"].keys()))
c.argument('operator', arg_group="Match Condition", help='Operator of the match condition.')
Expand Down
56 changes: 50 additions & 6 deletions src/azure-cli/azure/cli/command_modules/cdn/custom/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ def _update_mapper(existing, new, keys):
setattr(new, key, new_value if new_value is not None else existing_value)


def _convert_to_unified_delivery_rules(policy):
for existing_rule in policy.rules:
if existing_rule.conditions:
for con in existing_rule.conditions:
if con.parameters.operator is None and con.parameters.match_values is None:
if con.parameters.odata_type == UrlPathMatchConditionParameters.odata_type:
con.parameters.operator = con.parameters.additional_properties["matchType"]
con.parameters.match_values = con.parameters.additional_properties["path"].split(',')
if con.parameters.odata_type == UrlFileExtensionMatchConditionParameters.odata_type:
con.parameters.operator = "Any"
con.parameters.match_values = con.parameters.additional_properties["extensions"]


# region Custom Commands
def list_profiles(client, resource_group_name=None):
profiles = client.profiles
Expand Down Expand Up @@ -339,20 +352,29 @@ def create_action(action_name, cache_behavior=None, cache_duration=None, header_

# pylint: disable=too-many-locals
def add_rule(client, resource_group_name, profile_name, endpoint_name,
order, rule_name, action_name, match_variable=None, operator=None,
order, action_name, match_variable=None, operator=None,
match_values=None, selector=None, negate_condition=None, transform=None,
cache_behavior=None, cache_duration=None, header_action=None,
header_name=None, header_value=None, query_string_behavior=None, query_parameters=None,
redirect_type=None, redirect_protocol=None, custom_hostname=None, custom_path=None,
custom_querystring=None, custom_fragment=None, source_pattern=None,
destination=None, preserve_unmatched_path=None):
destination=None, preserve_unmatched_path=None, rule_name=None):

partner_skus = [SkuName.PREMIUM_VERIZON, SkuName.CUSTOM_VERIZON, SkuName.STANDARD_AKAMAI, SkuName.STANDARD_VERIZON]
profile = client.profiles.get(resource_group_name, profile_name)
if rule_name is None and profile.sku.name not in partner_skus:
raise CLIError("--rule-name is required for Microsoft SKU")

endpoint = client.endpoints.get(resource_group_name, profile_name, endpoint_name)

policy = endpoint.delivery_policy
if policy is None:
policy = EndpointPropertiesUpdateParametersDeliveryPolicy(
description='delivery_policy',
rules=[])

_convert_to_unified_delivery_rules(policy)

conditions = []
condition = create_condition(match_variable, operator, match_values, selector, negate_condition, transform)
if condition is not None:
Expand Down Expand Up @@ -422,14 +444,36 @@ def add_action(client, resource_group_name, profile_name, endpoint_name,
return client.endpoints.begin_update(resource_group_name, profile_name, endpoint_name, params)


def remove_rule(client, resource_group_name, profile_name, endpoint_name, rule_name):
def remove_rule(client, resource_group_name, profile_name, endpoint_name, rule_name=None, order: int = None):

if rule_name is None and order is None:
raise CLIError("Either --rule-name or --order must be specified")

if order is not None and order < 0:
raise CLIError("Order should be non-negative.")

endpoint = client.endpoints.get(resource_group_name, profile_name, endpoint_name)
policy = endpoint.delivery_policy
if policy is not None:
for rule in policy.rules:
if rule.name == rule_name:
policy.rules.remove(rule)
_convert_to_unified_delivery_rules(policy)
pop_index = -1
for idx, rule in enumerate(policy.rules):
if rule_name is not None and rule.name == rule_name:
pop_index = idx
break
elif order is not None and rule.order == order:
pop_index = idx
break

# To guarantee the consecutive rule order, we need to make sure the rule with order larger than the deleted one
# to decrease its order by one. Rule with order 0 is special and no rule order adjustment is required.
if pop_index != -1:
pop_order = policy.rules[pop_index].order
policy.rules.pop(pop_index)
for rule in policy.rules:
if rule.order > pop_order and pop_order != 0:
rule.order -= 1

else:
logger.warning("rule cannot be found. This command will be skipped. Please check the rule name")

Expand Down
Loading