def import_topology_json(request): logger.debug("---- import_topology_json ----") json_string = request.body # fixme - add some basic check to ensure we have the proper format here try: topology_json_string = wistarUtils.clone_topology(json_string) topology_json = json.loads(topology_json_string) for json_object in topology_json: if json_object["type"] == "wistar.info": name = json_object["name"] description = json_object["description"] break logger.debug("Creating new topology with name: %s" % name) t = Topology(name=name, description=description, json=topology_json_string) t.save() return apiUtils.return_json(True, "Topology Imported with id: %s" % t.id, topology_id=t.id) except Exception as e: logger.error(e) return apiUtils.return_json(False, "Topology Import Failed!")
def multi_clone(request): logger.debug('---- topology mulit_clone ----') required_fields = set(['clones', 'topoId']) if not required_fields.issubset(request.POST): logger.error('Invalid parameters in POST') return render(request, 'ajax/ajaxError.html', {'error': "Invalid Parameters in POST"}) topology_id = request.POST["topoId"] num_clones = request.POST["clones"] topology = get_object_or_404(Topology, pk=topology_id) json_string = topology.json i = 0 while i < num_clones: new_topo = topology orig_name = topology.name new_topo.name = orig_name new_topo.json = wistarUtils.clone_topology(json_string) # json = new_topo.json new_topo.id = None new_topo.save() image_list = Image.objects.all().order_by('name') context = {'image_list': image_list, 'topo': topology} return render(request, 'topologies/edit.html', context)
def clone(request, topo_id): logger.debug('---- topology clone ----') topology = get_object_or_404(Topology, pk=topo_id) orig_name = topology.name topology.name = orig_name + " clone" topology.json = wistarUtils.clone_topology(topology.json) topology.id = 0 image_list = Image.objects.all().order_by('name') script_list = Script.objects.all().order_by('name') vm_types = configuration.vm_image_types vm_types_string = json.dumps(vm_types) image_list_json = serializers.serialize('json', Image.objects.all(), fields=('name', 'type')) # also grab all the ips from the currently cloned topology currently_allocated_ips = wistarUtils.get_used_ips() cloned_ips = wistarUtils.get_used_ips_from_topology_json(topology.json) currently_allocated_ips += cloned_ips currently_allocated_ips.sort() if configuration.deployment_backend == "openstack": external_bridge = configuration.openstack_external_network else: external_bridge = configuration.kvm_external_bridge context = {'image_list': image_list, 'script_list': script_list, 'vm_types': vm_types_string, 'image_list_json': image_list_json, 'external_bridge': external_bridge, 'allocated_ips': currently_allocated_ips, 'topo': topology } return render(request, 'topologies/new.html', context)
def multi_clone_topology(request): response_data = {"result": True} required_fields = set(['clones', 'topologyId']) if not required_fields.issubset(request.POST): response_data["message"] = "Invalid Parameters in Post" return HttpResponse(json.dumps(response_data), content_type="application/json") topology_id = request.POST["topologyId"] num_clones = request.POST["clones"] logger.debug(num_clones) topology = Topology.objects.get(pk=topology_id) orig_name = topology.name json_data = topology.json i = 0 while i < int(num_clones): nj = wistarUtils.clone_topology(json_data) if nj is not None: new_topology = topology new_topology.name = orig_name + " " + str(i + 1).zfill(2) new_topology.json = nj json_data = new_topology.json new_topology.id = None new_topology.save() i += 1 return HttpResponse(json.dumps(response_data), content_type="application/json")
def import_topology_json(request): logger.debug("---- import_topology_json ----") json_string = request.body # fixme - add some basic check to ensure we have the proper format here topology_json_string = wistarUtils.clone_topology(json_string) if topology_json_string is None: return apiUtils.return_json(False, "Topology Import Failed!") try: topology_json = json.loads(topology_json_string) except ValueError as ve: logger.error('Could not parse topology json from Clone!') return apiUtils.return_json(False, "Topology Import Failed!") try: for json_object in topology_json: if json_object["type"] == "wistar.info": name = json_object["name"] description = json_object["description"] break if Topology.objects.filter(name=name).exists(): logger.info('Not importing existing topology with this name!') return apiUtils.return_json(True, "Topology Exists with name: %s" % name) logger.debug("Creating new topology with name: %s" % name) t = Topology(name=name, description=description, json=topology_json_string) t.save() return apiUtils.return_json(True, "Topology Imported with id: %s" % t.id, topology_id=t.id) except Exception as e: logger.error(e) return apiUtils.return_json(False, "Topology Import Failed!")