diff --git a/src/azure-cli-core/azure/cli/core/profiles/_shared.py b/src/azure-cli-core/azure/cli/core/profiles/_shared.py index 46085785326..d5e3fd1badf 100644 --- a/src/azure-cli-core/azure/cli/core/profiles/_shared.py +++ b/src/azure-cli-core/azure/cli/core/profiles/_shared.py @@ -60,12 +60,12 @@ class ResourceType(Enum): # pylint: disable=too-few-public-methods MGMT_ARO = ('azure.mgmt.redhatopenshift', 'AzureRedHatOpenShiftClient') MGMT_DATABOXEDGE = ('azure.mgmt.databoxedge', 'DataBoxEdgeManagementClient') MGMT_CUSTOMLOCATION = ('azure.mgmt.extendedlocation', 'CustomLocations') + MGMT_CONTAINERSERVICE = ('azure.mgmt.containerservice', 'ContainerServiceClient') # the "None" below will stay till a command module fills in the type so "get_mgmt_service_client" # can be provided with "ResourceType.XXX" to initialize the client object. This usually happens # when related commands start to support Multi-API DATA_COSMOS_TABLE = ('azure.multiapi.cosmosdb', None) - MGMT_CONTAINERSERVICE = ('azure.mgmt.containerservice', None) MGMT_ADVISOR = ('azure.mgmt.advisor', None) MGMT_MEDIA = ('azure.mgmt.media', None) MGMT_BACKUP = ('azure.mgmt.recoveryservicesbackup', None) @@ -212,7 +212,11 @@ def default_api_version(self): ResourceType.MGMT_IOTHUB: '2021-03-31', ResourceType.MGMT_ARO: '2020-04-30', ResourceType.MGMT_DATABOXEDGE: '2019-08-01', - ResourceType.MGMT_CUSTOMLOCATION: '2021-03-15-preview' + ResourceType.MGMT_CUSTOMLOCATION: '2021-03-15-preview', + ResourceType.MGMT_CONTAINERSERVICE: SDKProfile('2021-03-01', { + 'container_services': '2017-07-01', + 'open_shift_managed_clusters': '2019-10-27-preview' + }) }, '2020-09-01-hybrid': { ResourceType.MGMT_STORAGE: '2019-06-01', @@ -252,7 +256,12 @@ def default_api_version(self): ResourceType.MGMT_APPSERVICE: '2018-02-01', ResourceType.MGMT_EVENTHUB: '2018-01-01-preview', ResourceType.MGMT_IOTHUB: '2019-07-01-preview', - ResourceType.MGMT_DATABOXEDGE: '2019-08-01' + ResourceType.MGMT_DATABOXEDGE: '2019-08-01', + ResourceType.MGMT_CONTAINERREGISTRY: '2019-05-01', + ResourceType.MGMT_CONTAINERSERVICE: SDKProfile('2020-11-01', { + 'container_services': '2017-07-01', + 'open_shift_managed_clusters': '2019-10-27-preview' + }) }, '2019-03-01-hybrid': { ResourceType.MGMT_STORAGE: '2017-10-01', diff --git a/src/azure-cli/azure/cli/command_modules/acs/_client_factory.py b/src/azure-cli/azure/cli/command_modules/acs/_client_factory.py index be65ecde7a4..fde7325d37c 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_client_factory.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_client_factory.py @@ -59,9 +59,7 @@ def get_auth_management_client(cli_ctx, scope=None, **_): def get_container_service_client(cli_ctx, **_): - from azure.mgmt.containerservice import ContainerServiceClient - - return get_mgmt_service_client(cli_ctx, ContainerServiceClient) + return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_CONTAINERSERVICE) def get_osa_container_service_client(cli_ctx, **_): diff --git a/src/azure-cli/azure/cli/command_modules/acs/_helpers.py b/src/azure-cli/azure/cli/command_modules/acs/_helpers.py index 2bac1eacbad..4ad75defd71 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_helpers.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_helpers.py @@ -5,13 +5,18 @@ from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error # pylint: disable=no-name-in-module,import-error -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterAPIServerAccessProfile from knack.util import CLIError +from azure.cli.core.profiles import ResourceType from ._consts import CONST_OUTBOUND_TYPE_LOAD_BALANCER, CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING -def _populate_api_server_access_profile(api_server_authorized_ip_ranges, enable_private_cluster=False, instance=None): +def _populate_api_server_access_profile(cmd, + api_server_authorized_ip_ranges, + enable_private_cluster=False, instance=None): if instance is None or instance.api_server_access_profile is None: + ManagedClusterAPIServerAccessProfile = cmd.get_models('ManagedClusterAPIServerAccessProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') profile = ManagedClusterAPIServerAccessProfile() else: profile = instance.api_server_access_profile diff --git a/src/azure-cli/azure/cli/command_modules/acs/_loadbalancer.py b/src/azure-cli/azure/cli/command_modules/acs/_loadbalancer.py index 1fa5059531a..15bfd9c4d10 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_loadbalancer.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_loadbalancer.py @@ -6,11 +6,7 @@ from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error # pylint: disable=no-name-in-module,import-error -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfile -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileManagedOutboundIPs -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileOutboundIPPrefixes -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileOutboundIPs -from azure.mgmt.containerservice.v2021_03_01.models import ResourceReference +from azure.cli.core.profiles import ResourceType from knack.log import get_logger @@ -28,37 +24,40 @@ def set_load_balancer_sku(sku, kubernetes_version): return "standard" -def update_load_balancer_profile(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, +def update_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout, profile): """parse and update an existing load balancer profile""" if not is_load_balancer_profile_provided(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout): return profile - return configure_load_balancer_profile(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, + return configure_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout, profile) -def create_load_balancer_profile(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, +def create_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout): """parse and build load balancer profile""" if not is_load_balancer_profile_provided(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout): return None + ManagedClusterLoadBalancerProfile = cmd.get_models('ManagedClusterLoadBalancerProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') profile = ManagedClusterLoadBalancerProfile() - return configure_load_balancer_profile(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, + return configure_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout, profile) -def configure_load_balancer_profile(managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, +def configure_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout, profile): """configure a load balancer with customer supplied values""" if not profile: return profile - outbound_ip_resources = _get_load_balancer_outbound_ips(outbound_ips) + outbound_ip_resources = _get_load_balancer_outbound_ips(cmd, outbound_ips) outbound_ip_prefix_resources = _get_load_balancer_outbound_ip_prefixes( - outbound_ip_prefixes) + cmd, outbound_ip_prefixes) if managed_outbound_ip_count or outbound_ip_resources or outbound_ip_prefix_resources: # ips -> i_ps due to track 2 naming issue @@ -68,16 +67,28 @@ def configure_load_balancer_profile(managed_outbound_ip_count, outbound_ips, out profile.outbound_ip_prefixes = None if managed_outbound_ip_count: # ips -> i_ps due to track 2 naming issue + ManagedClusterLoadBalancerProfileManagedOutboundIPs = cmd.get_models( + 'ManagedClusterLoadBalancerProfileManagedOutboundIPs', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') profile.managed_outbound_i_ps = ManagedClusterLoadBalancerProfileManagedOutboundIPs( count=managed_outbound_ip_count ) if outbound_ip_resources: # ips -> i_ps due to track 2 naming issue + ManagedClusterLoadBalancerProfileOutboundIPs = cmd.get_models( + 'ManagedClusterLoadBalancerProfileOutboundIPs', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') profile.outbound_i_ps = ManagedClusterLoadBalancerProfileOutboundIPs( # ips -> i_ps due to track 2 naming issue public_i_ps=outbound_ip_resources ) if outbound_ip_prefix_resources: + ManagedClusterLoadBalancerProfileOutboundIPPrefixes = cmd.get_models( + 'ManagedClusterLoadBalancerProfileOutboundIPPrefixes', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') profile.outbound_ip_prefixes = ManagedClusterLoadBalancerProfileOutboundIPPrefixes( public_ip_prefixes=outbound_ip_prefix_resources ) @@ -97,9 +108,11 @@ def is_load_balancer_profile_provided(managed_outbound_ip_count, outbound_ips, i idle_timeout]) -def _get_load_balancer_outbound_ips(load_balancer_outbound_ips): +def _get_load_balancer_outbound_ips(cmd, load_balancer_outbound_ips): """parse load balancer profile outbound IP ids and return an array of references to the outbound IP resources""" load_balancer_outbound_ip_resources = None + ResourceReference = cmd.get_models('ResourceReference', resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') if load_balancer_outbound_ips: load_balancer_outbound_ip_resources = \ [ResourceReference(id=x.strip()) @@ -107,10 +120,12 @@ def _get_load_balancer_outbound_ips(load_balancer_outbound_ips): return load_balancer_outbound_ip_resources -def _get_load_balancer_outbound_ip_prefixes(load_balancer_outbound_ip_prefixes): +def _get_load_balancer_outbound_ip_prefixes(cmd, load_balancer_outbound_ip_prefixes): """parse load balancer profile outbound IP prefix ids and return an array \ of references to the outbound IP prefix resources""" load_balancer_outbound_ip_prefix_resources = None + ResourceReference = cmd.get_models('ResourceReference', resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') if load_balancer_outbound_ip_prefixes: load_balancer_outbound_ip_prefix_resources = \ [ResourceReference(id=x.strip()) diff --git a/src/azure-cli/azure/cli/command_modules/acs/_params.py b/src/azure-cli/azure/cli/command_modules/acs/_params.py index 5d43905d975..a8149fe83a7 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_params.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_params.py @@ -12,6 +12,7 @@ from azure.cli.core.commands.parameters import ( file_type, get_enum_type, get_resource_name_completion_list, name_type, tags_type, zones_type) from azure.cli.core.commands.validators import validate_file_or_dict +from azure.cli.core.profiles import ResourceType from knack.arguments import CLIArgumentType from ._completers import ( @@ -157,7 +158,7 @@ def load_arguments(self, _): completer=FilesCompleter(), help='Path to an SSH key file to use.') # AKS command argument configuration - with self.argument_context('aks') as c: + with self.argument_context('aks', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('resource_name', name_type, help='Name of the managed cluster.', completer=get_resource_name_completion_list('Microsoft.ContainerService/ManagedClusters')) c.argument('name', name_type, help='Name of the managed cluster.', @@ -167,7 +168,7 @@ def load_arguments(self, _): c.argument('node_count', options_list=['--node-count', '-c'], type=int) c.argument('tags', tags_type) - with self.argument_context('aks create') as c: + with self.argument_context('aks create', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('name', validator=validate_linux_host_name) c.argument('kubernetes_version', completer=get_k8s_versions_completion_list) @@ -265,7 +266,7 @@ def load_arguments(self, _): '--yes', '-y'], help='Do not prompt for confirmation.', action='store_true') c.argument('enable_sgxquotehelper', action='store_true') - with self.argument_context('aks update') as c: + with self.argument_context('aks update', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('attach_acr', acr_arg_type, validator=validate_acr) c.argument('detach_acr', acr_arg_type, validator=validate_acr) @@ -303,10 +304,10 @@ def load_arguments(self, _): c.argument('yes', options_list=[ '--yes', '-y'], help='Do not prompt for confirmation.', action='store_true') - with self.argument_context('aks disable-addons') as c: + with self.argument_context('aks disable-addons', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('addons', options_list=['--addons', '-a']) - with self.argument_context('aks enable-addons') as c: + with self.argument_context('aks enable-addons', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('addons', options_list=['--addons', '-a']) c.argument('subnet_name', options_list=[ '--subnet-name', '-s'], help='Name of an existing subnet to use with the virtual-node add-on.') @@ -322,7 +323,7 @@ def load_arguments(self, _): '--appgw-watch-namespace'], arg_group='Application Gateway') c.argument('enable_sgxquotehelper', action='store_true') - with self.argument_context('aks get-credentials') as c: + with self.argument_context('aks get-credentials', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('admin', options_list=['--admin', '-a'], default=False) c.argument('context_name', options_list=['--context'], help='If specified, overwrite the default context name.') @@ -356,17 +357,17 @@ def load_arguments(self, _): c.argument('aad_server_app_secret') c.argument('aad_tenant_id') - with self.argument_context('aks upgrade') as c: + with self.argument_context('aks upgrade', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('kubernetes_version', completer=get_k8s_upgrades_completion_list) c.argument('yes', options_list=[ '--yes', '-y'], help='Do not prompt for confirmation.', action='store_true') - with self.argument_context('aks scale') as c: + with self.argument_context('aks scale', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('nodepool_name', type=str, help='Node pool name, up to 12 alphanumeric characters', validator=validate_nodepool_name) - with self.argument_context('aks nodepool') as c: + with self.argument_context('aks nodepool', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c: c.argument('cluster_name', type=str, help='The cluster name.') for scope in ['aks nodepool add']: @@ -405,7 +406,7 @@ def load_arguments(self, _): c.argument('nodepool_name', type=str, options_list=[ '--name', '-n'], validator=validate_nodepool_name, help='The node pool name.') - with self.argument_context('aks nodepool update') as c: + with self.argument_context('aks nodepool update', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='agent_pools') as c: c.argument('enable_cluster_autoscaler', options_list=[ "--enable-cluster-autoscaler", "-e"], action='store_true') c.argument('disable_cluster_autoscaler', options_list=[ @@ -439,7 +440,7 @@ def load_arguments(self, _): '--yes', '-y'], action='store_true', help='Do not prompt for confirmation') # OpenShift command argument configuration - with self.argument_context('openshift') as c: + with self.argument_context('openshift', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='open_shift_managed_clusters') as c: c.argument('resource_name', name_type, help='Name of the managed OpenShift cluster.', completer=get_resource_name_completion_list('Microsoft.ContainerService/OpenShiftManagedClusters')) c.argument('name', name_type, help='Name of the managed OpenShift cluster.', @@ -448,14 +449,14 @@ def load_arguments(self, _): '--compute-count', '-c'], type=int, default=4) c.argument('tags', tags_type) - with self.argument_context('openshift create') as c: + with self.argument_context('openshift create', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='open_shift_managed_clusters') as c: c.argument('name', validator=validate_linux_host_name) c.argument('compute_vm_size', options_list=['--compute-vm-size', '-s']) c.argument('customer_admin_group_id', options_list=[ '--customer-admin-group-id']) c.argument('workspace_id') - with self.argument_context('openshift monitor enable') as c: + with self.argument_context('openshift monitor enable', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='open_shift_managed_clusters') as c: c.argument( 'workspace_id', help='The resource ID of an existing Log Analytics Workspace to use for storing monitoring data.') diff --git a/src/azure-cli/azure/cli/command_modules/acs/_validators.py b/src/azure-cli/azure/cli/command_modules/acs/_validators.py index a650e3ca351..7bca9a11186 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/acs/_validators.py @@ -13,12 +13,13 @@ # pylint: disable=no-name-in-module,import-error from knack.log import get_logger +from azure.cli.core.profiles import ResourceType + from azure.cli.core.commands.validators import validate_tag from azure.cli.core.util import CLIError from azure.cli.core.azclierror import InvalidArgumentValueError import azure.cli.core.keys as keys -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterPropertiesAutoScalerProfile logger = get_logger(__name__) @@ -107,7 +108,7 @@ def validate_k8s_version(namespace): 'such as "1.11.8" or "1.12.6"') -def validate_cluster_autoscaler_profile(namespace): +def validate_cluster_autoscaler_profile(cmd, namespace): """ Validates that cluster autoscaler profile is acceptable by: 1. Extracting the key[=value] format to map 2. Validating that the key isn't empty and that the key is valid @@ -116,12 +117,15 @@ def validate_cluster_autoscaler_profile(namespace): _extract_cluster_autoscaler_params(namespace) if namespace.cluster_autoscaler_profile is not None: for key in namespace.cluster_autoscaler_profile.keys(): - _validate_cluster_autoscaler_key(key) + _validate_cluster_autoscaler_key(cmd, key) -def _validate_cluster_autoscaler_key(key): +def _validate_cluster_autoscaler_key(cmd, key): if not key: raise CLIError('Empty key specified for cluster-autoscaler-profile') + ManagedClusterPropertiesAutoScalerProfile = cmd.get_models('ManagedClusterPropertiesAutoScalerProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') valid_keys = list(k.replace("_", "-") for k, v in ManagedClusterPropertiesAutoScalerProfile._attribute_map.items()) # pylint: disable=protected-access if key not in valid_keys: raise CLIError("'{0}' is an invalid key for cluster-autoscaler-profile. " diff --git a/src/azure-cli/azure/cli/command_modules/acs/commands.py b/src/azure-cli/azure/cli/command_modules/acs/commands.py index 7998ee57ca5..13b0fde6010 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/commands.py +++ b/src/azure-cli/azure/cli/command_modules/acs/commands.py @@ -6,6 +6,7 @@ # pylint: disable=no-name-in-module,import-error from azure.cli.core.commands import CliCommandType from azure.cli.core.commands.arm import deployment_validate_table_format +from azure.cli.core.profiles import ResourceType from ._client_factory import cf_container_services from ._client_factory import cf_managed_clusters @@ -25,26 +26,33 @@ def load_command_table(self, _): container_services_sdk = CliCommandType( - operations_tmpl='azure.mgmt.containerservice.v2017_07_01.operations.' + operations_tmpl='azure.mgmt.containerservice.operations.' '_container_services_operations#ContainerServicesOperations.{}', + operation_group='container_services', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, client_factory=cf_container_services ) managed_clusters_sdk = CliCommandType( - operations_tmpl='azure.mgmt.containerservice.v2021_03_01.operations.' + operations_tmpl='azure.mgmt.containerservice.operations.' '_managed_clusters_operations#ManagedClustersOperations.{}', + operation_group='managed_clusters', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, client_factory=cf_managed_clusters ) agent_pools_sdk = CliCommandType( operations_tmpl='azext_aks_preview.vendored_sdks.azure_mgmt_preview_aks.' 'operations._agent_pools_operations#AgentPoolsOperations.{}', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, client_factory=cf_managed_clusters ) openshift_managed_clusters_sdk = CliCommandType( - operations_tmpl='azure.mgmt.containerservice.v2018_09_30_preview.operations.' + operations_tmpl='azure.mgmt.containerservice.operations.' '_open_shift_managed_clusters_operations#OpenShiftManagedClustersOperations.{}', + operation_group='open_shift_managed_clusters', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, client_factory=cf_openshift_managed_clusters ) @@ -71,13 +79,15 @@ def load_command_table(self, _): client_factory=None) # ACS Kubernetes commands - with self.command_group('acs kubernetes', container_services_sdk, client_factory=cf_container_services) as g: + with self.command_group('acs kubernetes', container_services_sdk, + client_factory=cf_container_services) as g: g.custom_command('browse', 'k8s_browse') g.custom_command('get-credentials', 'k8s_get_credentials') g.custom_command('install-cli', 'k8s_install_cli', client_factory=None) # AKS commands - with self.command_group('aks', managed_clusters_sdk, client_factory=cf_managed_clusters) as g: + with self.command_group('aks', managed_clusters_sdk, + client_factory=cf_managed_clusters) as g: g.custom_command('browse', 'aks_browse') g.custom_command('create', 'aks_create', supports_no_wait=True) g.custom_command('update', 'aks_update', supports_no_wait=True) @@ -107,15 +117,17 @@ def load_command_table(self, _): confirmation='Kubernetes will be unavailable during certificate rotation process.\n' + 'Are you sure you want to perform this operation?') g.wait_command('wait') - g.command('stop', 'begin_stop', supports_no_wait=True) - g.command('start', 'begin_start', supports_no_wait=True) + g.command('stop', 'begin_stop', supports_no_wait=True, min_api='2020-09-01') + g.command('start', 'begin_start', supports_no_wait=True, min_api='2020-09-01') with self.command_group('aks', container_services_sdk, client_factory=cf_container_services) as g: g.custom_command('get-versions', 'aks_get_versions', table_transformer=aks_versions_table_format) # AKS agent pool commands - with self.command_group('aks nodepool', agent_pools_sdk, client_factory=cf_agent_pools) as g: + with self.command_group('aks nodepool', + agent_pools_sdk, + client_factory=cf_agent_pools) as g: g.custom_command('list', 'aks_agentpool_list', table_transformer=aks_agentpool_list_table_format) g.custom_show_command('show', 'aks_agentpool_show', diff --git a/src/azure-cli/azure/cli/command_modules/acs/custom.py b/src/azure-cli/azure/cli/command_modules/acs/custom.py index bb8a35e0795..cdaf10746ad 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/custom.py +++ b/src/azure-cli/azure/cli/command_modules/acs/custom.py @@ -53,6 +53,7 @@ MutuallyExclusiveArgumentError, ValidationError) from azure.cli.core._profile import Profile +from azure.cli.core.profiles import ResourceType from azure.cli.core.commands.client_factory import get_mgmt_service_client, get_subscription_id from azure.cli.core.keys import is_valid_ssh_rsa_public_key from azure.cli.core.util import in_cloud_console, shell_safe_json_parse, truncate_text, sdk_no_wait @@ -67,34 +68,6 @@ from azure.mgmt.containerservice.models import ContainerServiceOrchestratorTypes -from azure.mgmt.containerservice.v2021_03_01.models import ContainerServiceNetworkProfile -from azure.mgmt.containerservice.v2021_03_01.models import ContainerServiceLinuxProfile -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterServicePrincipalProfile -from azure.mgmt.containerservice.v2021_03_01.models import ContainerServiceSshConfiguration -from azure.mgmt.containerservice.v2021_03_01.models import ContainerServiceSshPublicKey -from azure.mgmt.containerservice.v2021_03_01.models import ManagedCluster -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterAADProfile -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterAddonProfile -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterAgentPoolProfile -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterIdentity -from azure.mgmt.containerservice.v2021_03_01.models import AgentPool -from azure.mgmt.containerservice.v2021_03_01.models import AgentPoolUpgradeSettings -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterSKU -from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterWindowsProfile -from azure.mgmt.containerservice.v2021_03_01.models import ( - Components1Umhcm8SchemasManagedclusteridentityPropertiesUserassignedidentitiesAdditionalproperties) -from azure.mgmt.containerservice.v2021_03_01.models import RunCommandRequest - -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedClusterAgentPoolProfile -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftAgentPoolProfileRole -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedClusterIdentityProvider -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedClusterAADIdentityProvider -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedCluster -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftRouterProfile -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedClusterAuthProfile -from azure.mgmt.containerservice.v2019_09_30_preview.models import NetworkProfile -from azure.mgmt.containerservice.v2019_09_30_preview.models import OpenShiftManagedClusterMonitorProfile - from ._client_factory import cf_container_services from ._client_factory import cf_resource_groups from ._client_factory import get_auth_management_client @@ -130,7 +103,6 @@ logger = get_logger(__name__) - # pylint:disable=too-many-lines,unused-argument @@ -1071,8 +1043,6 @@ def load_service_principals(config_path): def _invoke_deployment(cmd, resource_group_name, deployment_name, template, parameters, validate, no_wait, subscription_id=None): - - from azure.cli.core.profiles import ResourceType DeploymentProperties = cmd.get_models( 'DeploymentProperties', resource_type=ResourceType.MGMT_RESOURCE_RESOURCES) properties = DeploymentProperties( @@ -1453,7 +1423,7 @@ def create_role_assignment(cli_ctx, role, assignee, is_service_principal, resour def _create_role_assignment(cli_ctx, role, assignee, resource_group_name=None, scope=None, resolve_assignee=True): - from azure.cli.core.profiles import ResourceType, get_sdk + from azure.cli.core.profiles import get_sdk factory = get_auth_management_client(cli_ctx, scope) assignments_client = factory.role_assignments definitions_client = factory.role_definitions @@ -1654,6 +1624,9 @@ def aks_check_acr(cmd, client, resource_group_name, name, acr): # pylint: disable=too-many-statements,too-many-branches def aks_browse(cmd, client, resource_group_name, name, disable_browser=False, listen_address='127.0.0.1', listen_port='8001'): + ManagedClusterAddonProfile = cmd.get_models('ManagedClusterAddonProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') # verify the kube-dashboard addon was not disabled instance = client.get(resource_group_name, name) addon_profiles = instance.addon_profiles or {} @@ -1988,6 +1961,44 @@ def aks_create(cmd, client, resource_group_name, name, ssh_key_value, # pylint: no_wait=False, yes=False, enable_azure_rbac=False): + ManagedClusterWindowsProfile = cmd.get_models('ManagedClusterWindowsProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterSKU = cmd.get_models('ManagedClusterSKU', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ContainerServiceNetworkProfile = cmd.get_models('ContainerServiceNetworkProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ContainerServiceLinuxProfile = cmd.get_models('ContainerServiceLinuxProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterServicePrincipalProfile = cmd.get_models('ManagedClusterServicePrincipalProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ContainerServiceSshConfiguration = cmd.get_models('ContainerServiceSshConfiguration', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ContainerServiceSshPublicKey = cmd.get_models('ContainerServiceSshPublicKey', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterAADProfile = cmd.get_models('ManagedClusterAADProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterAgentPoolProfile = cmd.get_models('ManagedClusterAgentPoolProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterIdentity = cmd.get_models('ManagedClusterIdentity', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedCluster = cmd.get_models('ManagedCluster', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + Components1Umhcm8SchemasManagedclusteridentityPropertiesUserassignedidentitiesAdditionalproperties = cmd.get_models( + 'Components1Umhcm8SchemasManagedclusteridentityPropertiesUserassignedidentitiesAdditionalproperties', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + _validate_ssh_key(no_ssh_key, ssh_key_value) subscription_id = get_subscription_id(cmd.cli_ctx) if dns_name_prefix and fqdn_subdomain: @@ -2127,6 +2138,7 @@ def aks_create(cmd, client, resource_group_name, name, ssh_key_value, # pylint: 'Are you an Owner on this subscription?') load_balancer_profile = create_load_balancer_profile( + cmd, load_balancer_managed_outbound_ip_count, load_balancer_outbound_ips, load_balancer_outbound_ip_prefixes, @@ -2249,6 +2261,7 @@ def aks_create(cmd, client, resource_group_name, name, ssh_key_value, # pylint: raise CLIError("Please use standard load balancer for private cluster") if api_server_authorized_ip_ranges or enable_private_cluster: api_server_access_profile = _populate_api_server_access_profile( + cmd, api_server_authorized_ip_ranges, enable_private_cluster=enable_private_cluster ) @@ -2506,6 +2519,9 @@ def aks_update_credentials(cmd, client, resource_group_name, name, aad_client_app_id=None, aad_tenant_id=None, no_wait=False): + ManagedClusterServicePrincipalProfile = cmd.get_models('ManagedClusterServicePrincipalProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') if bool(reset_service_principal) == bool(reset_aad): raise CLIError( 'usage error: --reset-service-principal | --reset-aad-profile') @@ -2585,6 +2601,19 @@ def aks_update(cmd, client, resource_group_name, name, no_wait=False, enable_azure_rbac=False, disable_azure_rbac=False): + ManagedClusterSKU = cmd.get_models('ManagedClusterSKU', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterAADProfile = cmd.get_models('ManagedClusterAADProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + ManagedClusterIdentity = cmd.get_models('ManagedClusterIdentity', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') + Components1Umhcm8SchemasManagedclusteridentityPropertiesUserassignedidentitiesAdditionalproperties = cmd.get_models( + 'Components1Umhcm8SchemasManagedclusteridentityPropertiesUserassignedidentitiesAdditionalproperties', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') update_autoscaler = enable_cluster_autoscaler + \ disable_cluster_autoscaler + update_cluster_autoscaler update_lb_profile = is_load_balancer_profile_provided(load_balancer_managed_outbound_ip_count, @@ -2730,6 +2759,7 @@ def aks_update(cmd, client, resource_group_name, name, if update_lb_profile: instance.network_profile.load_balancer_profile = update_load_balancer_profile( + cmd, load_balancer_managed_outbound_ip_count, load_balancer_outbound_ips, load_balancer_outbound_ip_prefixes, @@ -2741,6 +2771,7 @@ def aks_update(cmd, client, resource_group_name, name, if api_server_authorized_ip_ranges is not None: instance.api_server_access_profile = \ _populate_api_server_access_profile( + cmd, api_server_authorized_ip_ranges, instance=instance) if enable_aad: @@ -2955,7 +2986,8 @@ def aks_runcommand(cmd, client, resource_group_name, name, command_string="", co if not command_string: raise ValidationError('Command cannot be empty.') - + RunCommandRequest = cmd.get_models('RunCommandRequest', resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') request_payload = RunCommandRequest(command=command_string) request_payload.context = _get_command_context(command_files) @@ -3143,6 +3175,9 @@ def _update_addons(cmd, instance, subscription_id, resource_group_name, name, ad appgw_watch_namespace=None, enable_sgxquotehelper=False, no_wait=False): + ManagedClusterAddonProfile = cmd.get_models('ManagedClusterAddonProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') # parse the comma-separated addons argument addon_args = addons.split(',') @@ -3269,6 +3304,9 @@ def _handle_addons_args(cmd, addons_str, subscription_id, resource_group_name, a appgw_subnet_id=None, appgw_watch_namespace=None, enable_sgxquotehelper=False): + ManagedClusterAddonProfile = cmd.get_models('ManagedClusterAddonProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='managed_clusters') if not addon_profiles: addon_profiles = {} addons = addons_str.split(',') if addons_str else [] @@ -3555,8 +3593,6 @@ def _ensure_default_log_analytics_workspace_for_monitoring(cmd, subscription_id, # TODO: track2/replace create_or_update with begin_create_or_update, depends on 'azure.mgmt.resource.resources' resource_groups.create_or_update(default_workspace_resource_group, { 'location': workspace_region}) - - from azure.cli.core.profiles import ResourceType GenericResource = cmd.get_models( 'GenericResource', resource_type=ResourceType.MGMT_RESOURCE_RESOURCES) generic_resource = GenericResource(location=workspace_region, properties={ @@ -3766,6 +3802,12 @@ def aks_agentpool_add(cmd, client, resource_group_name, cluster_name, nodepool_n mode="User", enable_encryption_at_host=False, no_wait=False): + AgentPool = cmd.get_models('AgentPool', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='agent_pools') + AgentPoolUpgradeSettings = cmd.get_models('AgentPoolUpgradeSettings', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='agent_pools') instances = client.list(resource_group_name, cluster_name) for agentpool_profile in instances: if agentpool_profile.name == nodepool_name: @@ -3868,6 +3910,7 @@ def aks_agentpool_upgrade(cmd, client, resource_group_name, cluster_name, node_image_only=False, max_surge=None, no_wait=False): + AgentPoolUpgradeSettings = cmd.get_models('AgentPoolUpgradeSettings', operation_group='agent_pools') if kubernetes_version != '' and node_image_only: raise CLIError( 'Conflicting flags. Upgrading the Kubernetes version will also ' @@ -3910,7 +3953,9 @@ def aks_agentpool_update(cmd, client, resource_group_name, cluster_name, nodepoo max_surge=None, mode=None, no_wait=False): - + AgentPoolUpgradeSettings = cmd.get_models('AgentPoolUpgradeSettings', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='agent_pools') update_autoscaler = enable_cluster_autoscaler + \ disable_cluster_autoscaler + update_cluster_autoscaler @@ -4063,13 +4108,17 @@ def _ensure_aks_service_principal(cli_ctx, } -def _ensure_osa_aad(cli_ctx, +def _ensure_osa_aad(cmd, + cli_ctx, aad_client_app_id=None, aad_client_app_secret=None, aad_tenant_id=None, identifier=None, name=None, create=False, customer_admin_group_id=None): + OpenShiftManagedClusterAADIdentityProvider = cmd.get_models('OpenShiftManagedClusterAADIdentityProvider', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') rbac_client = get_graph_rbac_management_client(cli_ctx) if create: # This reply_url is temporary set since Azure need one to create the AAD. @@ -4367,6 +4416,30 @@ def openshift_create(cmd, client, resource_group_name, name, # pylint: disable= no_wait=False, workspace_id=None, customer_admin_group_id=None): + OpenShiftManagedClusterAgentPoolProfile = cmd.get_models('OpenShiftManagedClusterAgentPoolProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftAgentPoolProfileRole = cmd.get_models('OpenShiftAgentPoolProfileRole', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftManagedClusterIdentityProvider = cmd.get_models('OpenShiftManagedClusterIdentityProvider', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftManagedCluster = cmd.get_models('OpenShiftManagedCluster', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftRouterProfile = cmd.get_models('OpenShiftRouterProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + NetworkProfile = cmd.get_models('NetworkProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftManagedClusterAuthProfile = cmd.get_models('OpenShiftManagedClusterAuthProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') + OpenShiftManagedClusterMonitorProfile = cmd.get_models('OpenShiftManagedClusterMonitorProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') logger.warning('Support for the creation of ARO 3.11 clusters ends 30 Nov 2020. Please see aka.ms/aro/4 for information on switching to ARO 4.') # pylint: disable=line-too-long if location is None: @@ -4412,7 +4485,8 @@ def openshift_create(cmd, client, resource_group_name, name, # pylint: disable= if aad_client_app_id is None and aad_client_app_secret is None and aad_tenant_id is None: create_aad = True - osa_aad_identity = _ensure_osa_aad(cmd.cli_ctx, + osa_aad_identity = _ensure_osa_aad(cmd, + cmd.cli_ctx, aad_client_app_id=aad_client_app_id, aad_client_app_secret=aad_client_app_secret, aad_tenant_id=aad_tenant_id, identifier=None, @@ -4463,7 +4537,8 @@ def openshift_create(cmd, client, resource_group_name, name, # pylint: disable= resource_group_name=resource_group_name, resource_name=name, parameters=osamc) result = LongRunningOperation(cmd.cli_ctx)(result) instance = client.get(resource_group_name, name) - _ensure_osa_aad(cmd.cli_ctx, + _ensure_osa_aad(cmd, + cmd.cli_ctx, aad_client_app_id=osa_aad_identity.client_id, aad_client_app_secret=osa_aad_identity.secret, aad_tenant_id=osa_aad_identity.tenant_id, identifier=instance.public_hostname, @@ -4507,6 +4582,9 @@ def openshift_scale(cmd, client, resource_group_name, name, compute_count, no_wa def openshift_monitor_enable(cmd, client, resource_group_name, name, workspace_id, no_wait=False): + OpenShiftManagedClusterMonitorProfile = cmd.get_models('OpenShiftManagedClusterMonitorProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') logger.warning('The az openshift command is deprecated and has been replaced by az aro for ARO 4 clusters. See http://aka.ms/aro/4 for information on switching to ARO 4.') # pylint: disable=line-too-long instance = client.get(resource_group_name, name) @@ -4519,6 +4597,9 @@ def openshift_monitor_enable(cmd, client, resource_group_name, name, workspace_i def openshift_monitor_disable(cmd, client, resource_group_name, name, no_wait=False): + OpenShiftManagedClusterMonitorProfile = cmd.get_models('OpenShiftManagedClusterMonitorProfile', + resource_type=ResourceType.MGMT_CONTAINERSERVICE, + operation_group='open_shift_managed_clusters') logger.warning('The az openshift command is deprecated and has been replaced by az aro for ARO 4 clusters. See http://aka.ms/aro/4 for information on switching to ARO 4.') # pylint: disable=line-too-long instance = client.get(resource_group_name, name) diff --git a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_custom.py b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_custom.py index c85497a75f8..ceb605d7a3e 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_custom.py +++ b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_custom.py @@ -13,6 +13,12 @@ import unittest import yaml +from knack import CLI + +from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX +from azure.cli.core.cloud import get_active_cloud +from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version + from msrestazure.azure_exceptions import CloudError from azure.graphrbac.models import GraphErrorException from azure.cli.command_modules.acs._params import (regions_in_preview, @@ -33,8 +39,37 @@ CONST_KUBE_DASHBOARD_ADDON_NAME, CONST_AZURE_POLICY_ADDON_NAME) +class MockCLI(CLI): + def __init__(self): + super(MockCLI, self).__init__(cli_name='mock_cli', config_dir=GLOBAL_CONFIG_DIR, + config_env_var_prefix=ENV_VAR_PREFIX, commands_loader_cls=MockLoader) + self.cloud = get_active_cloud(self) + + +class MockLoader(object): + def __init__(self, ctx): + self.ctx = ctx + + def get_models(self, *attr_args, **_): + from azure.cli.core.profiles import get_sdk + return get_sdk(self.ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterAddonProfile', + mod='models', operation_group='managed_clusters') + + +class MockCmd(object): + def __init__(self, ctx, arguments={}): + self.cli_ctx = ctx + self.loader = MockLoader(self.cli_ctx) + self.arguments = arguments + + def get_models(self, *attr_args, **kwargs): + return get_sdk(self.cli_ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterAddonProfile', + mod='models', operation_group='managed_clusters') class AcsCustomCommandTest(unittest.TestCase): + def setUp(self): + self.cli = MockCLI() + def test_list_acs_locations(self): client, cmd = mock.MagicMock(), mock.MagicMock() regions = list_acs_locations(client, cmd) @@ -590,21 +625,21 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): # http_application_routing enabled instance = mock.MagicMock() instance.addon_profiles = None - cmd = mock.MagicMock() - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'http_application_routing', enable=True) self.assertIn(CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME, instance.addon_profiles) addon_profile = instance.addon_profiles[CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME] self.assertTrue(addon_profile.enabled) # http_application_routing disabled - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'http_application_routing', enable=False) addon_profile = instance.addon_profiles[CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME] self.assertFalse(addon_profile.enabled) # monitoring added - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'monitoring', enable=True) monitoring_addon_profile = instance.addon_profiles[CONST_MONITORING_ADDON_NAME] self.assertTrue(monitoring_addon_profile.enabled) @@ -612,9 +647,9 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): self.assertFalse(routing_addon_profile.enabled) # monitoring disabled, routing enabled - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'monitoring', enable=False) - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'http_application_routing', enable=True) monitoring_addon_profile = instance.addon_profiles[CONST_MONITORING_ADDON_NAME] self.assertFalse(monitoring_addon_profile.enabled) @@ -623,7 +658,7 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): self.assertEqual(sorted(list(instance.addon_profiles)), [CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME, CONST_MONITORING_ADDON_NAME]) # azurepolicy added - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'azure-policy', enable=True) azurepolicy_addon_profile = instance.addon_profiles[CONST_AZURE_POLICY_ADDON_NAME] self.assertTrue(azurepolicy_addon_profile.enabled) @@ -633,9 +668,9 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): self.assertFalse(monitoring_addon_profile.enabled) # azurepolicy disabled, routing enabled - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'azure-policy', enable=False) - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'http_application_routing', enable=True) azurepolicy_addon_profile = instance.addon_profiles[CONST_AZURE_POLICY_ADDON_NAME] self.assertFalse(azurepolicy_addon_profile.enabled) @@ -646,14 +681,14 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): self.assertEqual(sorted(list(instance.addon_profiles)), [CONST_AZURE_POLICY_ADDON_NAME, CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME, CONST_MONITORING_ADDON_NAME]) # kube-dashboard disabled, no existing dashboard addon profile - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'kube-dashboard', enable=False) dashboard_addon_profile = instance.addon_profiles[CONST_KUBE_DASHBOARD_ADDON_NAME] self.assertFalse(dashboard_addon_profile.enabled) # kube-dashboard enabled, no existing dashboard addon profile instance.addon_profiles.pop(CONST_KUBE_DASHBOARD_ADDON_NAME, None) - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'kube-dashboard', enable=True) dashboard_addon_profile = instance.addon_profiles[CONST_KUBE_DASHBOARD_ADDON_NAME] self.assertTrue(dashboard_addon_profile.enabled) @@ -662,7 +697,7 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): instance.addon_profiles.pop(CONST_KUBE_DASHBOARD_ADDON_NAME, None) # test lower cased key name instance.addon_profiles['kubedashboard'] = ManagedClusterAddonProfile(enabled=True) - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'kube-dashboard', enable=False) dashboard_addon_profile = instance.addon_profiles[CONST_KUBE_DASHBOARD_ADDON_NAME] self.assertFalse(dashboard_addon_profile.enabled) @@ -671,7 +706,7 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): instance.addon_profiles.pop(CONST_KUBE_DASHBOARD_ADDON_NAME, None) # test lower cased key name instance.addon_profiles['kubedashboard'] = ManagedClusterAddonProfile(enabled=False) - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'kube-dashboard', enable=True) dashboard_addon_profile = instance.addon_profiles[CONST_KUBE_DASHBOARD_ADDON_NAME] self.assertTrue(dashboard_addon_profile.enabled) @@ -679,24 +714,24 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): # monitoring enabled and then enabled again should error instance = mock.Mock() instance.addon_profiles = None - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'monitoring', enable=True) with self.assertRaises(CLIError): - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'monitoring', enable=True) # virtual-node enabled instance = mock.MagicMock() instance.addon_profiles = None cmd = mock.MagicMock() - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'virtual-node', enable=True, subnet_name='foo') self.assertIn('aciConnectorLinux', instance.addon_profiles) addon_profile = instance.addon_profiles['aciConnectorLinux'] self.assertTrue(addon_profile.enabled) # virtual-node disabled - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'virtual-node', enable=False) addon_profile = instance.addon_profiles['aciConnectorLinux'] self.assertFalse(addon_profile.enabled) @@ -705,14 +740,14 @@ def test_update_addons(self, rg_def, cf_resource_groups, cf_resources): instance = mock.MagicMock() instance.addon_profiles = None cmd = mock.MagicMock() - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'ingress-appgw', enable=True, appgw_subnet_cidr='10.2.0.0/16') self.assertIn('ingressApplicationGateway', instance.addon_profiles) addon_profile = instance.addon_profiles['ingressApplicationGateway'] self.assertTrue(addon_profile.enabled) # ingress-appgw disabled - instance = _update_addons(cmd, instance, '00000000-0000-0000-0000-000000000000', + instance = _update_addons(MockCmd(self.cli), instance, '00000000-0000-0000-0000-000000000000', 'clitest000001', 'clitest000001', 'ingress-appgw', enable=False) addon_profile = instance.addon_profiles['ingressApplicationGateway'] self.assertFalse(addon_profile.enabled) diff --git a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_helpers.py b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_helpers.py index 7da178f8e29..a8dc8fff7c4 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_helpers.py +++ b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_helpers.py @@ -4,22 +4,60 @@ # -------------------------------------------------------------------------------------------- import unittest +import mock + +from knack import CLI + +from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX +from azure.cli.core.cloud import get_active_cloud +from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version + from azure.cli.command_modules.acs import _helpers as helpers +class MockCLI(CLI): + def __init__(self): + super(MockCLI, self).__init__(cli_name='mock_cli', config_dir=GLOBAL_CONFIG_DIR, + config_env_var_prefix=ENV_VAR_PREFIX, commands_loader_cls=MockLoader) + self.cloud = get_active_cloud(self) + + +class MockLoader(object): + def __init__(self, ctx): + self.ctx = ctx + + def get_models(self, *attr_args, **_): + from azure.cli.core.profiles import get_sdk + return get_sdk(self.ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterAPIServerAccessProfile', + mod='models', operation_group='managed_clusters') + + +class MockCmd(object): + def __init__(self, ctx, arguments={}): + self.cli_ctx = ctx + self.loader = MockLoader(self.cli_ctx) + self.arguments = arguments + + def get_models(self, *attr_args, **kwargs): + return get_sdk(self.cli_ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterAPIServerAccessProfile', + mod='models', operation_group='managed_clusters') + class TestPopulateApiServerAccessProfile(unittest.TestCase): + def setUp(self): + self.cli = MockCLI() + def test_single_cidr_with_spaces(self): api_server_authorized_ip_ranges = "0.0.0.0/32 " - profile = helpers._populate_api_server_access_profile(api_server_authorized_ip_ranges, enable_private_cluster=False) + profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), api_server_authorized_ip_ranges, enable_private_cluster=False) self.assertListEqual(profile.authorized_ip_ranges, ["0.0.0.0/32"]) def test_multi_cidr_with_spaces(self): api_server_authorized_ip_ranges = " 0.0.0.0/32 , 129.1.1.1/32" - profile = helpers._populate_api_server_access_profile(api_server_authorized_ip_ranges, enable_private_cluster=False) + profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), api_server_authorized_ip_ranges, enable_private_cluster=False) self.assertListEqual(profile.authorized_ip_ranges, ["0.0.0.0/32", "129.1.1.1/32"]) def test_private_cluster(self): - profile = helpers._populate_api_server_access_profile(None, enable_private_cluster=True) + profile = helpers._populate_api_server_access_profile(MockCmd(self.cli), None, enable_private_cluster=True) self.assertListEqual(profile.authorized_ip_ranges, []) self.assertEqual(profile.enable_private_cluster, True) diff --git a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_loadbalancer.py b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_loadbalancer.py index a21176578f2..c0d88ccbd36 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_loadbalancer.py +++ b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_loadbalancer.py @@ -3,6 +3,13 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import unittest +import mock + +from knack import CLI + +from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX +from azure.cli.core.cloud import get_active_cloud +from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfile from azure.mgmt.containerservice.v2021_03_01.models import ManagedClusterLoadBalancerProfileManagedOutboundIPs @@ -11,9 +18,10 @@ from azure.cli.core.util import CLIError from azure.cli.command_modules.acs import _loadbalancer as loadbalancer - class TestLoadBalancer(unittest.TestCase): + def test_configure_load_balancer_profile(self): + cmd = mock.MagicMock() managed_outbound_ip_count = 5 outbound_ips = None outbound_ip_prefixes = None @@ -31,7 +39,7 @@ def test_configure_load_balancer_profile(self): public_ip_prefixes="public_ip_prefixes" ) - p = loadbalancer.configure_load_balancer_profile( + p = loadbalancer.configure_load_balancer_profile(cmd, managed_outbound_ip_count, outbound_ips, outbound_ip_prefixes, outbound_ports, idle_timeout, profile) self.assertIsNotNone(p.managed_outbound_i_ps) diff --git a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py index c3ec8d14a60..1df8d527c3e 100644 --- a/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py +++ b/src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_validators.py @@ -3,9 +3,44 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import unittest +import mock + +from knack import CLI + +from azure.cli.core._config import GLOBAL_CONFIG_DIR, ENV_VAR_PREFIX +from azure.cli.core.cloud import get_active_cloud +from azure.cli.core.profiles import get_sdk, ResourceType, supported_api_version + from azure.cli.core.util import CLIError from azure.cli.command_modules.acs import _validators as validators +class MockCLI(CLI): + def __init__(self): + super(MockCLI, self).__init__(cli_name='mock_cli', config_dir=GLOBAL_CONFIG_DIR, + config_env_var_prefix=ENV_VAR_PREFIX, commands_loader_cls=MockLoader) + self.cloud = get_active_cloud(self) + + +class MockLoader(object): + def __init__(self, ctx): + self.ctx = ctx + + def get_models(self, *attr_args, **_): + from azure.cli.core.profiles import get_sdk + return get_sdk(self.ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterPropertiesAutoScalerProfile', + mod='models', operation_group='managed_clusters') + + +class MockCmd(object): + def __init__(self, ctx, arguments={}): + self.cli_ctx = ctx + self.loader = MockLoader(self.cli_ctx) + self.arguments = arguments + + def get_models(self, *attr_args, **kwargs): + return get_sdk(self.cli_ctx, ResourceType.MGMT_CONTAINERSERVICE, 'ManagedClusterPropertiesAutoScalerProfile', + mod='models', operation_group='managed_clusters') + class TestValidateIPRanges(unittest.TestCase): def test_simultaneous_allow_and_disallow_with_spaces(self): @@ -62,20 +97,23 @@ def test_IPv6(self): class TestClusterAutoscalerParamsValidators(unittest.TestCase): + def setUp(self): + self.cli = MockCLI() + def test_empty_key_empty_value(self): cluster_autoscaler_profile = ["="] namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile) err = "Empty key specified for cluster-autoscaler-profile" with self.assertRaises(CLIError) as cm: - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) self.assertEqual(str(cm.exception), err) def test_non_empty_key_empty_value(self): cluster_autoscaler_profile = ["scan-interval="] namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile) - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) def test_two_empty_keys_empty_value(self): cluster_autoscaler_profile = ["=", "="] @@ -83,7 +121,7 @@ def test_two_empty_keys_empty_value(self): err = "Empty key specified for cluster-autoscaler-profile" with self.assertRaises(CLIError) as cm: - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) self.assertEqual(str(cm.exception), err) def test_one_empty_key_in_pair_one_non_empty(self): @@ -92,7 +130,7 @@ def test_one_empty_key_in_pair_one_non_empty(self): err = "Empty key specified for cluster-autoscaler-profile" with self.assertRaises(CLIError) as cm: - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) self.assertEqual(str(cm.exception), err) def test_invalid_key(self): @@ -101,14 +139,14 @@ def test_invalid_key(self): err = "'bad-key' is an invalid key for cluster-autoscaler-profile" with self.assertRaises(CLIError) as cm: - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) self.assertIn(err, str(cm.exception),) def test_valid_parameters(self): cluster_autoscaler_profile = ["scan-interval=20s", "scale-down-delay-after-add=15m"] namespace = Namespace(cluster_autoscaler_profile=cluster_autoscaler_profile) - validators.validate_cluster_autoscaler_profile(namespace) + validators.validate_cluster_autoscaler_profile(MockCmd(self.cli), namespace) class Namespace: