def create_flavor(request): try: serializer = FlavorSerializer(data=request.data, context={"request": request}) if serializer.is_valid(): #LOG.info(serializer.data) LOG.info("************ CREATE FLAVOR ***************") novaadmin = get_nova_admin(request) #LOG.info(type(novaadmin)) #LOG.info(novaadmin) #LOG.info(novaadmin.flavors.list()) mem = request.data.get("memory") name = request.data.get("name") cpu = request.data.get("cpu") disk = request.data.get("disk") flavor = novaadmin.flavors.create(name = name , ram = mem, vcpus = cpu, disk = disk) #LOG.info(flavor.id) flavorid = flavor.id try: serializer.save(flavorid = flavor.id) except: traceback.print_exc() LOG.info(Flavor.objects.all().filter(flavorid = flavor.id)) return Response({'success': True, "msg": _('Flavor is created successfully!')}, status=status.HTTP_201_CREATED) else: return Response({"success": False, "msg": _('Flavor data is not valid!'), 'errors': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: LOG.error("Failed to create flavor, msg:[%s]" % e) return Response({"success": False, "msg": _('Failed to create flavor for unknown reason.')})
def update_flavor(request): try: flavor = Flavor.objects.get(pk=request.data['id']) serializer = FlavorSerializer(instance=flavor, data=request.data, context={"request": request}) if serializer.is_valid(): serializer.save() return Response( { 'success': True, "msg": _('Flavor is updated successfully!') }, status=status.HTTP_201_CREATED) else: return Response( { "success": False, "msg": _('Flavor data is not valid!'), 'errors': serializer.errors }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: LOG.error("Failed to create flavor, msg:[%s]" % e) return Response({ "success": False, "msg": _('Failed to update flavor for unknown reason.') })
def create_flavor(request): try: serializer = FlavorSerializer(data=request.data, context={"request": request}) if serializer.is_valid(): serializer.save() return Response({'success': True, "msg": _('Flavor is created successfully!')}, status=status.HTTP_201_CREATED) else: return Response({"success": False, "msg": _('Flavor data is not valid!'), 'errors': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: LOG.error("Failed to create flavor, msg:[%s]" % e) return Response({"success": False, "msg": _('Failed to create flavor for unknown reason.')})