Skip to content

Commit eb2e66e

Browse files
authored
Add verrazzano section to model for updating generated resource file (#1365)
* Added CRD schemas for Verrazzano, moved existing schemas to crds directory * Added verrazzano section to model, associate schemas to its CRD folders * Merge Verrazzano model contents to CRD; added unit tests to verify * Add ability for multiple object formats in CRD schemas; renamed schema files * Minimized Verrazzano schemas * Allow multiple content options for Verrazzano application traits in model help * Validate schema folders with multiple content options * Implement merging of schema folders with multiple content options * Revised comments for review * Use unicode_helper for string conversion
1 parent b1ce465 commit eb2e66e

31 files changed

+947
-154
lines changed

core/src/main/python/wlsdeploy/aliases/model_constants.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313
USER_ATTRIBUTES = 'UserAttribute'
314314
USE_SAMPLE_DATABASE = 'UseSampleDatabase'
315315
USE_SSL = "useSSL"
316+
VERRAZZANO = "verrazzano"
316317
VIRTUAL_HOST = 'VirtualHost'
317318
VIRTUAL_TARGET = 'VirtualTarget'
318319
VIRTUAL_USER_AUTHENTICATOR = 'VirtualUserAuthenticator'
@@ -428,7 +429,14 @@
428429
TOPOLOGY,
429430
RESOURCES,
430431
APP_DEPLOYMENTS,
431-
KUBERNETES
432+
KUBERNETES,
433+
VERRAZZANO
434+
]
435+
436+
# the contents of these sections are based on CRD schemas
437+
CRD_MODEL_SECTIONS = [
438+
KUBERNETES,
439+
VERRAZZANO
432440
]
433441

434442
# these domain attributes have special processing in create,

core/src/main/python/wlsdeploy/tool/modelhelp/model_kubernetes_printer.py renamed to core/src/main/python/wlsdeploy/tool/modelhelp/model_crd_section_printer.py

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2020, 2022, Oracle and/or its affiliates.
2+
Copyright (c) 2020, 2023, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
from wlsdeploy.exception import exception_helper
@@ -9,18 +9,20 @@
99
from wlsdeploy.tool.modelhelp import model_help_utils
1010
from wlsdeploy.tool.modelhelp.model_help_utils import ControlOptions
1111
import wlsdeploy.util.unicode_helper as str_helper
12+
from wlsdeploy.util import dictionary_utils
1213
from wlsdeploy.util.exit_code import ExitCode
1314

1415

15-
class ModelKubernetesPrinter(object):
16+
class ModelCrdSectionPrinter(object):
1617
"""
1718
Class for printing kubernetes sections as model samples.
1819
"""
19-
_class_name = "ModelKubernetesPrinter"
20+
_class_name = "ModelCrdSectionPrinter"
2021
_logger = PlatformLogger('wlsdeploy.modelhelp')
2122

2223
def __init__(self, model_context):
2324
self._crd_helper = model_crd_helper.get_helper(model_context)
25+
self._target = model_context.get_target()
2426

2527
def print_model_sample(self, model_path_tokens, control_option):
2628
"""
@@ -30,6 +32,10 @@ def print_model_sample(self, model_path_tokens, control_option):
3032
:raises CLAException: if a problem is encountered
3133
"""
3234
section_name = model_path_tokens[0]
35+
if section_name != self._crd_helper.get_model_section():
36+
print("")
37+
print(exception_helper.get_message('WLSDPLY-10113', section_name))
38+
return
3339

3440
if len(model_path_tokens) == 1:
3541
self._print_model_section_sample(section_name, control_option)
@@ -122,25 +128,49 @@ def _print_model_folder_sample(self, section_name, model_path_tokens, control_op
122128

123129
current_folder = schema
124130
for token in model_path_tokens[token_index:]:
131+
lookup_token = token
132+
option_key = None
133+
134+
# check for a folder with multiple options, such as (verrazzano/.../trait#MetricsTrait)
135+
if '#' in token:
136+
parts = token.split('#', 1)
137+
lookup_token = parts[0]
138+
option_key = parts[1]
139+
125140
properties = _get_properties(current_folder)
126141

127142
valid_subfolder_keys = _get_folder_names(properties)
128-
if token not in valid_subfolder_keys:
143+
if lookup_token not in valid_subfolder_keys:
129144
ex = exception_helper.create_cla_exception(ExitCode.ARG_VALIDATION_ERROR,
130-
"WLSDPLY-10111", model_path, token,
145+
"WLSDPLY-10111", model_path, lookup_token,
131146
', '.join(valid_subfolder_keys))
132147
self._logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
133148
raise ex
134149

135-
current_folder = properties[token]
150+
current_folder = properties[lookup_token]
151+
152+
# find matching option if folder has multiple options, such as (verrazzano/.../trait#MetricsTrait)
153+
token_suffix = ''
154+
folder_options = schema_helper.get_one_of_options(current_folder)
155+
if folder_options and option_key is not None:
156+
token_suffix = ' # ' + option_key
157+
current_folder = self._find_folder_option(folder_options, option_key, model_path + '/' + lookup_token)
136158

137-
_print_indent(token + ":", indent, in_object_array)
159+
_print_indent(lookup_token + ":" + token_suffix, indent, in_object_array)
138160
indent += 1
139161

140162
# apply to the next folder in the path
141163
in_object_array = schema_helper.is_object_array(current_folder)
142164
model_path = model_path + "/" + token
143165

166+
# finished writing the parent folders
167+
168+
# if this is a folder with multiple options, list the options and return
169+
folder_options = schema_helper.get_one_of_options(current_folder)
170+
if folder_options:
171+
print_folder_options(folder_options, model_path, indent)
172+
return
173+
144174
# list the attributes and folders, as specified
145175

146176
if model_help_utils.show_attributes(control_option):
@@ -247,6 +277,33 @@ def _print_attributes_sample(self, schema_folder, indent_level, in_object_array)
247277

248278
return in_object_array
249279

280+
def _find_folder_option(self, folder_options, option_key, model_path):
281+
"""
282+
Return the folder option that matches the specified index key.
283+
Try matching "kind" field of each option, then try lookup by numeric index.
284+
Throw an exception if no matching option is found.
285+
:param folder_options: the options to be examined
286+
:param option_key: the key to check against
287+
:param model_path: used for error logging
288+
:return: the matching option
289+
"""
290+
_method_name = '_find_folder_option'
291+
292+
for folder_option in folder_options:
293+
key = _get_kind_value(folder_option)
294+
if key == option_key:
295+
return folder_option
296+
297+
if option_key.isdecimal():
298+
index = int(option_key)
299+
if (index > -1) and (index < len(folder_options)):
300+
return folder_options[index]
301+
302+
ex = exception_helper.create_cla_exception(ExitCode.ARG_VALIDATION_ERROR, "WLSDPLY-10115",
303+
model_path, '#' + option_key)
304+
self._logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
305+
raise ex
306+
250307

251308
def _get_properties(schema_folder):
252309
# in array elements, the properties are under "items"
@@ -272,6 +329,35 @@ def _get_folder_names(schema_properties):
272329
return folder_names
273330

274331

332+
def print_folder_options(folder_options, model_path, indent_level):
333+
"""
334+
Print messages for a folder that has multiple content options.
335+
:param folder_options: the options to be printed
336+
:param model_path: the model path to be included in the output
337+
:param indent_level: the level to indent by, before printing output
338+
:return: the matching option, or None
339+
"""
340+
_print_indent("# " + exception_helper.get_message('WLSDPLY-10114', len(folder_options)), indent_level)
341+
for index, one_of_option in enumerate(folder_options):
342+
key = _get_kind_value(one_of_option) or index
343+
print("")
344+
_print_indent("# see " + model_path + "#" + str_helper.to_string(key), indent_level)
345+
346+
347+
def _get_kind_value(folder_option):
348+
"""
349+
Return the "kind" value of the specified folder option.
350+
If the option doesn't have a kind value, return None.
351+
:param folder_option: the folder option to be examined
352+
:return: the value of the "kind" field, or None
353+
"""
354+
properties = schema_helper.get_properties(folder_option)
355+
kind = dictionary_utils.get_dictionary_element(properties, "kind")
356+
enum = dictionary_utils.get_dictionary_element(kind, "enum")
357+
if enum and len(enum):
358+
return enum[0]
359+
360+
275361
def _print_indent(msg, level=1, first_in_list_object=False):
276362
"""
277363
Print a message at the specified indent level.

core/src/main/python/wlsdeploy/tool/modelhelp/model_help_printer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
"""
2-
Copyright (c) 2020, 2022, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2020, 2023, Oracle Corporation and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import re
66

77
from oracle.weblogic.deploy.exception import ExceptionHelper
88

9+
from wlsdeploy.aliases.model_constants import CRD_MODEL_SECTIONS
910
from wlsdeploy.aliases.model_constants import KNOWN_TOPLEVEL_MODEL_SECTIONS
1011
from wlsdeploy.aliases.model_constants import KUBERNETES
1112
from wlsdeploy.exception import exception_helper
1213
from wlsdeploy.tool.modelhelp.model_help_utils import ControlOptions
13-
from wlsdeploy.tool.modelhelp.model_kubernetes_printer import ModelKubernetesPrinter
14+
from wlsdeploy.tool.modelhelp.model_crd_section_printer import ModelCrdSectionPrinter
1415
from wlsdeploy.tool.modelhelp.model_sample_printer import ModelSamplePrinter
1516
from wlsdeploy.util import model
1617
import wlsdeploy.util.unicode_helper as str_helper
1718
from wlsdeploy.util.exit_code import ExitCode
1819

1920
_class_name = "ModelHelpPrinter"
20-
MODEL_PATH_PATTERN = re.compile(r'(^[a-zA-Z]+:?)?(/[a-zA-Z0-9^/]+)?$')
21+
MODEL_PATH_PATTERN = re.compile(r'(^[a-zA-Z]+:?)?(/[a-zA-Z0-9#^/]+)?$')
2122

2223

2324
class ModelHelpPrinter(object):
@@ -64,12 +65,12 @@ def print_model_help(self, model_path, control_option):
6465
elif control_option == ControlOptions.FOLDERS_ONLY:
6566
print(_format_message('WLSDPLY-10103', model_path))
6667
elif control_option == ControlOptions.ATTRIBUTES_ONLY:
67-
print( _format_message('WLSDPLY-10104', model_path))
68+
print(_format_message('WLSDPLY-10104', model_path))
6869
else:
6970
print(_format_message('WLSDPLY-10105', model_path))
7071

71-
if model_path_tokens[0] == KUBERNETES:
72-
sample_printer = ModelKubernetesPrinter(self._model_context)
72+
if model_path_tokens[0] in CRD_MODEL_SECTIONS:
73+
sample_printer = ModelCrdSectionPrinter(self._model_context)
7374
sample_printer.print_model_sample(model_path_tokens, control_option)
7475
else:
7576
sample_printer = ModelSamplePrinter(self._aliases, self._logger)

0 commit comments

Comments
 (0)