| Total Complexity | 7 | 
| Total Lines | 45 | 
| Duplicated Lines | 0 % | 
| 1 | import re  | 
            ||
| 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 | |||
| 51 | def _id_generator(self):  | 
            ||
| 52 | return uuid.uuid4().hex  | 
            ||
| 53 |