|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | +import time |
| 6 | + |
| 7 | +from azure.cli.command_modules.containerapp._utils import format_location |
| 8 | + |
| 9 | +from azure.cli.testsdk import CliTestError |
| 10 | +from azure.cli.testsdk.reverse_dependency import get_dummy_cli |
| 11 | +from azure.cli.testsdk.scenario_tests import SingleValueReplacer |
| 12 | +from azure.cli.testsdk.preparers import NoTrafficRecordingPreparer, ResourceGroupPreparer |
| 13 | +from .common import STAGE_LOCATION |
| 14 | + |
| 15 | +class SubnetPreparer(NoTrafficRecordingPreparer, SingleValueReplacer): |
| 16 | + def __init__(self, name_prefix='vnet', location="centralus", location_replace_stage="centralus", resource_group_parameter_name='resource_group', vnet_name=None, vnet_address_prefixes='14.0.0.0/23', subnet_address_prefixes='14.0.0.0/23', |
| 17 | + delegations=None, subnet_name="default", service_endpoints=None, skip_delete=False): |
| 18 | + super(SubnetPreparer, self).__init__(name_prefix, 15) |
| 19 | + self.cli_ctx = get_dummy_cli() |
| 20 | + self.location = location |
| 21 | + self.resource_group_parameter_name = resource_group_parameter_name |
| 22 | + self.vnet_name = vnet_name |
| 23 | + if vnet_name is None: |
| 24 | + self.vnet_name = self.create_random_name() |
| 25 | + self.vnet_address_prefixes = vnet_address_prefixes |
| 26 | + self.subnet_address_prefixes = subnet_address_prefixes |
| 27 | + self.delegations = delegations |
| 28 | + self.subnet_name = subnet_name |
| 29 | + self.service_endpoints = service_endpoints |
| 30 | + self.skip_delete = skip_delete |
| 31 | + self.location_replace_stage = location_replace_stage |
| 32 | + |
| 33 | + def create_resource(self, name, **kwargs): |
| 34 | + resource_group = self._get_resource_group(**kwargs) |
| 35 | + subnet_id = "FAKESUBNETID" |
| 36 | + location = self.location |
| 37 | + if format_location(location) == format_location(STAGE_LOCATION): |
| 38 | + location = self.location_replace_stage |
| 39 | + |
| 40 | + try: |
| 41 | + self.live_only_execute(self.cli_ctx, f"az network vnet create --address-prefixes {self.vnet_address_prefixes} -g {resource_group} -n {self.vnet_name} --subnet-name {self.subnet_name} --location {location}") |
| 42 | + subnet_command = f"az network vnet subnet update --address-prefixes {self.subnet_address_prefixes} " \ |
| 43 | + f"-n {self.subnet_name} " \ |
| 44 | + f"-g {resource_group} " \ |
| 45 | + f"--vnet-name {self.vnet_name} " |
| 46 | + if self.service_endpoints is not None: |
| 47 | + subnet_command += f'--service-endpoints {self.service_endpoints} ' |
| 48 | + |
| 49 | + if self.delegations is not None: |
| 50 | + subnet_command += f'--delegations {self.delegations} ' |
| 51 | + |
| 52 | + subnet_id = self.live_only_execute(self.cli_ctx, subnet_command).get_output_in_json()["id"] |
| 53 | + except AttributeError: # live only execute returns None if playing from record |
| 54 | + pass |
| 55 | + return {'subnet_id': subnet_id, |
| 56 | + 'vnet_name': self.vnet_name, |
| 57 | + 'subnet_name': self.subnet_name} |
| 58 | + |
| 59 | + def _get_resource_group(self, **kwargs): |
| 60 | + try: |
| 61 | + return kwargs.get(self.resource_group_parameter_name) |
| 62 | + except KeyError: |
| 63 | + template = 'Resource group is required. Please add ' \ |
| 64 | + 'decorator @{} in front of this preparer.' |
| 65 | + raise CliTestError(template.format(ResourceGroupPreparer.__name__, |
| 66 | + self.resource_group_parameter_name)) |
0 commit comments