Completed
Pull Request — master (#469)
by
unknown
02:24
created

Icinga2GetHost.run()   B

Complexity

Conditions 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 8.439
cc 5
1
#!/usr/bin/env python
2
from lib.client import Client
3
from lib.icinga2action import Icinga2Action
4
5
import sys,urllib
6
7
class Icinga2GetHost(Icinga2Action):
8
        
9
    def run(self, hosts):
10
        api_suffix = ''
11
        if hosts:
12
            if len(hosts) == 1:
13
                api_suffix = '?host=' + urllib.quote_plus(hosts[0])
14
            else:
15
                api_suffix += '?'
16
                for host in hosts[:-1]:
17
                    api_suffix += 'hosts=' + urllib.quote_plus(host) + '&'
18
                api_suffix += 'hosts=' + urllib.quote_plus(hosts[-1])
19
            
20
        self.logger.debug('URL suffix: %s', api_suffix)
21
        self.set_method('get')
22
        self.set_path('/objects/hosts' + str(api_suffix))        
23
        self.logger.debug('Action Icinga2GetHost started')
24
        client = self.get_client()
25
        client.make_call()
26
        if self.get_error() == 0:
27
            return self.get_body()
28
        else:
29
            sys.exit(self.get_error())
30
    
31