Skip to content

Commit

Permalink
Pools, Agents and Queues retrieval commands (#661)
Browse files Browse the repository at this point in the history
* Adding pool and queue commands
  • Loading branch information
atbagga committed Jun 14, 2019
1 parent 8549a08 commit cbb559a
Show file tree
Hide file tree
Showing 9 changed files with 1,609 additions and 2 deletions.
64 changes: 64 additions & 0 deletions azure-devops/azext_devops/dev/pipelines/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,67 @@ def _transform_pipeline_run_row(row):
table_row['Queued Time'] = str(queued_time.date()) + ' ' + str(queued_time.time())
table_row['Reason'] = row['reason']
return table_row


def transform_pipelines_pools_table_output(result):
table_output = []
for item in result:
table_output.append(_transform_pipeline_pool_row(item))
return table_output


def transform_pipelines_pool_table_output(result):
table_output = [_transform_pipeline_pool_row(result)]
return table_output


def _transform_pipeline_pool_row(row):
table_row = OrderedDict()
table_row['ID'] = row['id']
table_row['Name'] = row['name']
table_row['Is Hosted'] = row['isHosted']
table_row['Pool Type'] = row['poolType']
return table_row


def transform_pipelines_agents_table_output(result):
table_output = []
for item in result:
table_output.append(_transform_pipeline_agent_row(item))
return table_output


def transform_pipelines_agent_table_output(result):
table_output = [_transform_pipeline_agent_row(result)]
return table_output


def _transform_pipeline_agent_row(row):
table_row = OrderedDict()
table_row['ID'] = row['id']
table_row['Name'] = row['name']
table_row['Is Enabled'] = row['enabled']
table_row['Status'] = row['status']
table_row['Version'] = row['version']
return table_row


def transform_pipelines_queues_table_output(result):
table_output = []
for item in result:
table_output.append(_transform_pipeline_queue_row(item))
return table_output


def transform_pipelines_queue_table_output(result):
table_output = [_transform_pipeline_queue_row(result)]
return table_output


def _transform_pipeline_queue_row(row):
table_row = OrderedDict()
table_row['ID'] = row['id']
table_row['Name'] = row['name']
table_row['Pool IsHosted'] = row['pool']['isHosted']
table_row['Pool Type'] = row['pool']['poolType']
return table_row
20 changes: 19 additions & 1 deletion azure-devops/azext_devops/dev/pipelines/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def load_pipelines_help():
long-summary:
"""

helps['pipelines pool'] = """
type: group
short-summary: (PREVIEW) Manage agent pools.
long-summary:
"""

helps['pipelines agent'] = """
type: group
short-summary: (PREVIEW) Manage agents.
long-summary:
"""

helps['pipelines queue'] = """
type: group
short-summary: (PREVIEW) Manage agent queues.
long-summary:
"""

helps['pipelines build tag'] = """
type: group
short-summary: Manage build tags.
Expand Down Expand Up @@ -84,7 +102,7 @@ def load_pipelines_help():
- name: Create an Azure Pipeline for a repository hosted in a Azure Repo in the same project
text: |
az pipelines create --name 'ContosoBuild' --description 'Pipeline for contoso project'
--repository SampleRepoName --branch master --type tfsgit
--repository SampleRepoName --branch master --repository-type tfsgit
- name: Create an Azure Pipeline for a repository with the pipeline yaml already checked in into the repository
text: |
Expand Down
127 changes: 127 additions & 0 deletions azure-devops/azext_devops/dev/pipelines/agent_pool_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.log import get_logger
from azext_devops.dev.common.services import (get_new_task_agent_client,
resolve_instance,
resolve_instance_and_project)

logger = get_logger(__name__)


def list_pools(pool_name=None, pool_type=None, action=None, properties=None, organization=None, detect=None):
""" (PREVIEW) List agent pools
:param pool_name: Filter the list with matching pool name.
:type pool_name: str
:param pool_type: Filter the list with type of pool.
:type pool_type: str
:param action: Filter the list with user action permitted.
:type action: str
:param properties: Filter the list by agent pool properties. Comma separated list.
:type properties: str
"""
organization = resolve_instance(organization=organization, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
if properties:
properties = list(map(str, properties.split(',')))
return task_agent_client.get_agent_pools(
pool_name=pool_name, properties=properties, pool_type=pool_type, action_filter=action)


def show_pool(pool_id, action=None, properties=None, organization=None, detect=None):
""" (PREVIEW) Show agent pool details
:param pool_id: Id of the pool to list the details.
:type pool_id: int
:param action: Filter the list with user action permitted.
:type action: str
:param properties: Filter the list by agent pool properties. Comma separated list.
:type properties: str
"""
organization = resolve_instance(organization=organization, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
if properties:
properties = list(map(str, properties.split(',')))
return task_agent_client.get_agent_pool(pool_id=pool_id, properties=properties, action_filter=action)


def list_agents(pool_id, agent_name=None, include_capabilities=None, include_assigned_request=None, properties=None,
include_last_completed_request=None, demands=None, organization=None, detect=None):
""" (PREVIEW) Get a list of agents in a pool
:param pool_id: The agent pool containing the agents.
:type pool_id: int
:param agent_name: Filter on agent name.
:type agent_name: str
:param include_capabilities: Whether to include the agents' capabilities in the response.
:type include_capabilities: bool
:param include_assigned_request: Whether to include details about the agents' current work.
:type include_assigned_request: bool
:param include_last_completed_request: Whether to include details about the agents' most recent completed work.
:type include_last_completed_request: bool
:param properties: Filter which custom properties will be returned. Comma separated list.
:type properties: str
:param demands: Filter by demands the agents can satisfy. Comma separated list.
:type demands: str
"""
organization = resolve_instance(organization=organization, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
if properties:
properties = list(map(str, properties.split(',')))
if demands:
demands = list(map(str, demands.split(',')))
return task_agent_client.get_agents(
pool_id=pool_id, agent_name=agent_name, include_capabilities=include_capabilities,
include_last_completed_request=include_last_completed_request,
include_assigned_request=include_assigned_request, property_filters=properties, demands=demands)


def show_agent(pool_id, agent_id, include_capabilities=None, include_assigned_request=None, properties=None,
include_last_completed_request=None, organization=None, detect=None):
""" (PREVIEW) Show agent details
:param pool_id: The agent pool containing the agent.
:type pool_id: int
:param agent_id: The agent ID to get information about.
:type agent_id: str
:param include_capabilities: Whether to include the agents' capabilities in the response.
:type include_capabilities: bool
:param include_assigned_request: Whether to include details about the agents' current work.
:type include_assigned_request: bool
:param include_last_completed_request: Whether to include details about the agents' most recent completed work.
:type include_last_completed_request: bool
:param properties: Filter which custom properties will be returned. Comma separated list.
:type properties: [str]
"""
organization = resolve_instance(organization=organization, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
if properties:
properties = list(map(str, properties.split(',')))
return task_agent_client.get_agent(
pool_id=pool_id, agent_id=agent_id, include_capabilities=include_capabilities,
include_assigned_request=include_assigned_request,
include_last_completed_request=include_last_completed_request, property_filters=properties)


def list_queues(queue_name=None, action=None, project=None, organization=None, detect=None):
""" (PREVIEW) List agent queues
:param queue_name: Filter the list with matching queue name regex.
e.g. *ubuntu* for queue with name 'Hosted Ubuntu 1604'
:type queue_name: str
:param action: Filter by whether the calling user has use or manage permissions
:type action: str
"""
organization, project = resolve_instance_and_project(organization=organization, project=project, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
return task_agent_client.get_agent_queues(project=project, queue_name=queue_name, action_filter=action)


def show_queue(queue_id, action=None, project=None, organization=None, detect=None):
""" (PREVIEW) Show details of agent queue
:param queue_id: Id of the agent queue to get information about.
:type queue_id: str
:param action: Filter by whether the calling user has use or manage permissions
:type action: str
"""
organization, project = resolve_instance_and_project(organization=organization, project=project, detect=detect)
task_agent_client = get_new_task_agent_client(organization=organization)
return task_agent_client.get_agent_queue(project=project, queue_id=queue_id, action_filter=action)
20 changes: 20 additions & 0 deletions azure-devops/azext_devops/dev/pipelines/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# --------------------------------------------------------------------------------------------

from knack.arguments import enum_choice_list
from azure.cli.core.commands.parameters import get_three_state_flag

_BUILD_REASON_VALUES = ['all', 'batchedCI', 'buildCompletion', 'checkInShelveset',
'individualCI', 'manual', 'pullRequest', 'schedule',
Expand All @@ -18,6 +19,10 @@
_PIPELINES_RUNS_QUERY_ORDER = ['FinishTimeAsc', 'FinishTimeDesc', 'StartTimeAsc', 'StartTimeDesc',
'QueueTimeAsc', 'QueueTimeDesc']

_AGENT_POOL_TYPES = ['automation', 'deployment']

_AGENT_ACTION_FILTER_TYPES = ['use', 'manage', 'none']


def load_build_arguments(self, _):
with self.argument_context('pipelines build list') as context:
Expand Down Expand Up @@ -69,3 +74,18 @@ def load_build_arguments(self, _):

with self.argument_context('pipelines create') as context:
context.argument('repository_type', choices=['tfsgit', 'github'], type=str.lower)

with self.argument_context('pipelines pool') as context:
context.argument('pool_id', options_list=('--pool-id', '--id'))
context.argument('action', **enum_choice_list(_AGENT_ACTION_FILTER_TYPES))
context.argument('pool_type', **enum_choice_list(_AGENT_POOL_TYPES))

with self.argument_context('pipelines agent') as context:
context.argument('agent_id', options_list=('--agent-id', '--id'))
context.argument('include_capabilities', arg_type=get_three_state_flag())
context.argument('include_assigned_request', arg_type=get_three_state_flag())
context.argument('include_last_completed_request', arg_type=get_three_state_flag())

with self.argument_context('pipelines queue') as context:
context.argument('queue_id', options_list=('--queue-id', '--id'))
context.argument('action', **enum_choice_list(_AGENT_ACTION_FILTER_TYPES))
25 changes: 24 additions & 1 deletion azure-devops/azext_devops/dev/pipelines/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
transform_release_table_output,
transform_release_definitions_table_output,
transform_release_definition_table_output,
transform_runs_artifact_table_output)
transform_runs_artifact_table_output,
transform_pipelines_pools_table_output,
transform_pipelines_pool_table_output,
transform_pipelines_agents_table_output,
transform_pipelines_agent_table_output,
transform_pipelines_queues_table_output,
transform_pipelines_queue_table_output)

buildOps = CliCommandType(
operations_tmpl='azext_devops.dev.pipelines.build#{}',
Expand Down Expand Up @@ -65,6 +71,11 @@
exception_handler=azure_devops_exception_handler
)

pipelineAgentPoolQueueOps = CliCommandType(
operations_tmpl='azext_devops.dev.pipelines.agent_pool_queue#{}',
exception_handler=azure_devops_exception_handler
)


def load_build_commands(self, _):
with self.command_group('pipelines', command_type=pipelineCreateOps) as g:
Expand Down Expand Up @@ -118,3 +129,15 @@ def load_build_commands(self, _):
with self.command_group('pipelines runs', command_type=pipelinesRunOps) as g:
g.command('list', 'pipeline_run_list', table_transformer=transform_pipeline_runs_table_output)
g.command('show', 'pipeline_run_show', table_transformer=transform_pipeline_run_table_output)

with self.command_group('pipelines pool', command_type=pipelineAgentPoolQueueOps) as g:
g.command('list', 'list_pools', table_transformer=transform_pipelines_pools_table_output)
g.command('show', 'show_pool', table_transformer=transform_pipelines_pool_table_output)

with self.command_group('pipelines agent', command_type=pipelineAgentPoolQueueOps) as g:
g.command('list', 'list_agents', table_transformer=transform_pipelines_agents_table_output)
g.command('show', 'show_agent', table_transformer=transform_pipelines_agent_table_output)

with self.command_group('pipelines queue', command_type=pipelineAgentPoolQueueOps) as g:
g.command('list', 'list_queues', table_transformer=transform_pipelines_queues_table_output)
g.command('show', 'show_queue', table_transformer=transform_pipelines_queue_table_output)
Loading

0 comments on commit cbb559a

Please sign in to comment.