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

ThirdPartyResource   A

Complexity

Total Complexity 12

Size/Duplication

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A remove_trigger() 0 2 1
A run() 0 12 2
A cleanup() 0 2 1
A setup() 0 10 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._logger = self._sensor_service.get_logger(name=self.__class__.__name__)
0 ignored issues
show
Coding Style introduced by
The attribute _logger 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._logger.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._logger = self._sensor_service.get_logger(name=self.__class__.__name__)
0 ignored issues
show
Coding Style introduced by
The attribute _logger 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...
26
        self._logger.debug('Watch Kubernetes for thirdpartyresource information')
27
        r = self.client
28
        lines = r.iter_lines()
29
        # Save the first line for later or just skip it
30
#        first_line = next(lines)
31
        for line in lines:
32
            io = json.dumps(line)
33
            n = json.loads(io)
34
            d_list = ast.literal_eval(n)
35
            self._k8s_object(d_list=d_list)
36
#            self._resource_version(d_list=d_list)
37
38
    def _k8s_object(self, d_list):
39
        # Define some variables
40
        try:
41
            resource_type = d_list['type']
42
            object_kind = d_list['object']['kind']
43
            name = d_list['object']['metadata']['name']
44
            namespace = d_list['object']['metadata']['namespace']
45
            uid = d_list['object']['metadata']['uid']
46
        except:
47
            self.logger.debug("type, kind, name, namespace or uid do not exist in the object.\
48
                              must exit")
49
            sys.exit()
50
        # Now lets see if labels exist, if so build a trigger
51
        if 'labels' in d_list['object']['metadata']:
52
            labels_data = d_list['object']['metadata']['labels']
53
            self._build_a_trigger(resource_type=resource_type, name=name, labels=labels_data,
54
                                  namespace=namespace, object_kind=object_kind, uid=uid)
55
        else:
56
            print("No Labels for the resource below. Tough to proceed without knowing how \
57
                  to work with this object.")
58
            print(name, namespace, uid)
59
60
    def _build_a_trigger(self, resource_type, name, labels, namespace, object_kind, uid):
61
        trigger = 'kubernetes.thirdpartyobject'
62
        payload = {
63
            'resource': resource_type,
64
            'name': name,
65
            'labels': labels,
66
            'namespace': namespace,
67
            'object_kind': object_kind,
68
            'uid': uid
69
        }
70
        self._logger = self._sensor_service.get_logger(name=self.__class__.__name__)
0 ignored issues
show
Coding Style introduced by
The attribute _logger 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...
71
        self._logger.debug('Triggering Dispatch Now')
72
73
        # Create dispatch trigger
74
        self._sensor_service.dispatch(trigger=trigger, payload=payload)
75
76
    def cleanup(self):
77
        pass
78
79
    def add_trigger(self, trigger):
80
        pass
81
82
    def update_trigger(self, trigger):
83
        pass
84
85
    def remove_trigger(self, trigger):
86
        pass
87
88
    def _process_message(self, message):
89
90
        pass
91