def duplicateMixin(self, tenant_id: str = None, mixin_obj: dict = None)->dict: """ Return an object ready to be ingested for creation. Arguments: tenant_id : REQUIRED : tenant id for root object. mixin_obj : REQUIRED : mixin object to duplicate. """ if tenant_id is None: raise Exception( "Missing the tenant_id parameter") if mixin_obj is None: raise Exception( "Expecting a dictionary to be copied (argument : mixin_obj)") copy_obj = modules.deepcopy(mixin_obj) old_key = list(copy_obj['properties'].keys())[0] new_prop = {tenant_id: copy_obj['properties'][old_key]} obj = { "title": copy_obj['title'], "description": copy_obj['description'], "type": copy_obj['type'], "meta:intendedToExtend": copy_obj['meta:intendedToExtend'], "definitions": { "customFields": { "properties": new_prop} }, "meta:resourceType": "mixins", "allOf": [{"$ref": "#/definitions/customFields"}] } return obj
def duplicateSchema(schema_obj: dict)->dict: """ Duplication of schema requires you to pass an schema object WITH full parameter set to False. You would require to have the allOf in your keys of your object. Arguments: schema_obj : REQUIRED : the schema obj (not full). """ copy_obj = modules.deepcopy(schema_obj) if schema_obj is None: raise Exception( "Expecting a dictionary to be copied (argument : schema_obj)") if 'allOf' not in copy_obj.keys(): raise Exception("Expecting 'allOf' to be part of the schema") obj = { "type": "object", "title": copy_obj["title"], "description": copy_obj["description"], "allOf": [ { "$ref": copy_obj["meta:class"] } ] } for element in copy_obj["allOf"]: if "$ref" in element.keys(): obj['allOf'].append({"$ref": element['$ref']}) return obj
def _createSchemas(self, schemas: dict = None, header: dict = None, verbose: bool = False, save: bool = False)->dict: """ Create the class based on the element given. Returns a dictionary of old $id with new $id. Arguments: schemas : REQUIRED : Dictionary of schemas to create. header : REQUIRED : header from the instance of AEP API to use. verbose : OPTIONAL : set to True, it will print comment during the processing. """ if header is None: raise Exception("excepting the header to be passed") dict_ids = {} # require for the descriptors. for key in schemas: schema = modules.deepcopy(schemas[key]) if verbose: print(f"creating schema : {schema['title']}") old_id = schema['$id'] del schema['$id'] schema_instance = aep_schema.Schema(**header) res = schema_instance.createSchema(schema) # handling when issue creating schema if '$id' not in res: print(f"No Id found in response for {schema['title']}") print(res) res['$id'] = None dict_ids[old_id] = res['$id'] return dict_ids
def _createMixins(self, mixins: dict = None, header: dict = None, verbose: bool = False, save: bool = False)->dict: """ Create the mixins based on the elements given. Returns a dictionary of old $id with new $id. Arguments: mixins : REQUIRED : Dictionary of mixins to create. header : REQUIRED : header from the instance of AEP API to use. verbose : OPTIONAL : set to True, it will print comment during the processing. """ if header is None: raise Exception("excepting the header to be passed") dict_ids = {} for key in mixins: mixin = modules.deepcopy(mixins[key]) if verbose: print(f"creating mixin : {mixin['title']}") del mixin['$id'] #print(aepp.json.dumps(mixin, indent=4)) schema_instance = aep_schema.Schema(**header) res = schema_instance.createMixin(mixin) # handling error if '$id' not in res: print(f"No Id found in response for {mixin['title']}") print(res) res['$id'] = res['title'] dict_ids[mixins[key]['$id']] = res['$id'] return dict_ids
def _createClasses(self, class_elements: dict = None, header: dict = None, verbose: bool = False, save: bool = False)->dict: """ Create the class based on the element given. Returns a dictionary of old $id with new $id. Arguments: class_elements : REQUIRED : Dictionary of classes to create. header : REQUIRED : header from the instance of AEP API to use. verbose : OPTIONAL : set to True, it will print comment during the processing. """ if header is None: raise Exception("excepting the header to be passed") dict_ids = {} for key in class_elements: clas = modules.deepcopy(class_elements[key]) if verbose: print(f"creating class : {clas['title']}") del clas['$id'] schema_instance = aep_schema.Schema(**header) res = schema_instance.createClass(clas) if '$id' not in res: raise Exception( f"issue creating the class : {clas['title']}") print(res) res['$id'] = res['title'] dict_ids[class_elements[key]['$id']] = res['$id'] return dict_ids
def duplicateDataType(self, dt_obj: dict = None)->dict: """ Returns an object ready to be used for Data Type creation, based on the data type given. Arguments: dt_obj : REQUIRED : Data Type object to be duplicated. """ if dt_obj is None: raise Exception( "Expecting a dictionary to be copied (argument : dt_obj)") copy_obj = modules.deepcopy(dt_obj) obj = { "type": copy_obj["type"], "title": copy_obj["title"], "description": copy_obj["description"], "properties": copy_obj["properties"] } return obj
def duplicateSchemas(self, folder: str = "Schema", enableProfile: bool = False, verbose: bool = False)->dict: """ It will duplicate the schemas, mixins, classes from the original instance to the duplicates. Expecting a folder to exist with the different schema retrieved previously. Arguments: folder : OPTIONAL : if you have place the files in another folder. (default "Schema") enableProfile : OPTIONAL : If you want to enable schemas for Profile. verbose : OPTIONAL : set to True, it will print comment during the processing. """ dict_elements = self._readSchemas(folder=folder) list_descriptors = self._readDescriptors() aepp.importConfigFile( self.CONFIG['original']['config_file_location']) schema_instance = aep_schema.Schema() original_tenant = schema_instance.getTenantId() if enableProfile: if verbose: print('retrieving Enabled Schema for Union Profile') list_enabledSchemas = self._retrieveEnabledSchemas( schema_instance, verbose=verbose) if verbose: print(f"{len(dict_elements['classes'])} classes to create") print(f"{len(dict_elements['mixins'])} mixins to create") print(f"{len(dict_elements['schemas'])} schemas to create") duplicates_ids = {} for duplicate in self.CONFIG['duplicates']: if verbose: print(f"Selecting : {duplicate['duplicate_id']}") aepp.importConfigFile( duplicate['config_file_location']) schema_instance = aep_schema.Schema() tenant_id = schema_instance.getTenantId() # starting creating classes classes_translate = self._createClasses( class_elements=dict_elements['classes'], header=schema_instance.header, verbose=verbose) # need to change tenant_id for mixins old_mixins = dict_elements['mixins'] new_mixins = {} for mixin in old_mixins: obj = modules.deepcopy(old_mixins[mixin]) new_def = { "customFields": { "properties": { f"_{tenant_id}": old_mixins[mixin]['definitions']['customFields']['properties'][f"_{original_tenant}"]} } } obj["definitions"] = new_def # translate classes meta_extends = [] for clas in obj["meta:intendedToExtend"]: if clas in classes_translate.keys(): meta_extends.append(classes_translate[clas]) else: meta_extends.append(clas) obj["meta:intendedToExtend"] = meta_extends new_mixins[mixin] = obj mixins_translate = self._createMixins( new_mixins, header=schema_instance.header, verbose=verbose) # need to translate the schemas translat = {**classes_translate, **mixins_translate} old_schemas = dict_elements['schemas'] new_schemas = {} for schema in old_schemas: obj = modules.deepcopy(old_schemas[schema]) allOf = [] for element in obj['allOf']: if "$ref" in element.keys(): if element['$ref'] in translat.keys(): allOf.append( {'$ref': translat[element['$ref']]}) else: allOf.append({'$ref': element['$ref']}) obj['allOf'] = allOf new_schemas[schema] = obj # creating schemas trans_schemas = self._createSchemas( schemas=new_schemas, header=schema_instance.header, verbose=verbose) new_descriptors = [] if verbose: print('translating descriptors') for desc in list_descriptors: new_sourceProperty = desc["xdm:sourceProperty"].replace( f'_{original_tenant}/', f'_{tenant_id}/') new_descriptors.append({ "desc_type": "xdm:descriptorIdentity", "sourceSchema": trans_schemas[desc['xdm:sourceSchema']], "sourceProperty": new_sourceProperty, "namespace": desc["xdm:namespace"], "xdmProperty": "xdm:code", "primary": desc["xdm:isPrimary"] }) res = self._createDescriptors( new_descriptors, schema_instance.header, verbose=verbose) duplicates_ids[duplicate['duplicate_id']] = trans_schemas if enableProfile: if verbose: print( f'Enabling {len(list_enabledSchemas)} Schemas for Union Profile') self._duplicateEnabledSchemas( schema_instance, list_enabledSchemas, trans_schemas, verbose=verbose) return duplicates_ids # return old id and new id for schema