def remove_template_output(cmd, client, resource_group_name, image_template_name, output_name): _require_defer(cmd) existing_image_template = cached_get( cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) if not existing_image_template.distribute: raise CLIError("No outputs to remove.") new_distribute = [] for existing_distributor in existing_image_template.distribute: if existing_distributor.run_output_name.lower() == output_name.lower(): continue new_distribute.append(existing_distributor) if len(new_distribute) == len(existing_image_template.distribute): raise CLIError( "Output with output name {} not in image template distribute list." .format(output_name)) existing_image_template.distribute = new_distribute return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=existing_image_template, # pylint: disable=line-too-long resource_group_name=resource_group_name, image_template_name=image_template_name)
def remove_template_customizer(cmd, client, resource_group_name, image_template_name, customizer_name): existing_image_template = cached_get( cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) _require_defer(cmd) if not existing_image_template.customize: raise CLIError("No customizers to remove.") new_customize = [] for existing_customizer in existing_image_template.customize: if existing_customizer.name == customizer_name: continue new_customize.append(existing_customizer) if len(new_customize) == len(existing_image_template.customize): raise CLIError( "Customizer with name {} not in image template customizer list.". format(customizer_name)) existing_image_template.customize = new_customize return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=existing_image_template, # pylint: disable=line-too-long resource_group_name=resource_group_name, image_template_name=image_template_name)
def update_wp_custom_rule(cmd, resource_group_name, policy_name, rule_name, priority=None, action=None, rate_limit_duration=None, rate_limit_threshold=None, disabled=None): client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) foundRule = False for rule in policy.custom_rules.rules: if rule.name.lower() == rule_name.lower(): foundRule = True with UpdateContext(rule) as c: c.update_param('priority', priority, None) c.update_param('action', action, None) c.update_param('rate_limit_duration', rate_limit_duration, None) c.update_param('rate_limit_threshold', rate_limit_threshold, None) c.update_param('enabled_state', 'Enabled' if not disabled else 'Disabled', 'Disabled') if not foundRule: from knack.util import CLIError raise CLIError("rule '{}' not found".format(rule_name)) return cached_put(cmd, client.create_or_update, policy, resource_group_name, policy_name).result()
def add_custom_rule_match_condition(cmd, resource_group_name, policy_name, rule_name, match_variable, operator, values, negate=None, transforms=None): from azext_front_door.vendored_sdks.models import MatchCondition client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) foundRule = False for rule in policy.custom_rules.rules: if rule.name.upper() == rule_name.upper(): foundRule = True selector = None variable_parts = match_variable.split('.') if len(variable_parts) == 2: match_variable = variable_parts[0] selector = variable_parts[1] rule.match_conditions.append(MatchCondition( match_variable=match_variable, selector=selector, operator=operator, negate_condition=negate, match_value=values, transforms=transforms )) if not foundRule: from knack.util import CLIError raise CLIError("rule '{}' not found".format(rule_name)) return cached_put(cmd, client.create_or_update, policy, resource_group_name, policy_name).result()
def create_wp_custom_rule(cmd, resource_group_name, policy_name, rule_name, priority, rule_type, action, rate_limit_duration=None, rate_limit_threshold=None, disabled=None): if rule_type.lower() == "ratelimitrule" and (rate_limit_duration is None or rate_limit_threshold is None): from knack.util import CLIError raise CLIError( "rate_limit_duration and rate_limit_threshold are required for a RateLimitRule" ) from azext_front_door.vendored_sdks.models import CustomRule client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) rule = CustomRule(name=rule_name, priority=priority, rule_type=rule_type, action=action, match_conditions=[], rate_limit_duration_in_minutes=rate_limit_duration, rate_limit_threshold=rate_limit_threshold, enabled_state='Enabled' if not disabled else 'Disabled') policy.custom_rules.rules.append(rule) return cached_put(cmd, client.create_or_update, policy, resource_group_name, policy_name).result()
def show_wp_custom_rule(cmd, resource_group_name, policy_name, rule_name): client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) try: return next(x for x in policy.custom_rules.rules if x.name.lower() == rule_name.lower()) except StopIteration: from knack.util import CLIError raise CLIError("rule '{}' not found".format(rule_name))
def list_custom_rule_match_conditions(cmd, resource_group_name, policy_name, rule_name): client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) for rule in policy.custom_rules.rules: if rule.name.upper() == rule_name.upper(): return rule.match_conditions from knack.util import CLIError raise CLIError("rule '{}' not found".format(rule_name))
def add_template_output(cmd, client, resource_group_name, image_template_name, gallery_name=None, location=None, # pylint: disable=line-too-long, unused-argument output_name=None, is_vhd=None, tags=None, gallery_image_definition=None, gallery_replication_regions=None, managed_image=None, managed_image_location=None): # pylint: disable=line-too-long, unused-argument _require_defer(cmd) from azure.mgmt.imagebuilder.models import ( ImageTemplateManagedImageDistributor, ImageTemplateVhdDistributor, ImageTemplateSharedImageDistributor) existing_image_template = cached_get( cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) distributor = None if managed_image: parsed = parse_resource_id(managed_image) distributor = ImageTemplateManagedImageDistributor( run_output_name=output_name or parsed['name'], image_id=managed_image, location=managed_image_location or location) elif gallery_image_definition: parsed = parse_resource_id(gallery_image_definition) distributor = ImageTemplateSharedImageDistributor( run_output_name=output_name or parsed['child_name_1'], gallery_image_id=gallery_image_definition, replication_regions=gallery_replication_regions or [location]) elif is_vhd: distributor = ImageTemplateVhdDistributor(run_output_name=output_name) if distributor: distributor.artifact_tags = tags or {} if existing_image_template.distribute is None: existing_image_template.distribute = [] else: for existing_distributor in existing_image_template.distribute: if existing_distributor.run_output_name == distributor.run_output_name: raise CLIError( "Output with output name {} already exists in image template {}." .format(distributor.run_output_name.lower(), image_template_name)) existing_image_template.distribute.append(distributor) return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=existing_image_template, # pylint: disable=line-too-long resource_group_name=resource_group_name, image_template_name=image_template_name)
def clear_template_output(cmd, client, resource_group_name, image_template_name): _require_defer(cmd) existing_image_template = cached_get(cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) if not existing_image_template.distribute: raise CLIError("No outputs to remove.") existing_image_template.distribute = [] return cached_put(cmd, client.virtual_machine_image_templates.create_or_update, parameters=existing_image_template, resource_group_name=resource_group_name, image_template_name=image_template_name)
def add_template_customizer(cmd, client, resource_group_name, image_template_name, customizer_name, customizer_type, script_url=None, inline_script=None, valid_exit_codes=None, restart_command=None, restart_check_command=None, restart_timeout=None, file_source=None, dest_path=None, search_criteria=None, filters=None, update_limit=None): _require_defer(cmd) from azure.mgmt.imagebuilder.models import (ImageTemplateShellCustomizer, ImageTemplatePowerShellCustomizer, ImageTemplateRestartCustomizer, ImageTemplateFileCustomizer, ImageTemplateWindowsUpdateCustomizer) existing_image_template = cached_get(cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) if existing_image_template.customize is None: existing_image_template.customize = [] else: for existing_customizer in existing_image_template.customize: if existing_customizer.name == customizer_name: raise CLIError("Output with output name {} already exists in image template {}." .format(customizer_name, image_template_name)) new_customizer = None if customizer_type.lower() == ScriptType.SHELL.value.lower(): # pylint:disable=no-member new_customizer = ImageTemplateShellCustomizer(name=customizer_name, script_uri=script_url, inline=inline_script) elif customizer_type.lower() == ScriptType.POWERSHELL.value.lower(): # pylint:disable=no-member new_customizer = ImageTemplatePowerShellCustomizer(name=customizer_name, script_uri=script_url, inline=inline_script, valid_exit_codes=valid_exit_codes) elif customizer_type.lower() == ScriptType.WINDOWS_RESTART.value.lower(): # pylint:disable=no-member new_customizer = ImageTemplateRestartCustomizer(name=customizer_name, restart_command=restart_command, restart_check_command=restart_check_command, restart_timeout=restart_timeout) elif customizer_type.lower() == ScriptType.FILE.value.lower(): # pylint:disable=no-member new_customizer = ImageTemplateFileCustomizer(name=customizer_name, source_uri=file_source, destination=dest_path) elif customizer_type.lower() == ScriptType.WINDOWS_UPDATE.value.lower(): new_customizer = ImageTemplateWindowsUpdateCustomizer(name=customizer_name, search_criteria=search_criteria, filters=filters, update_limit=update_limit) if not new_customizer: raise CLIError("Cannot determine customizer from type {}.".format(customizer_type)) existing_image_template.customize.append(new_customizer) return cached_put(cmd, client.virtual_machine_image_templates.create_or_update, parameters=existing_image_template, resource_group_name=resource_group_name, image_template_name=image_template_name)
def clear_template_customizer(cmd, client, resource_group_name, image_template_name): _require_defer(cmd) existing_image_template = cached_get( cmd, client.virtual_machine_image_templates.get, resource_group_name=resource_group_name, image_template_name=image_template_name) if not existing_image_template.customize: raise CLIError("No customizers to remove.") existing_image_template.customize = [] return cached_put( cmd, client.virtual_machine_image_templates.begin_create_or_update, parameters=existing_image_template, # pylint: disable=line-too-long resource_group_name=resource_group_name, image_template_name=image_template_name)
def remove_custom_rule_match_condition(cmd, resource_group_name, policy_name, rule_name, index): client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) foundRule = False for rule in policy.custom_rules.rules: if rule.name.upper() == rule_name.upper(): foundRule = True if index >= len(rule.match_conditions): from knack.util import CLIError raise CLIError("Index out of bounds") rule.match_conditions = [v for (i, v) in enumerate(rule.match_conditions) if i != index] if not foundRule: from knack.util import CLIError raise CLIError("rule '{}' not found".format(rule_name)) return cached_put(cmd, client.create_or_update, policy, resource_group_name, policy_name).result()
def list_wp_custom_rules(cmd, resource_group_name, policy_name): client = cf_waf_policies(cmd.cli_ctx, None) policy = cached_get(cmd, client.get, resource_group_name, policy_name) return policy.custom_rules.rules
def handler(args): # pylint: disable=too-many-branches,too-many-statements cmd = args.get('cmd') context_copy = copy.copy(context) context_copy.cli_ctx = cmd.cli_ctx force_string = args.get('force_string', False) ordered_arguments = args.pop('ordered_arguments', []) dest_names = child_arg_name.split('.') child_names = [args.get(key, None) for key in dest_names] for item in ['properties_to_add', 'properties_to_set', 'properties_to_remove']: if args[item]: raise CLIError("Unexpected '{}' was not empty.".format(item)) del args[item] getter, getterargs = _extract_handler_and_args(args, cmd.command_kwargs, getter_op, context_copy) if child_collection_prop_name: parent = cached_get(cmd, getter, **getterargs) instance = find_child_item( parent, *child_names, path=child_collection_prop_name, key_path=child_collection_key) else: parent = None instance = cached_get(cmd, getter, **getterargs) # pass instance to the custom_function, if provided if custom_function_op: custom_function, custom_func_args = _extract_handler_and_args( args, cmd.command_kwargs, custom_function_op, context_copy) if child_collection_prop_name: parent = custom_function(instance=instance, parent=parent, **custom_func_args) else: instance = custom_function(instance=instance, **custom_func_args) # apply generic updates after custom updates setter, setterargs = _extract_handler_and_args(args, cmd.command_kwargs, setter_op, context_copy) for arg in ordered_arguments: arg_type, arg_values = arg if arg_type == '--set': try: for expression in arg_values: set_properties(instance, expression, force_string) except ValueError: raise CLIError('invalid syntax: {}'.format(set_usage)) elif arg_type == '--add': try: add_properties(instance, arg_values, force_string) except ValueError: raise CLIError('invalid syntax: {}'.format(add_usage)) elif arg_type == '--remove': try: remove_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(remove_usage)) # Done... update the instance! setterargs[setter_arg_name] = parent if child_collection_prop_name else instance # Handle no-wait supports_no_wait = cmd.command_kwargs.get('supports_no_wait', None) if supports_no_wait: no_wait_enabled = args.get('no_wait', False) augment_no_wait_handler_args(no_wait_enabled, setter, setterargs) else: no_wait_param = cmd.command_kwargs.get('no_wait_param', None) if no_wait_param: setterargs[no_wait_param] = args[no_wait_param] if setter_arg_name == 'parameters': result = cached_put(cmd, setter, **setterargs) else: result = cached_put(cmd, setter, setterargs[setter_arg_name], **setterargs) if supports_no_wait and no_wait_enabled: return None no_wait_param = cmd.command_kwargs.get('no_wait_param', None) if no_wait_param and setterargs.get(no_wait_param, None): return None if _is_poller(result): result = result.result() if child_collection_prop_name: result = find_child_item( result, *child_names, path=child_collection_prop_name, key_path=child_collection_key) return result
def handler(self, command_args): # pylint: disable=too-many-locals, too-many-statements, too-many-branches """ Callback function of CLICommand handler """ from knack.util import CLIError from azure.cli.core.commands import cached_get, cached_put, _is_poller from azure.cli.core.util import find_child_item, augment_no_wait_handler_args from azure.cli.core.commands.arm import add_usage, remove_usage, set_usage,\ add_properties, remove_properties, set_properties self.cmd = command_args.get('cmd') force_string = command_args.get('force_string', False) ordered_arguments = command_args.pop('ordered_arguments', []) dest_names = self.child_arg_name.split('.') child_names = [command_args.get(key, None) for key in dest_names] for item in [ 'properties_to_add', 'properties_to_set', 'properties_to_remove' ]: if command_args[item]: raise CLIError("Unexpected '{}' was not empty.".format(item)) del command_args[item] getter, getterargs = self._extract_op_handler_and_args( command_args, self.getter_op_path) if self.child_collection_prop_name: parent = cached_get(self.cmd, getter, **getterargs) instance = find_child_item(parent, *child_names, path=self.child_collection_prop_name, key_path=self.child_collection_key) else: parent = None instance = cached_get(self.cmd, getter, **getterargs) # pass instance to the custom_function, if provided if self.custom_function_op_path: custom_function, custom_func_args = self._extract_op_handler_and_args( command_args, self.custom_function_op_path) if self.child_collection_prop_name: parent = custom_function(instance=instance, parent=parent, **custom_func_args) else: instance = custom_function(instance=instance, **custom_func_args) # apply generic updates after custom updates setter, setterargs = self._extract_op_handler_and_args( command_args, self.setter_op_path) for arg in ordered_arguments: arg_type, arg_values = arg if arg_type == '--set': try: for expression in arg_values: set_properties(instance, expression, force_string) except ValueError: raise CLIError('invalid syntax: {}'.format(set_usage)) elif arg_type == '--add': try: add_properties(instance, arg_values, force_string) except ValueError: raise CLIError('invalid syntax: {}'.format(add_usage)) elif arg_type == '--remove': try: remove_properties(instance, arg_values) except ValueError: raise CLIError('invalid syntax: {}'.format(remove_usage)) # Done... update the instance! setterargs[ self. setter_arg_name] = parent if self.child_collection_prop_name else instance # Handle no-wait supports_no_wait = self.cmd.command_kwargs.get('supports_no_wait', None) if supports_no_wait: no_wait_enabled = command_args.get('no_wait', False) augment_no_wait_handler_args(no_wait_enabled, setter, setterargs) else: no_wait_param = self.cmd.command_kwargs.get('no_wait_param', None) if no_wait_param: setterargs[no_wait_param] = command_args[no_wait_param] if self.setter_arg_name == 'parameters': result = cached_put(self.cmd, setter, **setterargs) else: result = cached_put(self.cmd, setter, setterargs[self.setter_arg_name], setter_arg_name=self.setter_arg_name, **setterargs) if supports_no_wait and no_wait_enabled: return None no_wait_param = self.cmd.command_kwargs.get('no_wait_param', None) if no_wait_param and setterargs.get(no_wait_param, None): return None if _is_poller(result): result = result.result() if self.child_collection_prop_name: result = find_child_item(result, *child_names, path=self.child_collection_prop_name, key_path=self.child_collection_key) return result