Ejemplo n.º 1
0
def add_pool(data, user):
    _validate_pool(data[BLOCKS], data[TYPE])

    # create new pool
    pool = Pool()
    pool.name = data[NAME]
    pool.type = data[TYPE]
    pool.scope = data[SCOPE]
    pool.role = data[ROLE]
    pool.blocks = data[BLOCKS]
    pool.updated_by = user
    pool.save()

    if pool.scope == FABRIC:
        return pool

    # if scope is global create pool entries
    if pool.type == INTEGER:
        _create_integer_pool(pool, pool.blocks)
    elif pool.type == IPV4:
        _create_ipv4_pool(pool, pool.blocks)
    elif pool.type == IPV6:
        _create_ipv6_pool(pool, pool.blocks)

    return pool
Ejemplo n.º 2
0
    def post(self, request, format=None):
        serializer = PoolSerializer(data=request.data)
        if serializer.is_valid():

            col_object = Pool()
            col_object.type = serializer.data['type']
            col_object.name = serializer.data['name']
            col_object.scope = serializer.data['scope']
            col_object.save()
            
            type = serializer.data['type']
            coll_range=[{'start':0,'end':1}]
            if type == 'AutoGenerate':
                start = 10
                end = 20
                for flag in range(start,end + 1):
                    coll = PoolDetail()
                    coll.index = col_object
                    coll.value = flag
                    coll.save()
                coll_range[0]['start'] = start
                coll_range[0]['end'] = end
                col_object.range = json.dumps(coll_range)
            else:
                col_object.range = json.dumps(serializer.data['range'])
            col_object.save()

            
            if type == 'Integer' or type =='Vlan':
                for i in range(0,len(serializer.data['range'])):
                    start = int(serializer.data['range'][i]['start'])
                    end = int(serializer.data['range'][i]['end'])
                    for flag in range(start,end + 1):
                        coll = PoolDetail()
                        coll.index = col_object
                        coll.value = flag
                        coll.save()
                    
            if type == 'IP' or type == 'MgmtIP' or type == 'IPv6':
                for i in range(0,len(serializer.data['range'])):
                    start_ip = IPNetwork(str(serializer.data['range'][i]['start']))
                    end_ip = IPNetwork(str(serializer.data['range'][i]['end']))
                    for ip_val in range (start_ip.ip,end_ip.ip+1):
                        coll = PoolDetail()
                        coll.index = col_object
                        coll.value = IPAddress(ip_val).__str__()+"/"+start_ip.prefixlen.__str__()
                        coll.save()
                    
        
        #    if type == 'IPv6':
        #        start_ip = IPNetwork(str(serializer.data['range'][0]['start']))
        #        end_ip = IPNetwork(str(serializer.data['range'][0]['end']))
        #        for ip_val in range (start_ip.ip,end_ip.ip+1):
        #            coll = PoolDetail()
        #            coll.index = col_object
        #            coll.value = IPAddress(ip_val).__str__()+"/"+start_ip.prefixlen.__str__()
        #            coll.save()
                    
            get_coll_detail = PoolDetail.objects.filter(index = col_object.id,assigned ='')
            available = len(get_coll_detail)
            col_object.available = available
            col_object.used = len(PoolDetail.objects.filter(index = col_object.id)) - available
            col_object.save()
            serializer = PoolGetSerializer(col_object)
            collect_details = serializer.data
            collect_details['range'] = json.loads(collect_details['range'])
            return Response(collect_details, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)