Example #1
0
def _populate(netdevices, data_source, production_only, with_acls):
    """
    Populates the NetDevices with NetDevice objects.

    Abstracted from within NetDevices to prevent accidental repopulation of NetDevice
    objects.
    """
    #start = time.time()
    device_data = _munge_source_data(data_source=data_source)

    # Populate AclsDB if `with_acls` is set
    if with_acls:
        log.msg("NetDevices ACL associations: ENABLED")
        aclsdb = AclsDB()
    else:
        log.msg("NetDevices ACL associations: DISABLED")
        aclsdb = None

    # Populate `netdevices` dictionary with `NetDevice` objects!
    for obj in device_data:
        dev = NetDevice(data=obj, with_acls=aclsdb)

        # Only return devices with adminStatus of 'PRODUCTION' unless
        # `production_only` is True
        if dev.adminStatus != 'PRODUCTION' and production_only:
            #log.msg('DEVICE NOT PRODUCTION')
            continue

        # These checks should be done on generation of netdevices.xml.
        # Skip empty nodenames
        if dev.nodeName is None:
            continue

        # Add to dict
        netdevices[dev.nodeName] = dev
Example #2
0
def _populate(netdevices, data_source, data_format, production_only):
    """
    Populates the NetDevices with NetDevice objects.

    Abstracted from within NetDevices to prevent accidental repopulation of NetDevice
    objects.
    """
    #start = time.time()
    aclsdb = AclsDB()

    device_data = _munge_source_data(data_source=data_source, format=data_format)

    for obj in device_data:
        dev = NetDevice(data=obj)

        # Only return devices with adminStatus of 'PRODUCTION' unless
        # production_only is True
        if dev.adminStatus != 'PRODUCTION' and production_only:
            continue

        # These checks should be done on generation of netdevices.xml.
        ## skip empty nodenames
        if dev.nodeName is None:
            continue

        ## lowercase hostnames
        dev.nodeName = dev.nodeName.lower()

        ## cleanup whitespace from owning team
        dev.owningTeam = dev.owningTeam.strip()

        # Populate the ACLs for each device.
        dev.explicit_acls = dev.implicit_acls = dev.acls = dev.bulk_acls = set()
        acls_dict = aclsdb.get_acl_dict(dev)
        dev.explicit_acls = acls_dict['explicit']
        dev.implicit_acls = acls_dict['implicit']
        dev.acls = acls_dict['all']

        # Add to dict
        netdevices[dev.nodeName] = dev
Example #3
0
def _populate(netdevices, data_source, production_only, with_acls):
    """
    Populates the NetDevices with NetDevice objects.

    Abstracted from within NetDevices to prevent accidental repopulation of NetDevice
    objects.
    """
    #start = time.time()
    loader, device_data = _munge_source_data(data_source=data_source)
    netdevices.set_loader(loader)

    # Populate AclsDB if `with_acls` is set
    if with_acls:
        log.msg("NetDevices ACL associations: ENABLED")
        aclsdb = AclsDB()
    else:
        log.msg("NetDevices ACL associations: DISABLED")
        aclsdb = None

    # Populate `netdevices` dictionary with `NetDevice` objects!
    for obj in device_data:
        # Don't process it if it's already a NetDevice
        if isinstance(obj, NetDevice):
            dev = obj
        else:
            dev = NetDevice(data=obj, with_acls=aclsdb)

        # Only return devices with adminStatus of 'PRODUCTION' unless
        # `production_only` is True
        if dev.adminStatus.upper() != 'PRODUCTION' and production_only:
            log.msg('[%s] Skipping: adminStatus not PRODUCTION' % dev.nodeName)
            continue

        # These checks should be done on generation of netdevices.xml.
        # Skip empty nodenames
        if dev.nodeName is None:
            continue

        # Add to dict
        netdevices.add_device(dev)
Example #4
0
from trigger.acl import queue
from trigger.acl.models import create_tables
from trigger.acl.db import AclsDB
from trigger.netdevices import NetDevices
from trigger.utils import get_user
from trigger import exceptions
import unittest

# Globals
DEVICE_NAME = 'test1-abc.net.aol.com'
ACL_NAME = 'foo'
USERNAME = get_user()

# Setup
create_tables()
adb = AclsDB()
nd = NetDevices()
# These must happen after we populate the dummy AclsDB

def _setup_aclsdb(nd, device_name=DEVICE_NAME, acl=ACL_NAME):
    """Add an explicit ACL to the dummy AclsDB"""
    #print 'Setting up ACLsdb: %s => %s' % (acl, device_name)
    dev = nd.find(device_name)
    if acl not in dev.acls:
        adb.add_acl(dev, acl)
    NetDevices._Singleton = None
    nd = NetDevices()

class TestAclQueue(unittest.TestCase):
    def setUp(self):
        self.nd = NetDevices()
Example #5
0
__all__ = ['mock_redis']

# misc
from . import misc
from misc import *
__all__.extend(misc.__all__)

if __name__ == '__main__':
    os.environ['NETDEVICES_SOURCE'] = 'data/netdevices.xml'

    mock_redis.install()
    import redis
    from trigger.netdevices import NetDevices
    from trigger.acl.db import AclsDB

    r = redis.Redis()
    a = AclsDB()
    nd = NetDevices()

    dev = nd.find('test1-abc')

    print r.keys('*')
    print a.add_acl(dev, 'bacon')
    print r.keys('*')

    _k = 'acls:explicit:'
    key = _k + dev.nodeName

    print r.smembers(key)