aai should not check status code
[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
25 from ONAPLibrary.Utilities import Utilities
26
27
28 class HEATKeywords(object):
29     """Utilities useful for constructing OpenStack HEAT requests. """
30
31     def __init__(self):
32         super(HEATKeywords, self).__init__()
33         self.application_id = "robot-ete"
34         self.uuid = Utilities()
35         self.builtin = BuiltIn()
36
37     @keyword
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)
44             return yamlobj
45         return None
46
47     @keyword
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"""
51         contents = None
52         if isinstance(template_file, str) or isinstance(template_file, unicode):
53             fin = open(template_file, 'r')
54             yamlobj = yaml.load(fin)
55             fin.close()
56             if 'heat_template_version' in yamlobj:
57                 datetime = yamlobj['heat_template_version']
58                 yamlobj['heat_template_version'] = str(datetime)
59             fout = io.BytesIO()
60             json.dump(yamlobj, fout)
61             contents = fout.getvalue()
62             fout.close()
63         return contents
64
65     @keyword
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)
72             fin.close()
73             if 'parameters' in yamlobj:
74                 fout = io.BytesIO()
75                 json.dump(yamlobj['parameters'], fout)
76                 contents = fout.getvalue()
77                 fout.close()
78                 return contents
79         return None
80
81     @keyword
82     def stack_info_parse(self, stack_info):
83         """ returns a flattened version of the Openstack Find Stack results """
84         d = {}
85         if isinstance(stack_info, dict):
86             s = stack_info['stack']
87             p = s['parameters']
88             d = copy.deepcopy(p)
89             d['id'] = s['id']
90             d['name'] = s['stack_name']
91             d['stack_status'] = s['stack_status']
92         return d
93
94     @keyword
95     def match_fingerprint(self, pvt_file, pw, fingerprint):
96         try:
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:
102             return False
103
104     @keyword
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']):
108                 return keyfile
109         return None
110
111     @keyword
112     def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
113         ipaddr = None
114         try:
115             versions = server['addresses'][network_name]
116             for version in versions:
117                 if version['version'] == ipversion:
118                     ipaddr = version['addr']
119                     break
120         except ValueError:
121             return ipaddr
122         return ipaddr