summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/cli/commands/nodes.py
blob: 30f1dd44787c4a6a41de8fbcbf3a197b73ad4409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
CLI ``nodes`` sub-commands.
"""

from .. import table
from .. import utils
from ..core import aria


NODE_COLUMNS = ['id', 'name', 'service_name', 'node_template_name', 'state']


@aria.group(name='nodes')
@aria.options.verbose()
def nodes():
    """
    Manage services' nodes
    """
    pass


@nodes.command(name='show',
               short_help='Show information for a node')
@aria.argument('node_id')
@aria.options.verbose()
@aria.pass_model_storage
@aria.pass_logger
def show(node_id, model_storage, logger):
    """
    Show information for a node

    NODE_ID is the unique node ID.
    """
    logger.info('Showing node {0}'.format(node_id))
    node = model_storage.node.get(node_id)

    table.print_data(NODE_COLUMNS, node, 'Node:', col_max_width=50)

    # print node attributes
    logger.info('Node attributes:')
    if node.attributes:
        for param_name, param in node.attributes.iteritems():
            logger.info('\t{0}: {1}'.format(param_name, param.value))
    else:
        logger.info('\tNo attributes')


@nodes.command(name='list',
               short_help='List node')
@aria.options.service_name(required=False)
@aria.options.sort_by('service_name')
@aria.options.descending
@aria.options.verbose()
@aria.pass_model_storage
@aria.pass_logger
def list(service_name,
         sort_by,
         descending,
         model_storage,
         logger):
    """
    List nodes

    If SERVICE_NAME is provided, list nodes for that service. Otherwise, list nodes for all
    services.
    """
    if service_name:
        logger.info('Listing nodes for service {0}...'.format(service_name))
        service = model_storage.service.get_by_name(service_name)
        filters = dict(service=service)
    else:
        logger.info('Listing all nodes...')
        filters = {}

    nodes_list = model_storage.node.list(
        filters=filters,
        sort=utils.storage_sort_param(sort_by, descending))

    table.print_data(NODE_COLUMNS, nodes_list, 'Nodes:')