def update(res_type, obj): if not obj.uuid: obj.uuid = VncApiMock.name_to_id(res_type, obj.name) if obj.uuid is None: raise NoIdError("Object does not exist.") if obj.uuid not in DBMock.get_dict(res_type.replace('-', '_')): raise NoIdError("Object does not exist.") DBMock.update(res_type.replace('-', '_'), obj.uuid, VncApiMock.object_to_dict(obj))
def create(res_type, obj): if obj.name in VncApiMock.name_to_uuid.get(res_type): raise RefsExistError() obj.uuid = VncApiMock._alloc_uuid() DBMock.create(res_type.replace('-', '_'), obj.uuid, VncApiMock.object_to_dict(obj)) VncApiMock.name_to_uuid.get(res_type)[obj.name] = obj.uuid return obj.uuid
def delete(res_type, fq_name=None, id=None, ifmap_id=None): (args_ok, result) = VncApiMock._read_args_to_id(res_type, fq_name=fq_name, id=id) if not args_ok: return result id = result if id is None: raise NoIdError("Object does not exist.") obj = DBMock.read(res_type.replace('-', '_'), [id]) VncApiMock.name_to_uuid.pop(obj.get_name()) DBMock.delete(res_type.replace('-', '_'), id)
def read(res_type, fq_name=None, fq_name_str=None, id=None, fields=None): (args_ok, result) = VncApiMock._read_args_to_id(res_type, fq_name, fq_name_str, id) if not args_ok: return result id = result if id is None: raise NoIdError("Object does not exist.") ok, ret = DBMock.read(res_type.replace('-', '_'), [id]) if (not ok) or (len(ret) < 1): raise NoIdError("Object does not exist.") return VncApiMock.object_from_dict(res_type, ret[0])
def list(res_type, parent_id=None, parent_fq_name=None, obj_uuids=None, back_ref_id=None, fields=None, detail=False, count=False, filters=None, shared=False): ret = [] for obj_dict in DBMock.get_dict(res_type.replace('-', '_')).values(): obj = VncApiMock.object_from_dict(res_type, obj_dict) if parent_id is not None and parent_id != VncApiMock.name_to_uuid(obj.parent_name()): continue if parent_fq_name is not None and parent_fq_name != obj.get_parent_fq_name(): continue if obj_uuids is not None and obj.uuid not in obj_uuids: continue ret.append(obj) return ret
def init(): DBMock.init() VncApiMock.name_to_uuid = {} for object_type, resource_type in all_resource_type_tuples: VncApiMock.name_to_uuid[resource_type] = {} VncApiMock.next_uuid = 0