|
| 1 | +""" |
| 2 | +Copyright (c) 2022 Oracle Corporation and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 4 | +
|
| 5 | +Methods to update an output file with information from the kubernetes section of the model. |
| 6 | +""" |
| 7 | +from java.io import File |
| 8 | + |
| 9 | +from oracle.weblogic.deploy.yaml import YamlException |
| 10 | + |
| 11 | +from oracle.weblogic.deploy.util import PyOrderedDict |
| 12 | +from wlsdeploy.aliases.model_constants import KUBERNETES |
| 13 | +from wlsdeploy.logging.platform_logger import PlatformLogger |
| 14 | +from wlsdeploy.tool.extract import wko_schema_helper |
| 15 | +from wlsdeploy.tool.extract.domain_resource_extractor import COMPONENT |
| 16 | +from wlsdeploy.tool.extract.domain_resource_extractor import DEFAULT_KIND |
| 17 | +from wlsdeploy.tool.extract.domain_resource_extractor import KIND |
| 18 | +from wlsdeploy.tool.extract.domain_resource_extractor import SPEC |
| 19 | +from wlsdeploy.tool.extract.domain_resource_extractor import TEMPLATE |
| 20 | +from wlsdeploy.tool.extract.domain_resource_extractor import VERRAZZANO_WEBLOGIC_WORKLOAD |
| 21 | +from wlsdeploy.tool.extract.domain_resource_extractor import WORKLOAD |
| 22 | +from wlsdeploy.util import dictionary_utils |
| 23 | +from wlsdeploy.yaml.yaml_translator import PythonToYaml |
| 24 | +from wlsdeploy.yaml.yaml_translator import YamlToPython |
| 25 | + |
| 26 | +__class_name = 'output_file_helper' |
| 27 | +__logger = PlatformLogger('wlsdeploy.tool.util') |
| 28 | + |
| 29 | + |
| 30 | +def update_from_model(output_dir, output_file_name, model): |
| 31 | + """ |
| 32 | + Update the output content with information from the kubernetes section of the model. |
| 33 | + Output files are (currently) always Kubernetes resource files. |
| 34 | + :param output_dir: the directory of the output file to update |
| 35 | + :param output_file_name: the name of the output file |
| 36 | + :param model: the model to use for update |
| 37 | + """ |
| 38 | + _method_name = 'update_from_model' |
| 39 | + |
| 40 | + # if model doesn't have kubernetes section, return |
| 41 | + kubernetes_content = model.get_model_kubernetes() |
| 42 | + if not kubernetes_content: |
| 43 | + return |
| 44 | + |
| 45 | + # failures will be logged as severe, but not cause tool failure. |
| 46 | + # this will allow the unaltered output file to be examined for problems. |
| 47 | + |
| 48 | + output_file = File(output_dir, output_file_name) |
| 49 | + __logger.info('WLSDPLY-01675', output_file, KUBERNETES, class_name=__class_name, method_name=_method_name) |
| 50 | + |
| 51 | + try: |
| 52 | + reader = YamlToPython(output_file.getPath(), True) |
| 53 | + documents = reader.parse_documents() |
| 54 | + except YamlException, ex: |
| 55 | + __logger.severe('WLSDPLY-01673', output_file, str(ex), error=ex, class_name=__class_name, |
| 56 | + method_name=_method_name) |
| 57 | + return |
| 58 | + |
| 59 | + _update_documents(documents, kubernetes_content, output_file.getPath()) |
| 60 | + |
| 61 | + try: |
| 62 | + writer = PythonToYaml(documents) |
| 63 | + writer.write_to_yaml_file(output_file.getPath()) |
| 64 | + except YamlException, ex: |
| 65 | + __logger.severe('WLSDPLY-01674', output_file, str(ex), error=ex, class_name=__class_name, |
| 66 | + method_name=_method_name) |
| 67 | + return |
| 68 | + |
| 69 | + |
| 70 | +def _update_documents(documents, kubernetes_content, output_file_path): |
| 71 | + _method_name = '_update_documents' |
| 72 | + found = False |
| 73 | + |
| 74 | + # update section(s) based on their kind, etc. |
| 75 | + for document in documents: |
| 76 | + if isinstance(document, dict): |
| 77 | + kind = dictionary_utils.get_element(document, KIND) |
| 78 | + |
| 79 | + # is this a standard WKO document? |
| 80 | + if kind == DEFAULT_KIND: |
| 81 | + _update_dictionary(document, kubernetes_content, None, output_file_path) |
| 82 | + found = True |
| 83 | + |
| 84 | + # is this a Verrazzano WebLogic workload document? |
| 85 | + elif kind == COMPONENT: |
| 86 | + spec = dictionary_utils.get_dictionary_element(document, SPEC) |
| 87 | + workload = dictionary_utils.get_dictionary_element(spec, WORKLOAD) |
| 88 | + component_kind = dictionary_utils.get_element(workload, KIND) |
| 89 | + if component_kind == VERRAZZANO_WEBLOGIC_WORKLOAD: |
| 90 | + component_spec = _get_or_create_dictionary(workload, SPEC) |
| 91 | + component_template = _get_or_create_dictionary(component_spec, TEMPLATE) |
| 92 | + _update_dictionary(component_template, kubernetes_content, None, output_file_path) |
| 93 | + found = True |
| 94 | + |
| 95 | + if not found: |
| 96 | + __logger.warning('WLSDPLY-01676', output_file_path, class_name=__class_name, method_name=_method_name) |
| 97 | + |
| 98 | + |
| 99 | +def _update_dictionary(output_dictionary, model_dictionary, schema_path, output_file_path): |
| 100 | + """ |
| 101 | + Update output_dictionary with attributes from model_dictionary. |
| 102 | + :param output_dictionary: the dictionary to be updated |
| 103 | + :param model_dictionary: the dictionary to update from (type previously validated) |
| 104 | + :param schema_path: used for wko_schema_helper lookups and logging |
| 105 | + :param output_file_path: used for logging |
| 106 | + """ |
| 107 | + _method_name = '_update_dictionary' |
| 108 | + if not isinstance(output_dictionary, dict): |
| 109 | + __logger.warning('WLSDPLY-01677', schema_path, output_file_path, class_name=__class_name, |
| 110 | + method_name=_method_name) |
| 111 | + return |
| 112 | + |
| 113 | + for key, value in model_dictionary.items(): |
| 114 | + if key not in output_dictionary: |
| 115 | + output_dictionary[key] = value |
| 116 | + elif isinstance(value, dict): |
| 117 | + next_schema_path = wko_schema_helper.append_path(schema_path, key) |
| 118 | + _update_dictionary(output_dictionary[key], value, next_schema_path, output_file_path) |
| 119 | + elif isinstance(value, list): |
| 120 | + if not value: |
| 121 | + # if the model has an empty list, override output value |
| 122 | + output_dictionary[key] = value |
| 123 | + else: |
| 124 | + next_schema_path = wko_schema_helper.append_path(schema_path, key) |
| 125 | + _update_list(output_dictionary[key], value, next_schema_path, output_file_path) |
| 126 | + else: |
| 127 | + output_dictionary[key] = value |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +def _update_list(output_list, model_list, schema_path, output_file_path): |
| 132 | + """ |
| 133 | + Update output_list from model_list, overriding or merging existing values |
| 134 | + :param output_list: the list to be updated |
| 135 | + :param model_list: the list to update from (type previously validated) |
| 136 | + :param schema_path: used for wko_schema_helper lookups and logging |
| 137 | + :param output_file_path: used for logging |
| 138 | + """ |
| 139 | + _method_name = '_update_list' |
| 140 | + if not isinstance(output_list, list): |
| 141 | + __logger.warning('WLSDPLY-01678', schema_path, output_file_path, class_name=__class_name, |
| 142 | + method_name=_method_name) |
| 143 | + return |
| 144 | + |
| 145 | + for item in model_list: |
| 146 | + if isinstance(item, dict): |
| 147 | + match = _find_object_match(item, output_list, schema_path) |
| 148 | + if match: |
| 149 | + _update_dictionary(match, item, schema_path, output_file_path) |
| 150 | + else: |
| 151 | + output_list.append(item) |
| 152 | + elif item not in output_list: |
| 153 | + output_list.append(item) |
| 154 | + |
| 155 | + |
| 156 | +def _find_object_match(item, match_list, schema_path): |
| 157 | + """ |
| 158 | + Find an object in match_list that has a name matching the item. |
| 159 | + :param item: the item to be matched |
| 160 | + :param match_list: a list of items |
| 161 | + :param schema_path: used for wko_schema_helper key lookup |
| 162 | + :return: a matching dictionary object |
| 163 | + """ |
| 164 | + key = wko_schema_helper.get_object_list_key(schema_path) |
| 165 | + item_key = item[key] |
| 166 | + if item_key: |
| 167 | + for match_item in match_list: |
| 168 | + if isinstance(match_item, dict): |
| 169 | + if item_key == match_item[key]: |
| 170 | + return match_item |
| 171 | + return None |
| 172 | + |
| 173 | + |
| 174 | +def _get_or_create_dictionary(dictionary, key): |
| 175 | + if key not in dictionary: |
| 176 | + dictionary[key] = PyOrderedDict() |
| 177 | + |
| 178 | + return dictionary[key] |
0 commit comments