From fb06f1d25f74954063f901bc9322cc986f06f804 Mon Sep 17 00:00:00 2001 From: Fayaz Shaik Date: Thu, 14 Jul 2022 19:39:13 +0530 Subject: [PATCH 1/3] Added support for ALIAS record for dns zone export --- .../azure/cli/command_modules/network/custom.py | 8 +++++++- .../network/zone_file/parse_zone_file.py | 5 +++-- .../network/zone_file/record_processors.py | 10 +++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/network/custom.py b/src/azure-cli/azure/cli/command_modules/network/custom.py index 3d569a5077d..b294e88f814 100644 --- a/src/azure-cli/azure/cli/command_modules/network/custom.py +++ b/src/azure-cli/azure/cli/command_modules/network/custom.py @@ -2474,7 +2474,13 @@ def export_zone(cmd, resource_group_name, zone_name, file_name=None): # pylint: if record_type not in zone_obj[record_set_name]: zone_obj[record_set_name][record_type] = [] - if record_type == 'aaaa' or record_type == 'a': + # Checking for alias record + if (record_type == 'a' or record_type == 'aaaa' or record_type == 'cname') and record_set.target_resource.id: + target_resource_id = record_set.target_resource.id + record_obj.update({'target-resource-id': record_type.upper() + " " + target_resource_id}) + record_type = 'alias' + zone_obj[record_set_name][record_type] = [] + elif record_type == 'aaaa' or record_type == 'a': record_obj.update({'ip': ''}) elif record_type == 'cname': record_obj.update({'alias': ''}) diff --git a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py index 07fb76e08aa..ab46ed1da64 100644 --- a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py +++ b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py @@ -71,7 +71,8 @@ 'ptr': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pin)\s+)?(?Pptr)\s+(?P[\w\.-]+)', 'srv': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pin)\s+)?(?Psrv)\s+(?P\d+)\s+(?P\d+)\s+(?P\d+)\s+(?P[@\w\.-]+)', 'spf': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pin)\s+)?(?Pspf)\s+(?P.+)', - 'uri': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pin)\s+)?(?Puri)\s+(?P\d+)\s+(?P\d+)\s+(?P[\w\.]+)' + 'uri': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pin)\s+)?(?Puri)\s+(?P\d+)\s+(?P\d+)\s+(?P[\w\.]+)', + 'alias': r'(?P[@\*\w\.-]*)\s+(?:(?P\d+\w*)\s+)?(?:(?Pazure)\s+)?(?Palias)\s+(?Paaaa|a|cname)\s+(?P[a-zA-Z0-9/._-]*)', } _COMPILED_REGEX = {k: re.compile(v, re.IGNORECASE) for k, v in _REGEX.items()} @@ -435,7 +436,7 @@ def parse_zone_file(text, zone_name, ignore_invalid=False): if not parse_match and not ignore_invalid: raise InvalidArgumentValueError('Unable to parse: {}'.format(record_line)) - + record_type = record['delim'].lower() if record_type == '$origin': origin_value = record['val'] diff --git a/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py b/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py index d9896a6b83f..e4776ef1905 100644 --- a/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py +++ b/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py @@ -83,8 +83,12 @@ def process_rr(io, data, record_type, record_keys, name, print_name): elif not isinstance(record_keys, list): raise ValueError('record_keys must be a string or list of strings') + in_or_azure = "IN" + if record_type == 'ALIAS': + in_or_azure = "AZURE" + name_display = name if print_name else ' ' * len(name) - print('{} {} IN {} '.format(name_display, data['ttl'], record_type), end='', file=io) + print('{} {} {} {} '.format(name_display, data['ttl'], in_or_azure, record_type), end='', file=io) for i, key in enumerate(record_keys): print(data[key], end='\n' if i == len(record_keys) - 1 else ' ', file=io) @@ -124,3 +128,7 @@ def process_txt(io, data, name, print_name=False): def process_srv(io, data, name, print_name=False): return process_rr(io, data, 'SRV', ['priority', 'weight', 'port', 'target'], name, print_name) + + +def process_alias(io, data, name, print_name=False): + return process_rr(io, data, 'ALIAS', 'target-resource-id', name, print_name) \ No newline at end of file From 5feaaa2ddf6d73d4f8277cf0ca8eb3f22106410a Mon Sep 17 00:00:00 2001 From: fayazshaik7 <34137825+fayazshaik7@users.noreply.github.com> Date: Thu, 14 Jul 2022 23:26:51 +0530 Subject: [PATCH 2/3] Update src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py Co-authored-by: necusjz --- .../cli/command_modules/network/zone_file/parse_zone_file.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py index ab46ed1da64..4d99f0cd736 100644 --- a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py +++ b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py @@ -436,7 +436,6 @@ def parse_zone_file(text, zone_name, ignore_invalid=False): if not parse_match and not ignore_invalid: raise InvalidArgumentValueError('Unable to parse: {}'.format(record_line)) - record_type = record['delim'].lower() if record_type == '$origin': origin_value = record['val'] From f2784c3ebb98e90177e904339f50efda6b29e43f Mon Sep 17 00:00:00 2001 From: Fayaz Shaik Date: Thu, 14 Jul 2022 23:33:53 +0530 Subject: [PATCH 3/3] resolved comments --- .../cli/command_modules/network/zone_file/parse_zone_file.py | 1 + .../cli/command_modules/network/zone_file/record_processors.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py index 4d99f0cd736..77c17bb2c5b 100644 --- a/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py +++ b/src/azure-cli/azure/cli/command_modules/network/zone_file/parse_zone_file.py @@ -436,6 +436,7 @@ def parse_zone_file(text, zone_name, ignore_invalid=False): if not parse_match and not ignore_invalid: raise InvalidArgumentValueError('Unable to parse: {}'.format(record_line)) + record_type = record['delim'].lower() if record_type == '$origin': origin_value = record['val'] diff --git a/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py b/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py index e4776ef1905..0c3de6eba9e 100644 --- a/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py +++ b/src/azure-cli/azure/cli/command_modules/network/zone_file/record_processors.py @@ -131,4 +131,4 @@ def process_srv(io, data, name, print_name=False): def process_alias(io, data, name, print_name=False): - return process_rr(io, data, 'ALIAS', 'target-resource-id', name, print_name) \ No newline at end of file + return process_rr(io, data, 'ALIAS', 'target-resource-id', name, print_name)