Esempio n. 1
0
def network_create(network, owner, access, net_id):
    """Create a network.

    If the network with that name already exists, a DuplicateError will be
    raised.

    If the combination of owner, access, and net_id is illegal, a
    BadArgumentError will be raised.

    If network ID allocation was requested, and the network cannot be
    allocated (due to resource exhaustion), an AllocationError will be raised.

    Pass 'admin' as owner for an administrator-owned network.  Pass '' as
    access for a publicly accessible network.  Pass '' as net_id if you wish
    to use the HaaS's network-id allocation pool.

    Details of the various combinations of network attributes are in
    docs/networks.md
    """
    auth_backend = get_auth_backend()
    _assert_absent(model.Network, network)

    # Check authorization and legality of arguments, and find correct 'access'
    # and 'owner'
    if owner != "admin":
        owner = _must_find(model.Project, owner)
        auth_backend.require_project_access(owner)
        # Project-owned network
        if access != owner.label:
            raise BadArgumentError(
                "Project-owned networks must be accessible by the owner.")
        if net_id != "":
            raise BadArgumentError(
                "Project-owned networks must use network ID allocation")
        access = [_must_find(model.Project, access)]
    else:
        # Administrator-owned network
        auth_backend.require_admin()
        owner = None
        if access == "":
            access = []
        else:
            access = [_must_find(model.Project, access)]

    # Allocate net_id, if requested
    if net_id == "":
        net_id = get_network_allocator().get_new_network_id()
        if net_id is None:
            raise AllocationError('No more networks')
        allocated = True
    else:
        if not get_network_allocator().validate_network_id(net_id):
            raise BadArgumentError("Invalid net_id")
        allocated = False

    network = model.Network(owner, access, allocated, net_id, network)
    db.session.add(network)
    db.session.commit()
Esempio n. 2
0
def network_create(network, creator, access, net_id):
    """Create a network.

    If the network with that name already exists, a DuplicateError will be
    raised.

    If the combination of creator, access, and net_id is illegal, a
    BadArgumentError will be raised.

    If network ID allocation was requested, and the network cannot be
    allocated (due to resource exhaustion), an AllocationError will be raised.

    Pass 'admin' as creator for an administrator-owned network.  Pass '' as
    access for a publicly accessible network.  Pass '' as net_id if you wish
    to use the HaaS's network-id allocation pool.

    Details of the various combinations of network attributes are in
    docs/networks.md
    """
    db = model.Session()
    _assert_absent(db, model.Network, network)

    # Check legality of arguments, and find correct 'access' and 'creator'
    if creator != "admin":
        # Project-owned network
        if access != creator:
            raise BadArgumentError(
                "Project-created networks must be accessed only by that project."
            )
        if net_id != "":
            raise BadArgumentError(
                "Project-created networks must use network ID allocation")
        creator = _must_find(db, model.Project, creator)
        access = _must_find(db, model.Project, access)
    else:
        # Administrator-owned network
        creator = None
        if access == "":
            access = None
        else:
            access = _must_find(db, model.Project, access)

    # Allocate net_id, if requested
    if net_id == "":
        driver_name = cfg.get('general', 'driver')
        driver = importlib.import_module('haas.drivers.' + driver_name)
        net_id = driver.get_new_network_id(db)
        if net_id is None:
            raise AllocationError('No more networks')
        allocated = True
    else:
        allocated = False

    network = model.Network(creator, access, allocated, net_id, network)
    db.add(network)
    db.commit()
Esempio n. 3
0
 def network(self):
     project = model.Project('anvil-nextgen')
     return model.Network(project, project, True, '102', 'hammernet')
Esempio n. 4
0
def network_create(network, creator, access, net_id):
    """Create a network.

    If the network with that name already exists, a DuplicateError will be
    raised.

    If the combination of creator, access, and net_id is illegal, a
    BadArgumentError will be raised.

    If network ID allocation was requested, and the network cannot be
    allocated (due to resource exhaustion), an AllocationError will be raised.

    Pass 'admin' as creator for an administrator-owned network.  Pass '' as
    access for a publicly accessible network.  Pass '' as net_id if you wish
    to use the HaaS's network-id allocation pool.

    Details of the various combinations of network attributes are in
    docs/networks.md
    """
    db = model.Session()
    _assert_absent(db, model.Network, network)

    # Check legality of arguments, and find correct 'access' and 'creator'
    if creator != "admin":
        # Project-owned network
        if access != creator:
            raise BadArgumentError(
                "Project-created networks must be accessed only by that project."
            )
        if net_id != "":
            raise BadArgumentError(
                "Project-created networks must use network ID allocation")
        creator = _must_find(db, model.Project, creator)
        access = _must_find(db, model.Project, access)
    else:
        # Administrator-owned network
        creator = None
        if access == "":
            access = None
        else:
            access = _must_find(db, model.Project, access)

    if cfg.getboolean('recursive', 'rHaaS'):

        #TODO - work out whether there is such a thing as an admin-created network in rHaaS
        # if so, how do we handle this case at bHaaS

        project = creator.label
        b_project = cfg.get('recursive', 'project')
        allocated = True
        #rHaaS always has allocated netIDs
        net_id = "dummy"

        #if creator.label == "admin":
        #   b_project = cfg.get('recursive','project')
        #else:
        #   project = creator.label
        #   print project

        bHaaS_out = check_output(
            [
                'haas',
                'network_create',
                network + '_' + project,
                b_project,  #how to handle case of admin in rHaaS?
                b_project,  #access and creator must be the same?
                ""
            ],
            stderr=STDOUT,
            shell=False)
        error_checker(
            bHaaS_out
        )  #can you get the assigned netID here? for now just dummy it out?
        network = model.Network(creator, access, allocated, net_id, network)
    else:
        # Allocate net_id, if requested
        if net_id == "":
            driver_name = cfg.get('general', 'driver')
            driver = importlib.import_module('haas.drivers.' + driver_name)
            net_id = driver.get_new_network_id(db)
            if net_id is None:
                raise AllocationError('No more networks')
            allocated = True
        else:
            allocated = False
        network = model.Network(creator, access, allocated, net_id, network)

    db.add(network)

    if cfg.getboolean('recursive', 'rHaaS'):
        b_project = cfg.get('recursive', 'project')
        network_name = network.label + b_project
        net_id = ''
        cli.network_create(network_name, b_project, b_project, net_id)

    db.commit()