Coverage for osmclient/cli_commands/nslcm_ops.py: 32%

77 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2024-06-22 09:01 +0000

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 

16import click 

17from osmclient.cli_commands import utils 

18from prettytable import PrettyTable 

19import yaml 

20import json 

21from datetime import datetime 

22import logging 

23 

24logger = 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 

35def 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 

133def 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) 

152 

153 

154@click.command(name="ns-op-cancel", short_help="cancels an ongoing NS operation") 

155@click.argument("id") 

156@click.option( 

157 "--cancel_mode", 

158 required=False, 

159 default="GRACEFUL", 

160 show_default=True, 

161 help="Mode of cancellation, can be FORCEFUL or GRACEFUL", 

162) 

163@click.option( 

164 "--wait", 

165 required=False, 

166 default=False, 

167 is_flag=True, 

168 help="do not return the control immediately, but keep it " 

169 "until the operation is completed, or timeout", 

170) 

171@click.pass_context 

172def ns_op_cancel(ctx, id, cancel_mode, wait): 

173 """Cancels an ongoing NS operation 

174 

175 ID: operation identifier 

176 """ 

177 logger.debug("") 

178 utils.check_client_version(ctx.obj, ctx.command.name) 

179 ctx.obj.ns.cancel_op(id, cancel_mode, wait)