Completed
Pull Request — master (#504)
by
unknown
03:08
created

CreateAlertAction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 76 14
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
            body["alias"] = alias
59
60
        if description:
61
            if len(description) > 15000:
62
                raise ValueError("Description is too long, can't be over 15000 chars")
63
            else:
64
                body["description"] = description
65
66
        if recipients:
67
            body["recipients"] = recipients
68
69
        if actions:
70
            body["actions"] = actions
71
72
        if source:
73
            body["source"] = source
74
75
        if tags:
76
            body["tags"] = tags
77
78
        if details:
79
            body["details"] = details
80
81
        if entity:
82
            body["entity"] = entity
83
84
        if user:
85
            body['user'] = user
86
87
        if note:
88
            body['note'] = note
89
90
        data = self._req("POST",
91
                         "v1/json/alert",
92
                         body=body)
93
94
        return data
95