+ def create_alarm(self, alarm):
+ self._logger.debug("")
+ self._client.get_token()
+ data = {}
+ data["create_alarm_request"] = {}
+ data["create_alarm_request"]["alarm_create_request"] = alarm
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint="/test/message/alarm_request", postfields_dict=data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ # resp = json.loads(resp)
+ print("Alarm created")
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException('error: code: {}, resp: {}'.format(
+ # http_code, msg))
+ except ClientException as exc:
+ message = "failed to create alarm: alarm {}\n{}".format(alarm, str(exc))
+ raise ClientException(message)
+
+ def delete_alarm(self, name):
+ self._logger.debug("")
+ self._client.get_token()
+ data = {}
+ data["delete_alarm_request"] = {}
+ data["delete_alarm_request"]["alarm_delete_request"] = {}
+ data["delete_alarm_request"]["alarm_delete_request"]["alarm_uuid"] = name
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint="/test/message/alarm_request", postfields_dict=data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ # resp = json.loads(resp)
+ print("Alarm deleted")
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException('error: code: {}, resp: {}'.format(
+ # http_code, msg))
+ except ClientException as exc:
+ message = "failed to delete alarm: alarm {}\n{}".format(name, str(exc))
+ raise ClientException(message)
+
+ def get_alarm(self, project_name=None, ns_id=None, uuid=None):
+ self._client.get_token()
+ try:
+ self._apiName = "/nsfm"
+ self._apiResource = "/alarms"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ if uuid:
+ # if request is for any uuid
+ http_code, resp = self._http.get2_cmd(
+ "{}/{}".format(self._apiBase, uuid)
+ )
+ if not uuid:
+ http_code, resp = self._http.get2_cmd(
+ "{}/{}/{}/{}".format(self._apiBase, uuid, project_name, ns_id)
+ )
+ if http_code == 200:
+ if resp:
+ resp = json.loads(resp)
+ return resp
+ else:
+ raise ClientException("unexpected response from server")
+ else:
+ msg = resp or ""
+ raise ClientException(msg)
+ except ClientException as exc:
+ message = "failed to get alarm :\nerror:\n{}".format(str(exc))
+ raise ClientException(message)
+
+ def update_alarm(self, uuid, threshold=None, is_enable=None, wait=None):
+ self._client.get_token()
+ try:
+ op_data = {}
+ op_data["uuid"] = uuid
+ op_data["threshold"] = threshold
+ op_data["is_enable"] = is_enable
+ self._apiName = "/nsfm"
+ self._apiResource = "/alarms"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ http_code, resp = self._http.patch_cmd(
+ endpoint="{}".format(self._apiBase), postfields_dict=op_data
+ )
+ if resp:
+ resp = json.loads(resp)
+ print(resp)
+ return resp
+ except ClientException as exc:
+ message = "failed to update alarm :\nerror:\n{}".format(str(exc))
+ raise ClientException(message)
+
+ def export_metric(self, metric):
+ self._logger.debug("")
+ self._client.get_token()
+ data = {}
+ data["read_metric_data_request"] = metric
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint="/test/message/metric_request", postfields_dict=data
+ )
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ # if http_code in (200, 201, 202, 204):
+ # resp = json.loads(resp)
+ return "Metric exported"
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException('error: code: {}, resp: {}'.format(
+ # http_code, msg))
+ except ClientException as exc:
+ message = "failed to export metric: metric {}\n{}".format(metric, str(exc))
+ raise ClientException(message)
+
+ def get_field(self, ns_name, field):
+ self._logger.debug("")
+ nsr = self.get(ns_name)
+ print(yaml.safe_dump(nsr))
+ if nsr is None:
+ raise NotFound("failed to retrieve ns {}".format(ns_name))
+
+ if field in nsr:
+ return nsr[field]
+
+ raise NotFound("failed to find {} in ns {}".format(field, ns_name))