Skip to content

Commit 18ec10c

Browse files
Maddageokala
authored andcommitted
CYBL-626 Add more summaries and sub-fields
1 parent a7fab5a commit 18ec10c

1 file changed

Lines changed: 224 additions & 15 deletions

File tree

cloudify_cli/commands/summary.py

Lines changed: 224 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
from .. import utils
22
from ..cli import cfy
3+
from ..logger import get_global_json_output
34
from ..table import print_data
45
import click
56

67

8+
BASE_SUMMARY_FIELDS = [
9+
'tenant_name',
10+
'visibility',
11+
]
12+
DEPLOYMENTS_SUMMARY_FIELDS = [
13+
'blueprint_id',
14+
] + BASE_SUMMARY_FIELDS
15+
NODES_SUMMARY_FIELDS = [
16+
'deployment_id',
17+
] + BASE_SUMMARY_FIELDS
18+
NODE_INSTANCES_SUMMARY_FIELDS = [
19+
'deployment_id',
20+
'node_id',
21+
'state',
22+
'host_id',
23+
] + BASE_SUMMARY_FIELDS
24+
EXECUTIONS_SUMMARY_FIELDS = [
25+
'status',
26+
'blueprint_id',
27+
'deployment_id',
28+
'workflow_id',
29+
] + BASE_SUMMARY_FIELDS
30+
BLUEPRINTS_SUMMARY_FIELDS = BASE_SUMMARY_FIELDS
31+
32+
733
@cfy.group(name='summary')
834
@cfy.options.common_options
935
@cfy.assert_manager_active()
@@ -13,49 +39,127 @@ def summary():
1339
pass
1440

1541

42+
def _structure_results_and_columns(results, target_field, sub_field,
43+
summary_type):
44+
"""Restructure the results returned from the rest client.
45+
46+
This is needed in case sub-fields are provided, as sub-fields will result
47+
in output that looks like:
48+
[
49+
{
50+
"<target_field>": "<value>",
51+
"<summary_type>": <total count>,
52+
"by <sub_field>": [
53+
{
54+
"<sub_field>": "<sub_field value>",
55+
"<summary_type>": <count>,
56+
},
57+
... more sub-field results for this value of target_field ...
58+
],
59+
},
60+
... more results ...
61+
]
62+
63+
For compatibility with the CLI output tools, we want to turn this into:
64+
[
65+
{
66+
"<target_field>": "<value>",
67+
"<sub_field>": "<sub_field value>",
68+
"<summary_type>": <count>,
69+
},
70+
... more sub-field results for this value of target_field ...
71+
{
72+
"<target_field>": "<value>",
73+
"<sub_field>": "<TOTAL if not json, empty if json>",
74+
"<summary_type>": <count>,
75+
},
76+
... sub-fields followed by totals for other target_field values ...
77+
]
78+
"""
79+
if sub_field:
80+
columns = [target_field, sub_field, summary_type]
81+
structured_result = []
82+
for result in results:
83+
for sub_result in result['by ' + sub_field]:
84+
structured_result.append(
85+
{
86+
target_field: result[target_field],
87+
sub_field: sub_result[sub_field],
88+
summary_type: sub_result[summary_type],
89+
}
90+
)
91+
structured_result.append(
92+
{
93+
target_field: result[target_field],
94+
sub_field: '' if get_global_json_output() else 'TOTAL',
95+
summary_type: result[summary_type],
96+
}
97+
)
98+
else:
99+
columns = [target_field, summary_type]
100+
structured_result = results
101+
return columns, structured_result
102+
103+
16104
@summary.command(name='nodes',
17105
short_help='Retrieve summary of node details [manager only]')
18-
@cfy.argument('target_field', type=click.Choice(['deployment_id']))
106+
@cfy.argument('target_field', type=click.Choice(NODES_SUMMARY_FIELDS))
107+
@cfy.argument('sub_field', type=click.Choice(NODES_SUMMARY_FIELDS),
108+
default=None, required=False)
19109
@cfy.options.common_options
20110
@cfy.options.tenant_name(required=False, resource_name_for_help='summary')
111+
@cfy.options.all_tenants
21112
@cfy.pass_logger
22113
@cfy.pass_client()
23-
def nodes(target_field, logger, client, tenant_name):
114+
def nodes(target_field, sub_field, logger, client, tenant_name, all_tenants):
24115
"""Retrieve summary of nodes, e.g. a count of each node with the same
25116
deployment ID.
26117
27118
`TARGET_FIELD` is the field to summarise nodes on.
28-
Valid fields: deployment_id
29119
"""
30120
utils.explicit_tenant_name_message(tenant_name, logger)
31121
logger.info('Retrieving summary of nodes on field {field}'.format(
32122
field=target_field))
33123

34124
summary = client.summary.nodes.get(
35125
_target_field=target_field,
126+
_sub_field=sub_field,
127+
_all_tenants=all_tenants,
36128
)
37129

38-
print_data(
39-
[target_field, 'nodes'],
130+
columns, items = _structure_results_and_columns(
40131
summary.items,
132+
target_field,
133+
sub_field,
134+
'nodes',
135+
)
136+
137+
print_data(
138+
columns,
139+
items,
41140
'Node summary by {field}'.format(field=target_field),
42141
)
43142

44143

45144
@summary.command(name='node_instances',
46145
short_help='Retrieve summary of node instance details '
47146
'[manager only]')
48-
@cfy.argument('target_field', type=click.Choice(['deployment_id', 'node_id']))
147+
@cfy.argument('target_field',
148+
type=click.Choice(NODE_INSTANCES_SUMMARY_FIELDS))
149+
@cfy.argument('sub_field',
150+
type=click.Choice(NODE_INSTANCES_SUMMARY_FIELDS),
151+
default=None, required=False)
49152
@cfy.options.common_options
50153
@cfy.options.tenant_name(required=False, resource_name_for_help='summary')
154+
@cfy.options.all_tenants
51155
@cfy.pass_logger
52156
@cfy.pass_client()
53-
def node_instances(target_field, logger, client, tenant_name):
157+
def node_instances(target_field, sub_field, logger, client, tenant_name,
158+
all_tenants):
54159
"""Retrieve summary of node instances, e.g. a count of each node instance
55160
with the same deployment ID.
56161
57162
`TARGET_FIELD` is the field to summarise node instances on.
58-
Valid fields: deployment_id, node_id
59163
"""
60164
utils.explicit_tenant_name_message(tenant_name, logger)
61165
logger.info(
@@ -66,40 +170,145 @@ def node_instances(target_field, logger, client, tenant_name):
66170

67171
summary = client.summary.node_instances.get(
68172
_target_field=target_field,
173+
_sub_field=sub_field,
174+
_all_tenants=all_tenants,
69175
)
70176

71-
print_data(
72-
[target_field, 'node_instances'],
177+
columns, items = _structure_results_and_columns(
73178
summary.items,
179+
target_field,
180+
sub_field,
181+
'node_instances',
182+
)
183+
184+
print_data(
185+
columns,
186+
items,
74187
'Node instance summary by {field}'.format(field=target_field),
75188
)
76189

77190

78191
@summary.command(name='deployments',
79192
short_help='Retrieve summary of deployment details '
80193
'[manager only]')
81-
@cfy.argument('target_field', type=click.Choice(['blueprint_id']))
194+
@cfy.argument('target_field', type=click.Choice(DEPLOYMENTS_SUMMARY_FIELDS))
195+
@cfy.argument('sub_field', type=click.Choice(DEPLOYMENTS_SUMMARY_FIELDS),
196+
default=None, required=False)
82197
@cfy.options.common_options
83198
@cfy.options.tenant_name(required=False, resource_name_for_help='summary')
199+
@cfy.options.all_tenants
84200
@cfy.pass_logger
85201
@cfy.pass_client()
86-
def deployments(target_field, logger, client, tenant_name):
202+
def deployments(target_field, sub_field, logger, client, tenant_name,
203+
all_tenants):
87204
"""Retrieve summary of deployments, e.g. a count of each deployment with
88205
the same blueprint ID.
89206
90207
`TARGET_FIELD` is the field to summarise deployments on.
91-
Valid fields: blueprint_id
92208
"""
93209
utils.explicit_tenant_name_message(tenant_name, logger)
94210
logger.info('Retrieving summary of deployments on field {field}'.format(
95211
field=target_field))
96212

97213
summary = client.summary.deployments.get(
98214
_target_field=target_field,
215+
_sub_field=sub_field,
216+
_all_tenants=all_tenants,
99217
)
100218

101-
print_data(
102-
[target_field, 'deployments'],
219+
columns, items = _structure_results_and_columns(
103220
summary.items,
221+
target_field,
222+
sub_field,
223+
'deployments',
224+
)
225+
226+
print_data(
227+
columns,
228+
items,
104229
'Deployment summary by {field}'.format(field=target_field),
105230
)
231+
232+
233+
@summary.command(name='executions',
234+
short_help='Retrieve summary of execution details '
235+
'[manager only]')
236+
@cfy.argument('target_field', type=click.Choice(EXECUTIONS_SUMMARY_FIELDS))
237+
@cfy.argument('sub_field', type=click.Choice(EXECUTIONS_SUMMARY_FIELDS),
238+
default=None, required=False)
239+
@cfy.options.common_options
240+
@cfy.options.tenant_name(required=False, resource_name_for_help='summary')
241+
@cfy.options.all_tenants
242+
@cfy.pass_logger
243+
@cfy.pass_client()
244+
def executions(target_field, sub_field, logger, client, tenant_name,
245+
all_tenants):
246+
"""Retrieve summary of executions, e.g. a count of each execution with
247+
the same deployment ID.
248+
249+
`TARGET_FIELD` is the field to summarise executions on.
250+
"""
251+
utils.explicit_tenant_name_message(tenant_name, logger)
252+
logger.info('Retrieving summary of executions on field {field}'.format(
253+
field=target_field))
254+
255+
summary = client.summary.executions.get(
256+
_target_field=target_field,
257+
_sub_field=sub_field,
258+
_all_tenants=all_tenants,
259+
)
260+
261+
columns, items = _structure_results_and_columns(
262+
summary.items,
263+
target_field,
264+
sub_field,
265+
'executions',
266+
)
267+
268+
print_data(
269+
columns,
270+
items,
271+
'Execution summary by {field}'.format(field=target_field),
272+
)
273+
274+
275+
@summary.command(name='blueprints',
276+
short_help='Retrieve summary of blueprint details '
277+
'[manager only]')
278+
@cfy.argument('target_field', type=click.Choice(BLUEPRINTS_SUMMARY_FIELDS))
279+
@cfy.argument('sub_field', type=click.Choice(BLUEPRINTS_SUMMARY_FIELDS),
280+
default=None, required=False)
281+
@cfy.options.common_options
282+
@cfy.options.tenant_name(required=False, resource_name_for_help='summary')
283+
@cfy.options.all_tenants
284+
@cfy.pass_logger
285+
@cfy.pass_client()
286+
def blueprints(target_field, sub_field, logger, client, tenant_name,
287+
all_tenants):
288+
"""Retrieve summary of blueprints, e.g. a count of each blueprint with
289+
the same tenant name.
290+
291+
`TARGET_FIELD` is the field to summarise blueprints on.
292+
"""
293+
utils.explicit_tenant_name_message(tenant_name, logger)
294+
logger.info('Retrieving summary of blueprints on field {field}'.format(
295+
field=target_field))
296+
297+
summary = client.summary.blueprints.get(
298+
_target_field=target_field,
299+
_sub_field=sub_field,
300+
_all_tenants=all_tenants,
301+
)
302+
303+
columns, items = _structure_results_and_columns(
304+
summary.items,
305+
target_field,
306+
sub_field,
307+
'blueprints',
308+
)
309+
310+
print_data(
311+
columns,
312+
items,
313+
'Blueprint summary by {field}'.format(field=target_field),
314+
)

0 commit comments

Comments
 (0)