RIC-769: Committing individual files rather than tar archive
[ric-plt/appmgr.git] / xapp_orchestrater / dev / xapp_onboarder / xapp_onboarder / api / charts.py
1 ################################################################################
2 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
3 #                                                                              #
4 #   Licensed under the Apache License, Version 2.0 (the "License");            #
5 #   you may not use this file except in compliance with the License.           #
6 #   You may obtain a copy of the License at                                    #
7 #                                                                              #
8 #       http://www.apache.org/licenses/LICENSE-2.0                             #
9 #                                                                              #
10 #   Unless required by applicable law or agreed to in writing, software        #
11 #   distributed under the License is distributed on an "AS IS" BASIS,          #
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13 #   See the License for the specific language governing permissions and        #
14 #   limitations under the License.                                             #
15 ################################################################################
16
17 import logging
18 import io
19 import tarfile
20 from xapp_onboarder.repo_manager.repo_manager import repo_manager, RepoManagerError
21 from xapp_onboarder.api.models.response_models import error_message_model, response
22
23 log = logging.getLogger(__name__)
24
25
26 def get_charts_list(xapp_chart_name=None):
27
28     if not repo_manager.is_repo_ready():
29         response_message = response(model=error_message_model, status_code=500,
30                                     error_source="xapp_onboarder",
31                                     error_message="Cannot connect to local helm repo.",
32                                     status="Service not ready.")
33         return response_message.get_return()
34
35     try:
36         content = repo_manager.get_xapp_list(xapp_chart_name=xapp_chart_name)
37     except RepoManagerError as err:
38         log.error(str(err))
39         response_message = response(model=error_message_model, status_code=err.status_code,
40                                     error_source="charts_fetcher",
41                                     error_message=str(err),
42                                     status="Fetching helm chart list failed")
43         return response_message.get_return()
44     return content, 200
45
46
47 def download_chart_package(xapp_chart_name, version):
48
49     if not repo_manager.is_repo_ready():
50         response_message = response(model=error_message_model, status_code=500,
51                                     error_source="xapp_onboarder",
52                                     error_message="Cannot connect to local helm repo.",
53                                     status="Service not ready.")
54         return response_message.get_return()
55     try:
56         content = repo_manager.download_xapp_chart(xapp_chart_name=xapp_chart_name, version=version)
57     except RepoManagerError as err:
58         log.error(str(err))
59         response_message = response(model=error_message_model, status_code=err.status_code,
60                                     error_source="charts_fetcher",
61                                     error_message=str(err),
62                                     status="Downloading helm chart package failed")
63         return response_message.get_return()
64     return content, 200
65
66
67 def download_values_yaml(xapp_chart_name, version):
68
69     content, status = download_chart_package(xapp_chart_name=xapp_chart_name, version=version)
70
71     if status != 200:
72         return content, status
73
74     file_stream = io.BytesIO(content)
75
76     with tarfile.open(fileobj=file_stream) as tar:
77         values_yaml_file = tar.extractfile(xapp_chart_name + '/values.yaml')
78         return_response = values_yaml_file.read()
79
80     return return_response, 200
81