Update INFO.yaml with new PTL
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / HEATKeywords.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from robot.api.deco import keyword
16 from robot.libraries.BuiltIn import BuiltIn
17 import json
18 import yaml
19 import io
20 import copy
21 from hashlib import md5
22 from paramiko import RSAKey
23 from paramiko.ssh_exception import PasswordRequiredException
24 from six import string_types
25 from ONAPLibrary.Utilities import Utilities
26
27
28 class HEATKeywords(object):
29     """Utilities useful for constructing OpenStack HEAT requests. """
30
31     ROBOT_LIBRARY_SCOPE = "GLOBAL"
32
33     def __init__(self):
34         super(HEATKeywords, self).__init__()
35         self.application_id = "robot-ete"
36         self.uuid = Utilities()
37         self.builtin = BuiltIn()
38
39     @keyword
40     def get_yaml(self, template_file):
41         """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included
42         in an Openstack Add Stack Request"""
43         if isinstance(template_file, string_types):
44             fin = open(template_file, 'r')
45             yamlobj = yaml.load(fin)
46             return yamlobj
47         return None
48
49     @keyword
50     def template_yaml_to_json(self, template_file):
51         """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included
52         in an Openstack Add Stack Request"""
53         contents = None
54         if isinstance(template_file, string_types):
55             fin = open(template_file, 'r')
56             yamlobj = yaml.load(fin)
57             fin.close()
58             if 'heat_template_version' in yamlobj:
59                 datetime = yamlobj['heat_template_version']
60                 yamlobj['heat_template_version'] = str(datetime)
61             fout = io.BytesIO()
62             json.dump(yamlobj, fout)
63             contents = fout.getvalue()
64             fout.close()
65         return contents
66
67     @keyword
68     def env_yaml_to_json(self, template_file):
69         """Env Yaml To JSon reads a YAML Heat env file and returns a JSON string that can be used included
70         in an Openstack Add Stack Request"""
71         if isinstance(template_file, string_types):
72             fin = open(template_file, 'r')
73             yamlobj = yaml.load(fin)
74             fin.close()
75             if 'parameters' in yamlobj:
76                 fout = io.BytesIO()
77                 json.dump(yamlobj['parameters'], fout)
78                 contents = fout.getvalue()
79                 fout.close()
80                 return contents
81         return None
82
83     @keyword
84     def stack_info_parse(self, stack_info):
85         """ returns a flattened version of the Openstack Find Stack results """
86         d = {}
87         if isinstance(stack_info, dict):
88             s = stack_info['stack']
89             p = s['parameters']
90             d = copy.deepcopy(p)
91             d['id'] = s['id']
92             d['name'] = s['stack_name']
93             d['stack_status'] = s['stack_status']
94         return d
95
96     @keyword
97     def match_fingerprint(self, pvt_file, pw, fingerprint):
98         try:
99             ssh_key = RSAKey.from_private_key_file(pvt_file, pw)
100             keybytes = md5(ssh_key.asbytes()).hexdigest()
101             printable_fingerprint = ':'.join(a + b for a, b in zip(keybytes[::2], keybytes[1::2]))
102             return printable_fingerprint == fingerprint.__str__()
103         except PasswordRequiredException:
104             return False
105
106     @keyword
107     def match_private_key_file_to_keypair(self, files, keypair):
108         for keyfile in files:
109             if self.match_fingerprint(keyfile, None, keypair['keypair']['fingerprint']):
110                 return keyfile
111         return None
112
113     @keyword
114     def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
115         ipaddr = None
116         try:
117             versions = server['addresses'][network_name]
118             for version in versions:
119                 if version['version'] == ipversion:
120                     ipaddr = version['addr']
121                     break
122         except ValueError:
123             return ipaddr
124         return ipaddr