1 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from osmclient
.cli_commands
import utils
18 from prettytable
import PrettyTable
21 from datetime
import datetime
24 logger
= logging
.getLogger("osmclient")
28 name
="ns-op-list", short_help
="shows the history of operations over a NS instance"
30 @click.argument("name")
32 "--long", is_flag
=True, help="get more details of the NS operation (date, )."
35 def ns_op_list(ctx
, name
, long):
36 """shows the history of operations over a NS instance
38 NAME: name or ID of the NS instance
41 def formatParams(params
):
42 if params
["lcmOperationType"] == "instantiate":
43 params
.pop("nsDescription")
47 elif params
["lcmOperationType"] == "action":
48 params
.pop("primitive")
49 params
.pop("lcmOperationType")
50 params
.pop("nsInstanceId")
54 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
55 resp
= ctx
.obj
.ns
.list_op(name
)
72 ["id", "operation", "action_name", "status", "date", "detail"]
75 # print(yaml.safe_dump(resp))
78 if op
["lcmOperationType"] == "action":
79 action_name
= op
["operationParams"]["primitive"]
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"):
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(
97 op
["lcmOperationType"],
100 text
=json
.dumps(formatParams(op
["operationParams"]), indent
=2),
103 op
["operationState"],
106 utils
.wrap_text(text
=detail
, width
=50),
113 op
["lcmOperationType"],
115 op
["operationState"],
117 utils
.wrap_text(text
=detail
or "", width
=50),
124 @click.command(name
="ns-op-show", short_help
="shows the info of a NS operation")
125 @click.argument("id")
129 help="restricts the information to the fields in the filter",
131 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
133 def ns_op_show(ctx
, id, filter, literal
):
134 """shows the detailed info of a NS operation
136 ID: operation identifier
139 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
140 op_info
= ctx
.obj
.ns
.get_op(id)
143 print(yaml
.safe_dump(op_info
, indent
=4, default_flow_style
=False))
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)])