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

DatabaseRdsSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %
Metric Value
dl 0
loc 47
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _id_generator() 0 3 1
B run() 0 37 5
A _user_name() 0 4 1
1
import re
2
import uuid
3
import sys
4
5
from st2actions.runners.pythonrunner import Action
6
7
8
class DatabaseRdsSpec(Action):
9
    def run(self, payload, config):
10
        # take the payload name and replace any non-alphanumerical characters with "-"
11
        # to create a name for the database
12
        try:
13
            db_name = re.sub('[^0-9a-zA-Z]+', '-', payload['name']) + "-" + payload['namespace']
14
        except:
15
            sys.exit()
16
        # Lets get a username generated
17
        try:
18
            user_name = self._user_name(uid=payload['uid'])
19
        except:
20
            self.logger.info("No name or namespace in payload")
21
            sys.exit()
22
23
        l = dict(self.config['rds'])
24
        pw = self._id_generator()
25
26
        newpayload = {
27
            'db_name': db_name,
28
            'user_name': user_name,
29
            'pw': pw
30
        }
31
32
        # Parse through config.yaml for rds:. If rds.key exists in labels.keys(),
33
        # use label.value otherwise use default value from config.yaml
34
        # then add to newpayload dict.
35
        for i in l.keys():
36
            if i in payload['labels'].keys():
37
                key = i
38
                value = payload['labels'][i]
39
                newpayload[key] = value
40
            else:
41
                key = i
42
                valuealt = l[i]
43
                newpayload[key] = valuealt
44
45
        return newpayload
46
47
    def _user_name(self, uid):
48
        short_uid = uid[0:7]
49
        return "db_" + short_uid
50
        pass
51
52
    def _id_generator(self):
53
        return uuid.uuid4().hex
54
        pass
55