From 20bdc1ca702a8b93a5d54e7f13aedb70624eefdf Mon Sep 17 00:00:00 2001 From: Xiaojian Xu Date: Fri, 17 Apr 2020 10:04:01 +0800 Subject: [PATCH] [Network] Enable local context feature for vnet/subnet parameter --- .../cli/command_modules/network/_params.py | 23 +- .../test_network_vnet_local_context.yaml | 573 ++++++++++++++++++ .../tests/latest/test_network_commands.py | 34 ++ 3 files changed, 626 insertions(+), 4 deletions(-) create mode 100644 src/azure-cli/azure/cli/command_modules/network/tests/latest/recordings/test_network_vnet_local_context.yaml diff --git a/src/azure-cli/azure/cli/command_modules/network/_params.py b/src/azure-cli/azure/cli/command_modules/network/_params.py index 5c5e456f439..488c013b80d 100644 --- a/src/azure-cli/azure/cli/command_modules/network/_params.py +++ b/src/azure-cli/azure/cli/command_modules/network/_params.py @@ -16,6 +16,7 @@ get_three_state_flag, get_enum_type) from azure.cli.core.commands.validators import get_default_location_from_resource_group from azure.cli.core.commands.template_create import get_folded_parameter_help_string +from azure.cli.core.local_context import LocalContextAttribute, SET, GET, ALL from azure.cli.command_modules.network._validators import ( dns_zone_name_type, validate_ssl_cert, validate_cert, validate_inbound_nat_rule_id_list, @@ -87,8 +88,10 @@ def load_arguments(self, _): nic_type = CLIArgumentType(options_list='--nic-name', metavar='NAME', help='The network interface (NIC).', id_part='name', completer=get_resource_name_completion_list('Microsoft.Network/networkInterfaces')) nsg_name_type = CLIArgumentType(options_list='--nsg-name', metavar='NAME', help='Name of the network security group.') circuit_name_type = CLIArgumentType(options_list='--circuit-name', metavar='NAME', help='ExpressRoute circuit name.', id_part='name', completer=get_resource_name_completion_list('Microsoft.Network/expressRouteCircuits')) - virtual_network_name_type = CLIArgumentType(options_list='--vnet-name', metavar='NAME', help='The virtual network (VNet) name.', completer=get_resource_name_completion_list('Microsoft.Network/virtualNetworks')) - subnet_name_type = CLIArgumentType(options_list='--subnet-name', metavar='NAME', help='The subnet name.') + virtual_network_name_type = CLIArgumentType(options_list='--vnet-name', metavar='NAME', help='The virtual network (VNet) name.', completer=get_resource_name_completion_list('Microsoft.Network/virtualNetworks'), + local_context_attribute=LocalContextAttribute(name='vnet_name', actions=[GET])) + subnet_name_type = CLIArgumentType(options_list='--subnet-name', metavar='NAME', help='The subnet name.', + local_context_attribute=LocalContextAttribute(name='subnet_name', actions=[GET])) load_balancer_name_type = CLIArgumentType(options_list='--lb-name', metavar='NAME', help='The load balancer name.', completer=get_resource_name_completion_list('Microsoft.Network/loadBalancers'), id_part='name') private_ip_address_type = CLIArgumentType(help='Static private IP address to use.', validator=validate_private_ip_address) cookie_based_affinity_type = CLIArgumentType(arg_type=get_three_state_flag(positive_label='Enabled', negative_label='Disabled', return_label=True)) @@ -1583,16 +1586,21 @@ def load_arguments(self, _): with self.argument_context('network vnet create') as c: c.argument('location', get_location_type(self.cli_ctx)) - c.argument('vnet_name', virtual_network_name_type, options_list=['--name', '-n'], completer=None) + c.argument('vnet_name', virtual_network_name_type, options_list=['--name', '-n'], completer=None, + local_context_attribute=LocalContextAttribute(name='vnet_name', actions=[SET], scopes=[ALL])) with self.argument_context('network vnet create', arg_group='Subnet') as c: - c.argument('subnet_name', help='Name of a new subnet to create within the VNet.') + c.argument('subnet_name', help='Name of a new subnet to create within the VNet.', + local_context_attribute=LocalContextAttribute(name='subnet_name', actions=[SET], scopes=[ALL])) c.argument('subnet_prefix', help='IP address prefix for the new subnet. If omitted, automatically reserves a /24 (or as large as available) block within the VNet address space.', metavar='PREFIX', max_api='2018-07-01') c.argument('subnet_prefix', options_list='--subnet-prefixes', nargs='+', min_api='2018-08-01', help='Space-separated list of address prefixes in CIDR format for the new subnet. If omitted, automatically reserves a /24 (or as large as available) block within the VNet address space.', metavar='PREFIXES') with self.argument_context('network vnet update') as c: c.argument('address_prefixes', nargs='+') + with self.argument_context('network vnet delete') as c: + c.argument('virtual_network_name', local_context_attribute=None) + with self.argument_context('network vnet peering') as c: c.argument('virtual_network_name', virtual_network_name_type) c.argument('virtual_network_peering_name', options_list=['--name', '-n'], help='The name of the VNet peering.', id_part='child_name_1') @@ -1618,6 +1626,10 @@ def load_arguments(self, _): c.argument('disable_private_endpoint_network_policies', arg_type=get_three_state_flag(), min_api='2019-04-01', help='Disable private endpoint network policies on the subnet.') c.argument('disable_private_link_service_network_policies', arg_type=get_three_state_flag(), min_api='2019-04-01', help='Disable private link service network policies on the subnet.') + with self.argument_context('network vnet subnet create') as c: + c.argument('subnet_name', arg_type=subnet_name_type, options_list=['--name', '-n'], id_part='child_name_1', + local_context_attribute=LocalContextAttribute(name='subnet_name', actions=[SET], scopes=[ALL])) + with self.argument_context('network vnet subnet update') as c: c.argument('network_security_group', validator=get_nsg_validator(), help='Name or ID of a network security group (NSG). Use empty string "" to detach it.') c.argument('route_table', help='Name or ID of a route table to associate with the subnet. Use empty string "" to detach it. You can also append "--remove routeTable" in "az network vnet subnet update" to detach it.') @@ -1627,6 +1639,9 @@ def load_arguments(self, _): c.argument('ids', deprecate_info=c.deprecate(hide=True, expiration='3.0.0')) c.argument('virtual_network_name', id_part=None) + with self.argument_context('network vnet subnet delete') as c: + c.argument('subnet_name', local_context_attribute=None) + # endregion # region VirtualNetworkGateways diff --git a/src/azure-cli/azure/cli/command_modules/network/tests/latest/recordings/test_network_vnet_local_context.yaml b/src/azure-cli/azure/cli/command_modules/network/tests/latest/recordings/test_network_vnet_local_context.yaml new file mode 100644 index 00000000000..a37643ad1e1 --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/network/tests/latest/recordings/test_network_vnet_local_context.yaml @@ -0,0 +1,573 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet create + Connection: + - keep-alive + ParameterSetName: + - -g -n --subnet-name + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-resource/8.0.1 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest.rg000001?api-version=2019-07-01 + response: + body: + string: '{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001","name":"clitest.rg000001","type":"Microsoft.Resources/resourceGroups","location":"westus","tags":{"product":"azurecli","cause":"automation","date":"2020-04-17T01:58:15Z","StorageType":"Standard_LRS","type":"test"},"properties":{"provisioningState":"Succeeded"}}' + headers: + cache-control: + - no-cache + content-length: + - '471' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:22 GMT + expires: + - '-1' + pragma: + - no-cache + strict-transport-security: + - max-age=31536000; includeSubDomains + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + status: + code: 200 + message: OK +- request: + body: '{"location": "westus", "tags": {}, "properties": {"addressSpace": {"addressPrefixes": + ["10.0.0.0/16"]}, "dhcpOptions": {}, "subnets": [{"properties": {"addressPrefix": + "10.0.0.0/24"}, "name": "subnet-000003"}]}}' + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet create + Connection: + - keep-alive + Content-Length: + - '210' + Content-Type: + - application/json; charset=utf-8 + ParameterSetName: + - -g -n --subnet-name + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: PUT + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002?api-version=2020-03-01 + response: + body: + string: "{\r\n \"name\": \"vnet-000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002\",\r\n + \ \"etag\": \"W/\\\"492dbc6f-b34a-468d-8cee-f2fef79945c9\\\"\",\r\n \"type\": + \"Microsoft.Network/virtualNetworks\",\r\n \"location\": \"westus\",\r\n + \ \"tags\": {},\r\n \"properties\": {\r\n \"provisioningState\": \"Updating\",\r\n + \ \"resourceGuid\": \"76c0d7e8-abbe-49b5-91e3-940a1984532b\",\r\n \"addressSpace\": + {\r\n \"addressPrefixes\": [\r\n \"10.0.0.0/16\"\r\n ]\r\n + \ },\r\n \"dhcpOptions\": {\r\n \"dnsServers\": []\r\n },\r\n + \ \"subnets\": [\r\n {\r\n \"name\": \"subnet-000003\",\r\n + \ \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003\",\r\n + \ \"etag\": \"W/\\\"492dbc6f-b34a-468d-8cee-f2fef79945c9\\\"\",\r\n + \ \"properties\": {\r\n \"provisioningState\": \"Updating\",\r\n + \ \"addressPrefix\": \"10.0.0.0/24\",\r\n \"delegations\": + [],\r\n \"privateEndpointNetworkPolicies\": \"Enabled\",\r\n \"privateLinkServiceNetworkPolicies\": + \"Enabled\"\r\n },\r\n \"type\": \"Microsoft.Network/virtualNetworks/subnets\"\r\n + \ }\r\n ],\r\n \"virtualNetworkPeerings\": [],\r\n \"enableDdosProtection\": + false,\r\n \"enableVmProtection\": false\r\n }\r\n}" + headers: + azure-asyncnotification: + - Enabled + azure-asyncoperation: + - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/147b6cbd-ce92-4965-9705-6dbb33cb2830?api-version=2020-03-01 + cache-control: + - no-cache + content-length: + - '1468' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:29 GMT + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - c8f8ed49-df28-44f6-9e5a-236ca290221d + x-ms-ratelimit-remaining-subscription-writes: + - '1199' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet create + Connection: + - keep-alive + ParameterSetName: + - -g -n --subnet-name + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/147b6cbd-ce92-4965-9705-6dbb33cb2830?api-version=2020-03-01 + response: + body: + string: "{\r\n \"status\": \"Succeeded\"\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '29' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:33 GMT + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 1eafe0a7-72f6-466c-b7e8-8c90e180d76d + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet create + Connection: + - keep-alive + ParameterSetName: + - -g -n --subnet-name + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002?api-version=2020-03-01 + response: + body: + string: "{\r\n \"name\": \"vnet-000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002\",\r\n + \ \"etag\": \"W/\\\"696b37f4-ff66-4ff0-8c3b-29f084c98335\\\"\",\r\n \"type\": + \"Microsoft.Network/virtualNetworks\",\r\n \"location\": \"westus\",\r\n + \ \"tags\": {},\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\",\r\n + \ \"resourceGuid\": \"76c0d7e8-abbe-49b5-91e3-940a1984532b\",\r\n \"addressSpace\": + {\r\n \"addressPrefixes\": [\r\n \"10.0.0.0/16\"\r\n ]\r\n + \ },\r\n \"dhcpOptions\": {\r\n \"dnsServers\": []\r\n },\r\n + \ \"subnets\": [\r\n {\r\n \"name\": \"subnet-000003\",\r\n + \ \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003\",\r\n + \ \"etag\": \"W/\\\"696b37f4-ff66-4ff0-8c3b-29f084c98335\\\"\",\r\n + \ \"properties\": {\r\n \"provisioningState\": \"Succeeded\",\r\n + \ \"addressPrefix\": \"10.0.0.0/24\",\r\n \"delegations\": + [],\r\n \"privateEndpointNetworkPolicies\": \"Enabled\",\r\n \"privateLinkServiceNetworkPolicies\": + \"Enabled\"\r\n },\r\n \"type\": \"Microsoft.Network/virtualNetworks/subnets\"\r\n + \ }\r\n ],\r\n \"virtualNetworkPeerings\": [],\r\n \"enableDdosProtection\": + false,\r\n \"enableVmProtection\": false\r\n }\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '1470' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:33 GMT + etag: + - W/"696b37f4-ff66-4ff0-8c3b-29f084c98335" + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 3fc04f25-1e6a-4211-9c12-ac490d55fe37 + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet show + Connection: + - keep-alive + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002?api-version=2020-03-01 + response: + body: + string: "{\r\n \"name\": \"vnet-000002\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002\",\r\n + \ \"etag\": \"W/\\\"696b37f4-ff66-4ff0-8c3b-29f084c98335\\\"\",\r\n \"type\": + \"Microsoft.Network/virtualNetworks\",\r\n \"location\": \"westus\",\r\n + \ \"tags\": {},\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\",\r\n + \ \"resourceGuid\": \"76c0d7e8-abbe-49b5-91e3-940a1984532b\",\r\n \"addressSpace\": + {\r\n \"addressPrefixes\": [\r\n \"10.0.0.0/16\"\r\n ]\r\n + \ },\r\n \"dhcpOptions\": {\r\n \"dnsServers\": []\r\n },\r\n + \ \"subnets\": [\r\n {\r\n \"name\": \"subnet-000003\",\r\n + \ \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003\",\r\n + \ \"etag\": \"W/\\\"696b37f4-ff66-4ff0-8c3b-29f084c98335\\\"\",\r\n + \ \"properties\": {\r\n \"provisioningState\": \"Succeeded\",\r\n + \ \"addressPrefix\": \"10.0.0.0/24\",\r\n \"delegations\": + [],\r\n \"privateEndpointNetworkPolicies\": \"Enabled\",\r\n \"privateLinkServiceNetworkPolicies\": + \"Enabled\"\r\n },\r\n \"type\": \"Microsoft.Network/virtualNetworks/subnets\"\r\n + \ }\r\n ],\r\n \"virtualNetworkPeerings\": [],\r\n \"enableDdosProtection\": + false,\r\n \"enableVmProtection\": false\r\n }\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '1470' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:35 GMT + etag: + - W/"696b37f4-ff66-4ff0-8c3b-29f084c98335" + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 50095a40-a4a9-4404-ae8e-e87ccc7a3642 + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet subnet show + Connection: + - keep-alive + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003?api-version=2020-03-01 + response: + body: + string: "{\r\n \"name\": \"subnet-000003\",\r\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003\",\r\n + \ \"etag\": \"W/\\\"696b37f4-ff66-4ff0-8c3b-29f084c98335\\\"\",\r\n \"properties\": + {\r\n \"provisioningState\": \"Succeeded\",\r\n \"addressPrefix\": \"10.0.0.0/24\",\r\n + \ \"delegations\": [],\r\n \"privateEndpointNetworkPolicies\": \"Enabled\",\r\n + \ \"privateLinkServiceNetworkPolicies\": \"Enabled\"\r\n },\r\n \"type\": + \"Microsoft.Network/virtualNetworks/subnets\"\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '603' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:36 GMT + etag: + - W/"696b37f4-ff66-4ff0-8c3b-29f084c98335" + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - e7445811-6cd0-4f37-b785-eacae629fb61 + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet subnet delete + Connection: + - keep-alive + Content-Length: + - '0' + ParameterSetName: + - -n + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: DELETE + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002/subnets/subnet-000003?api-version=2020-03-01 + response: + body: + string: '' + headers: + azure-asyncoperation: + - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/e0623d74-185e-4189-96f7-99fde6e33d7a?api-version=2020-03-01 + cache-control: + - no-cache + content-length: + - '0' + date: + - Fri, 17 Apr 2020 01:58:37 GMT + expires: + - '-1' + location: + - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operationResults/e0623d74-185e-4189-96f7-99fde6e33d7a?api-version=2020-03-01 + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 7796d202-dd3e-471d-9b6b-e88bda713407 + x-ms-ratelimit-remaining-subscription-deletes: + - '14999' + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet subnet delete + Connection: + - keep-alive + ParameterSetName: + - -n + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/e0623d74-185e-4189-96f7-99fde6e33d7a?api-version=2020-03-01 + response: + body: + string: "{\r\n \"status\": \"Succeeded\"\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '29' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:58:49 GMT + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 295c49eb-4253-4351-98f4-94dfa7a87172 + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet delete + Connection: + - keep-alive + Content-Length: + - '0' + ParameterSetName: + - -n + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + accept-language: + - en-US + method: DELETE + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.Network/virtualNetworks/vnet-000002?api-version=2020-03-01 + response: + body: + string: '' + headers: + azure-asyncnotification: + - Enabled + azure-asyncoperation: + - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/47148ea0-ceb5-4292-a24e-4ae188762461?api-version=2020-03-01 + cache-control: + - no-cache + content-length: + - '0' + date: + - Fri, 17 Apr 2020 01:58:50 GMT + expires: + - '-1' + location: + - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operationResults/47148ea0-ceb5-4292-a24e-4ae188762461?api-version=2020-03-01 + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - ddd01939-3c13-4dcb-8b3d-a2ea374c71e8 + x-ms-ratelimit-remaining-subscription-deletes: + - '14999' + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - network vnet delete + Connection: + - keep-alive + ParameterSetName: + - -n + User-Agent: + - python/3.8.2 (Windows-10-10.0.18362-SP0) msrest/0.6.11 msrest_azure/0.6.3 + azure-mgmt-network/10.1.0 Azure-SDK-For-Python AZURECLI/2.3.1 + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Network/locations/westus/operations/47148ea0-ceb5-4292-a24e-4ae188762461?api-version=2020-03-01 + response: + body: + string: "{\r\n \"status\": \"Succeeded\"\r\n}" + headers: + cache-control: + - no-cache + content-length: + - '29' + content-type: + - application/json; charset=utf-8 + date: + - Fri, 17 Apr 2020 01:59:01 GMT + expires: + - '-1' + pragma: + - no-cache + server: + - Microsoft-HTTPAPI/2.0 + - Microsoft-HTTPAPI/2.0 + strict-transport-security: + - max-age=31536000; includeSubDomains + transfer-encoding: + - chunked + vary: + - Accept-Encoding + x-content-type-options: + - nosniff + x-ms-arm-service-request-id: + - 7739bcc8-d699-4475-a558-7db40f309632 + status: + code: 200 + message: OK +version: 1 diff --git a/src/azure-cli/azure/cli/command_modules/network/tests/latest/test_network_commands.py b/src/azure-cli/azure/cli/command_modules/network/tests/latest/test_network_commands.py index 7182812a6d6..aabc88e8ed2 100644 --- a/src/azure-cli/azure/cli/command_modules/network/tests/latest/test_network_commands.py +++ b/src/azure-cli/azure/cli/command_modules/network/tests/latest/test_network_commands.py @@ -7,6 +7,7 @@ # pylint: disable=too-many-lines import os import unittest +import tempfile from azure_devtools.scenario_tests import AllowLargeResponse from azure.cli.core.commands.client_factory import get_subscription_id @@ -3490,5 +3491,38 @@ def test_network_batsion_host_create(self, resource_group): self.cmd('network bastion delete -g {rg} -n {bastion}') +class NetworkVnetLocalContextScenarioTest(ScenarioTest): + + @ResourceGroupPreparer() + def test_network_vnet_local_context(self): + self.kwargs.update({ + 'vnet': self.create_random_name(prefix='vnet-', length=12), + 'subnet': self.create_random_name(prefix='subnet-', length=12) + }) + original_working_dir = os.getcwd() + working_dir = tempfile.mkdtemp() + os.chdir(working_dir) + self.cmd('local-context on') + + self.cmd('network vnet create -g {rg} -n {vnet} --subnet-name {subnet}', + checks=[self.check('newVNet.name', self.kwargs['vnet'])]) + self.cmd('network vnet show', checks=[ + self.check('name', self.kwargs['vnet']) + ]) + self.cmd('network vnet subnet show', checks=[ + self.check('name', self.kwargs['subnet']) + ]) + with self.assertRaises(CLIError): + self.cmd('network vnet delete') + with self.assertRaises(CLIError): + self.cmd('network vnet subnet delete') + + self.cmd('network vnet subnet delete -n {subnet}') + self.cmd('network vnet delete -n {vnet}') + + self.cmd('local-context off --yes') + os.chdir(original_working_dir) + + if __name__ == '__main__': unittest.main()