def create_object_model(module_name, object_name, **kwargs):
    '''
    Assemble an object from incoming parameters.
    '''
    object_kwargs = {}

    try:
        model_module = importlib.import_module('azure.mgmt.{0}.models'.format(module_name))
        # pylint: disable=invalid-name
        Model = getattr(model_module, object_name)
    except ImportError:
        raise sys.exit(
            'The {0} model in the {1} Azure module is not available.'.format(object_name, module_name)
        )

    if '_attribute_map' in dir(Model):
        for attr, items in Model._attribute_map.items():
            param = kwargs.get(attr)
            if param:
                if items['type'][0].isupper() and isinstance(param, dict):
                    object_kwargs[attr] = create_object_model(module_name, items['type'], **param)
                elif items['type'][0] == '{' and isinstance(param, dict):
                    object_kwargs[attr] = param
                elif items['type'][0] == '[' and isinstance(param, list):
                    obj_list = []
                    for list_item in param:
                        if items['type'][1].isupper() and isinstance(list_item, dict):
                            obj_list.append(
                                create_object_model(
                                    module_name,
                                    items['type'][items['type'].index('[')+1:items['type'].rindex(']')],
                                    **list_item
                                )
                            )
                        elif items['type'][1] == '{' and isinstance(list_item, dict):
                            obj_list.append(list_item)
                        elif not items['type'][1].isupper() and items['type'][1] != '{':
                            obj_list.append(list_item)
                    object_kwargs[attr] = obj_list
                else:
                    object_kwargs[attr] = param

    # wrap calls to this function to catch TypeError exceptions
    return Model(**object_kwargs)
def create_object_model ( module_name , object_name , ** kwargs ) : object_kwargs = { } try : model_module = importlib . import_module ( 'azure.mgmt.{0}.models' . format ( module_name ) ) Model = getattr ( model_module , object_name ) except ImportError : raise sys . exit ( 'The {0} model in the {1} Azure module is not available.' . format ( object_name , module_name ) ) if '_attribute_map' in dir ( Model ) : for attr , items in Model . _attribute_map . items ( ) : param = kwargs . get ( attr ) if param : if items [ 'type' ] [ 0 ] . isupper ( ) and isinstance ( param , dict ) : object_kwargs [ attr ] = create_object_model ( module_name , items [ 'type' ] , ** param ) elif items [ 'type' ] [ 0 ] == '{' and isinstance ( param , dict ) : object_kwargs [ attr ] = param elif items [ 'type' ] [ 0 ] == '[' and isinstance ( param , list ) : obj_list = [ ] for list_item in param : if items [ 'type' ] [ 1 ] . isupper ( ) and isinstance ( list_item , dict ) : obj_list . append ( create_object_model ( module_name , items [ 'type' ] [ items [ 'type' ] . index ( '[' ) + 1 : items [ 'type' ] . rindex ( ']' ) ] , ** list_item ) ) elif items [ 'type' ] [ 1 ] == '{' and isinstance ( list_item , dict ) : obj_list . append ( list_item ) elif not items [ 'type' ] [ 1 ] . isupper ( ) and items [ 'type' ] [ 1 ] != '{' : obj_list . append ( list_item ) object_kwargs [ attr ] = obj_list else : object_kwargs [ attr ] = param return Model ( ** object_kwargs )
Assemble an object from incoming parameters.