1 |
|
# Copyright ETSI Contributors and Others. |
2 |
|
# All Rights Reserved. |
3 |
|
# |
4 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
5 |
|
# not use this file except in compliance with the License. You may obtain |
6 |
|
# a copy of the License at |
7 |
|
# |
8 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
# |
10 |
|
# Unless required by applicable law or agreed to in writing, software |
11 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 |
|
# License for the specific language governing permissions and limitations |
14 |
|
# under the License. |
15 |
|
|
16 |
1 |
import click |
17 |
1 |
from osmclient.cli_commands import utils |
18 |
1 |
from prettytable import PrettyTable |
19 |
1 |
import yaml |
20 |
1 |
import json |
21 |
1 |
from datetime import datetime |
22 |
1 |
import logging |
23 |
|
|
24 |
1 |
logger = logging.getLogger("osmclient") |
25 |
|
|
26 |
|
|
27 |
1 |
@click.command( |
28 |
|
name="ns-op-list", short_help="shows the history of operations over a NS instance" |
29 |
|
) |
30 |
1 |
@click.argument("name") |
31 |
1 |
@click.option( |
32 |
|
"--long", is_flag=True, help="get more details of the NS operation (date, )." |
33 |
|
) |
34 |
1 |
@click.pass_context |
35 |
1 |
def ns_op_list(ctx, name, long): |
36 |
|
"""shows the history of operations over a NS instance |
37 |
|
|
38 |
|
NAME: name or ID of the NS instance |
39 |
|
""" |
40 |
|
|
41 |
0 |
def formatParams(params): |
42 |
0 |
if params["lcmOperationType"] == "instantiate": |
43 |
0 |
params.pop("nsDescription") |
44 |
0 |
params.pop("nsName") |
45 |
0 |
params.pop("nsdId") |
46 |
0 |
params.pop("nsr_id") |
47 |
0 |
elif params["lcmOperationType"] == "action": |
48 |
0 |
params.pop("primitive") |
49 |
0 |
params.pop("lcmOperationType") |
50 |
0 |
params.pop("nsInstanceId") |
51 |
0 |
return params |
52 |
|
|
53 |
0 |
logger.debug("") |
54 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
55 |
0 |
resp = ctx.obj.ns.list_op(name) |
56 |
|
|
57 |
0 |
if long: |
58 |
0 |
table = PrettyTable( |
59 |
|
[ |
60 |
|
"id", |
61 |
|
"operation", |
62 |
|
"action_name", |
63 |
|
"operation_params", |
64 |
|
"status", |
65 |
|
"date", |
66 |
|
"last update", |
67 |
|
"detail", |
68 |
|
] |
69 |
|
) |
70 |
|
else: |
71 |
0 |
table = PrettyTable( |
72 |
|
["id", "operation", "action_name", "status", "date", "detail"] |
73 |
|
) |
74 |
|
|
75 |
|
# print(yaml.safe_dump(resp)) |
76 |
0 |
for op in resp: |
77 |
0 |
action_name = "N/A" |
78 |
0 |
if op["lcmOperationType"] == "action": |
79 |
0 |
action_name = op["operationParams"]["primitive"] |
80 |
0 |
detail = "-" |
81 |
0 |
if op["operationState"] == "PROCESSING": |
82 |
0 |
if op.get("queuePosition") is not None and op.get("queuePosition") > 0: |
83 |
0 |
detail = "In queue. Current position: {}".format(op["queuePosition"]) |
84 |
0 |
elif op["lcmOperationType"] in ("instantiate", "terminate"): |
85 |
0 |
if op["stage"]: |
86 |
0 |
detail = op["stage"] |
87 |
0 |
elif op["operationState"] in ("FAILED", "FAILED_TEMP"): |
88 |
0 |
detail = op.get("errorMessage", "-") |
89 |
0 |
date = datetime.fromtimestamp(op["startTime"]).strftime("%Y-%m-%dT%H:%M:%S") |
90 |
0 |
last_update = datetime.fromtimestamp(op["statusEnteredTime"]).strftime( |
91 |
|
"%Y-%m-%dT%H:%M:%S" |
92 |
|
) |
93 |
0 |
if long: |
94 |
0 |
table.add_row( |
95 |
|
[ |
96 |
|
op["id"], |
97 |
|
op["lcmOperationType"], |
98 |
|
action_name, |
99 |
|
utils.wrap_text( |
100 |
|
text=json.dumps(formatParams(op["operationParams"]), indent=2), |
101 |
|
width=50, |
102 |
|
), |
103 |
|
op["operationState"], |
104 |
|
date, |
105 |
|
last_update, |
106 |
|
utils.wrap_text(text=detail, width=50), |
107 |
|
] |
108 |
|
) |
109 |
|
else: |
110 |
0 |
table.add_row( |
111 |
|
[ |
112 |
|
op["id"], |
113 |
|
op["lcmOperationType"], |
114 |
|
action_name, |
115 |
|
op["operationState"], |
116 |
|
date, |
117 |
|
utils.wrap_text(text=detail or "", width=50), |
118 |
|
] |
119 |
|
) |
120 |
0 |
table.align = "l" |
121 |
0 |
print(table) |
122 |
|
|
123 |
|
|
124 |
1 |
@click.command(name="ns-op-show", short_help="shows the info of a NS operation") |
125 |
1 |
@click.argument("id") |
126 |
1 |
@click.option( |
127 |
|
"--filter", |
128 |
|
multiple=True, |
129 |
|
help="restricts the information to the fields in the filter", |
130 |
|
) |
131 |
1 |
@click.option("--literal", is_flag=True, help="print literally, no pretty table") |
132 |
1 |
@click.pass_context |
133 |
1 |
def ns_op_show(ctx, id, filter, literal): |
134 |
|
"""shows the detailed info of a NS operation |
135 |
|
|
136 |
|
ID: operation identifier |
137 |
|
""" |
138 |
0 |
logger.debug("") |
139 |
0 |
utils.check_client_version(ctx.obj, ctx.command.name) |
140 |
0 |
op_info = ctx.obj.ns.get_op(id) |
141 |
|
|
142 |
0 |
if literal: |
143 |
0 |
print(yaml.safe_dump(op_info, indent=4, default_flow_style=False)) |
144 |
0 |
return |
145 |
|
|
146 |
0 |
table = PrettyTable(["field", "value"]) |
147 |
0 |
for k, v in list(op_info.items()): |
148 |
0 |
if not filter or k in filter: |
149 |
0 |
table.add_row([k, utils.wrap_text(json.dumps(v, indent=2), 100)]) |
150 |
0 |
table.align = "l" |
151 |
0 |
print(table) |