Coverage for osmclient/cli_commands/alarms.py: 39%
87 statements
« prev ^ index » next coverage.py v7.3.1, created at 2024-06-29 09:50 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2024-06-29 09:50 +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.
16import click
17from osmclient.common.exceptions import ClientException
18from osmclient.cli_commands import utils
19from prettytable import PrettyTable
20import json
21import os
22import logging
24logger = logging.getLogger("osmclient")
27@click.command(name="ns-alarm-create")
28@click.argument("name")
29@click.option("--ns", prompt=True, help="NS instance id or name")
30@click.option(
31 "--vnf", prompt=True, help="VNF name (VNF member index as declared in the NSD)"
32)
33@click.option("--vdu", prompt=True, help="VDU name (VDU name as declared in the VNFD)")
34@click.option("--metric", prompt=True, help="Name of the metric (e.g. cpu_utilization)")
35@click.option(
36 "--severity",
37 default="WARNING",
38 help="severity of the alarm (WARNING, MINOR, MAJOR, CRITICAL, INDETERMINATE)",
39)
40@click.option(
41 "--threshold_value",
42 prompt=True,
43 help="threshold value that, when crossed, an alarm is triggered",
44)
45@click.option(
46 "--threshold_operator",
47 prompt=True,
48 help="threshold operator describing the comparison (GE, LE, GT, LT, EQ)",
49)
50@click.option(
51 "--statistic",
52 default="AVERAGE",
53 help="statistic (AVERAGE, MINIMUM, MAXIMUM, COUNT, SUM)",
54)
55@click.pass_context
56def ns_alarm_create(
57 ctx,
58 name,
59 ns,
60 vnf,
61 vdu,
62 metric,
63 severity,
64 threshold_value,
65 threshold_operator,
66 statistic,
67):
68 """creates a new alarm for a NS instance"""
69 # TODO: Check how to validate threshold_value.
70 # Should it be an integer (1-100), percentage, or decimal (0.01-1.00)?
71 logger.debug("")
72 ns_instance = ctx.obj.ns.get(ns)
73 alarm = {}
74 alarm["alarm_name"] = name
75 alarm["ns_id"] = ns_instance["_id"]
76 alarm["correlation_id"] = ns_instance["_id"]
77 alarm["vnf_member_index"] = vnf
78 alarm["vdu_name"] = vdu
79 alarm["metric_name"] = metric
80 alarm["severity"] = severity
81 alarm["threshold_value"] = int(threshold_value)
82 alarm["operation"] = threshold_operator
83 alarm["statistic"] = statistic
84 utils.check_client_version(ctx.obj, ctx.command.name)
85 ctx.obj.ns.create_alarm(alarm)
88@click.command(name="alarm-show", short_help="show alarm details")
89@click.argument("uuid")
90@click.pass_context
91def alarm_show(ctx, uuid):
92 """Show alarm's detail information"""
94 utils.check_client_version(ctx.obj, ctx.command.name)
95 resp = ctx.obj.ns.get_alarm(uuid=uuid)
96 alarm_filter = [
97 "uuid",
98 "name",
99 "metric",
100 "statistic",
101 "threshold",
102 "operation",
103 "ns-id",
104 "vnf-id",
105 "vdu_name",
106 "action",
107 "status",
108 ]
109 table = PrettyTable(["key", "attribute"])
110 try:
111 # Arrange and return the response data
112 alarm = resp.replace("ObjectId", "")
113 for key in alarm_filter:
114 if key == "uuid":
115 value = alarm.get(key)
116 key = "alarm-id"
117 elif key == "name":
118 value = alarm.get(key)
119 key = "alarm-name"
120 elif key == "ns-id":
121 value = alarm["tags"].get("ns_id")
122 elif key == "vdu_name":
123 value = alarm["tags"].get("vdu_name")
124 elif key == "status":
125 value = alarm["alarm_status"]
126 else:
127 value = alarm[key]
128 table.add_row(
129 [key, utils.wrap_text(text=json.dumps(value, indent=2), width=100)]
130 )
131 table.align = "l"
132 print(table)
133 except Exception:
134 print(resp)
135 # TODO: check the reason for the try-except
138# List alarm
139@click.command(name="alarm-list", short_help="list all alarms")
140@click.option(
141 "--ns_id", default=None, required=False, help="List out alarm for given ns id"
142)
143@click.pass_context
144def alarm_list(ctx, ns_id):
145 """list all alarms"""
147 utils.check_client_version(ctx.obj, ctx.command.name)
148 project_name = os.getenv("OSM_PROJECT", "admin")
149 # TODO: check the reason for this^
150 resp = ctx.obj.ns.get_alarm(project_name=project_name, ns_id=ns_id)
152 table = PrettyTable(
153 ["alarm-id", "metric", "threshold", "operation", "action", "status"]
154 )
155 if resp:
156 # return the response data in a table
157 resp = resp.replace("ObjectId", "")
158 for alarm in resp:
159 table.add_row(
160 [
161 utils.wrap_text(text=str(alarm["uuid"]), width=38),
162 alarm["metric"],
163 alarm["threshold"],
164 alarm["operation"],
165 utils.wrap_text(text=alarm["action"], width=25),
166 alarm["alarm_status"],
167 ]
168 )
169 table.align = "l"
170 print(table)
173# Update alarm
174@click.command(name="alarm-update", short_help="Update a alarm")
175@click.argument("uuid")
176@click.option("--threshold", default=None, help="Alarm threshold")
177@click.option("--is_enable", default=None, type=bool, help="enable or disable alarm")
178@click.pass_context
179def alarm_update(ctx, uuid, threshold, is_enable):
180 """
181 Update alarm
183 """
184 if not threshold and is_enable is None:
185 raise ClientException(
186 "Please provide option to update i.e threshold or is_enable"
187 )
188 ctx.obj.ns.update_alarm(uuid, threshold, is_enable)