Skip to content

Commit a76628e

Browse files
committed
feat:AlarmApiClient
1 parent d368edf commit a76628e

13 files changed

+993
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""Implementation of Alarm Client"""
2+
3+
from typing import Optional
4+
5+
from nisystemlink.clients import core
6+
from nisystemlink.clients.core._uplink._base_client import BaseClient
7+
from nisystemlink.clients.core._uplink._methods import delete, get, post
8+
from uplink import Path, retry
9+
10+
from . import models
11+
12+
13+
@retry(
14+
when=retry.when.status(408, 429, 502, 503, 504),
15+
stop=retry.stop.after_attempt(5),
16+
on_exception=retry.CONNECTION_ERROR,
17+
)
18+
class AlarmClient(BaseClient):
19+
def __init__(self, configuration: Optional[core.HttpConfiguration] = None):
20+
"""Initialize an instance.
21+
22+
Args:
23+
configuration: Defines the web server to connect to and information about
24+
how to connect. If not provided, the
25+
:class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>`
26+
is used to obtain the configuration.
27+
28+
Raises:
29+
ApiException: if unable to communicate with the Alarm Service.
30+
"""
31+
if configuration is None:
32+
configuration = core.HttpConfigurationManager.get_configuration()
33+
super().__init__(configuration, base_path="/nialarm/v1/")
34+
35+
@post("acknowledge-instances-by-instance-id")
36+
def acknowledge_instances_by_instance_id(
37+
self, request: models.AcknowledgeByInstanceIdRequest
38+
) -> models.AcknowledgeByInstanceIdResponse:
39+
"""Acknowledges one or more alarm instances by their instance IDs.
40+
41+
Args:
42+
request: The request containing instance IDs to acknowledge and optional force clear flag.
43+
44+
Returns:
45+
A response containing the instance IDs that were successfully acknowledged,
46+
the instance IDs that failed, and error details for failures.
47+
48+
Raises:
49+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
50+
"""
51+
...
52+
53+
@post("instances")
54+
def create_or_update_alarm(
55+
self, request: models.CreateOrUpdateAlarmRequest
56+
) -> models.CreateOrUpdateAlarmResponse:
57+
"""Creates or updates an instance, or occurrence, of an alarm.
58+
59+
Creates or updates an alarm based on the requested transition and the state
60+
of the current active alarm with the given alarmId.
61+
62+
Args:
63+
request: The request containing alarm information and transition details.
64+
65+
Returns:
66+
A response containing the ID of the created or modified alarm.
67+
68+
Raises:
69+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
70+
"""
71+
...
72+
73+
@get("instances/{instance_id}")
74+
def get_alarm(self, instance_id: str) -> models.Alarm:
75+
"""Gets an alarm by its instanceId.
76+
77+
Args:
78+
instance_id: Unique ID of a particular instance, or occurrence, of an alarm.
79+
80+
Returns:
81+
The alarm with the specified instance ID.
82+
83+
Raises:
84+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
85+
"""
86+
...
87+
88+
@delete("instances/{instance_id}")
89+
def delete_alarm(self, instance_id: str) -> None:
90+
"""Deletes an alarm by its instanceId.
91+
92+
Args:
93+
instance_id: Unique ID of a particular instance, or occurrence, of an alarm.
94+
95+
Raises:
96+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
97+
"""
98+
...
99+
100+
@post("delete-instances-by-instance-id")
101+
def delete_instances_by_instance_id(
102+
self, request: models.DeleteByInstanceIdRequest
103+
) -> models.DeleteByInstanceIdResponse:
104+
"""Deletes multiple alarm instances by their instanceIds.
105+
106+
Args:
107+
request: Contains the instanceIds of the alarms to delete.
108+
109+
Returns:
110+
A response containing lists of successfully deleted and failed instanceIds,
111+
along with error information for failures.
112+
113+
Raises:
114+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
115+
"""
116+
...
117+
118+
@post("query-instances-with-filter")
119+
def query_alarms(
120+
self, request: models.QueryWithFilterRequest
121+
) -> models.QueryWithFilterResponse:
122+
"""Queries for instances, or occurrences, of alarms using Dynamic LINQ.
123+
124+
Specifying an empty JSON object in the request body will result in all alarms being returned.
125+
126+
Args:
127+
request: The request containing filter information and query options.
128+
129+
Returns:
130+
A response containing the list of alarms that match the query, along with
131+
optional total count and continuation token for pagination.
132+
133+
Raises:
134+
ApiException: if unable to communicate with the Alarm Service or provided invalid arguments.
135+
"""
136+
...
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ._acknowledge_by_instance_id_request import AcknowledgeByInstanceIdRequest
2+
from ._acknowledge_by_instance_id_response import AcknowledgeByInstanceIdResponse
3+
from ._alarm import Alarm
4+
from ._create_or_update_alarm_request import CreateOrUpdateAlarmRequest
5+
from ._create_or_update_alarm_response import CreateOrUpdateAlarmResponse
6+
from ._delete_by_instance_id_request import DeleteByInstanceIdRequest
7+
from ._delete_by_instance_id_response import DeleteByInstanceIdResponse
8+
from ._query_alarms_request import QueryWithFilterRequest
9+
from ._query_alarms_response import QueryWithFilterResponse
10+
11+
# flake8: noqa
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List, Optional
2+
3+
from nisystemlink.clients.core._uplink._json_model import JsonModel
4+
5+
6+
class AcknowledgeByInstanceIdRequest(JsonModel):
7+
"""Contains information about the alarms to acknowledge."""
8+
9+
instance_ids: List[str]
10+
"""The instanceIds of the alarms which should be acknowledged."""
11+
12+
force_clear: Optional[bool] = False
13+
"""Whether or not the affected alarms should have their clear field set to true."""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
from nisystemlink.clients.alarm.models._partial_success_response_base import (
4+
PartialSuccessResponseBase,
5+
)
6+
7+
8+
class AcknowledgeByInstanceIdResponse(PartialSuccessResponseBase):
9+
"""Contains information about which alarms were acknowledged."""
10+
11+
acknowledged: List[str]
12+
"""The instanceIds of the alarms which were successfully acknowledged."""
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
from datetime import datetime
2+
from enum import Enum
3+
from typing import Dict, List, Optional
4+
5+
from nisystemlink.clients.core._uplink._json_model import JsonModel
6+
7+
8+
class AlarmTransitionType(str, Enum):
9+
"""Specifies a type of alarm transition."""
10+
11+
CLEAR = "CLEAR"
12+
"""The transition corresponds to the clearing of the condition tracked by the alarm."""
13+
14+
SET = "SET"
15+
"""The transition corresponds to the condition tracked by the alarm entering into the triggered state."""
16+
17+
18+
class AlarmTransition(JsonModel):
19+
"""A transition within an instance, or occurrence, of an alarm.
20+
21+
Alarm transitions record changes to an alarm's clear field as well as an alarm
22+
increasing or decreasing in severity.
23+
"""
24+
25+
transition_type: AlarmTransitionType
26+
"""Specifies a type of alarm transition: CLEAR or SET."""
27+
28+
occurred_at: datetime
29+
"""The date and time when the transition occurred."""
30+
31+
severity_level: int
32+
"""The severity of the transition.
33+
34+
Valid values for CLEAR transitions are [-1, -1].
35+
Valid values for SET transitions are [1, infinity].
36+
Note that the SystemLink Alarm UI only has display strings for SET severities in the range [1, 4].
37+
"""
38+
39+
value: str
40+
"""The value that caused the alarm to transition."""
41+
42+
condition: str
43+
"""A description of the condition associated with the transition."""
44+
45+
short_text: str
46+
"""A short description of the condition."""
47+
48+
detail_text: str
49+
"""A detailed description of the condition."""
50+
51+
keywords: List[str]
52+
"""Words or phrases associated with a transition.
53+
54+
Useful for attaching metadata to a transition which could aid in an investigation
55+
into an alarm's root cause in the future.
56+
"""
57+
58+
properties: Dict[str, str]
59+
"""Key-value pair metadata associated with a transition.
60+
61+
Useful for attaching additional metadata to a transition which could aid in an investigation
62+
into an alarm's root cause in the future. Property keys must be between 1 and 255 characters.
63+
"""
64+
65+
66+
class AlarmNote(JsonModel):
67+
"""Information about a particular alarm instance.
68+
69+
Such as a description of the root cause of the alarm.
70+
"""
71+
72+
note: str
73+
"""Information about a particular alarm instance, such as a description of the root cause of the alarm."""
74+
75+
created_at: Optional[datetime] = None
76+
"""The date and time when the note was created."""
77+
78+
user: Optional[str] = None
79+
"""The userId of the person who created the note."""
80+
81+
82+
class Alarm(JsonModel):
83+
"""An individual instance, or occurrence, of an alarm.
84+
85+
The lifecycle of an alarm begins the first time it is triggered and ends when it has been
86+
both acknowledged and cleared. The service enforces the invariant that there can be at most
87+
one active=True alarm with the same alarmId.
88+
"""
89+
90+
instance_id: str
91+
"""The unique identifier for the instance, or occurrence, of the alarm."""
92+
93+
alarm_id: str
94+
"""A value meant to uniquely identify the particular process or condition tracked by the alarm.
95+
96+
For example, alarms created by a tag rule engine might have their alarmIds set to the
97+
concatenation of the path of the tag which caused the rule to be triggered and the ID of the rule.
98+
"""
99+
100+
workspace: str
101+
"""The workspace the alarm belongs to."""
102+
103+
active: bool
104+
"""Whether or not the alarm is active.
105+
106+
An active alarm deserves human or automated attention. Alarms always begin life with active=True.
107+
This field will be automatically set to false when the clear and acknowledged fields are set to true.
108+
"""
109+
110+
clear: bool
111+
"""When set to true, the condition that triggered the alarm no longer matches the trigger condition.
112+
113+
When an alarm is created, clear is initially set to false. The creator of the alarm (typically a
114+
rule engine or application element of some sort) may determine that the trigger condition no longer
115+
applies, and may send an update, clearing the alarm. Clearing the alarm does not deactivate the alarm,
116+
unless the alarm had previously been acknowledged. As long as the alarm is active=true, the alarm may
117+
transition from clear=True to clear=False (or vice versa) multiple times.
118+
"""
119+
120+
acknowledged: bool
121+
"""When set to true, the alarm has been acknowledged by an alarm handler, which is typically a human.
122+
123+
Alarms always begin life with acknowledged=False. Acknowledging an alarm will not affect the active
124+
flag unless clear is also true. When clear and acknowledged are true, the alarm will become inactive
125+
(active=False). This field is automatically reset to false when the alarm's highestSeverityLevel field increases.
126+
"""
127+
128+
acknowledged_at: Optional[datetime]
129+
"""The date and time when the alarm instance was acknowledged.
130+
131+
This field will be cleared when the alarm's highestSeverityLevel field increases.
132+
"""
133+
134+
acknowledged_by: Optional[str]
135+
"""The userId of the individual who acknowledged the alarm.
136+
137+
This field will be cleared when the alarm's highestSeverityLevel field increases.
138+
"""
139+
140+
occurred_at: datetime
141+
"""The date and time when the alarm was created/first occurred."""
142+
143+
updated_at: datetime
144+
"""The date and time when the alarm was last updated or modified."""
145+
146+
created_by: str
147+
"""An identifier for who or what created the alarm.
148+
149+
This is usually used to identify the particular rule engine which requested that the alarm be created.
150+
"""
151+
152+
transitions: List[AlarmTransition]
153+
"""A collection of AlarmTransitions for the alarm.
154+
155+
The service stores the first transition and the most recent 99 other transitions by default.
156+
The configuration for the service determines the total number of stored transitions per alarm.
157+
"""
158+
159+
transition_overflow_count: int
160+
"""The number of transitions which overflowed the transitions field for the alarm.
161+
162+
For example, if the alarm transitioned 250 times, transitionOverflowCount is set to 150 and
163+
transitions contains the first and most recent 99 transitions. The configuration for the service
164+
determines the total number of stored transitions per alarm.
165+
"""
166+
167+
notification_strategy_ids: List[str]
168+
"""The IDs of the notification strategies which will be triggered.
169+
170+
These are triggered when the alarm is first created and when its highestSeverityLevel increases.
171+
"""
172+
173+
current_severity_level: int
174+
"""The current severity level of the alarm."""
175+
176+
highest_severity_level: int
177+
"""The highest severity level that the alarm has ever been in."""
178+
179+
most_recent_set_occurred_at: Optional[datetime]
180+
"""The date and time of the most recent occurrence of a SET transition.
181+
182+
This property only considers transitions that cause an alarm state change.
183+
"""
184+
185+
most_recent_transition_occurred_at: Optional[datetime]
186+
"""The date and time of the most recent occurrence of a transition.
187+
188+
This property only considers transitions that cause an alarm state change.
189+
"""
190+
191+
channel: str
192+
"""An identifier for the tag or resource associated with the alarm."""
193+
194+
condition: str
195+
"""A description of the condition associated with the alarm."""
196+
197+
display_name: str
198+
"""Optional display name for the alarm. Display names do not need to be unique."""
199+
200+
description: str
201+
"""Optional description text for the alarm."""
202+
203+
keywords: List[str]
204+
"""Words or phrases associated with the alarm.
205+
206+
Alarms can be tagged with keywords to make it easier to find them with queries.
207+
"""
208+
209+
notes: List[AlarmNote]
210+
"""A collection of notes for a given alarm instance.
211+
212+
Notes are set by humans to record information such as the root cause of the alarm.
213+
"""
214+
215+
properties: Dict[str, str]
216+
"""The alarm's custom properties."""
217+
218+
resource_type: str
219+
"""The type of resource associated with the alarm."""

0 commit comments

Comments
 (0)