|
| 1 | +"""Implementation of Alarm Client""" |
| 2 | + |
| 3 | +from typing import List, Literal, overload |
| 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 Field, 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 | + |
| 20 | + def __init__(self, configuration: core.HttpConfiguration | None = None): |
| 21 | + """Initialize an instance. |
| 22 | +
|
| 23 | + Args: |
| 24 | + configuration: Defines the web server to connect to and information about |
| 25 | + how to connect. If not provided, the |
| 26 | + :class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>` |
| 27 | + is used to obtain the configuration. |
| 28 | +
|
| 29 | + Raises: |
| 30 | + ApiException: if unable to communicate with the `/nialarm` Service. |
| 31 | + """ |
| 32 | + if configuration is None: |
| 33 | + configuration = core.HttpConfigurationManager.get_configuration() |
| 34 | + |
| 35 | + super().__init__(configuration, base_path="/nialarm/v1/") |
| 36 | + |
| 37 | + @post( |
| 38 | + "acknowledge-instances-by-instance-id", |
| 39 | + args=[Field("instanceIds"), Field("forceClear")], |
| 40 | + ) |
| 41 | + def acknowledge_alarms( |
| 42 | + self, instance_ids: List[str], *, force_clear: bool = False |
| 43 | + ) -> models.AcknowledgeAlarmsResponse: |
| 44 | + """Acknowledges one or more alarm instances by their instance IDs. |
| 45 | +
|
| 46 | + Args: |
| 47 | + instance_ids: List of instance IDs (unique occurrence identifiers) of the alarms to acknowledge. |
| 48 | + These are the server-generated IDs returned when creating/updating alarms, |
| 49 | + not the user-defined alarm_id. |
| 50 | + force_clear: Whether or not the affected alarms should have their clear field set to true. |
| 51 | + Defaults to False. |
| 52 | +
|
| 53 | + Returns: |
| 54 | + A response containing the instance IDs that were successfully acknowledged, |
| 55 | + the instance IDs that failed, and error details for failures. |
| 56 | +
|
| 57 | + Raises: |
| 58 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 59 | + """ |
| 60 | + ... |
| 61 | + |
| 62 | + @overload |
| 63 | + def create_or_update_alarm( # noqa: E704 |
| 64 | + self, |
| 65 | + request: models.CreateOrUpdateAlarmRequest, |
| 66 | + *, |
| 67 | + ignore_conflict: Literal[False] = False, |
| 68 | + ) -> str: ... |
| 69 | + |
| 70 | + @overload |
| 71 | + def create_or_update_alarm( # noqa: E704 |
| 72 | + self, |
| 73 | + request: models.CreateOrUpdateAlarmRequest, |
| 74 | + *, |
| 75 | + ignore_conflict: Literal[True], |
| 76 | + ) -> str | None: ... |
| 77 | + |
| 78 | + def create_or_update_alarm( |
| 79 | + self, |
| 80 | + request: models.CreateOrUpdateAlarmRequest, |
| 81 | + *, |
| 82 | + ignore_conflict: bool = False, |
| 83 | + ) -> str | None: |
| 84 | + """Creates or updates an instance, or occurrence, of an alarm. |
| 85 | +
|
| 86 | + Creates or updates an alarm based on the requested transition and the state |
| 87 | + of the current active alarm with the given alarm_id (specified in the request). |
| 88 | + Multiple calls with the same alarm_id will update the same alarm instance. |
| 89 | +
|
| 90 | + Args: |
| 91 | + request: The request containing alarm_id (user-defined identifier), |
| 92 | + transition details, and other alarm properties. |
| 93 | + ignore_conflict: If True, 409 Conflict errors will be ignored and None will be returned. |
| 94 | + If False (default), 409 errors will raise an ApiException. |
| 95 | + Setting this to True is useful for stateless applications that want to |
| 96 | + attempt state transitions without checking the current alarm state first. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + The instance_id (unique occurrence identifier) of the created or modified alarm. |
| 100 | + Use this ID for operations like get_alarm(), delete_alarm(), or acknowledge. |
| 101 | + Returns None if ignore_conflict is True and a 409 Conflict occurs. |
| 102 | +
|
| 103 | + Raises: |
| 104 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 105 | + A 409 Conflict error occurs when the request does not represent a valid transition |
| 106 | + for an existing alarm, such as attempting to clear an alarm which is already clear, |
| 107 | + or attempting to set an alarm which is already set at the given severity level. |
| 108 | + This error can be suppressed by setting ignore_conflict=True. |
| 109 | + """ |
| 110 | + try: |
| 111 | + return self._create_or_update_alarm(request) |
| 112 | + except core.ApiException as e: |
| 113 | + if ignore_conflict and e.http_status_code == 409: |
| 114 | + return None |
| 115 | + raise |
| 116 | + |
| 117 | + @post("instances", return_key="instanceId") |
| 118 | + def _create_or_update_alarm( |
| 119 | + self, request: models.CreateOrUpdateAlarmRequest |
| 120 | + ) -> str: |
| 121 | + """Internal implementation of create_or_update_alarm.""" |
| 122 | + ... |
| 123 | + |
| 124 | + @get("instances/{instance_id}", args=[Path("instance_id")]) |
| 125 | + def get_alarm(self, instance_id: str) -> models.Alarm: |
| 126 | + """Gets an alarm by its instance_id. |
| 127 | +
|
| 128 | + Args: |
| 129 | + instance_id: The unique instance ID (occurrence identifier) of the alarm to retrieve. |
| 130 | + This is the server-generated ID returned from create_or_update_alarm(), |
| 131 | + not the user-defined alarm_id. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + The alarm with the specified instance_id. |
| 135 | +
|
| 136 | + Raises: |
| 137 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 138 | + """ |
| 139 | + ... |
| 140 | + |
| 141 | + @delete("instances/{instance_id}", args=[Path("instance_id")]) |
| 142 | + def delete_alarm(self, instance_id: str) -> None: |
| 143 | + """Deletes an alarm by its instance_id. |
| 144 | +
|
| 145 | + Args: |
| 146 | + instance_id: The unique instance ID (occurrence identifier) of the alarm to delete. |
| 147 | + This is the server-generated ID returned from create_or_update_alarm(), |
| 148 | + not the user-defined alarm_id. |
| 149 | +
|
| 150 | + Raises: |
| 151 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 152 | + """ |
| 153 | + ... |
| 154 | + |
| 155 | + @post("delete-instances-by-instance-id", args=[Field("instanceIds")]) |
| 156 | + def delete_alarms(self, instance_ids: List[str]) -> models.DeleteAlarmsResponse: |
| 157 | + """Deletes multiple alarm instances by their instance IDs. |
| 158 | +
|
| 159 | + Args: |
| 160 | + instance_ids: List of instance IDs (unique occurrence identifiers) of the alarms to delete. |
| 161 | + These are the server-generated IDs returned when creating/updating alarms, |
| 162 | + not the user-defined alarm_id. |
| 163 | +
|
| 164 | + Returns: |
| 165 | + A response containing lists of successfully deleted and failed instance IDs, |
| 166 | + along with error information for failures. |
| 167 | +
|
| 168 | + Raises: |
| 169 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 170 | + """ |
| 171 | + ... |
| 172 | + |
| 173 | + @post("query-instances-with-filter") |
| 174 | + def query_alarms( |
| 175 | + self, request: models.QueryAlarmsWithFilterRequest |
| 176 | + ) -> models.QueryAlarmsWithFilterResponse: |
| 177 | + """Queries for instances, or occurrences, of alarms using Dynamic LINQ. |
| 178 | +
|
| 179 | + Specifying an empty JSON object in the request body will result in all alarms being returned. |
| 180 | +
|
| 181 | + Args: |
| 182 | + request: The request containing filter information and query options. |
| 183 | +
|
| 184 | + Returns: |
| 185 | + A response containing the list of alarms that match the query, along with |
| 186 | + optional total count and continuation token for pagination. |
| 187 | +
|
| 188 | + Raises: |
| 189 | + ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments. |
| 190 | + """ |
| 191 | + ... |
0 commit comments