93d556a61ae18e93dc610eacaa5daa436e3eb6bf
[testsuite/python-testing-utils.git] / robotframework-onap / eteutils / HEATUtils.py
1 import json
2 import yaml
3 import io
4 import copy
5 from hashlib import md5
6 from paramiko import RSAKey
7 from paramiko.ssh_exception import PasswordRequiredException
8
9
10 class HEATUtils:
11     """ Utilities useful for constructing OpenStack HEAT requests """
12
13     def get_yaml(self, template_file):
14         """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included in an Openstack Add Stack Request"""
15         if isinstance(template_file, str):
16             fin = open(template_file, 'r')
17             yamlobj = yaml.load(fin)
18             return yamlobj
19         return None
20
21     def template_yaml_to_json(self, template_file):
22         """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included in an Openstack Add Stack Request"""
23         if isinstance(template_file, str):
24             fin = open(template_file, 'r')
25             yamlobj = yaml.load(fin)
26             fin.close()
27             if 'heat_template_version' in yamlobj:
28                 datetime = yamlobj['heat_template_version']
29                 yamlobj['heat_template_version'] = str(datetime)
30             fout = io.BytesIO()
31             json.dump(yamlobj, fout)
32             contents = fout.getvalue()
33             fout.close()
34         return contents
35
36     def env_yaml_to_json(self, template_file):
37         """Env Yaml To JSon reads a YAML Heat env file and returns a JSON string that can be used included in an Openstack Add Stack Request"""
38         if isinstance(template_file, str):
39             fin = open(template_file, 'r')
40             yamlobj = yaml.load(fin)
41             fin.close()
42             if 'parameters' in yamlobj:
43                 fout = io.BytesIO()
44                 json.dump(yamlobj['parameters'], fout)
45                 contents = fout.getvalue()
46                 fout.close()
47                 return contents
48         return None
49
50     def stack_info_parse(self, stack_info):
51         """ returns a flattened version of the Openstack Find Stack results """
52         d = {}
53         if isinstance(stack_info, dict):
54             s = stack_info['stack']
55             p = s['parameters']
56             d = copy.deepcopy(p)
57             d['id'] = s['id']
58             d['name'] = s['stack_name']
59             d['stack_status'] = s['stack_status']
60         return d
61
62     def match_fingerprint(self, pvt_file, pw, fingerprint):
63         try:
64             sshKey = RSAKey.from_private_key_file(pvt_file, pw)
65             keybytes = md5(sshKey.asbytes()).hexdigest()
66             printableFingerprint = ':'.join(a + b for a, b in zip(keybytes[::2], keybytes[1::2]))
67             return printableFingerprint == fingerprint.__str__()
68         except PasswordRequiredException:
69             return False
70
71     def match_private_key_file_to_keypair(self, files, keypair):
72         for keyfile in files:
73             if (self.match_fingerprint(keyfile, None, keypair['keypair']['fingerprint'])):
74                 return keyfile
75         return None
76
77     def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
78         ipaddr = None
79         try:
80             versions = server['addresses'][network_name]
81             for version in versions:
82                 if version['version'] == ipversion:
83                     ipaddr = version['addr']
84                     break;
85         except ValueError:
86             return ipaddr
87         return ipaddr