Esempio n. 1
0
def import_topology(request):
    logger.debug('---- topology import ----')
    try:
        if request.method == "POST":
            logger.debug(str(request.FILES))

            json_file = request.FILES['file']
            logger.debug(str(json_file))
            json_string = json_file.read()
            json_data = json.loads(json_string)

            topology = Topology()
            topology.name = "Imported Topology"
            topology.id = 0

            # keep track of all the ips that are currently used
            # we will import this topology and ensure that any assigned ips are unique for this environment
            currently_allocated_ips = wistarUtils.get_used_ips()
            next_ip_floor = 2
            logger.debug("Iterating json objects in imported data")
            for json_object in json_data:
                if "userData" in json_object and "wistarVm" in json_object["userData"]:
                    logger.debug("Found one")
                    ud = json_object["userData"]
                    # check if we have this type of image
                    image_list = Image.objects.filter(type=ud["type"])
                    if len(image_list) == 0:
                        # nope, bail out and let the user know what happened!
                        logger.error("Could not find image of type " + ud["type"])
                        return error(request, 'Could not find a valid image of type ' + ud['type'] +
                                     '! Please upload an image of this type and try again')

                    image = image_list[0]
                    logger.debug(str(image.id))
                    json_object["userData"]["image"] = image.id

                    valid_ip = wistarUtils.get_next_ip(currently_allocated_ips, next_ip_floor)
                    next_ip_floor = valid_ip

                    json_object["userData"]["ip"] = configuration.management_prefix + str(valid_ip)

                elif json_object["type"] == "wistar.info":
                    topology.name = json_object["name"]
                    topology.description = json_object["description"]

            topology.json = json.dumps(json_data)

            image_list = Image.objects.all().order_by('name')
            image_list_json = serializers.serialize('json', Image.objects.all(), fields=('name', 'type'))
            script_list = Script.objects.all().order_by('name')
            vm_types = configuration.vm_image_types
            vm_types_string = json.dumps(vm_types)

            context = {'image_list': image_list,
                       'image_list_json': image_list_json,
                       'allocated_ips': currently_allocated_ips,
                       'script_list': script_list,
                       'vm_types': vm_types_string,
                       'topo': topology
                       }

            return render(request, 'topologies/new.html', context)

        else:
            form = ImportForm()
            context = {'form': form}
            return render(request, 'topologies/import.html', context)
    except Exception as e:
        logger.error('Could not parse imported data!')
        logger.error(e)
        return error(request, 'Could not parse imported data')
Esempio n. 2
0
def import_topology(request):
    logger.debug('---- topology import ----')
    try:
        if request.method == "POST":
            logger.debug(str(request.FILES))

            json_file = request.FILES['file']
            logger.debug(str(json_file))
            json_string = json_file.read()
            json_data = json.loads(json_string)

            topology = Topology()
            topology.name = "Imported Topology"
            topology.id = 0

            # keep track of all the ips that are currently used
            # we will import this topology and ensure that any assigned ips are unique for this environment
            currently_allocated_ips = wistarUtils.get_used_ips()
            next_ip_floor = 2
            logger.debug("Iterating json objects in imported data")
            for json_object in json_data:
                if "userData" in json_object and "wistarVm" in json_object[
                        "userData"]:
                    # logger.debug("Found one")
                    ud = json_object["userData"]

                    # Tries to import with the same image index and type
                    # If it doesn't exist, we'll use the closest type
                    image_id = wistarUtils.get_same_or_similar_image(
                        ud["image"], ud["type"])

                    if image_id == None:
                        # Failed to find the same image or even one with a similar type
                        logger.error("Could not find image of type " +
                                     ud["type"])
                        return error(
                            request, 'Could not find a valid image of type ' +
                            ud['type'] +
                            '! Please upload an image of this type and try again'
                        )
                    else:
                        # Either we found a suitable one or even the same one
                        json_object["userData"]["image"] = image_id

                    valid_ip = wistarUtils.get_next_ip(currently_allocated_ips,
                                                       next_ip_floor)
                    next_ip_floor = valid_ip

                    json_object["userData"][
                        "ip"] = configuration.management_prefix + str(valid_ip)

                elif json_object["type"] == "wistar.info":
                    topology.name = json_object["name"]
                    topology.description = json_object["description"]

            topology.json = json.dumps(json_data)

            image_list = Image.objects.all().order_by('name')
            image_list_json = serializers.serialize('json',
                                                    Image.objects.all(),
                                                    fields=('name', 'type'))
            script_list = Script.objects.all().order_by('name')
            vm_types = configuration.vm_image_types
            vm_types_string = json.dumps(vm_types)

            dhcp_reservations = wistarUtils.get_consumed_management_ips()

            context = {
                'image_list': image_list,
                'image_list_json': image_list_json,
                'allocated_ips': currently_allocated_ips,
                'script_list': script_list,
                'vm_types': vm_types_string,
                'dhcp_reservations': dhcp_reservations,
                'topo': topology
            }

            return render(request, 'topologies/new.html', context)

        else:
            form = ImportForm()
            context = {'form': form}
            return render(request, 'topologies/import.html', context)
    except Exception as e:
        logger.error('Could not parse imported data!')
        logger.error(e)
        return error(request, 'Could not parse imported data')