1
1
"""
2
- Copyright (c) 2020, 2022 , Oracle and/or its affiliates.
2
+ Copyright (c) 2020, 2023 , Oracle and/or its affiliates.
3
3
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4
4
"""
5
5
from wlsdeploy .exception import exception_helper
9
9
from wlsdeploy .tool .modelhelp import model_help_utils
10
10
from wlsdeploy .tool .modelhelp .model_help_utils import ControlOptions
11
11
import wlsdeploy .util .unicode_helper as str_helper
12
+ from wlsdeploy .util import dictionary_utils
12
13
from wlsdeploy .util .exit_code import ExitCode
13
14
14
15
15
- class ModelKubernetesPrinter (object ):
16
+ class ModelCrdSectionPrinter (object ):
16
17
"""
17
18
Class for printing kubernetes sections as model samples.
18
19
"""
19
- _class_name = "ModelKubernetesPrinter "
20
+ _class_name = "ModelCrdSectionPrinter "
20
21
_logger = PlatformLogger ('wlsdeploy.modelhelp' )
21
22
22
23
def __init__ (self , model_context ):
23
24
self ._crd_helper = model_crd_helper .get_helper (model_context )
25
+ self ._target = model_context .get_target ()
24
26
25
27
def print_model_sample (self , model_path_tokens , control_option ):
26
28
"""
@@ -30,6 +32,10 @@ def print_model_sample(self, model_path_tokens, control_option):
30
32
:raises CLAException: if a problem is encountered
31
33
"""
32
34
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
33
39
34
40
if len (model_path_tokens ) == 1 :
35
41
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
122
128
123
129
current_folder = schema
124
130
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
+
125
140
properties = _get_properties (current_folder )
126
141
127
142
valid_subfolder_keys = _get_folder_names (properties )
128
- if token not in valid_subfolder_keys :
143
+ if lookup_token not in valid_subfolder_keys :
129
144
ex = exception_helper .create_cla_exception (ExitCode .ARG_VALIDATION_ERROR ,
130
- "WLSDPLY-10111" , model_path , token ,
145
+ "WLSDPLY-10111" , model_path , lookup_token ,
131
146
', ' .join (valid_subfolder_keys ))
132
147
self ._logger .throwing (ex , class_name = self ._class_name , method_name = _method_name )
133
148
raise ex
134
149
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 )
136
158
137
- _print_indent (token + ":" , indent , in_object_array )
159
+ _print_indent (lookup_token + ":" + token_suffix , indent , in_object_array )
138
160
indent += 1
139
161
140
162
# apply to the next folder in the path
141
163
in_object_array = schema_helper .is_object_array (current_folder )
142
164
model_path = model_path + "/" + token
143
165
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
+
144
174
# list the attributes and folders, as specified
145
175
146
176
if model_help_utils .show_attributes (control_option ):
@@ -247,6 +277,33 @@ def _print_attributes_sample(self, schema_folder, indent_level, in_object_array)
247
277
248
278
return in_object_array
249
279
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
+
250
307
251
308
def _get_properties (schema_folder ):
252
309
# in array elements, the properties are under "items"
@@ -272,6 +329,35 @@ def _get_folder_names(schema_properties):
272
329
return folder_names
273
330
274
331
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
+
275
361
def _print_indent (msg , level = 1 , first_in_list_object = False ):
276
362
"""
277
363
Print a message at the specified indent level.
0 commit comments