summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Resource/node.py
blob: 5a65121fc4c853b5b0e194137886a2161acb28fa (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
'''
Created on May 31, 2012

@author: steger
'''
from Resource.resource import resource
from Resource.interface import interface

class node(resource):
    def __init__(self, name = None, resourceid = None):
        resource.__init__(self, name, resourceid)
        self._public = False
        self._interfaces = {}
    
    @property
    def ispublic(self):
        if not len(self._interfaces):
            raise Exception("No interfaces defined yet for %s" % self.resourceid)
        return self._public
    
    def addinterface(self, iface):
        if not isinstance(iface, interface):
            raise Exception("Wrong resource type %s is not an interface" % iface)
        self._interfaces[iface.interface] = iface
        self._public |= iface.ispublic
    
    def interfaces(self):
        for iface in self._interfaces.itervalues():
            if not iface.isvalid:
                print "WW: invalid interface:", iface.resourceid
                continue
            yield iface.interface, iface.address, iface.ispublic, iface.hostname, iface.direction
    
    def get_ipaddress(self, interfacename):
        for ifname, address, _, _, _ in self.interfaces():
            if ifname == interfacename:
                return address
        raise Exception("%s has no interface %s" % (self.resourceid, interfacename))

    def get_hostname(self, interfacename):
        for ifname, address, _, hostname, _ in self.interfaces():
            if ifname != interfacename:
                continue
            if hostname:
                return hostname
            else:
                return address
        raise Exception("%s has no interface %s" % (self.resourceid, interfacename))