Completed
Pull Request — master (#395)
by
unknown
04:05
created

ThirdPartyResource   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %
Metric Value
dl 0
loc 78
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 10 1
A remove_trigger() 0 2 1
A run() 0 11 2
A cleanup() 0 2 1
A _build_a_trigger() 0 15 1
A add_trigger() 0 2 1
A _k8s_object() 0 21 3
A update_trigger() 0 2 1
A _process_message() 0 3 1
1
#!/usr/bin/env python
2
import json
3
import requests
4
import sys
5
from requests.auth import HTTPBasicAuth
6
import ast
7
# from [email protected]:mward29/python-k8sclient.git
8
9
from st2reactor.sensor.base import Sensor
10
11
12
class ThirdPartyResource(Sensor):
13
    def setup(self):
14
        self._log = self._sensor_service.get_logger(__name__)
0 ignored issues
show
Coding Style introduced by
The attribute _log was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
15
        self._log.debug('Connecting to Kubernetes via api_client')
16
        extension = self._config['extension_url']
17
        KUBERNETES_API_URL = self._config['kubernetes_api_url'] + extension
18
        user = self._config['user']
19
        password = self._config['password']
20
#        verify = self._config['verify']
21
        self.client = requests.get(KUBERNETES_API_URL, auth=HTTPBasicAuth(user, password),
0 ignored issues
show
Coding Style introduced by
The attribute client was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
22
                                   verify=False, stream=True)
23
24
    def run(self):
25
        self._log.debug('Watch Kubernetes for thirdpartyresource information')
26
        r = self.client
27
        lines = r.iter_lines()
28
        # Save the first line for later or just skip it
29
#        first_line = next(lines)
30
        for line in lines:
31
            io = json.dumps(line)
32
            n = json.loads(io)
33
            d_list = ast.literal_eval(n)
34
            self._k8s_object(d_list=d_list)
35
#            self._resource_version(d_list=d_list)
36
37
    def _k8s_object(self, d_list):
38
        # Define some variables
39
        try:
40
            resource_type = d_list['type']
41
            object_kind = d_list['object']['kind']
42
            name = d_list['object']['metadata']['name']
43
            namespace = d_list['object']['metadata']['namespace']
44
            uid = d_list['object']['metadata']['uid']
45
        except:
46
            self.log.debug("type, kind, name, namespace or uid do not exist in the object.\
47
                              must exit")
48
            sys.exit()
49
        # Now lets see if labels exist, if so build a trigger
50
        if 'labels' in d_list['object']['metadata']:
51
            labels_data = d_list['object']['metadata']['labels']
52
            self._build_a_trigger(resource_type=resource_type, name=name, labels=labels_data,
53
                                  namespace=namespace, object_kind=object_kind, uid=uid)
54
        else:
55
            self._log.debug("No Labels for the resource below. Tough to proceed without knowing how \
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
56
                  to work with this object.")
57
            self._log.debug(name, namespace, uid)
58
59
    def _build_a_trigger(self, resource_type, name, labels, namespace, object_kind, uid):
60
        trigger = 'kubernetes.thirdpartyobject'
61
        payload = {
62
            'resource': resource_type,
63
            'name': name,
64
            'labels': labels,
65
            'namespace': namespace,
66
            'object_kind': object_kind,
67
            'uid': uid
68
        }
69
70
        self._log.debug('Triggering Dispatch Now')
71
72
        # Create dispatch trigger
73
        self._sensor_service.dispatch(trigger=trigger, payload=payload)
74
75
    def cleanup(self):
76
        pass
77
78
    def add_trigger(self, trigger):
79
        pass
80
81
    def update_trigger(self, trigger):
82
        pass
83
84
    def remove_trigger(self, trigger):
85
        pass
86
87
    def _process_message(self, message):
88
89
        pass
90