Skip to content

Commit 39b240b

Browse files
committed
Revised k8s schema test to generate WDT 2.0 model; renamed some helper methods
1 parent 29c956f commit 39b240b

File tree

5 files changed

+88
-93
lines changed

5 files changed

+88
-93
lines changed

core/src/main/python/wlsdeploy/tool/extract/domain_resource_extractor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,14 @@ def _process_folder(self, model_dict, schema_folder, target_dict, schema_path):
123123
for key, model_value in model_dict.items():
124124
properties = folder_properties[key]
125125

126-
if wko_schema_helper.is_single_folder(properties):
127-
# single object instance
126+
if wko_schema_helper.is_single_object(properties):
128127
next_schema_path = wko_schema_helper.append_path(schema_path, key)
129128
if not wko_schema_helper.is_unsupported_folder(next_schema_path):
130129
next_target_dict = PyOrderedDict()
131130
target_dict[key] = next_target_dict
132131
self._process_folder(model_value, properties, next_target_dict, next_schema_path)
133132

134-
elif wko_schema_helper.is_multiple_folder(properties):
135-
# multiple object instances
133+
elif wko_schema_helper.is_object_array(properties):
136134
next_schema_path = wko_schema_helper.append_path(schema_path, key)
137135
if not wko_schema_helper.is_unsupported_folder(next_schema_path):
138136
item_info = wko_schema_helper.get_array_item_info(properties)

core/src/main/python/wlsdeploy/tool/extract/wko_schema_helper.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,37 @@ def get_domain_resource_schema(exception_type=ExceptionType.DEPLOY):
6262
return schema
6363

6464

65-
def is_single_folder(schema_map):
65+
def is_single_object(schema_map):
6666
"""
67-
Return True if the schema map describes a single folder.
67+
Return True if the schema map describes a single object.
6868
:param schema_map: the schema map to be examined
69-
:return: True if the map identifies a single folder
69+
:return: True if the map identifies a single object
7070
"""
7171
property_type = get_type(schema_map)
7272
if property_type in OBJECT_TYPES:
7373
return get_map_element_type(schema_map) is None
7474
return False
7575

7676

77-
def is_multiple_folder(schema_map):
77+
def is_object_array(schema_map):
7878
"""
79-
Return True if the schema map identifies a multiple folder.
79+
Return True if the schema map identifies an object array.
8080
:param schema_map: the schema map to be examined
81-
:return: True if the map identifies a multiple folder
81+
:return: True if the map identifies an object array
8282
"""
8383
property_type = get_type(schema_map)
8484
if property_type == "array":
8585
return get_array_element_type(schema_map) in OBJECT_TYPES
8686
return False
8787

8888

89-
def is_folder(schema_map):
90-
return is_single_folder(schema_map) or is_multiple_folder(schema_map)
89+
def is_object_type(schema_map):
90+
"""
91+
Return True is the schema map identifies an object or object array.
92+
:param schema_map: the schema map to be examined
93+
:return: True if the map identifies an object or object array
94+
"""
95+
return is_single_object(schema_map) or is_object_array(schema_map)
9196

9297

9398
def is_simple_map(schema_map):
@@ -114,6 +119,16 @@ def is_simple_array(schema_map):
114119
return False
115120

116121

122+
def is_simple_type(schema_map):
123+
"""
124+
Return True if the schema map describes a simple type.
125+
:param schema_map: the schema map to be examined
126+
:return: True if the map identifies a simple type
127+
"""
128+
property_type = get_type(schema_map)
129+
return property_type in SIMPLE_TYPES
130+
131+
117132
def get_array_element_type(schema_map):
118133
item_info = get_array_item_info(schema_map)
119134
return get_type(item_info)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _print_model_folder_sample(self, section_name, model_path_tokens, control_op
8686
_print_indent(token + ":", indent)
8787
indent += 1
8888

89-
if wko_schema_helper.is_multiple_folder(current_folder):
89+
if wko_schema_helper.is_object_array(current_folder):
9090
name = token + '-1'
9191
_print_indent(name + ":", indent)
9292
indent += 1
@@ -119,10 +119,10 @@ def _print_subfolders_sample(self, schema_folder, control_option, indent_level,
119119
property_map = folder_info[key]
120120

121121
if property_map is not None:
122-
if wko_schema_helper.is_single_folder(property_map):
122+
if wko_schema_helper.is_single_object(property_map):
123123
folder_map[key] = property_map
124124

125-
elif wko_schema_helper.is_multiple_folder(property_map):
125+
elif wko_schema_helper.is_object_array(property_map):
126126
folder_map[key] = wko_schema_helper.get_array_item_info(property_map)
127127
multi_folders.append(key)
128128

@@ -173,7 +173,7 @@ def _print_attributes_sample(self, schema_folder, indent_level):
173173
# array of simple type
174174
attribute_map[key] = 'list of ' + wko_schema_helper.get_array_element_type(property_map)
175175

176-
elif not wko_schema_helper.is_folder(property_map):
176+
elif not wko_schema_helper.is_object_type(property_map):
177177
type_text = wko_schema_helper.get_type(property_map)
178178
enum_values = wko_schema_helper.get_enum_values(property_map)
179179
if enum_values:
@@ -199,7 +199,7 @@ def _print_attributes_sample(self, schema_folder, indent_level):
199199

200200
def _get_properties(schema_folder):
201201
# in array elements, the properties are under "items"
202-
if wko_schema_helper.is_multiple_folder(schema_folder):
202+
if wko_schema_helper.is_object_array(schema_folder):
203203
item_info = wko_schema_helper.get_array_item_info(schema_folder)
204204
return wko_schema_helper.get_properties(item_info)
205205
else:
@@ -216,7 +216,7 @@ def _get_folder_names(schema_properties):
216216
for key in schema_properties:
217217
property_map = schema_properties[key]
218218
if property_map is not None:
219-
if wko_schema_helper.is_single_folder(property_map) or wko_schema_helper.is_multiple_folder(property_map):
219+
if wko_schema_helper.is_object_type(property_map):
220220
folder_names.append(key)
221221
return folder_names
222222

core/src/main/python/wlsdeploy/tool/validate/kubernetes_validator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ def validate_folder(self, model_folder, schema_folder, schema_path, model_path):
6161

6262
if properties is not None:
6363

64-
if wko_schema_helper.is_single_folder(properties):
64+
if wko_schema_helper.is_single_object(properties):
6565
# single object instance
6666
self._log_debug(' ' + key + ': folder')
6767
next_schema_path = wko_schema_helper.append_path(schema_path, key)
6868
next_model_path = model_path + "/" + key
6969
if self._check_folder_path(next_schema_path, next_model_path):
7070
self.validate_folder(model_value, properties, next_schema_path, next_model_path)
7171

72-
elif wko_schema_helper.is_multiple_folder(properties):
73-
# multiple object instances
72+
elif wko_schema_helper.is_object_array(properties):
7473
self._log_debug(' ' + key + ': multiple folder')
7574
next_schema_path = wko_schema_helper.append_path(schema_path, key)
7675
next_model_path = model_path + "/" + key

core/src/test/python/wlsdeploy/tool/extract/kubernetes_schema_test.py

Lines changed: 55 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
from oracle.weblogic.deploy.util import PyOrderedDict
99

1010
from wlsdeploy.aliases.model_constants import KUBERNETES
11-
from wlsdeploy.tool.extract import domain_resource_extractor
1211
from wlsdeploy.tool.extract import wko_schema_helper
13-
from wlsdeploy.util import dictionary_utils
1412

1513

1614
class KubernetesSchemaTest(unittest.TestCase):
@@ -31,93 +29,78 @@ def tearDown(self):
3129
def testKubernetesSchema(self):
3230
# create a model with every element.
3331
# verify that there are no unknown types or structures.
34-
self._write_folder(self.schema_map, "", False, "", "")
35-
36-
def _write_folder(self, folder, name, is_multiple, path, indent):
37-
label = name
38-
if not label:
39-
label = KUBERNETES
40-
41-
if wko_schema_helper.is_unsupported_folder(path):
42-
self._write_line("\n" + indent + "# " + label + ": (unsupported folder)")
43-
return
44-
45-
self._write_line("\n" + indent + label + ":")
46-
indent = indent + " "
47-
48-
properties = wko_schema_helper.get_properties(folder)
49-
50-
multi_key = None
32+
self._write_line(KUBERNETES + ":")
33+
self._write_folder(self.schema_map, False, "", " ")
34+
35+
def _write_folder(self, folder, is_multiple, path, indent):
36+
# for a multiple (object list) type, the first field is prefixed with a hyphen
37+
plain_indent = indent
38+
hyphen_indent = indent[:-2] + "- "
39+
this_indent = plain_indent
5140
if is_multiple:
52-
mapped_key = domain_resource_extractor.get_mapped_key(path)
53-
multi_property = dictionary_utils.get_element(properties, mapped_key)
54-
if multi_property:
55-
multi_key = mapped_key
56-
comment = 'maps to ' + multi_key
57-
else:
58-
comment = 'unique key for each'
59-
60-
self._write_line(indent + "'" + name + "-1': # " + comment)
61-
indent = indent + " "
41+
this_indent = hyphen_indent
6242

43+
properties = wko_schema_helper.get_properties(folder)
6344
property_names = list(properties.keys())
6445
property_names.sort()
6546

6647
sub_folders = PyOrderedDict()
6748
multi_sub_folders = []
6849
for property_name in property_names:
6950
property_map = properties[property_name]
70-
71-
property_type = dictionary_utils.get_element(property_map, "type")
72-
73-
if property_type in wko_schema_helper.OBJECT_TYPES:
74-
additional = dictionary_utils.get_dictionary_element(property_map, "additionalProperties")
75-
additional_type = dictionary_utils.get_element(additional, "type")
76-
if additional_type:
77-
if additional_type not in wko_schema_helper.SIMPLE_TYPES:
78-
self.fail('Unknown map type ' + additional_type + ' for ' + path + ' ' + property_name)
79-
nest_indent = indent + " "
80-
self._write_line(indent + property_name + ":")
81-
self._write_line(nest_indent + "'key-1': " + _get_sample_value(additional_type))
82-
self._write_line(nest_indent + "'key-2': " + _get_sample_value(additional_type))
83-
else:
84-
# single object instance
85-
sub_folders[property_name] = property_map
86-
87-
elif property_type == "array":
88-
array_items = dictionary_utils.get_dictionary_element(property_map, "items")
89-
array_type = dictionary_utils.get_element(array_items, "type")
90-
if array_type in wko_schema_helper.OBJECT_TYPES:
91-
# multiple object instances
92-
sub_folders[property_name] = array_items
93-
multi_sub_folders.append(property_name)
94-
elif array_type in wko_schema_helper.SIMPLE_TYPES:
95-
nest_indent = indent + " "
96-
self._write_line(indent + property_name + ": [")
97-
self._write_line(nest_indent + _get_sample_value(array_type) + ",")
98-
self._write_line(nest_indent + _get_sample_value(array_type))
99-
self._write_line(indent + "]")
100-
else:
101-
self.fail('Unknown array type ' + array_type + ' for ' + path + ' ' + property_name)
102-
103-
elif property_type in wko_schema_helper.SIMPLE_TYPES:
104-
if property_name != multi_key:
105-
value = _get_sample_value(property_type)
106-
enum_values = wko_schema_helper.get_enum_values(property_map)
107-
if enum_values:
108-
value = "'" + enum_values[0] + "' # " + ', '.join(enum_values)
109-
self._write_line(indent + str(property_name) + ": " + value)
51+
property_type = wko_schema_helper.get_type(property_map)
52+
53+
if wko_schema_helper.is_simple_map(property_map):
54+
additional_type = wko_schema_helper.get_map_element_type(property_map)
55+
if additional_type not in wko_schema_helper.SIMPLE_TYPES:
56+
self.fail('Unknown map type ' + additional_type + ' for ' + path + ' ' + property_name)
57+
self._write_line(this_indent + property_name + ":")
58+
this_indent = plain_indent
59+
nest_indent = this_indent + " "
60+
self._write_line(nest_indent + "'key-1': " + _get_sample_value(additional_type))
61+
self._write_line(nest_indent + "'key-2': " + _get_sample_value(additional_type))
62+
63+
elif wko_schema_helper.is_single_object(property_map):
64+
# single object instance
65+
sub_folders[property_name] = property_map
66+
67+
elif wko_schema_helper.is_object_array(property_map):
68+
array_items = wko_schema_helper.get_array_item_info(property_map)
69+
sub_folders[property_name] = array_items
70+
multi_sub_folders.append(property_name)
71+
72+
elif wko_schema_helper.is_simple_array(property_map):
73+
array_type = wko_schema_helper.get_array_element_type(property_map)
74+
self._write_line(this_indent + property_name + ":")
75+
this_indent = plain_indent
76+
each_indent = this_indent + "- "
77+
self._write_line(each_indent + _get_sample_value(array_type))
78+
self._write_line(each_indent + _get_sample_value(array_type))
79+
80+
elif wko_schema_helper.is_simple_type(property_map):
81+
value = _get_sample_value(property_type)
82+
enum_values = wko_schema_helper.get_enum_values(property_map)
83+
if enum_values:
84+
value = "'" + enum_values[0] + "' # " + ', '.join(enum_values)
85+
self._write_line(this_indent + str(property_name) + ": " + value)
86+
this_indent = plain_indent
11087

11188
else:
11289
self.fail('Unknown property type ' + str(property_type) + ' for ' + str(path) + ' '
11390
+ str(property_name))
11491

11592
# process sub-folders after attributes for clarity
11693
for property_name in sub_folders:
117-
subfolder = sub_folders[property_name]
118-
is_multiple = property_name in multi_sub_folders
11994
next_path = wko_schema_helper.append_path(path, property_name)
120-
self._write_folder(subfolder, property_name, is_multiple, next_path, indent)
95+
if wko_schema_helper.is_unsupported_folder(next_path):
96+
self._write_line(indent + "# " + property_name + ": (unsupported folder)")
97+
else:
98+
self._write_line(this_indent + property_name + ":")
99+
this_indent = plain_indent
100+
subfolder = sub_folders[property_name]
101+
is_multiple = property_name in multi_sub_folders
102+
child_indent = this_indent + " "
103+
self._write_folder(subfolder, is_multiple, next_path, child_indent)
121104

122105
def _write_line(self, text):
123106
self.out_file.write(text + "\n")

0 commit comments

Comments
 (0)