예제 #1
0
def get_vcenter_object_by_name(connection, object_type, name):
    """
    Find a vcenter object
    :param connection: A vcenter connection
    :param object_type: A vcenter object type, like vim.VirtualMachine
    :param name: The name of the object

    :return: The object found

    :raise: TooManyObjectsFound: If more than one object is found
    :raise: NoObjectFound: If no results are found
    """
    content = connection.RetrieveContent()
    view = content.viewManager.CreateContainerView
    objects = [
        obj for obj in view(content.rootFolder, [object_type], True).view
        if hasattr(obj, 'name') and obj.name == name
    ]
    count = len(objects)
    if count == 1:
        return objects[0]
    elif count > 1:
        raise TooManyObjectsFound(object_type, name)
    else:
        raise NoObjectFound(object_type, name)
예제 #2
0
def get_vcenter_object_by_name(connection, object_type, name):
    """
    Find a vcenter object
    :param connection: A vcenter connection
    :param object_type: A vcenter object type, like vim.VirtualMachine
    :param name: The name of the object

    :return: The object found

    :raise: TooManyObjectsFound: If more than one object is found
    :raise: NoObjectFound: If no results are found
    """
    content = connection.RetrieveContent()
    view = content.viewManager.CreateContainerView

    def name_matches(obj):
        try:
            return obj.name == name
        except (vmodl.fault.ManagedObjectNotFound, AttributeError):
            pass
        return False

    objects = [
        obj for obj in view(content.rootFolder, [object_type], True).view
        if name_matches(obj)
    ]
    count = len(objects)
    if count == 1:
        return objects[0]
    elif count > 1:
        raise TooManyObjectsFound(object_type, name)
    else:
        raise NoObjectFound(object_type, name)
예제 #3
0
파일: vm.py 프로젝트: GMaxera/vcdriver
    def find_snapshot(self, name):
        """
        Find a snapshot by name
        :param name: The name of the snapshot

        :return: The given snapshot

        :raise: TooManyObjectsFound: If more than one object is found
        :raise: NoObjectFound: If no results are found
        """
        if self._vm_object:
            if self._vm_object.snapshot is not None:
                found_snapshots = self._get_snapshots_by_name(
                    self._vm_object.snapshot.rootSnapshotList, name)
            else:
                found_snapshots = []
            if len(found_snapshots) > 1:
                raise TooManyObjectsFound(vim.vm.Snapshot, name)
            elif len(found_snapshots) == 0:
                raise NoObjectFound(vim.vm.Snapshot, name)
            else:
                return found_snapshots[0].snapshot