|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain 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, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
|
|
15
|
|
|
from lib.actions import OpsGenieBaseAction |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class CreateAlertAction(OpsGenieBaseAction): |
|
19
|
|
|
def run(self, message, teams=None, alias=None, |
|
20
|
|
|
description=None, recipients=None, actions=None, |
|
21
|
|
|
source="StackStorm", tags=None, details=None, |
|
22
|
|
|
entity=None, user=None, note=None): |
|
23
|
|
|
""" |
|
24
|
|
|
Create alert in OpsGenie. |
|
25
|
|
|
|
|
26
|
|
|
Args: |
|
27
|
|
|
- message: Alert text limited to 130 characters |
|
28
|
|
|
- teams: List of team names which will be responsible for the alert |
|
29
|
|
|
- alias: Used for alert deduplication. |
|
30
|
|
|
- description: detailed description of the alert. |
|
31
|
|
|
- recipients: Optional user, group, schedule or escalation names. |
|
32
|
|
|
- actions: A comma separated list of actions that can be executed. |
|
33
|
|
|
- source: Field to specify source of alert. |
|
34
|
|
|
- tags: A comma separated list of labels attached to the alert. |
|
35
|
|
|
- details: Set of user defined properties. |
|
36
|
|
|
- entity: The entity the alert is related to. |
|
37
|
|
|
- user: Default owner of the execution. |
|
38
|
|
|
- note: Additional alert note. |
|
39
|
|
|
|
|
40
|
|
|
Returns: |
|
41
|
|
|
- dict: Data returned by OpsGenie. |
|
42
|
|
|
|
|
43
|
|
|
Raises: |
|
44
|
|
|
- ValueError: If description or message is too long. |
|
45
|
|
|
""" |
|
46
|
|
|
|
|
47
|
|
|
if len(message) > 130: |
|
48
|
|
|
raise ValueError("Message length ({}) is over 130 chars".format( |
|
49
|
|
|
len(message))) |
|
50
|
|
|
|
|
51
|
|
|
body = {"apiKey": self.api_key, |
|
52
|
|
|
"message": message} |
|
53
|
|
|
|
|
54
|
|
|
if teams: |
|
55
|
|
|
body["teams"] = teams |
|
56
|
|
|
|
|
57
|
|
|
if alias: |
|
58
|
|
|
if len(alias) > 512: |
|
59
|
|
|
raise ValueError("alias is too long, can't be over 512 chars.") |
|
60
|
|
|
else: |
|
61
|
|
|
body["alias"] = alias |
|
62
|
|
|
|
|
63
|
|
|
if description: |
|
64
|
|
|
if len(description) > 15000: |
|
65
|
|
|
raise ValueError("Description is too long, can't be over 15000 chars.") |
|
66
|
|
|
else: |
|
67
|
|
|
body["description"] = description |
|
68
|
|
|
|
|
69
|
|
|
if recipients: |
|
70
|
|
|
body["recipients"] = recipients |
|
71
|
|
|
|
|
72
|
|
|
if actions: |
|
73
|
|
|
body["actions"] = actions |
|
74
|
|
|
|
|
75
|
|
|
if source: |
|
76
|
|
|
if len(source) > 512: |
|
77
|
|
|
raise ValueError("Source is too long, can't be over 512 chars.") |
|
78
|
|
|
else: |
|
79
|
|
|
body["source"] = source |
|
80
|
|
|
|
|
81
|
|
|
if tags: |
|
82
|
|
|
body["tags"] = tags |
|
83
|
|
|
|
|
84
|
|
|
if details: |
|
85
|
|
|
body["details"] = details |
|
86
|
|
|
|
|
87
|
|
|
if entity: |
|
88
|
|
|
if len(entity) > 512: |
|
89
|
|
|
raise ValueError("Entity is too long, can't be over 512 chars.") |
|
90
|
|
|
else: |
|
91
|
|
|
body["entity"] = entity |
|
92
|
|
|
|
|
93
|
|
|
if user: |
|
94
|
|
|
if len(user) > 100: |
|
95
|
|
|
raise ValueError("User is too long, can't be over 100 chars.") |
|
96
|
|
|
else: |
|
97
|
|
|
body['user'] = user |
|
98
|
|
|
|
|
99
|
|
|
if note: |
|
100
|
|
|
body['note'] = note |
|
101
|
|
|
|
|
102
|
|
|
data = self._req("POST", |
|
103
|
|
|
"v1/json/alert", |
|
104
|
|
|
body=body) |
|
105
|
|
|
|
|
106
|
|
|
return data |
|
107
|
|
|
|