summaryrefslogtreecommitdiff
path: root/virtinst/pollhelpers.py
blob: f9fcc3fa058668430027ead9776e968c2588233b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
# Copyright (C) 2013 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#

from .logger import log


def _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb):
    """
    Helper for new style listAll* APIs
    """
    current = {}
    new = {}
    objs = []

    try:
        if support_cb():
            objs = list_cb()
    except Exception as e:  # pragma: no cover
        log.debug("Unable to list all %ss: %s", typename, e)

    for obj in objs:
        name = obj.name()

        if name not in origmap:
            # Object is brand new this period
            current[name] = build_cb(obj, name)
            new[name] = current[name]
        else:
            # Previously known object
            current[name] = origmap[name]
            del origmap[name]

    return (list(origmap.values()), list(new.values()), list(current.values()))


def fetch_nets(backend, origmap, build_cb):
    typename = "network"
    list_cb = backend.listAllNetworks
    support_cb = backend.support.conn_network
    return _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb)


def fetch_pools(backend, origmap, build_cb):
    typename = "pool"
    list_cb = backend.listAllStoragePools
    support_cb = backend.support.conn_storage
    return _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb)


def fetch_volumes(backend, pool, origmap, build_cb):
    typename = "volume"
    list_cb = pool.listAllVolumes
    support_cb = backend.support.conn_storage
    return _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb)


def fetch_nodedevs(backend, origmap, build_cb):
    typename = "nodedev"
    list_cb = backend.listAllDevices
    support_cb = backend.support.conn_nodedev
    return _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb)


def fetch_vms(backend, origmap, build_cb):
    typename = "domain"
    list_cb = backend.listAllDomains
    support_cb = backend.support.conn_domain
    return _new_poll_helper(origmap, typename, list_cb, build_cb, support_cb)