RIC-769: Committing individual files rather than tar archive
[ric-plt/appmgr.git] / xapp_orchestrater / dev / xapp_onboarder / tests / mock_helm_repo / mock_helm_repo.py
1 #!/usr/bin/env python3
2 ################################################################################
3 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17
18 import pkg_resources
19 import logging.config
20 import yaml
21 import json
22 import os
23 import tarfile
24 from flask import Flask, jsonify, send_file
25 from tests.constants import config_file, controls_schema_file, helm_repo_index_response
26 from xapp_onboarder.server import settings
27
28
29 logger_config = pkg_resources.resource_filename("xapp_onboarder", 'logging.conf')
30 logging.config.fileConfig(logger_config)
31 log = logging.getLogger(__name__)
32
33 listen_address = '0.0.0.0:8080'
34 app = Flask(__name__)
35 app.config['SERVER_NAME'] = listen_address
36
37
38
39
40 @app.route('/')
41 def root_dir():
42     return {'health': 'OK'}
43
44 @app.route('/index.yaml')
45 def get_index():
46     return yaml.dump(helm_repo_index_response)
47
48 @app.route('/api/charts', methods=['POST'])
49 def upload_chart():
50     return {"saved": True}, 201
51
52 @app.route('/api/charts/test_xapp/1.0.0', methods=['DELETE'])
53 def delete_chart():
54     return {"deleted":True}, 200
55
56 @app.route('/api/charts/test_xapp', methods=['GET'])
57 def get_all_xapp_version():
58     test_xapp = helm_repo_index_response['entries']['test_xapp']
59     return jsonify(test_xapp), 200
60
61 @app.route('/api/charts', methods=['GET'])
62 def get_all_xapp():
63     return jsonify(helm_repo_index_response['entries']), 200
64
65 @app.route('/charts/test_xapp-1.0.0.tgz', methods=['GET'])
66 def download_xapp_helm_package():
67     if not os.path.exists(settings.MOCK_TEST_HELM_REPO_TEMP_DIR):
68         os.makedirs(settings.MOCK_TEST_HELM_REPO_TEMP_DIR)
69     with open(settings.MOCK_TEST_HELM_REPO_TEMP_DIR + '/values.yaml', 'w') as file:
70         file.write(json.dumps(helm_repo_index_response))
71
72     with tarfile.open(settings.MOCK_TEST_HELM_REPO_TEMP_DIR + '/test_xapp-1.0.0.tgz', "w:gz") as tar:
73         tar.addfile(tarfile.TarInfo("test_xapp/values.yaml"), open(settings.MOCK_TEST_HELM_REPO_TEMP_DIR + '/values.yaml'))
74
75     return send_file(settings.MOCK_TEST_HELM_REPO_TEMP_DIR + '/test_xapp-1.0.0.tgz', mimetype='application/zip')
76
77
78
79 @app.route('/schema.json', methods=['GET'])
80 def get_schema():
81
82
83     return controls_schema_file, 200
84
85 @app.route('/config-file.json', methods=['GET'])
86 def get_config_file():
87
88
89     return config_file, 200
90
91 def run():
92     log.info('>>>>> Starting mock helm_repo at http://{}<<<<<'.format(listen_address))
93     app.run(debug=True)
94
95 if __name__ == '__main__':
96     run()
97
98
99
100
101
102