Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / nslcm_ops.py
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 import click
17 from osmclient.cli_commands import utils
18 from prettytable import PrettyTable
19 import yaml
20 import json
21 from datetime import datetime
22 import logging
23
24 logger = logging.getLogger("osmclient")
25
26
27 @click.command(
28 name="ns-op-list", short_help="shows the history of operations over a NS instance"
29 )
30 @click.argument("name")
31 @click.option(
32 "--long", is_flag=True, help="get more details of the NS operation (date, )."
33 )
34 @click.pass_context
35 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 def formatParams(params):
42 if params["lcmOperationType"] == "instantiate":
43 params.pop("nsDescription")
44 params.pop("nsName")
45 params.pop("nsdId")
46 params.pop("nsr_id")
47 elif params["lcmOperationType"] == "action":
48 params.pop("primitive")
49 params.pop("lcmOperationType")
50 params.pop("nsInstanceId")
51 return params
52
53 logger.debug("")
54 utils.check_client_version(ctx.obj, ctx.command.name)
55 resp = ctx.obj.ns.list_op(name)
56
57 if long:
58 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 table = PrettyTable(
72 ["id", "operation", "action_name", "status", "date", "detail"]
73 )
74
75 # print(yaml.safe_dump(resp))
76 for op in resp:
77 action_name = "N/A"
78 if op["lcmOperationType"] == "action":
79 action_name = op["operationParams"]["primitive"]
80 detail = "-"
81 if op["operationState"] == "PROCESSING":
82 if op.get("queuePosition") is not None and op.get("queuePosition") > 0:
83 detail = "In queue. Current position: {}".format(op["queuePosition"])
84 elif op["lcmOperationType"] in ("instantiate", "terminate"):
85 if op["stage"]:
86 detail = op["stage"]
87 elif op["operationState"] in ("FAILED", "FAILED_TEMP"):
88 detail = op.get("errorMessage", "-")
89 date = datetime.fromtimestamp(op["startTime"]).strftime("%Y-%m-%dT%H:%M:%S")
90 last_update = datetime.fromtimestamp(op["statusEnteredTime"]).strftime(
91 "%Y-%m-%dT%H:%M:%S"
92 )
93 if long:
94 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 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 table.align = "l"
121 print(table)
122
123
124 @click.command(name="ns-op-show", short_help="shows the info of a NS operation")
125 @click.argument("id")
126 @click.option(
127 "--filter",
128 multiple=True,
129 help="restricts the information to the fields in the filter",
130 )
131 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
132 @click.pass_context
133 def ns_op_show(ctx, id, filter, literal):
134 """shows the detailed info of a NS operation
135
136 ID: operation identifier
137 """
138 logger.debug("")
139 utils.check_client_version(ctx.obj, ctx.command.name)
140 op_info = ctx.obj.ns.get_op(id)
141
142 if literal:
143 print(yaml.safe_dump(op_info, indent=4, default_flow_style=False))
144 return
145
146 table = PrettyTable(["field", "value"])
147 for k, v in list(op_info.items()):
148 if not filter or k in filter:
149 table.add_row([k, utils.wrap_text(json.dumps(v, indent=2), 100)])
150 table.align = "l"
151 print(table)