Completed
Pull Request — master (#395)
by
unknown
01:49
created

ThirdPartyResource   A

Complexity

Total Complexity 12

Size/Duplication

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A remove_trigger() 0 2 1
A run() 0 14 2
A cleanup() 0 2 1
A setup() 0 9 1
A _build_a_trigger() 0 16 1
A __init__() 0 3 1
A add_trigger() 0 2 1
A _k8s_object() 0 16 2
A update_trigger() 0 2 1
A _process_message() 0 3 1
1
#!/usr/bin/env python
2
import base64
0 ignored issues
show
Unused Code introduced by
The import base64 seems to be unused.
Loading history...
3
import json
4
import requests
5
from requests.auth import HTTPBasicAuth
6
import yaml
0 ignored issues
show
Unused Code introduced by
The import yaml seems to be unused.
Loading history...
7
import re
0 ignored issues
show
Unused Code introduced by
The import re seems to be unused.
Loading history...
8
import os, sys
0 ignored issues
show
Unused Code introduced by
The import os seems to be unused.
Loading history...
Unused Code introduced by
The import sys seems to be unused.
Loading history...
9
import pprint
0 ignored issues
show
Unused Code introduced by
The import pprint seems to be unused.
Loading history...
10
import ast
11
import uuid
0 ignored issues
show
Unused Code introduced by
The import uuid seems to be unused.
Loading history...
12
# from [email protected]:mward29/python-k8sclient.git
13
14
from st2reactor.sensor.base import Sensor
15
16
17
class ThirdPartyResource(Sensor):
18
    def __init__(self, sensor_service, config=None):
19
        super(ThirdPartyResource, self).__init__(sensor_service=sensor_service,
20
                                                  config=config)
21
#        self._labels = self._config['labels'].get('thirdpartyresource', [])\
22
23
    def setup(self):
24
        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...
25
        self._logger.debug('Connecting to Kubernetes via api_client')
26
        extension = self._config['extension_url']
27
        KUBERNETES_API_URL = self._config['kubernetes_api_url'] + extension
28
        user = self._config['user']
29
        password = self._config['password']
30
        self.client = requests.get(KUBERNETES_API_URL, auth=HTTPBasicAuth(user, password), verify=False, stream=True)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
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...
31
        pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
32
33
    def run(self):
34
        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...
35
        self._logger.debug('Watch Kubernetes for thirdpartyresource information')
36
        r = self.client
37
        lines = r.iter_lines()
38
        # Save the first line for later or just skip it
39
        first_line = next(lines)
0 ignored issues
show
Unused Code introduced by
The variable first_line seems to be unused.
Loading history...
40
        for line in lines:
41
            io = json.dumps(line)
42
            n = json.loads(io)
43
            d_list = ast.literal_eval(n)
44
            self._k8s_object(d_list=d_list)
45
#            self._resource_version(d_list=d_list)
46
        pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
47
48
    def _k8s_object(self, d_list):
49
        # Define some variables
50
        resource_type = d_list['type']
51
        object_kind = d_list['object']['kind']
52
        api_version = d_list['object']['apiVersion']
0 ignored issues
show
Unused Code introduced by
The variable api_version seems to be unused.
Loading history...
53
        name = d_list['object']['metadata']['name']
54
        namespace = d_list['object']['metadata']['namespace']
55
        uid = d_list['object']['metadata']['uid']
56
        # Now lets see if labels exist, if so build a trigger
57
        if 'labels' in d_list['object']['metadata']:
58
            labels_data = d_list['object']['metadata']['labels']
59
            self._build_a_trigger(resource_type=resource_type, name=name, labels=labels_data, namespace=namespace, object_kind=object_kind, uid=uid)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (148/100).

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

Loading history...
60
        else:
61
            print("No Labels for the resource below. Tough to proceed without knowing how to work with this object.")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
62
            print(name, namespace, uid)
63
        pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
64
65
    def _build_a_trigger(self, resource_type, name, labels, namespace, object_kind, uid):
66
        trigger = 'kubernetes.thirdpartyobject'
67
        payload = {
68
            'resource': resource_type,
69
            'name': name,
70
            'labels': labels,
71
            'namespace': namespace,
72
            'object_kind': object_kind,
73
            'uid': uid
74
        }
75
        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...
76
        self._logger.debug('Triggering Dispatch Now')
77
78
        # Create dispatch trigger
79
        self._sensor_service.dispatch(trigger=trigger, payload=payload)
80
        pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
81
82
    def cleanup(self):
83
        pass
84
85
    def add_trigger(self,  trigger):
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
def add_trigger(self, trigger):
^
Loading history...
86
        pass
87
88
    def update_trigger(self, trigger):
89
        pass
90
91
    def remove_trigger(self, trigger):
92
        pass
93
94
    def _process_message(self, message):
95
96
        pass
97