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
25 from ONAPLibrary.Utilities import Utilities
28 class HEATKeywords(object):
29 """Utilities useful for constructing OpenStack HEAT requests. """
32 super(HEATKeywords, self).__init__()
33 self.application_id = "robot-ete"
34 self.uuid = Utilities()
35 self.builtin = BuiltIn()
38 def get_yaml(self, template_file):
39 """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included
40 in an Openstack Add Stack Request"""
41 if isinstance(template_file, str) or isinstance(template_file, unicode):
42 fin = open(template_file, 'r')
43 yamlobj = yaml.load(fin)
48 def template_yaml_to_json(self, template_file):
49 """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included
50 in an Openstack Add Stack Request"""
52 if isinstance(template_file, str) or isinstance(template_file, unicode):
53 fin = open(template_file, 'r')
54 yamlobj = yaml.load(fin)
56 if 'heat_template_version' in yamlobj:
57 datetime = yamlobj['heat_template_version']
58 yamlobj['heat_template_version'] = str(datetime)
60 json.dump(yamlobj, fout)
61 contents = fout.getvalue()
66 def env_yaml_to_json(self, template_file):
67 """Env Yaml To JSon reads a YAML Heat env file and returns a JSON string that can be used included
68 in an Openstack Add Stack Request"""
69 if isinstance(template_file, str) or isinstance(template_file, unicode):
70 fin = open(template_file, 'r')
71 yamlobj = yaml.load(fin)
73 if 'parameters' in yamlobj:
75 json.dump(yamlobj['parameters'], fout)
76 contents = fout.getvalue()
82 def stack_info_parse(self, stack_info):
83 """ returns a flattened version of the Openstack Find Stack results """
85 if isinstance(stack_info, dict):
86 s = stack_info['stack']
90 d['name'] = s['stack_name']
91 d['stack_status'] = s['stack_status']
95 def match_fingerprint(self, pvt_file, pw, fingerprint):
97 ssh_key = RSAKey.from_private_key_file(pvt_file, pw)
98 keybytes = md5(ssh_key.asbytes()).hexdigest()
99 printable_fingerprint = ':'.join(a + b for a, b in zip(keybytes[::2], keybytes[1::2]))
100 return printable_fingerprint == fingerprint.__str__()
101 except PasswordRequiredException:
105 def match_private_key_file_to_keypair(self, files, keypair):
106 for keyfile in files:
107 if self.match_fingerprint(keyfile, None, keypair['keypair']['fingerprint']):
112 def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
115 versions = server['addresses'][network_name]
116 for version in versions:
117 if version['version'] == ipversion:
118 ipaddr = version['addr']