Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 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
Пример #4
0
 def _createDescriptors(self, list_descriptors: list, header: dict = None, verbose: bool = False):
     """
     Takes the list of descriptors and create them.
     Arguments:
         list_descriptors : REQUIRED : List of all the descriptors to create with their fields required for creation.
     """
     if header is None:
         raise Exception("excepting the header to be passed")
     if verbose:
         print(f"creating descriptors")
     list_result = []
     for element in list_descriptors:
         schema_instance = aep_schema.Schema(**header)
         print(element['namespace'])
         res = schema_instance.createDescriptor(**element)
         if "xdm:sourceSchema" not in res.keys():
             print('Issue creating descriptor')
             print(res)
         list_result.append(res)
     return list_result
Пример #5
0
 def duplicateIdentities(self, folder: str = "Identity", save: bool = False, verbose: bool = False)->dict:
     """
     Create the identities in the duplicate instances placed in the config file.
     Expecting a file identities.json in a folder location. It returns a dictionary of the different duplicate_ids with identities created.
     Arguments:
         folder : OPTIONAL : if you have place the identities.json file in another folder. (default "Identity")
         save : OPTIONAL : if set to True, will generate a json file for your new identities.
         verbose : OPTIONAL : set to True, it will print comment during the processing.
     """
     identities = self._readIdentities(folder=folder)
     dict_identities = {}
     if verbose:
         print(f"Number of identities to create : {len(identities)}")
     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()
         identity_instance = aep_identity.Identity(
             duplicate['instance_region'])
         list_identities = []
         for identity in identities:
             if verbose:
                 print(f"Creating : {identity['name']}")
             try:
                 new_identity = identity_instance.createIdentity(name=identity['name'], code=identity['code'], idType=identity['idType'], description=identity.get(
                     'description', 'Python duplication tool'),)
                 list_identities.append(new_identity)
             except Exception as e:
                 print(
                     f"Error trying to create Identity : {identity['name']}, idType: {identity['idType']}")
                 print(e)
         if verbose and save:
             print(
                 f"creating a file in Identity Folder : {duplicate['duplicate_id']}_identities.json")
         aepp.saveFile(module='identity', file=list_identities,
                       filename=f"{duplicate['duplicate_id']}_identities.json")
         dict_identities[duplicate['duplicate_id']] = list_identities
     return dict_identities
Пример #6
0
 def retrieveSchemas(self)->None:
     """
     Retrieve the schema and the mixin
     Arguments:
     """
     aepp.importConfigFile(
         self.CONFIG['original']['config_file_location'])
     schema_instance = aep_schema.Schema()
     tenant_id = schema_instance.getTenantId()
     schemas = schema_instance.getSchemas()
     obj_schema = {schema['title']: schema['meta:altId']
                   for schema in schemas if schema['title'].startswith("Adhoc") == False}
     for schema in obj_schema.values():
         schema = schema_instance.getSchema(schema, full=False, save=True)
         allOf = schema['allOf']
         allOf = [element for element in allOf if '$ref' in element.keys()]
         for element in allOf:
             if 'classes' in element['$ref']:
                 myClass = schema_instance.getClass(
                     element['$ref'], save=True)
             if 'mixins' in element['$ref']:
                 myMixin = schema_instance.getMixin(
                     element['$ref'], save=True)
Пример #7
0
    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
Пример #8
0
 def retrieveDescriptors(self)->None:
     aepp.importConfigFile(
         self.CONFIG['original']['config_file_location'])
     schema_instance = aep_schema.Schema()
     descriptors = schema_instance.getDescriptors(
         type_desc="xdm:descriptorIdentity", save=True)