Skip to content

Commit

Permalink
[ACR] Add new commands az acr taskrun show/list/delete for show lis…
Browse files Browse the repository at this point in the history
…t and remove (#11858)
  • Loading branch information
huanwu authored and yugangw-msft committed Jan 24, 2020
1 parent fdf1771 commit 3b133db
Show file tree
Hide file tree
Showing 21 changed files with 1,077 additions and 192 deletions.
2 changes: 2 additions & 0 deletions src/azure-cli/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Release History
**ACR**

* [BREAKING CHANGE] `az acr delete` will prompt
* [BREAKING CHANGE] 'az acr task delete' will prompt
* Add a new command group 'az acr taskrun show/list/delete' for taskrun management

**AppConfig**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def cf_acr_tasks(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).tasks


def cf_acr_taskruns(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).task_runs


def cf_acr_runs(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).runs

Expand Down
2 changes: 2 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
TASK_VALID_VSTS_URLS = ['visualstudio.com', 'dev.azure.com']
TASK_RESOURCE_ID_TEMPLATE = '/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.ContainerRegistry/registries/{reg}/tasks/{name}'

TASKRUN_RESOURCE_TYPE = REGISTRY_RESOURCE_TYPE + '/taskruns'

ACR_TASK_YAML_DEFAULT_NAME = 'acb.yaml'

ACR_CACHED_BUILDER_IMAGES = ('cloudfoundry/cnb:bionic',)
Expand Down
20 changes: 19 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def task_output_format(result):
return _output_format(result, _task_format_group)


def taskrun_output_format(result):
return _output_format(result, _taskrun_format_group)


def build_output_format(result):
return _output_format(result, _build_format_group)

Expand Down Expand Up @@ -184,6 +188,19 @@ def _task_format_group(item):
])


def _taskrun_format_group(item):
return OrderedDict([
('NAME', _get_value(item, 'name')),
('RUN ID', _get_value(item, 'runResult', 'runId')),
('TASK', _get_value(item, 'runResult', 'task')),
('PLATFORM', _get_value(item, 'runResult', 'platform', 'os')),
('STATUS', _get_value(item, 'runResult', 'status')),
('STARTED', _format_datetime(_get_value(item, 'runResult', 'startTime'))),
('DURATION', _get_duration(_get_value(item, 'runResult', 'startTime'),
_get_value(item, 'runResult', 'finishTime')))
])


def _build_format_group(item):
return OrderedDict([
('BUILD ID', _get_value(item, 'buildId')),
Expand All @@ -193,7 +210,8 @@ def _build_format_group(item):
("TRIGGER", _get_build_trigger(_get_value(item, 'imageUpdateTrigger'),
_get_value(item, 'sourceTrigger', 'eventType'))),
('STARTED', _format_datetime(_get_value(item, 'startTime'))),
('DURATION', _get_duration(_get_value(item, 'startTime'), _get_value(item, 'finishTime')))
('DURATION', _get_duration(_get_value(item, 'startTime'),
_get_value(item, 'finishTime')))
])


Expand Down
42 changes: 42 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,48 @@
az acr task update-run -r MyRegistry --run-id runId --no-archive false
"""

helps['acr taskrun'] = """
type: group
short-summary: Manage taskruns using Azure Container Registries.
"""


helps['acr taskrun delete'] = """
type: command
short-summary: Delete a taskrun from an Azure Container Registry.
examples:
- name: Delete a taskrun from an Azure Container Registry.
text: >
az acr taskrun delete -r MyRegistry -n MyTaskRun -g MyResourceGroup
"""

helps['acr taskrun list'] = """
type: command
short-summary: List the taskruns for an Azure Container Registry.
examples:
- name: List taskruns and show the results in a table.
text: >
az acr taskrun list -r MyRegistry -g MyResourceGroup -o table
"""

helps['acr taskrun show'] = """
type: command
short-summary: Get the properties of a named taskrun for an Azure Container Registry.
examples:
- name: Get the properties of a taskrun, displaying the results in a table.
text: >
az acr taskrun show -r MyRegistry -n MyTaskRun -o table
"""

helps['acr taskrun logs'] = """
type: command
short-summary: Show run logs for a particular taskrun.
examples:
- name: Show run logs for a particular taskrun.
text: >
az acr taskrun logs -r MyRegistry -n MyTaskRun
"""

helps['acr token'] = """
type: group
short-summary: Manage tokens for an Azure Container Registry.
Expand Down
7 changes: 6 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
REGISTRY_RESOURCE_TYPE,
WEBHOOK_RESOURCE_TYPE,
REPLICATION_RESOURCE_TYPE,
TASK_RESOURCE_TYPE
TASK_RESOURCE_TYPE,
TASKRUN_RESOURCE_TYPE
)
from ._validators import (
validate_headers,
Expand Down Expand Up @@ -236,6 +237,10 @@ def load_arguments(self, _): # pylint: disable=too-many-statements
c.argument('timer_schedule', options_list=['--schedule'], help="The schedule of the timer trigger represented as a cron expression.")
c.argument('enabled', help="Indicates whether the timer trigger is enabled.", arg_type=get_three_state_flag())

with self.argument_context('acr taskrun') as c:
c.argument('registry_name', options_list=['--registry', '-r'])
c.argument('taskrun_name', options_list=['--name', '-n'], help='The name of the taskrun.', completer=get_resource_name_completion_list(TASKRUN_RESOURCE_TYPE))

with self.argument_context('acr helm') as c:
c.argument('resource_group_name', deprecate_info=c.deprecate(hide=True))
c.argument('repository', help=argparse.SUPPRESS)
Expand Down
15 changes: 15 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
replication_output_format,
build_output_format,
task_output_format,
taskrun_output_format,
run_output_format,
helm_list_output_format,
helm_show_output_format,
Expand All @@ -29,6 +30,7 @@
cf_acr_replications,
cf_acr_webhooks,
cf_acr_tasks,
cf_acr_taskruns,
cf_acr_runs,
cf_acr_scope_maps,
cf_acr_tokens,
Expand Down Expand Up @@ -105,6 +107,12 @@ def load_command_table(self, _): # pylint: disable=too-many-statements
client_factory=cf_acr_tasks
)

acr_taskrun_util = CliCommandType(
operations_tmpl='azure.cli.command_modules.acr.taskrun#{}',
table_transformer=taskrun_output_format,
client_factory=cf_acr_taskruns
)

acr_helm_util = CliCommandType(
operations_tmpl='azure.cli.command_modules.acr.helm#{}'
)
Expand Down Expand Up @@ -237,6 +245,13 @@ def load_command_table(self, _): # pylint: disable=too-many-statements
g.command('logs', 'acr_task_logs', client_factory=cf_acr_runs,
table_transformer=None)

with self.command_group('acr taskrun', acr_taskrun_util, is_preview=True) as g:
g.command('list', 'acr_taskrun_list')
g.command('delete', 'acr_taskrun_delete')
g.command('show', 'acr_taskrun_show')
g.command('logs', 'acr_taskrun_logs', client_factory=cf_acr_runs,
table_transformer=None)

with self.command_group('acr config content-trust', acr_policy_util) as g:
g.command('show', 'acr_config_content_trust_show')
g.command('update', 'acr_config_content_trust_update')
Expand Down
9 changes: 7 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=C0302
import re
from msrest.exceptions import ValidationError
from knack.log import get_logger
Expand All @@ -17,7 +18,8 @@
build_timers_info,
remove_timer_trigger,
get_task_id_from_task_name,
prepare_source_location
prepare_source_location,
user_confirmation
)
from ._stream_utils import stream_logs
from ._constants import (
Expand Down Expand Up @@ -283,9 +285,12 @@ def acr_task_delete(cmd,
client,
task_name,
registry_name,
resource_group_name=None):
resource_group_name=None,
yes=False):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASK_NOT_SUPPORTED)

user_confirmation("Are you sure you want to delete the task '{}' ".format(task_name), yes)
return client.delete(resource_group_name, registry_name, task_name)


Expand Down
62 changes: 62 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/taskrun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from ._stream_utils import stream_logs
from ._utils import (
user_confirmation,
validate_managed_registry
)


TASKRUN_NOT_SUPPORTED = 'TaskRun is only supported for managed registries.'


def acr_taskrun_show(cmd,
client,
taskrun_name,
registry_name,
resource_group_name=None):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)
return client.get(resource_group_name, registry_name, taskrun_name)


def acr_taskrun_list(cmd,
client,
registry_name,
resource_group_name=None):

_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)
return client.list(resource_group_name, registry_name)


def acr_taskrun_delete(cmd,
client,
taskrun_name,
registry_name,
resource_group_name=None,
yes=False):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)

user_confirmation("Are you sure you want to delete the taskrun '{}' ".format(taskrun_name), yes)
return client.delete(resource_group_name, registry_name, taskrun_name)


def acr_taskrun_logs(cmd,
client, # cf_acr_runs
registry_name,
taskrun_name,
resource_group_name=None):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)

from ._client_factory import cf_acr_taskruns
client_taskruns = cf_acr_taskruns(cmd.cli_ctx)
response = client_taskruns.get(resource_group_name, registry_name, taskrun_name)
run_id = response.run_result.run_id

return stream_logs(client, run_id, registry_name, resource_group_name)
Loading

0 comments on commit 3b133db

Please sign in to comment.