Querying node groups: add client/gnt_group.py
[ganeti-local] / lib / client / gnt_group.py
1 #
2 #
3
4 # Copyright (C) 2010 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 """Node group related commands"""
22
23 # pylint: disable-msg=W0401,W0614
24 # W0401: Wildcard import ganeti.cli
25 # W0614: Unused import %s from wildcard import (since we need cli)
26
27 from ganeti.cli import *
28 from ganeti import compat
29
30
31 #: default list of fields for L{ListGroups}
32 _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt"]
33
34
35 #: headers (and full field list) for L{ListGroups}
36 _LIST_HEADERS = {
37   "name": "Group", "uuid": "UUID",
38   "node_cnt": "Nodes", "node_list": "NodeList",
39   "pinst_cnt": "Instances", "pinst_list": "InstanceList",
40 }
41
42
43 def ListGroups(opts, args):
44   """List node groups and their properties.
45
46   @param opts: the command line options selected by the user
47   @type args: list
48   @param args: groups to list, or empty for all
49   @rtype: int
50   @return: the desired exit code
51
52   """
53   desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
54
55   output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
56
57   if opts.no_headers:
58     headers = None
59   else:
60     headers = _LIST_HEADERS
61
62   int_type_fields = frozenset(["node_cnt", "pinst_cnt"])
63   list_type_fields = frozenset(["node_list", "pinst_list"])
64
65   for row in output:
66     for idx, field in enumerate(desired_fields):
67       val = row[idx]
68
69       if field in list_type_fields:
70         val = ",".join(val)
71       elif opts.roman_integers and field in int_type_fields:
72         val = compat.TryToRoman(val)
73       elif val is None:
74         val = "?"
75
76       row[idx] = str(val)
77
78   data = GenerateTable(separator=opts.separator, headers=headers,
79                        fields=desired_fields, data=output)
80
81   for line in data:
82     ToStdout(line)
83
84   return 0
85
86
87 commands = {
88   "list": (
89     ListGroups, ARGS_MANY_GROUPS,
90     [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
91     "[<group_name>...]", "Lists the node groups in the cluster."),
92 }
93
94
95 def Main():
96   return GenericMain(commands)