moira-python-client 4.2.0__py3-none-any.whl → 4.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
moira_client/client.py CHANGED
@@ -21,6 +21,14 @@ class InvalidJSONError(Exception):
21
21
  """
22
22
  self.content = content
23
23
 
24
+ class MoiraApiError(Exception):
25
+ def __init__(self, body):
26
+ """
27
+
28
+ :param body: response body
29
+ """
30
+ self.body = body
31
+
24
32
 
25
33
  class Client:
26
34
  def __init__(self, api_url, auth_custom=None, auth_user=None, auth_pass=None, login=None):
@@ -64,7 +72,12 @@ class Client:
64
72
  :raises: InvalidJSONError
65
73
  """
66
74
  r = requests.get(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs)
67
- r.raise_for_status()
75
+
76
+ try:
77
+ r.raise_for_status()
78
+ except requests.exceptions.HTTPError as err:
79
+ raise MoiraApiError(r.content)
80
+
68
81
  try:
69
82
  return r.json()
70
83
  except ValueError:
@@ -81,7 +94,12 @@ class Client:
81
94
  :raises: InvalidJSONError
82
95
  """
83
96
  r = requests.delete(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs)
84
- r.raise_for_status()
97
+
98
+ try:
99
+ r.raise_for_status()
100
+ except requests.exceptions.HTTPError as err:
101
+ raise MoiraApiError(r.content)
102
+
85
103
  try:
86
104
  return r.json()
87
105
  except ValueError:
@@ -98,7 +116,12 @@ class Client:
98
116
  :raises: InvalidJSONError
99
117
  """
100
118
  r = requests.put(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs)
101
- r.raise_for_status()
119
+
120
+ try:
121
+ r.raise_for_status()
122
+ except requests.exceptions.HTTPError as err:
123
+ raise MoiraApiError(r.content)
124
+
102
125
  try:
103
126
  return r.json()
104
127
  except ValueError:
@@ -115,7 +138,12 @@ class Client:
115
138
  :raises: InvalidJSONError
116
139
  """
117
140
  r = requests.post(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs)
118
- r.raise_for_status()
141
+
142
+ try:
143
+ r.raise_for_status()
144
+ except requests.exceptions.HTTPError as err:
145
+ raise MoiraApiError(r.content)
146
+
119
147
  try:
120
148
  return r.json()
121
149
  except ValueError:
@@ -21,33 +21,43 @@ class Contact(Base):
21
21
  self.type = type
22
22
  self.value = value
23
23
  self.user = kwargs.get('user', None)
24
+ self.name = kwargs.get('name', None)
24
25
  self._id = kwargs.get('id', None)
26
+ self.team_id = kwargs.get('team_id', None)
25
27
 
26
28
 
27
29
  class ContactManager:
28
30
  def __init__(self, client):
29
31
  self._client = client
30
32
 
31
- def add(self, value, contact_type):
33
+ def add(self, value, contact_type, name=None):
32
34
  """
33
35
  Add new contact
34
36
 
35
37
  :param value: str contact value
36
38
  :param contact_type: str contact type (one of CONTACT_* constants)
39
+ :param name: str contact name
37
40
  :return: Contact
38
41
 
39
42
  :raises: ResponseStructureError
40
43
  """
41
- data = {
42
- 'value': value,
43
- 'type': contact_type
44
- }
45
44
 
46
45
  contacts = self.fetch_by_current_user()
47
46
  for contact in contacts:
48
47
  if contact.value == value and contact.type == contact_type:
48
+ if name and contact.name != name:
49
+ contact.name = name
50
+ self.update(contact)
51
+ return contact
49
52
  return contact
50
53
 
54
+ data = {
55
+ 'value': value,
56
+ 'type': contact_type
57
+ }
58
+ if name:
59
+ data['name'] = name
60
+
51
61
  result = self._client.put(self._full_path(), json=data)
52
62
  if 'id' not in result:
53
63
  raise ResponseStructureError('No id in response', result)
@@ -135,6 +145,8 @@ class ContactManager:
135
145
  'type': contact.type,
136
146
  'value': contact.value,
137
147
  }
148
+ if contact.name:
149
+ data['name'] = contact.name
138
150
 
139
151
  try:
140
152
  self._client.put(self._full_path(contact.id), json=data)
@@ -2,12 +2,10 @@ from ..client import InvalidJSONError
2
2
  from ..client import ResponseStructureError
3
3
  from .base import Base
4
4
 
5
- class Subscription(Base):
6
- def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, sched=None,
5
+ class SubscriptionModel(Base):
6
+ def __init__(self, tags, contacts=None, enabled=None, throttling=None, sched=None,
7
7
  ignore_warnings=False, ignore_recoverings=False, plotting=None, any_tags=False, **kwargs):
8
8
  """
9
-
10
- :param client: api client
11
9
  :param tags: list of str tags
12
10
  :param contacts: list of contact id's
13
11
  :param enabled: bool is enabled
@@ -19,8 +17,6 @@ class Subscription(Base):
19
17
  :param any_tags: bool any tags
20
18
  :param kwargs: additional parameters
21
19
  """
22
- self._client = client
23
-
24
20
  self._id = kwargs.get('id', None)
25
21
  self.tags = tags if not any_tags else []
26
22
  if not contacts:
@@ -38,32 +34,7 @@ class Subscription(Base):
38
34
  if not plotting:
39
35
  plotting = {'enabled': False, 'theme': 'light'}
40
36
  self.plotting = plotting
41
-
42
- def _send_request(self, subscription_id=None):
43
- data = {
44
- 'contacts': self.contacts,
45
- 'tags': self.tags,
46
- 'enabled': self.enabled,
47
- 'any_tags': self.any_tags,
48
- 'throttling': self.throttling,
49
- 'sched': self.sched,
50
- 'ignore_warnings': self.ignore_warnings,
51
- 'ignore_recoverings': self.ignore_recoverings,
52
- 'plotting': self.plotting
53
- }
54
-
55
- if subscription_id:
56
- data['id'] = subscription_id
57
-
58
- if subscription_id:
59
- result = self._client.put('subscription/{id}'.format(id=subscription_id), json=data)
60
- else:
61
- result = self._client.put('subscription', json=data)
62
- if 'id' not in result:
63
- raise ResponseStructureError("id doesn't exist in response", result)
64
-
65
- self._id = result['id']
66
- return self._id
37
+ self.team_id = kwargs.get('team_id', None)
67
38
 
68
39
  def disable_day(self, day):
69
40
  """
@@ -124,26 +95,6 @@ class Subscription(Base):
124
95
  'theme': 'light'
125
96
  }
126
97
 
127
- def save(self):
128
- """
129
- Save subscription
130
-
131
- :return: subscription id
132
- """
133
- if self._id:
134
- return self.update()
135
- self._send_request()
136
-
137
- def update(self):
138
- """
139
- Update subscription
140
-
141
- :return: subscription id
142
- """
143
- if not self._id:
144
- return self.save()
145
- self._send_request(self._id)
146
-
147
98
  def set_start_hour(self, hour):
148
99
  """
149
100
  Set start hour
@@ -185,6 +136,85 @@ class Subscription(Base):
185
136
  self._end_minute = minute
186
137
 
187
138
 
139
+ class Subscription(SubscriptionModel):
140
+ def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, sched=None,
141
+ ignore_warnings=False, ignore_recoverings=False, plotting=None, any_tags=False, **kwargs):
142
+ """
143
+ :param client: api client
144
+ :param tags: list of str tags
145
+ :param contacts: list of contact id's
146
+ :param enabled: bool is enabled
147
+ :param throttling: bool throttling
148
+ :param sched: dict schedule
149
+ :param ignore_warnings: bool ignore warnings
150
+ :param ignore_recoverings: bool ignore recoverings
151
+ :param plotting: dict plotting settings
152
+ :param any_tags: bool any tags
153
+ :param kwargs: additional parameters
154
+ """
155
+ self._client = client
156
+
157
+ super().__init__(
158
+ tags=tags,
159
+ contacts=contacts,
160
+ enabled=enabled,
161
+ throttling=throttling,
162
+ sched=sched,
163
+ ignore_warnings=ignore_warnings,
164
+ ignore_recoverings=ignore_recoverings,
165
+ plotting=plotting,
166
+ any_tags=any_tags,
167
+ **kwargs,
168
+ )
169
+
170
+ def _send_request(self, subscription_id=None):
171
+ data = {
172
+ 'contacts': self.contacts,
173
+ 'tags': self.tags,
174
+ 'enabled': self.enabled,
175
+ 'any_tags': self.any_tags,
176
+ 'throttling': self.throttling,
177
+ 'sched': self.sched,
178
+ 'ignore_warnings': self.ignore_warnings,
179
+ 'ignore_recoverings': self.ignore_recoverings,
180
+ 'plotting': self.plotting,
181
+ 'team_id': self.team_id,
182
+ }
183
+
184
+ if subscription_id:
185
+ data['id'] = subscription_id
186
+
187
+ if subscription_id:
188
+ result = self._client.put('subscription/{id}'.format(id=subscription_id), json=data)
189
+ else:
190
+ result = self._client.put('subscription', json=data)
191
+ if 'id' not in result:
192
+ raise ResponseStructureError("id doesn't exist in response", result)
193
+
194
+ self._id = result['id']
195
+ return self._id
196
+
197
+ def save(self):
198
+ """
199
+ Save subscription
200
+
201
+ :return: subscription id
202
+ """
203
+ if self._id:
204
+ return self.update()
205
+ self._send_request()
206
+
207
+ def update(self):
208
+ """
209
+ Update subscription
210
+
211
+ :return: subscription id
212
+ """
213
+ if not self._id:
214
+ return self.save()
215
+ self._send_request(self._id)
216
+
217
+
188
218
  class SubscriptionManager:
189
219
  def __init__(self, client):
190
220
  self._client = client
moira_client/moira.py CHANGED
@@ -9,6 +9,7 @@ from .models.trigger import TriggerManager
9
9
  from .models.health import HealthManager
10
10
  from .models.config import ConfigManager
11
11
  from .models.user import UserManager
12
+ from .models.team import TeamManager
12
13
 
13
14
 
14
15
  class Moira:
@@ -34,6 +35,7 @@ class Moira:
34
35
  self._health = None
35
36
  self._config = None
36
37
  self._user = None
38
+ self._team = None
37
39
 
38
40
  @property
39
41
  def trigger(self):
@@ -145,3 +147,12 @@ class Moira:
145
147
  if not self._user:
146
148
  self._user = UserManager(self._client)
147
149
  return self._user
150
+
151
+ @property
152
+ def team(self) -> TeamManager:
153
+ """Get team manager
154
+ """
155
+ if not self._team:
156
+ self._team = TeamManager(self._client)
157
+
158
+ return self._team
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: moira-python-client
3
- Version: 4.2.0
3
+ Version: 4.3.0
4
4
  Summary: Client for Moira - Alerting system based on Graphite data
5
5
  Home-page: https://github.com/moira-alert/python-moira-client
6
6
  Author: Alexander Lukyanchenko
@@ -1,20 +1,20 @@
1
1
  moira_client/__init__.py,sha256=BUWUwGU1V5wvdi5-mUXd8AJlsGyrAvFKOjs8-vkUV4o,67
2
- moira_client/client.py,sha256=S7_EvZ2whx1-AnOAa7uZ8da4QnFo31DD6sRFyigE_zQ,3474
3
- moira_client/moira.py,sha256=40OmxdnUTyrhjZhDwhy9OToeZhYyMLkytB619X0p8z4,3601
2
+ moira_client/client.py,sha256=SGakaO2SB2EjsZPlfD1Dv2naIgwBr5F5QufYKm-Ro1w,4106
3
+ moira_client/moira.py,sha256=Pa3grEVhGu2gcPxWR8sdu7GGowNF3AXBYvK1nmRiZi8,3859
4
4
  moira_client/models/__init__.py,sha256=K3Kcjh8F_jV703ii-QMeN9cMwqcCfbfeNF7e9MfCD1c,126
5
5
  moira_client/models/base.py,sha256=cYVLxnc5PGjmUcoZRZ7AFx5f0Ij96HypJpOjpwr_D-Q,385
6
6
  moira_client/models/config.py,sha256=BbsShuKhe5H0DTXkIeOndHyXbi2xW2BRP0pwoIQl4Ok,2178
7
- moira_client/models/contact.py,sha256=2MJMko_OZLj32cuv2mVpGAsSSsmU3_NQm-YAwGly5fQ,4561
7
+ moira_client/models/contact.py,sha256=-z8p32EIUfG9x8KRbUPeJR3DroOuPHfBN9pv8yq4lgI,4987
8
8
  moira_client/models/event.py,sha256=pXsuU-X3i3DX89rs1wBaB7SBvw_q5dgJwLJgQFrp7vg,1375
9
9
  moira_client/models/health.py,sha256=a2bGgHVVbcIJCraArjI9V-gqpwdRZ1B5SnNrsLqR6mI,1708
10
10
  moira_client/models/notification.py,sha256=stk78RW0lENkBX7FHm9cTskHkz8SxdXmpcXP72XwWio,1946
11
11
  moira_client/models/pattern.py,sha256=Ef8Roupkjrs8gb41vrgBwvgpgTmJU4igsU58z19LHcg,1619
12
- moira_client/models/subscription.py,sha256=xyB8MXM6mC0VjE_2aezBnSSaFER9u4LAQ0kJuzJsxas,7891
12
+ moira_client/models/subscription.py,sha256=vAGuxrcTkGLtrj8D_cC_lo3EMGtAnClOXdmFfMl45E0,9050
13
13
  moira_client/models/tag.py,sha256=2sDmf9CKAzzZRqQykBdR3-jKgcfo62hGO7eivI1X_sc,4336
14
14
  moira_client/models/trigger.py,sha256=61Gs4blN0pcPrCEnOzpaKkA_UgeChz3Kv2qV-zXySYE,17868
15
15
  moira_client/models/user.py,sha256=9zG7pbmEA9ESUWFMzvqfyLP0lqcjz1pc0n8U7mL1UMk,1767
16
- moira_python_client-4.2.0.dist-info/LICENSE,sha256=HJk93uYDrJpwAa6G3ufYfC4ZgCKH2flOtdgOv1esxgI,1062
17
- moira_python_client-4.2.0.dist-info/METADATA,sha256=0PKjYFhuVhd1pIqK4nUBjSYpVhG9hP1VSspMUDe5tCY,1243
18
- moira_python_client-4.2.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
19
- moira_python_client-4.2.0.dist-info/top_level.txt,sha256=zBB6_FP28feugBNn2cLmi5nCniRPnuChI6HuXUqFdIA,13
20
- moira_python_client-4.2.0.dist-info/RECORD,,
16
+ moira_python_client-4.3.0.dist-info/LICENSE,sha256=HJk93uYDrJpwAa6G3ufYfC4ZgCKH2flOtdgOv1esxgI,1062
17
+ moira_python_client-4.3.0.dist-info/METADATA,sha256=_OsSWx_61kPxAoKt0oIaH1h90ZT_fCyA40liA2i3SdY,1243
18
+ moira_python_client-4.3.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
19
+ moira_python_client-4.3.0.dist-info/top_level.txt,sha256=zBB6_FP28feugBNn2cLmi5nCniRPnuChI6HuXUqFdIA,13
20
+ moira_python_client-4.3.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: setuptools (75.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5