Completed
Pull Request — master (#336)
by James
03:24
created

_post_event_to_st2()   B

Complexity

Conditions 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
dl 0
loc 17
rs 8
1
import requests
2
3
from st2actions.runners.pythonrunner import Action
4
5
6
class GetAppHealthStatusAction(Action):
7
    """
8
    Get health status of new relic application(s).
9
    """
10
    def __init__(self, *args, **kwargs):
11
        super(GetAppHealthStatusAction, Action).__init__(*args, **kwargs)
12
        self.url = 'https://api.newrelic.com/v2/applications.json'
13
        self.headers = {
14
            'User-Agent': 'StackStorm-New-Relic-Sensor/1.0.0 python-requests/2.7.0',
15
            'content-type': 'application/x-www-form-urlencoded',
16
        }
17
        self.headers['X-Api-Key'] = self.config['api_key']
18
19
    def run(self, app_name=None):
20
        params = None
21
        if app_name:
22
            params = {'filter[name]': app_name}
23
24
        resp = requests.get(self.url, headers=self.headers, params=params).json()
25
26
        app_status_map = {}
27
        for application in resp['applications']:
28
            app_status_map[application['name']] = application['health_status']
29
30
        return app_status_map
31