diff --git a/src/azure-cli/azure/cli/command_modules/batch/_help.py b/src/azure-cli/azure/cli/command_modules/batch/_help.py index 5916f11b328..ff5545f306a 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/_help.py +++ b/src/azure-cli/azure/cli/command_modules/batch/_help.py @@ -158,6 +158,7 @@ helps['batch account network-profile set'] = """ type: command short-summary: Set the Network profile for Batch account. +long-summary: The network profile only takes effect when public network access is enabled, so this command also sets public network access to Enabled. examples: - name: Set the BatchAccount network-profile to the Allow text: > @@ -181,6 +182,7 @@ helps['batch account network-profile network-rule add'] = """ type: command short-summary: Add a Network rule from a Network Profile. +long-summary: The network profile only takes effect when public network access is enabled, so this command also sets public network access to Enabled. examples: - name: Add ip address to BatchAccount network rule text: > @@ -190,6 +192,7 @@ helps['batch account network-profile network-rule delete'] = """ type: command short-summary: Delete a Network rule from a Network Profile. +long-summary: The network profile only takes effect when public network access is enabled, so this command also sets public network access to Enabled. examples: - name: Delete ip address from BatchAccount network rule text: > diff --git a/src/azure-cli/azure/cli/command_modules/batch/custom.py b/src/azure-cli/azure/cli/command_modules/batch/custom.py index 792aaccd751..f16072135ac 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/custom.py +++ b/src/azure-cli/azure/cli/command_modules/batch/custom.py @@ -152,7 +152,8 @@ def assign_batch_identity(cmd, client, resource_group_name, account_name, mi_sys my_dict = {mi_user_assigned: useridentity} identity = BatchAccountIdentity(type=ResourceIdentityType.USER_ASSIGNED, user_assigned_identities=my_dict) - parameters = BatchAccountUpdateParameters(identity=identity) + # Leave public network access as it is: azure-mgmt-batch sends 'Enabled' when it isn't set + parameters = BatchAccountUpdateParameters(identity=identity, public_network_access=None) client.update(resource_group_name=resource_group_name, account_name=account_name, parameters=parameters) @@ -180,7 +181,8 @@ def remove_batch_identity(cmd, client, resource_group_name, account_name, mi_sys if batch_account.identity.type == ResourceIdentityType.USER_ASSIGNED else ResourceIdentityType.SYSTEM_ASSIGNED) - parameters = BatchAccountUpdateParameters(identity=batch_account.identity) + # Leave public network access as it is: azure-mgmt-batch sends 'Enabled' when it isn't set + parameters = BatchAccountUpdateParameters(identity=batch_account.identity, public_network_access=None) client.update(resource_group_name=resource_group_name, account_name=account_name, parameters=parameters) @@ -270,7 +272,9 @@ def update_network_profile(cmd, client, resource_group_name=None, account_name=N networkprofile.node_management_access.default_action = default_action - parameters = BatchAccountUpdateParameters(network_profile=networkprofile) + # The network profile only takes effect when public network access is enabled + parameters = BatchAccountUpdateParameters(public_network_access=PublicNetworkAccessType.ENABLED, + network_profile=networkprofile) return client.update(resource_group_name=resource_group_name, account_name=account_name, @@ -307,7 +311,7 @@ def add_network_rule(cmd, client, resource_group_name=None, account_name=None, p networkprofile.node_management_access.ip_rules = [] networkprofile.node_management_access.ip_rules.append(IPRule(value=ip_address)) - # Not sure if i want to enable public_network_access as part of this + # The network profile only takes effect when public network access is enabled parameters = BatchAccountUpdateParameters(public_network_access=PublicNetworkAccessType.ENABLED, network_profile=networkprofile) @@ -347,7 +351,7 @@ def delete_network_rule(cmd, client, resource_group_name=None, account_name=None if iprule.value == ip_address: networkprofile.node_management_access.ip_rules.remove(iprule) - # Not sure if i want to enable public_network_access as part of this + # The network profile only takes effect when public network access is enabled parameters = BatchAccountUpdateParameters(public_network_access=PublicNetworkAccessType.ENABLED, network_profile=networkprofile) diff --git a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_mgmt_commands.py b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_mgmt_commands.py index c4e85dd1b8d..ea0f35ed92e 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_mgmt_commands.py +++ b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_mgmt_commands.py @@ -3,8 +3,16 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- +import io import tempfile import time +import unittest +from unittest import mock + +from azure.mgmt.batch.models import BatchAccount, BatchAccountIdentity +from azure.cli.core.mock import DummyCli +from azure.cli.core.util import rmtree_with_retry +from azure.cli.command_modules.batch import _client_factory from azure.cli.testsdk import ( ScenarioTest, ResourceGroupPreparer, StorageAccountPreparer) from azure.cli.testsdk.scenario_tests import AllowLargeResponse @@ -410,3 +418,56 @@ def test_batch_byos_account_cmd(self, resource_group): # test batch account delete self.cmd('batch account delete -g {rg} -n {byos_n} --yes') #self.cmd('batch account list -g {rg}').assert_with_checks(self.is_empty()) + +class TestBatchAccountPublicNetworkAccess(unittest.TestCase): + """Run the commands that update a Batch account with its management client mocked, and check whether the + update they send sets public network access.""" + + USER_IDENTITY = ('/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/' + 'Microsoft.ManagedIdentity/userAssignedIdentities/identity1') + + def setUp(self): + self.cli = DummyCli(random_config_dir=True) + self.addCleanup(self.cli.env_patch.stop) + self.addCleanup(rmtree_with_retry, self.cli.config.config_dir) + self.client = mock.Mock() + self.client.get.side_effect = self._account + self.client.update.side_effect = self._account + factory_patch = mock.patch.object(_client_factory, 'mgmt_batch_account_client_factory', + return_value=self.client) + factory_patch.start() + self.addCleanup(factory_patch.stop) + + @staticmethod + def _account(*_, **__): + """An account with public network access disabled, new for each request""" + return BatchAccount(location='eastus', public_network_access='Disabled', + identity=BatchAccountIdentity(type='SystemAssigned')) + + def _update_sent(self, command): + """Run the command and return the body of the account update it sends.""" + self.client.update.reset_mock() + self.assertEqual(self.cli.invoke(command.split(), out_file=io.StringIO()), 0) + self.client.update.assert_called_once() + return self.client.update.call_args.kwargs['parameters'].serialize() + + def test_batch_account_identity_keeps_public_network_access(self): + for command in ['batch account identity assign -g rg -n acc --system-assigned', + f'batch account identity assign -g rg -n acc --user-assigned {self.USER_IDENTITY}', + 'batch account identity remove -g rg -n acc --system-assigned --yes']: + with self.subTest(command=command): + body = self._update_sent(command) + self.assertIn('identity', body) + self.assertNotIn('publicNetworkAccess', body.get('properties', {})) + + def test_batch_account_network_profile_enables_public_network_access(self): + # The network profile only takes effect when public network access is enabled + for command in ['batch account network-profile set -g rg -n acc --profile BatchAccount --default-action Deny', + 'batch account network-profile network-rule add -g rg -n acc --profile BatchAccount ' + '--ip-address 1.2.3.4', + 'batch account network-profile network-rule delete -g rg -n acc --profile BatchAccount ' + '--ip-address 1.2.3.4 --yes']: + with self.subTest(command=command): + body = self._update_sent(command) + self.assertIn('networkProfile', body['properties']) + self.assertEqual(body['properties']['publicNetworkAccess'], 'Enabled')