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
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import json |
17
|
|
|
import requests |
18
|
|
|
|
19
|
|
|
from st2actions.runners.pythonrunner import Action |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class OpsGenieBaseAction(Action): |
23
|
|
|
def __init__(self, config): |
24
|
|
|
super(OpsGenieBaseAction, self).__init__(config) |
25
|
|
|
|
26
|
|
|
self.client = None |
27
|
|
|
self.session = requests.Session() |
28
|
|
|
|
29
|
|
|
try: |
30
|
|
|
self.api_key = self.config["api_key"] |
31
|
|
|
self.api_host = self.config["api_host"] |
32
|
|
|
except KeyError: |
33
|
|
|
raise ValueError("api_key and api_host needs to be configured!") |
34
|
|
|
|
35
|
|
|
def _url(self, uri): |
|
|
|
|
36
|
|
|
""" |
37
|
|
|
""" |
38
|
|
|
return "{}/{}".format(self.api_host.rstrip("/"), |
39
|
|
|
uri) |
40
|
|
|
|
41
|
|
|
def _req(self, method, uri, payload=None, body=None): |
|
|
|
|
42
|
|
|
""" |
43
|
|
|
""" |
44
|
|
|
kwargs = {} |
45
|
|
|
|
46
|
|
|
if payload is None and body is None: |
47
|
|
|
raise ValueError("Need body or payload") |
48
|
|
|
|
49
|
|
|
if payload: |
50
|
|
|
kwargs["params"] = payload |
51
|
|
|
|
52
|
|
|
if body: |
53
|
|
|
kwargs['data'] = json.dumps(body) |
54
|
|
|
|
55
|
|
|
try: |
56
|
|
|
r = self.session.request(method, |
57
|
|
|
self._url(uri), |
58
|
|
|
**kwargs) |
59
|
|
|
r.raise_for_status() |
60
|
|
|
except requests.exceptions.HTTPError: |
61
|
|
|
raise ValueError("HTTP error: {}: '{}'".format( |
62
|
|
|
r.status_code, r.text)) |
63
|
|
|
|
64
|
|
|
return r.json() |
65
|
|
|
|