79370025675346c00c18625a949e0bbbb5168737
[sdc.git] / catalog-be / src / main / resources / scripts / sdcBePy / tosca / models / node_type_client.py
1 #  -
2 #   ============LICENSE_START=======================================================
3 #   Copyright (C) 2021 Nordix Foundation.
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 #   SPDX-License-Identifier: Apache-2.0
18 #   ============LICENSE_END=========================================================
19 #
20 #        http://www.apache.org/licenses/LICENSE-2.0
21 #  Unless required by applicable law or agreed to in writing, software
22 #  distributed under the License is distributed on an "AS IS" BASIS,
23 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 #  See the License for the specific language governing permissions and
25 #  limitations under the License.
26 #
27 #  SPDX-License-Identifier: Apache-2.0
28 #  ============LICENSE_END========================================================
29
30 from io import BytesIO
31 from pathlib import Path
32
33 import pycurl
34
35 from sdcBePy.common import logger
36
37
38 class NodeTypeClient:
39
40     def __init__(self, sdc_be_proxy):
41         self.__base_path = Path('/sdc2/rest/v1/catalog/upload')
42         self.__import_all_path = self.__base_path / 'resource/import'
43         self.__sdc_be_proxy = sdc_be_proxy
44
45     def import_all(self, node_type_yaml_path, node_type_metadata_json_str, is_update=False):
46         logger.debug("Starting to import node types '{}'".format(node_type_yaml_path))
47
48         multi_part_form_data = []
49
50         node_type_yaml_param = ('nodeTypesYaml', (pycurl.FORM_FILE, str(node_type_yaml_path)))
51         multi_part_form_data.append(node_type_yaml_param)
52
53         node_type_metadata_json_param = ('nodeTypeMetadataJson', (
54             pycurl.FORM_CONTENTS, node_type_metadata_json_str,
55             pycurl.FORM_CONTENTTYPE, 'application/json'
56         ))
57         multi_part_form_data.append(node_type_metadata_json_param)
58
59         if is_update is not None:
60             create_new_version_param = ('createNewVersion', (
61                 pycurl.FORM_CONTENTS, str(is_update).lower(),
62                 pycurl.FORM_CONTENTTYPE, 'text/plain'
63             ))
64             multi_part_form_data.append(create_new_version_param)
65
66         response_buffer = BytesIO()
67         response_code = self.__sdc_be_proxy.post_file(str(self.__import_all_path), multi_part_form_data,
68                                                       response_buffer)
69         logger.debug("Import all node types response code '{}'".format(response_code))
70         if response_code != 201:
71             error_msg = "Failed to import node types '{}'".format(node_type_yaml_path)
72             logger.log(error_msg, response_buffer.getvalue())
73             raise Exception(error_msg)
74         logger.log("Failed to import node types '{}'".format(node_type_yaml_path))
75