1
1
from exporting .special_treatment_objects import handle_fields
2
2
from lists_and_dictionaries import no_export_fields_and_subfields , \
3
3
singular_to_plural_dictionary , group_objects_field , placeholder_type_by_obj_type , \
4
- no_export_fields_by_api_type , special_treatment_types , no_export_fields
4
+ no_export_fields_by_api_type , special_treatment_types , \
5
+ fields_to_convert_from_obj_to_identifier_by_api_type , fields_to_exclude_due_to_value_of_other_fields
5
6
from utils import debug_log , merge_data , flatten_json , find_min_position_group , compare_versions , \
6
7
check_for_export_error , \
7
8
generate_new_dummy_ip_address
@@ -481,8 +482,44 @@ def cleanse_object_dictionary(object_dictionary):
481
482
def clean_objects (data_dict ):
482
483
for api_type in data_dict :
483
484
for obj in data_dict [api_type ]:
485
+ # currently fields_to_exclude_due_to_value_of_other_fields is relevant only for the types below. if there'll
486
+ # be more relevant types should change fields_to_exclude_due_to_value_of_other_fields to be by_api_type
487
+ if api_type == 'simple-cluster' or api_type == 'simple-gateway' or 'service' in api_type :
488
+ for dependent_field in fields_to_exclude_due_to_value_of_other_fields :
489
+ if dependent_field in obj :
490
+ # currently there's just 1 independent field; if there'll be more will need to add a 'break' if the
491
+ # presence of any of the independent fields requires removal of the dependent field
492
+ for independent_field in fields_to_exclude_due_to_value_of_other_fields [dependent_field ].keys ():
493
+ if independent_field in obj and obj [independent_field ] == fields_to_exclude_due_to_value_of_other_fields [dependent_field ][independent_field ]:
494
+ obj .pop (dependent_field , None )
495
+ debug_log ("The field " + dependent_field + " was removed from object of type " +
496
+ api_type + " named " + obj ["name" ] + " since it cannot be present when the "
497
+ "value of " + independent_field + " is " + str (obj [independent_field ]))
498
+
499
+ # converted_fields_to_add = {}
484
500
for field in list (obj ):
485
501
sub_fields = field .split ("." )
502
+ # in cases where the request is a single value (e.g name/list of names) and the reply is the corresponding object/s, we wish to create
503
+ # a new field named same as the object with the identifier as its value (and delete the rest of the object's fields!)
504
+ if api_type in fields_to_convert_from_obj_to_identifier_by_api_type :
505
+ field_removed = False
506
+ for field_to_convert in fields_to_convert_from_obj_to_identifier_by_api_type [api_type ]:
507
+ if field .startswith (field_to_convert + "." ):
508
+ # check whether the field is the name of an object (might be an object in a list)
509
+ # if field == field_to_convert + ".name" or \
510
+ # (field_to_convert + "." + sub_fields[-2] + ".name" == field and sub_fields[-2].isnumeric()):
511
+ # identifier = obj[field]
512
+ # converted_field = field[:-5] # in cases of list of objects (remove .name)
513
+ # converted_fields_to_add[converted_field] = identifier
514
+ obj .pop (field , None )
515
+ field_removed = True
516
+ debug_log ("The field " + field + " was removed from object of type " + api_type + " named "
517
+ + obj ["name" ] + " since we don't support the export of " + field_to_convert )
518
+ # todo - when converted_fields can be imported successfully change reason in debug info above
519
+ break
520
+ if field_removed :
521
+ continue # Already handled the field so the code below is irrelevant
522
+
486
523
local_no_export_fields_and_subfields = list (no_export_fields_and_subfields )
487
524
if api_type == "time" :
488
525
# For time objects, these two fields are required and must be retained!
@@ -493,7 +530,16 @@ def clean_objects(data_dict):
493
530
local_no_export_fields_and_subfields .remove ("type" )
494
531
if api_type == "exception-group" :
495
532
local_no_export_fields_and_subfields .remove ("layer" )
496
- if any (x for x in sub_fields if x in local_no_export_fields_and_subfields ) or (
497
- sub_fields [0 ] in no_export_fields ) or (api_type in no_export_fields_by_api_type and any (
498
- x for x in sub_fields if x in no_export_fields_by_api_type [api_type ])):
499
- obj .pop (field ,None )
533
+
534
+ if any (x for x in sub_fields if x in local_no_export_fields_and_subfields ) or \
535
+ (api_type in no_export_fields_by_api_type and (
536
+ any (x for x in sub_fields if x in no_export_fields_by_api_type [api_type ])
537
+ or field in no_export_fields_by_api_type [api_type ])):
538
+ obj .pop (field , None )
539
+
540
+ # todo - uncomment code below and converted_fields vars above when the converted_fields objects
541
+ # can be imported successfully. Currently if the converted_field is a created object, the server importing
542
+ # the data needs to create this object as well but this object isn't exported in a file so as a result we
543
+ # can get a bad link error when importing
544
+ # for converted_field in converted_fields_to_add:
545
+ # obj[converted_field] = converted_fields_to_add[converted_field]
0 commit comments