1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from robot.api.deco import keyword
16 from robot.libraries.BuiltIn import BuiltIn
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
28 class HEATKeywords(object):
29 """Utilities useful for constructing OpenStack HEAT requests. """
31 ROBOT_LIBRARY_SCOPE = "GLOBAL"
34 super(HEATKeywords, self).__init__()
35 self.application_id = "robot-ete"
36 self.uuid = Utilities()
37 self.builtin = BuiltIn()
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)
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"""
54 if isinstance(template_file, string_types):
55 fin = open(template_file, 'r')
56 yamlobj = yaml.load(fin)
58 if 'heat_template_version' in yamlobj:
59 datetime = yamlobj['heat_template_version']
60 yamlobj['heat_template_version'] = str(datetime)
62 json.dump(yamlobj, fout)
63 contents = fout.getvalue()
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)
75 if 'parameters' in yamlobj:
77 json.dump(yamlobj['parameters'], fout)
78 contents = fout.getvalue()
84 def stack_info_parse(self, stack_info):
85 """ returns a flattened version of the Openstack Find Stack results """
87 if isinstance(stack_info, dict):
88 s = stack_info['stack']
92 d['name'] = s['stack_name']
93 d['stack_status'] = s['stack_status']
97 def match_fingerprint(self, pvt_file, pw, fingerprint):
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:
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']):
114 def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
117 versions = server['addresses'][network_name]
118 for version in versions:
119 if version['version'] == ipversion:
120 ipaddr = version['addr']