Skip to content

Commit

Permalink
[Container] Update container cmdlets to use 2024-05-01-preview versio…
Browse files Browse the repository at this point in the history
…n API (#30260)
  • Loading branch information
kaushik-ms authored Nov 6, 2024
1 parent 259c264 commit 2768685
Show file tree
Hide file tree
Showing 31 changed files with 7,511 additions and 6,347 deletions.
41 changes: 41 additions & 0 deletions linter_exclusions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,47 @@ consumption pricesheet show:
rule_exclusions:
- option_length_too_long
container create:
parameters:
azure_file_volume_account_key:
rule_exclusions:
- option_length_too_long
azure_file_volume_account_name:
rule_exclusions:
- option_length_too_long
azure_file_volume_mount_path:
rule_exclusions:
- option_length_too_long
azure_file_volume_share_name:
rule_exclusions:
- option_length_too_long
log_analytics_workspace:
rule_exclusions:
- option_length_too_long
log_analytics_workspace_key:
rule_exclusions:
- option_length_too_long
registry_login_server:
rule_exclusions:
- option_length_too_long
secure_environment_variables:
rule_exclusions:
- option_length_too_long
subnet_address_prefix:
rule_exclusions:
- option_length_too_long
container_group_profile_id:
rule_exclusions:
- option_length_too_long
fail_container_group_create_on_reuse_failure:
rule_exclusions:
- option_length_too_long
standby_pool_profile_id:
rule_exclusions:
- option_length_too_long
container_group_profile_revision:
rule_exclusions:
- option_length_too_long
container container-group-profile create:
parameters:
azure_file_volume_account_key:
rule_exclusions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ def cf_msi(cli_ctx):
from azure.mgmt.msi import ManagedServiceIdentityClient
from azure.cli.core.commands.client_factory import get_mgmt_service_client
return get_mgmt_service_client(cli_ctx, ManagedServiceIdentityClient)


def cf_container_group_profiles(cli_ctx, *_):
return _container_instance_client_factory(cli_ctx).container_group_profiles


def cf_container_group_profile(cli_ctx, *_):
return _container_instance_client_factory(cli_ctx).container_group_profile
16 changes: 16 additions & 0 deletions src/azure-cli/azure/cli/command_modules/container/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ def transform_container_group(result):
def transform_container_group_list(result):
"""Transform a container group list to table output. """
return [transform_container_group(container_group) for container_group in result]


def transform_container_group_profile(result):
"""Transform a container group profile to table output. """
return OrderedDict([('Name', result['name']),
('ResourceGroup', result['resourceGroup']),
('Image', _get_images(result)),
('IP:ports', _format_ip_address(result)),
('CPU/Memory', _format_cpu_memory(result)),
('OsType', result.get('osType')),
('Location', result['location'])])


def transform_container_group_profile_list(result):
"""Transform a container group profile list to table output. """
return [transform_container_group_profile(container_group_profile) for container_group_profile in result]
37 changes: 37 additions & 0 deletions src/azure-cli/azure/cli/command_modules/container/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,40 @@
text: az container show --name MyContainerGroup --resource-group MyResourceGroup
crafted: true
"""

helps['container container-group-profile'] = """
type: group
short-summary: Manage Azure Container Instance Container Group Profile.
"""

helps['container container-group-profile create'] = """
type: command
short-summary: Create a container group profile.
examples:
- name: Create a container group profile.
text: az container container-group-profile create --resource-group MyResourceGroup --name myapp --image myimage:latest --cpu 1 --memory 1
"""

helps['container container-group-profile show'] = """
type: command
short-summary: Get a container group profile.
examples:
- name: Get a container group profile.
text: az container container-group-profile show --resource-group MyResourceGroup --name mycgprofile
"""

helps['container container-group-profile show-revision'] = """
type: command
short-summary: Show a container group profile revision.
examples:
- name: Show a container group profile.
text: az container container-group-profile show-revision --resource-group MyResourceGroup --name mycgprofile -r 1
"""

helps['container container-group-profile delete'] = """
type: command
short-summary: Delete a container group profile.
examples:
- name: Delete a container group profile.
text: az container container-group-profile delete --resource-group MyResourceGroup --name mycgprofile
"""
81 changes: 81 additions & 0 deletions src/azure-cli/azure/cli/command_modules/container/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ def _secure_environment_variables_type(value):
raise CLIError(message)


def _config_map_type(key_value_pair):
"""Space-separated values in 'key=value' format."""
try:
key, value = key_value_pair.split('=', 1)
return {'key': key, 'value': value}
except ValueError:
message = ("Incorrectly formatted config map key-value pairs. "
"Argument values should be in the format a=b c=d")
raise CLIError(message)


secrets_type = CLIArgumentType(
validator=validate_secrets,
help="space-separated secrets in 'key=value' format.",
Expand All @@ -61,6 +72,7 @@ def load_arguments(self, _):
c.argument('image', validator=validate_image, help='The container image name')
c.argument('cpu', type=float, help='The required number of CPU cores of the containers, accurate to one decimal place')
c.argument('memory', type=float, help='The required memory of the containers in GB, accurate to one decimal place')
c.argument('config_map', nargs='+', type=_config_map_type, help='A list of config map key-value pairs for the container. Space-separated values in \'key=value\' format.')
c.argument('os_type', arg_type=get_enum_type(OperatingSystemTypes), help='The OS type of the containers')
c.argument('ip_address', arg_type=get_enum_type(IP_ADDRESS_TYPES), help='The IP address type of the container group')
c.argument('ports', type=int, nargs='+', default=[80], help='A list of ports to open. Space-separated list of ports')
Expand Down Expand Up @@ -116,6 +128,14 @@ def load_arguments(self, _):
c.argument('log_analytics_workspace', help='The Log Analytics workspace name or id. Use the current subscription or use --subscription flag to set the desired subscription.')
c.argument('log_analytics_workspace_key', help='The Log Analytics workspace key.')

with self.argument_context('container create', arg_group='Container Group Profile Reference') as c:
c.argument('container_group_profile_id', help='The reference container group profile ARM resource id.')
c.argument('container_group_profile_revision', help='The reference container group profile revision.')

with self.argument_context('container create', arg_group='Standby Pool Profile') as c:
c.argument('standby_pool_profile_id', help='The standby pool profile ARM resource id from which the container will be reused.')
c.argument('fail_container_group_create_on_reuse_failure', help='The flag indicating whether to fail the container group creation if the standby pool reuse failed.', action='store_true')

with self.argument_context('container create', arg_group='Git Repo Volume') as c:
c.argument('gitrepo_url', help='The URL of a git repository to be mounted as a volume')
c.argument('gitrepo_dir', validator=validate_gitrepo_directory, help="The target directory path in the git repository. Must not contain '..'.")
Expand All @@ -137,3 +157,64 @@ def load_arguments(self, _):

with self.argument_context('container attach') as c:
c.argument('container_name', help='The container to attach to. If omitted, the first container in the container group will be chosen')

with self.argument_context('container container-group-profile') as c:
c.argument('resource_group_name', arg_type=resource_group_name_type)
c.argument('container_group_profile_name', options_list=['--name', '-n'], help="The name of the container group profile.")
c.argument('location', arg_type=get_location_type(self.cli_ctx))

with self.argument_context('container container-group-profile create') as c:
c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=get_default_location_from_resource_group)
c.argument('image', validator=validate_image, help='The container image name')
c.argument('cpu', type=float, help='The required number of CPU cores of the containers, accurate to one decimal place')
c.argument('memory', type=float, help='The required memory of the containers in GB, accurate to one decimal place')
c.argument('config_map', nargs='+', type=_config_map_type, help='A list of config map key-value pairs for the container. Space-separated values in \'key=value\' format.')
c.argument('os_type', arg_type=get_enum_type(OperatingSystemTypes), help='The OS type of the containers')
c.argument('ip_address', arg_type=get_enum_type(IP_ADDRESS_TYPES), help='The IP address type of the container group')
c.argument('ports', type=int, nargs='+', default=[80], help='A list of ports to open. Space-separated list of ports')
c.argument('protocol', arg_type=get_enum_type(ContainerNetworkProtocol), help='The network protocol to use')
c.argument('restart_policy', arg_type=get_enum_type(ContainerGroupRestartPolicy), help='Restart policy for all containers within the container group')
c.argument('command_line', help='The command line to run when the container is started, e.g. \'/bin/bash -c myscript.sh\'')
c.argument('environment_variables', nargs='+', options_list=['--environment-variables', '-e'], type=_environment_variables_type, help='A list of environment variable for the container. Space-separated values in \'key=value\' format.')
c.argument('secure_environment_variables', nargs='+', type=_secure_environment_variables_type, help='A list of secure environment variable for the container. Space-separated values in \'key=value\' format.')
c.argument('secrets', secrets_type)
c.argument('secrets_mount_path', validator=validate_volume_mount_path, help="The path within the container where the secrets volume should be mounted. Must not contain colon ':'.")
c.argument('file', options_list=['--file', '-f'], help="The path to the input file.")
c.argument('zone', help="The zone to place the container group.")
c.argument('priority', help='The priority of the container group')
c.argument('sku', help="The SKU of the container group")

with self.argument_context('container container-group-profile create', arg_group='Confidential Container Group') as c:
c.argument('cce_policy', help="The CCE policy for the confidential container group")
c.argument('allow_privilege_escalation', options_list=['--allow-escalation'], help="Allow whether a process can gain more privileges than its parent process.", action='store_true')
c.argument('privileged', help='The flag to determine if the container permissions is elevated to Privileged', action='store_true')
c.argument('run_as_user', help="Set the User GID for the container")
c.argument('run_as_group', help="Set the User UID for the container")
c.argument('seccomp_profile', help="A base64 encoded string containing the contents of the JSON in the seccomp profile")
c.argument('add_capabilities', nargs='+', help="A List of security context capabilities to be added")
c.argument('drop_capabilities', nargs='+', help="A List of security context capabilities to be dropped")

with self.argument_context('container container-group-profile create', arg_group='Image Registry') as c:
c.argument('registry_login_server', help='The container image registry login server')
c.argument('registry_username', help='The username to log in container image registry server')
c.argument('registry_password', help='The password to log in container image registry server')
c.argument('acr_identity', help='The identity with access to the container registry')

with self.argument_context('container container-group-profile create', arg_group='Azure File Volume') as c:
c.argument('azure_file_volume_share_name', help='The name of the Azure File share to be mounted as a volume')
c.argument('azure_file_volume_account_name', help='The name of the storage account that contains the Azure File share')
c.argument('azure_file_volume_account_key', help='The storage account access key used to access the Azure File share')
c.argument('azure_file_volume_mount_path', validator=validate_volume_mount_path, help="The path within the container where the azure file volume should be mounted. Must not contain colon ':'.")

with self.argument_context('container container-group-profile create', arg_group='Log Analytics') as c:
c.argument('log_analytics_workspace', help='The Log Analytics workspace name or id. Use the current subscription or use --subscription flag to set the desired subscription.')
c.argument('log_analytics_workspace_key', help='The Log Analytics workspace key.')

with self.argument_context('container container-group-profile create', arg_group='Git Repo Volume') as c:
c.argument('gitrepo_url', help='The URL of a git repository to be mounted as a volume')
c.argument('gitrepo_dir', validator=validate_gitrepo_directory, help="The target directory path in the git repository. Must not contain '..'.")
c.argument('gitrepo_revision', help='The commit hash for the specified revision')
c.argument('gitrepo_mount_path', validator=validate_volume_mount_path, help="The path within the container where the git repo volume should be mounted. Must not contain colon ':'.")

with self.argument_context('container container-group-profile show-revision') as c:
c.argument('revision', options_list=['-r'], help="The revision of the container group profile.")
21 changes: 19 additions & 2 deletions src/azure-cli/azure/cli/command_modules/container/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType
from ._client_factory import cf_container_groups, cf_container
from ._format import transform_container_group_list, transform_container_group
from ._client_factory import cf_container_groups, cf_container, cf_container_group_profiles, cf_container_group_profile
from ._format import (transform_container_group_list, transform_container_group,
transform_container_group_profile_list, transform_container_group_profile)


container_group_sdk = CliCommandType(
Expand All @@ -30,3 +31,19 @@ def load_command_table(self, _):
g.command('restart', 'begin_restart', supports_no_wait=True)
g.command('stop', 'stop')
g.command('start', 'begin_start', supports_no_wait=True)

with self.command_group('container container-group-profile',
client_factory=cf_container_group_profiles) as g:
g.custom_command('list', 'list_container_group_profiles',
table_transformer=transform_container_group_profile_list)
g.custom_command('create', 'create_container_group_profile',
supports_no_wait=True, table_transformer=transform_container_group_profile)
g.custom_show_command('show', 'get_container_group_profile',
table_transformer=transform_container_group_profile)
g.custom_command('delete', 'delete_container_group_profile', confirmation=True)

with self.command_group('container container-group-profile', client_factory=cf_container_group_profile) as g:
g.custom_command('list-revisions', 'list_container_group_profile_revisions',
table_transformer=transform_container_group_profile_list)
g.custom_show_command('show-revision', 'get_container_group_profile_revision',
table_transformer=transform_container_group_profile)
Loading

0 comments on commit 2768685

Please sign in to comment.