Skip to content

Commit

Permalink
[Container Registry] Migrate to track2 SDK (#18611)
Browse files Browse the repository at this point in the history
  • Loading branch information
00Kai0 authored Jun 30, 2021
1 parent 8cb75ca commit aaffbc5
Show file tree
Hide file tree
Showing 40 changed files with 4,081 additions and 4,239 deletions.
7 changes: 6 additions & 1 deletion src/azure-cli-core/azure/cli/core/profiles/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ def default_api_version(self):
'role_definitions': '2018-01-01-preview',
'provider_operations_metadata': '2018-01-01-preview'
}),
ResourceType.MGMT_CONTAINERREGISTRY: '2020-11-01-preview',
ResourceType.MGMT_CONTAINERREGISTRY: SDKProfile('2020-11-01-preview', {
'agent_pools': '2019-06-01-preview',
'tasks': '2019-06-01-preview',
'task_runs': '2019-06-01-preview',
'runs': '2019-06-01-preview',
}),
ResourceType.DATA_KEYVAULT: '7.0',
ResourceType.DATA_PRIVATE_KEYVAULT: '7.2',
ResourceType.DATA_KEYVAULT_ADMINISTRATION_BACKUP: '7.2-preview',
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ACRCommandsLoader(AzCommandsLoader):
def __init__(self, cli_ctx=None):
from azure.cli.core.profiles import ResourceType
super(ACRCommandsLoader, self).__init__(cli_ctx=cli_ctx,
resource_type=ResourceType.MGMT_CONTAINERREGISTRY)
resource_type=ResourceType.MGMT_CONTAINERREGISTRY,
operation_group='webhooks')

def load_command_table(self, args):
from azure.cli.command_modules.acr.commands import load_command_table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import time

from msrest import Deserializer
from msrest.polling import PollingMethod, LROPoller
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import HttpResponseError
from azure.core.polling import PollingMethod, LROPoller

from ._constants import get_acr_task_models, get_succeeded_agentpool_status, get_finished_agentpool_status

Expand All @@ -26,7 +26,7 @@ def deserialize_agentpool(response):
return LROPoller(
client=client,
initial_response=client.get(
resource_group_name, registry_name, agent_pool_name, raw=True),
resource_group_name, registry_name, agent_pool_name, cls=lambda x, y, z: x),
deserialization_callback=deserialize_agentpool,
polling_method=RunPolling(
cmd=cmd,
Expand All @@ -50,9 +50,9 @@ def __init__(self, cmd, registry_name, agent_pool_name, timeout=30):
self.operation_result = None

def initialize(self, client, initial_response, deserialization_callback):
self._client = client
self._client = client._client # pylint: disable=protected-access
self._response = initial_response
self._url = initial_response.request.url
self._url = initial_response.http_request.url
self._deserialize = deserialization_callback

self._set_operation_status(initial_response)
Expand Down Expand Up @@ -84,13 +84,13 @@ def resource(self):

def _set_operation_status(self, response):
AgentPoolStatus = self._cmd.get_models('ProvisioningState')
if response.status_code == 200 or response.status_code == 404:
if response.http_response.status_code == 200 or response.http_response.status_code == 404:
self.operation_result = self._deserialize(response)
self.operation_status = self.operation_result.provisioning_state or AgentPoolStatus.succeeded.value
return
raise CloudError(response)
raise HttpResponseError(response)

def _update_status(self):
self._response = self._client.send(
self._response = self._client._pipeline.run( # pylint: disable=protected-access
self._client.get(self._url), stream=False)
self._set_operation_status(self._response)
16 changes: 8 additions & 8 deletions src/azure-cli/azure/cli/command_modules/acr/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ def get_premium_sku(cmd):


def get_valid_os(cmd):
OS = cmd.get_models('OS')
OS = cmd.get_models('OS', operation_group='task_runs')
return [item.value.lower() for item in OS]


def get_valid_architecture(cmd):
Architecture = cmd.get_models('Architecture')
Architecture = cmd.get_models('Architecture', operation_group='task_runs')
return [item.value.lower() for item in Architecture]


def get_valid_variant(cmd):
Variant = cmd.get_models('Variant')
Variant = cmd.get_models('Variant', operation_group='task_runs')
return [item.value.lower() for item in Variant]


def get_finished_run_status(cmd):
RunStatus = cmd.get_models('RunStatus')
RunStatus = cmd.get_models('RunStatus', operation_group='task_runs')
return [RunStatus.succeeded.value,
RunStatus.failed.value,
RunStatus.canceled.value,
Expand All @@ -66,22 +66,22 @@ def get_finished_run_status(cmd):


def get_succeeded_run_status(cmd):
RunStatus = cmd.get_models('RunStatus')
RunStatus = cmd.get_models('RunStatus', operation_group='task_runs')
return [RunStatus.succeeded.value]


def get_acr_task_models(cmd):
from azure.cli.core.profiles import get_sdk
return get_sdk(cmd.cli_ctx, ResourceType.MGMT_CONTAINERREGISTRY, 'models')
return get_sdk(cmd.cli_ctx, ResourceType.MGMT_CONTAINERREGISTRY, 'models', operation_group='tasks')


def get_succeeded_agentpool_status(cmd):
AgentPoolStatus = cmd.get_models('ProvisioningState')
AgentPoolStatus = cmd.get_models('ProvisioningState', operation_group='agent_pools')
return [AgentPoolStatus.succeeded.value]


def get_finished_agentpool_status(cmd):
AgentPoolStatus = cmd.get_models('ProvisioningState')
AgentPoolStatus = cmd.get_models('ProvisioningState', operation_group='agent_pools')
return [AgentPoolStatus.succeeded.value,
AgentPoolStatus.failed.value,
AgentPoolStatus.canceled.value]
7 changes: 4 additions & 3 deletions src/azure-cli/azure/cli/command_modules/acr/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@


def load_arguments(self, _): # pylint: disable=too-many-statements
SkuName, PasswordName, DefaultAction, PolicyStatus, WebhookAction, WebhookStatus, TaskStatus, \
BaseImageTriggerType, RunStatus, SourceRegistryLoginMode, UpdateTriggerPayloadType, \
SkuName, PasswordName, DefaultAction, PolicyStatus, WebhookAction, WebhookStatus, \
TokenStatus, ZoneRedundancy = self.get_models(
'SkuName', 'PasswordName', 'DefaultAction', 'PolicyStatus', 'WebhookAction', 'WebhookStatus',
'TaskStatus', 'BaseImageTriggerType', 'RunStatus', 'SourceRegistryLoginMode', 'UpdateTriggerPayloadType',
'TokenStatus', 'ZoneRedundancy')
TaskStatus, BaseImageTriggerType, SourceRegistryLoginMode, UpdateTriggerPayloadType = self.get_models(
'TaskStatus', 'BaseImageTriggerType', 'SourceRegistryLoginMode', 'UpdateTriggerPayloadType', operation_group='tasks')
RunStatus = self.get_models('RunStatus', operation_group='runs')

with self.argument_context('acr') as c:
c.argument('tags', arg_type=tags_type)
Expand Down
12 changes: 6 additions & 6 deletions src/azure-cli/azure/cli/command_modules/acr/_run_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import time

from azure.core.polling import PollingMethod, LROPoller
from msrest import Deserializer
from msrest.polling import PollingMethod, LROPoller
from msrestazure.azure_exceptions import CloudError

from ._constants import get_acr_task_models, get_finished_run_status, get_succeeded_run_status
Expand All @@ -26,7 +26,7 @@ def deserialize_run(response):
return LROPoller(
client=client,
initial_response=client.get(
resource_group_name, registry_name, run_id, raw=True),
resource_group_name, registry_name, run_id, cls=lambda x, y, z: x),
deserialization_callback=deserialize_run,
polling_method=RunPolling(
cmd=cmd,
Expand All @@ -50,9 +50,9 @@ def __init__(self, cmd, registry_name, run_id, timeout=30):
self.operation_result = None

def initialize(self, client, initial_response, deserialization_callback):
self._client = client
self._client = client._client # pylint: disable=protected-access
self._response = initial_response
self._url = initial_response.request.url
self._url = initial_response.http_request.url
self._deserialize = deserialization_callback

self._set_operation_status(initial_response)
Expand Down Expand Up @@ -86,13 +86,13 @@ def resource(self):

def _set_operation_status(self, response):
RunStatus = self._cmd.get_models('RunStatus')
if response.status_code == 200:
if response.http_response.status_code == 200:
self.operation_result = self._deserialize(response)
self.operation_status = self.operation_result.status or RunStatus.queued.value
return
raise CloudError(response)

def _update_status(self):
self._response = self._client.send(
self._response = self._client._pipeline.run( # pylint: disable=protected-access
self._client.get(self._url), stream=False)
self._set_operation_status(self._response)
29 changes: 15 additions & 14 deletions src/azure-cli/azure/cli/command_modules/acr/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from knack.util import CLIError
from knack.log import get_logger

from msrestazure.azure_exceptions import CloudError
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.commands.parameters import get_resources_in_subscription
from azure.core.exceptions import ResourceNotFoundError

from ._constants import (
REGISTRY_RESOURCE_TYPE,
Expand Down Expand Up @@ -198,7 +198,8 @@ def get_validate_platform(cmd, platform):
"""Gets and validates the Platform from both flags
:param str platform: The name of Platform passed by user in --platform flag
"""
OS, Architecture = cmd.get_models('OS', 'Architecture')
OS, Architecture = cmd.get_models('OS', 'Architecture', operation_group='runs')

# Defaults
platform_os = OS.linux.value
platform_arch = Architecture.amd64.value
Expand Down Expand Up @@ -285,10 +286,14 @@ def get_custom_registry_credentials(cmd,
:param str password: The password for custom registry (plain text or a key vault secret URI)
:param str identity: The task managed identity used for the credential
"""
Credentials, SourceRegistryCredentials, CustomRegistryCredentials, SecretObject, \
SecretObjectType = cmd.get_models(
'Credentials', 'CustomRegistryCredentials', 'SourceRegistryCredentials', 'SecretObject',
'SecretObjectType',
operation_group='tasks')

source_registry_credentials = None
if auth_mode:
SourceRegistryCredentials = cmd.get_models('SourceRegistryCredentials')
source_registry_credentials = SourceRegistryCredentials(
login_mode=auth_mode)

Expand All @@ -301,11 +306,6 @@ def get_custom_registry_credentials(cmd,
if not username and not password:
is_identity_credential = identity is not None

CustomRegistryCredentials, SecretObject, SecretObjectType = cmd.get_models(
'CustomRegistryCredentials',
'SecretObject',
'SecretObjectType')

if not is_remove:
if is_identity_credential:
custom_reg_credential = CustomRegistryCredentials(
Expand All @@ -328,17 +328,15 @@ def get_custom_registry_credentials(cmd,

custom_registries = {login_server: custom_reg_credential}

Credentials = cmd.get_models('Credentials')
return Credentials(
source_registry=source_registry_credentials,
custom_registries=custom_registries
)


def build_timers_info(cmd, schedules):
TimerTrigger, TriggerStatus = cmd.get_models(
'TimerTrigger', 'TriggerStatus')
timer_triggers = []
TriggerStatus, TimerTrigger = cmd.get_models('TriggerStatus', 'TimerTrigger', operation_group='tasks')

# Provide a default name for the timer if no name was provided.
for index, schedule in enumerate(schedules, start=1):
Expand Down Expand Up @@ -507,11 +505,14 @@ def create_default_scope_map(cmd,
raise CLIError('The default scope map was already configured with different repository permissions.' +
'\nPlease use "az acr scope-map update -r {} -n {} --add <REPO> --remove <REPO>" to update.'
.format(registry_name, scope_map_name))
except CloudError:
except ResourceNotFoundError:
pass
logger.info('Creating a scope map "%s" for provided permissions.', scope_map_name)
poller = scope_map_client.create(resource_group_name, registry_name, scope_map_name,
actions, scope_map_description)
scope_map_request = {
'actions': actions,
'scope_map_description': scope_map_description
}
poller = scope_map_client.begin_create(resource_group_name, registry_name, scope_map_name, scope_map_request)
scope_map = LongRunningOperation(cmd.cli_ctx)(poller)
return scope_map

Expand Down
28 changes: 16 additions & 12 deletions src/azure-cli/azure/cli/command_modules/acr/agentpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def acr_agentpool_create(cmd,
registry, resource_group_name = get_registry_by_name(
cmd.cli_ctx, registry_name, resource_group_name)

AgentPool = cmd.get_models('AgentPool')
AgentPool = cmd.get_models('AgentPool', operation_group='agent_pools')

agentpool_create_parameters = AgentPool(
location=registry.location,
Expand All @@ -43,10 +43,10 @@ def acr_agentpool_create(cmd,
)

try:
return client.create(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name,
agent_pool=agentpool_create_parameters)
return client.begin_create(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name,
agent_pool=agentpool_create_parameters)
except ValidationError as e:
raise CLIError(e)

Expand All @@ -61,11 +61,15 @@ def acr_agentpool_update(cmd,
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name)

AgentPoolUpdateParameters = cmd.get_models('AgentPoolUpdateParameters', operation_group='agent_pools')

update_parameters = AgentPoolUpdateParameters(count=count)

try:
return client.update(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name,
count=count)
return client.begin_update(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name,
update_parameters=update_parameters)
except ValidationError as e:
raise CLIError(e)

Expand All @@ -84,9 +88,9 @@ def acr_agentpool_delete(cmd,
user_confirmation("Are you sure you want to delete the agentpool '{}' in registry '{}'?".format(
agent_pool_name, registry_name), yes)
try:
response = client.delete(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name)
response = client.begin_delete(resource_group_name=resource_group_name,
registry_name=registry_name,
agent_pool_name=agent_pool_name)

if no_wait:
logger.warning("Started to delete the agent pool '%s': %s", agent_pool_name, response.status())
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def acr_credential_show(cmd, client, registry_name, resource_group_name=None):

def acr_credential_renew(cmd, client, registry_name, password_name, resource_group_name=None):
registry, resource_group_name = get_registry_by_name(cmd.cli_ctx, registry_name, resource_group_name)

regenerate_credential_parameters = {'name': password_name}
if registry.admin_user_enabled: # pylint: disable=no-member
return client.regenerate_credential(
resource_group_name, registry_name, password_name)
resource_group_name, registry_name, regenerate_credential_parameters)

raise admin_not_enabled_error(registry_name)

Expand Down
Loading

0 comments on commit aaffbc5

Please sign in to comment.