Exemplo n.º 1
0
    async def createvirtualrouters(self, routers: [{"?id": str,
                                                     "?routes": [tuple_((cidr_nonstrict_type, ip_address_type))]}]):
        """
        Create multiple virtual routers in a transaction
        """
        idset = set()
        newrouters = []
        for router in routers:
            router = copy.deepcopy(router)
            if "id" not in router:
                router["id"] = str(uuid1())
            else:
                if router["id"] in idset:
                    raise ValueError("Repeated ID: " + router['id'])
                else:
                    idset.add(router['id'])

            newrouters.append(router)
        
        routerkeys = [VRouter.default_key(r['id']) for r in newrouters]
        routersetkey = [VRouterSet.default_key()]

        routerobjects = [self._createvirtualrouter(**r) for r in newrouters ]

        def createrouterdb(keys,values):
            routerset = values[0]

            for i in range(0,len(routerkeys)):
                values[i+1] = set_new(values[i+1],routerobjects[i])
                routerset.set.dataset().add(routerobjects[i].create_weakreference())

            return keys,values
        await call_api(self.app_routine,"objectdb","transact",
                         {"keys":routersetkey+routerkeys,"updater":createrouterdb})
        return await self._dumpkeys(routerkeys)
Exemplo n.º 2
0
    async def updatevirtualrouters(self, routers: [{"id": str,
                                                    "?routes": [tuple_((cidr_nonstrict_type, ip_address_type))]}]):
        "Update multiple virtual routers"
        idset = set()
        for router in routers:
            if 'id' not in router:
                raise ValueError("must specify id")
            else:
                if router['id'] in idset:
                    raise ValueError("Repeated ID: " + router['id'])
                else:
                    idset.add(router['id'])
        routerkeys = [VRouter.default_key(r['id']) for r in routers]

        def updaterouter(keys,values):

            for i in range(0,len(routers)):
                if values[i]:
                    for k,v in routers[i].items():
                        if k == 'routes':
                            values[i].routes[:] = copy.deepcopy(v)
                        else:
                            setattr(values[i], k, v)
                else:
                    raise ValueError("Virtual router not exists: " + routers[i]['id'])
            
            return keys,values
        await call_api(self.app_routine,'objectdb','transact',
                         {'keys':routerkeys,'updater':updaterouter})
        return await self._dumpkeys(routerkeys)
Exemplo n.º 3
0
 async def updatevirtualrouter(self, id: str, **kwargs: {"?routes": [tuple_((cidr_nonstrict_type, ip_address_type))]}):
     """
     Update virtual router
     """
     if not id:
         raise ValueError("must specify id")
     router = {"id":id}
     router.update(kwargs)
     return await self.updatevirtualrouters([router])
Exemplo n.º 4
0
    async def createvirtualrouter(self, id: (str, None) = None,
                                        **kwargs: {"?routes": [tuple_((cidr_nonstrict_type, ip_address_type))]}):
        """
        Create a virtual router
        
        :param id: Virtual router id. If omitted, an UUID is generated.
        
        :param \*\*kwargs: extra attributes for creation.
        
        :return: A dictionary of information of the virtual router.
        """
        if not id:
            id = str(uuid1())

        router = {"id":id}
        router.update(kwargs)

        return await self.createvirtualrouters([router])
Exemplo n.º 5
0
    return retdict


dhcp_option_tag_name_type = extra()


def _convert_to_tag(tag_name):
    tag = dhcp_tag.getValue(tag_name)
    if tag is None:
        raise TypeMismatchException(tag_name, dhcp_option_tag_name_type,
                                    "Not a valid DHCP option name")
    return tag


dhcp_option_tag_name_type.bind(str, convert=_convert_to_tag)

dhcp_option_tag_type = (int, dhcp_option_tag_name_type)

dhcp_options_type = extra()


def _check_dhcp_options(options):
    try:
        create_dhcp_options(options)
    except Exception as e:
        raise TypeMismatchException(options, dhcp_options_type, str(e))


dhcp_options_type.bind([tuple_((dhcp_option_tag_type, object))],
                       check=_check_dhcp_options)