示例#1
0
def update(request):
	regionName = request.REQUEST.get("regionname", "")
	zoneName = request.REQUEST.get("zonename", "")
	redirectURL = '/cloudman/message/?msg='
	groups = request.META.get('ADFS_GROUP','')
	groupsList = groups.split(';') 
	## update operation is allowed for a user if he/she has any one of the following rights
	## cloudman resource manager privileges
	## or has membership of the admin_group of the region to which this zone belongs to
	userIsSuperUser = isSuperUser(groupsList)
	if not userIsSuperUser:
		userIsAdmin = isAdminForRegion(regionName, groupsList)
		if not userIsAdmin:
		  message = "You neither have membership of administrative group of region " + regionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to Edit Zone";
		  html = "<html><body> %s.</body></html>" % message
		  return HttpResponse(html)

	## Get the Zone Object
	zoneObject = None
	try:
	   zoneObject = Zone.objects.get(name=zoneName, region__name=regionName)
	except Zone.DoesNotExist:
	   failureMessage = "Zone with Name " + zoneName + " in Region " + regionName + " could not be found"
	   return HttpResponseRedirect(redirectURL+failureMessage)
	oldZoneInfo = getZoneInfo(zoneObject)
	## if the request is from a update form submission, then try to update the values or else return to
	## display the form
	if request.method == 'POST':
	   ## Existing values
	   currRegionName = zoneObject.region.name
	   currName = zoneObject.name
	   currDescription = zoneObject.description
	   currHepSpec = zoneObject.hepspecs
	   currMemory = zoneObject.memory
	   currStorage = zoneObject.storage
	   currBandwidth = zoneObject.bandwidth
	   currHepspec_overcommit = zoneObject.hepspec_overcommit
	   currMemory_overcommit = zoneObject.memory_overcommit
	   currRTList = ZoneAllowedResourceType.objects.filter(zone__name=zoneName, zone__region__name=regionName).values_list('resource_type__name', flat=True)
	   ## new values
	   newRegionName = request.POST['region']
	   newName = request.POST['name']
	   newDescription = request.POST['description']
	   newHepSpec = request.POST['hepspecs']
	   newMemory = request.POST['memory']
	   newStorage = request.POST['storage']
	   newBandwidth = request.POST['bandwidth']
	   newHepspec_overcommit = request.POST['hepspec_overcommit']
	   newMemory_overcommit = request.POST['memory_overcommit']
	   newRTList = request.POST.getlist('zoneallowedrt')
	   comment = request.POST['comment']
	   try:
			validate_name(newRegionName)
			validate_name(newName)
			validate_descr(newDescription)
			validate_float(newHepSpec)
			validate_float(newMemory)
			validate_float(newStorage)
			validate_float(newBandwidth)
			validate_comment(comment)
	   except ValidationError as e:
			message ='Edit Zone Form  '+', '.join(e.messages)
			html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % message
			return HttpResponse(html)
	   ## validate the new resource parameter values
	   errorMsg = checkAttributeValues(newHepSpec, newMemory, newStorage, newBandwidth)
	   if (errorMsg != ''):
		   return HttpResponseRedirect(redirectURL + errorMsg)

	   newHepspec_overcommit = float(newHepspec_overcommit)
	   newMemory_overcommit = float(newMemory_overcommit)
	   if newHepspec_overcommit < 1:
		   msgFailure = "Hepspec Over Commit value should be greater than or equal to 1. Hence Edit Zone Operation Stopped"
		   return HttpResponseRedirect(redirectURL + msgFailure)

	   if newMemory_overcommit < 1:
		   msgFailure = "Memory Over Commit value should be greater than or equal to 1. Hence Edit Zone Operation Stopped"
		   return HttpResponseRedirect(redirectURL + msgFailure)

	   ## check whether any new resource type has been selected or existing resource type is de-selected
	   rtNotChanged = True;
	   for newRt in newRTList:
		   if not newRt in currRTList:
			  rtNotChanged = False

	   for oldRt in currRTList:
		   if not oldRt in newRTList:
			  rtNotChanged = False

	   if (newHepSpec == ''):
		  newHepSpec = None
	   else:
		  newHepSpec = round((float(newHepSpec)), 3)
	   if (newMemory == ''):
		  newMemory = None
	   else:
		  newMemory = round((float(newMemory)), 3)
	   if (newStorage == ''):
		  newStorage = None
	   else:
		  newStorage = round((float(newStorage)), 3)
	   if (newBandwidth == ''):
		  newBandwidth = None
	   else:
		  newBandwidth = round((float(newBandwidth)), 3)

	   ## check if atleast one field value has been changed
	   if ( (currRegionName == newRegionName) and (currName == newName) and (currDescription == newDescription) and (currHepSpec == newHepSpec) and (currMemory == newMemory) and (currStorage == newStorage) and (currBandwidth == newBandwidth) and (currHepspec_overcommit == newHepspec_overcommit) and (currMemory_overcommit == newMemory_overcommit) and (rtNotChanged) ):
		   message = 'No New Value provided for any field to perform Edit Operation. Hence Edit Zone ' + zoneName + ' in Region ' + regionName + ' aborted'
		   return HttpResponseRedirect(redirectURL + message)

	   ## if region is changed, then validate it, check if user has either cloudman resource manager privileges
	   ## or new region admin_group membership
	   ## if all is fine, then assign the new region name and get the new Region Object 
	   if (currRegionName != newRegionName):
		  if newRegionName == '':
			 errorMsg = 'Region name field cannot be left blank. So Edit Zone operation stopped'
			 return HttpResponseRedirect(redirectURL + errorMsg)
		  # do for region
		  if not userIsSuperUser:
			 userIsAdmin = isAdminForRegion(newRegionName, groupsList)
			 if not userIsAdmin:
				message = "You neither have membership of administrative group of region " + newRegionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to shift Zone from Region " + currRegionName + " to Region " + newRegionName;
				html = "<html><body> %s.</body></html>" % message
				return HttpResponse(html) 
		  regionName = newRegionName
		  regionObject = None
		  try:
			 regionObject = Region.objects.get(name=newRegionName)
		  except Region.DoesNotExist:
			 errorMessage = 'No record found for Region with name ' + newRegionName + '. Hence Edit Zone operation stopped'
			 return HttpResponseRedirect(redirectURL + errorMessage)
		  zoneObject.region = regionObject

	   ## if zone name is changed, then validate it and assign the new name
	   if (currName != newName):
		   if (newName == ''):
			  errorMsg = 'Name name field cannot be left blank. So Edit Zone operation stopped'
			  return HttpResponseRedirect(redirectURL + errorMsg)
		   if (currRegionName != newRegionName):
			  nameExists = checkNameIgnoreCase(newRegionName, newName)
			  if nameExists:
				  msgAlreadyExists = 'Zone ' + newName + ' in Region ' + newRegionName + ' already exists. Hence Edit Zone Operation Stopped'
				  return HttpResponseRedirect(redirectURL + msgAlreadyExists);
		   else:
			  nameExists = checkNameIgnoreCase(currRegionName, newName)
			  if nameExists:
				  msgAlreadyExists = 'Zone ' + newName + ' in Region ' + currRegionName + ' already exists. Hence Edit Zone Operation Stopped'
				  return HttpResponseRedirect(redirectURL + msgAlreadyExists);
		   zoneObject.name = newName

	   ## check if description is changed and if so, assign the new value
	   if (currDescription != newDescription):
		   zoneObject.description = newDescription
	   
	   if ( (currHepSpec != newHepSpec) or (currMemory != newMemory) or (currStorage != newStorage) or (currBandwidth != newBandwidth) ):
		  newResourceValues = {'hepspecs': newHepSpec, 'memory': newMemory, 'storage': newStorage, 'bandwidth': newBandwidth, 'hepspec_overcommit': newHepspec_overcommit, 'memory_overcommit': newMemory_overcommit}	  
		  ## check whether top level allocations of this zone can be met with the new resource values
		  errorMessage = checkTpLevelAllocations(currName, currRegionName, newResourceValues)
		  if errorMessage != '':
			 return HttpResponseRedirect(redirectURL + errorMessage)

	   ## Assign the new resource values if changed
	   if (currHepSpec != newHepSpec):
		  zoneObject.hepspecs = newHepSpec
	   if (currMemory != newMemory):
		  zoneObject.memory = newMemory
	   if (currStorage != newStorage):
		  zoneObject.storage = newStorage
	   if (currBandwidth != newBandwidth):
		  zoneObject.bandwidth = newBandwidth
	   if (currHepspec_overcommit != newHepspec_overcommit):
		  zoneObject.hepspec_overcommit = newHepspec_overcommit
	   
	   if (currMemory_overcommit != newMemory_overcommit):
		  zoneObject.memory_overcommit = newMemory_overcommit

	   ## delete the resouce types which are de-selected
	   if not rtNotChanged:
		  for oldRt in currRTList:
			 if not oldRt in newRTList:
			   try:
				  #rtObject = ResourceType.objects.get(name=oldRt)
				  zrt = ZoneAllowedResourceType.objects.get(resource_type__name=oldRt, zone__name=currName, zone__region__name=currRegionName)
				  zrt.delete()
			   except ZoneAllowedResourceType.DoesNotExist:
				  errorMessage = 'No Record Found for Resource Type ' + oldRt + '. Hence Edit Zone Operation Stopped'
				  return HttpResponseRedirect(redirectURL + errorMessage)

	   ## Finally save all the zone object changes	   
	   zoneObject.save()
	  # newZoneObj = copy.copy(zoneObject)

	   ## if a new resource type is selected, then add it to the zone allowed resource type table
	   if not rtNotChanged:
		  for newRt in newRTList:
			 if not newRt in currRTList:
				try:
				   rtObject = ResourceType.objects.get(name=newRt)
				   zrt = ZoneAllowedResourceType(zone=zoneObject, resource_type=rtObject)
				   zrt.save()
				except Exception, err:
				   errorMessage = "Error in Creating Zone Allowed Resource Type , reason : %s" % str(err)
				   return HttpResponseRedirect(redirectURL + errorMessage)

	   #Write The Log
  	   newZoneInfo = getZoneInfo(zoneObject)
	   objectId = zoneObject.id 	   
	   if addUpdateLog(request,newName,objectId,comment,oldZoneInfo,newZoneInfo,'zone',True):
	   		transaction.commit()
	   	 	message = 'Zone ' + zoneName + ' in Region ' + currRegionName + ' Successfully Updated'
	   else:
	   		transaction.rollback()
	   		message = 'Error in updating Zone ' + zoneName + ' in Region ' + currRegionName 
	   ## Finally display a success message to the user
	   html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % message
	   return HttpResponse(html)
示例#2
0
def addnew(request):
	## The add zone function can be used in two ways.
	## one way, is to click add new zone from the zone list page.....
	## which will display all the regions and create a zone by selecting a region from the list
	## second way, is to click add new zone from the region detailed page...
	## which means you already selected the region and adding a zone in that region
	## that's why the following variable says whether you already selected region or not
	selRegionName = request.REQUEST.get("regionname", "")
	## Check if there are any regions defined. If not, then zones cannot be added
	regionsCount = getRegionCount()
	if (regionsCount == 0):
		message = "No Regions Defined. First create Region and then try to add Zone"
		html = "<html><body> %s.</body></html>" % message
		return HttpResponse(html)
	## Check if there are any resource types defined. If not, then zones cannot be added
	resourceTypesCount = getResourceTypesCount()
	if (resourceTypesCount == 0):
		message = "No Resource Types Defined. First create Resource Types and then try to add Zone"
		html = "<html><body> %s. </body></html>" % message
		return HttpResponse(html)
	groups = request.META.get('ADFS_GROUP','')
	groupsList = groups.split(';')
	userIsSuperUser = isSuperUser(groupsList)
	## If the request is through form submission, then try to add the region or else display the add form 
	if request.method == 'POST':
		redirectURL = '/cloudman/message/?msg='
		regionName = request.POST['region']
		zoneName = request.POST['name']
		description = request.POST['description']
		hepSpecs = request.POST['hepspecs']
		memory = request.POST['memory']
		storage = request.POST['storage']
		bandwidth = request.POST['bandwidth']
		hepSpec_overcommit = request.POST['hepspec_overcommit']
		memory_overcommit = request.POST['memory_overcommit']
		comment = request.POST['comment']
		try:
			validate_name(regionName)
			validate_name(zoneName)
			validate_descr(description)
			validate_float(hepSpecs)
			validate_int(memory)
			validate_int(storage)
			validate_float(bandwidth)
			validate_comment(comment)
			validate_float(hepSpec_overcommit)
			validate_float(memory_overcommit)
		except ValidationError as e:
			message ='Add Zone Form  '+', '.join(e.messages)
			html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % message
			return HttpResponse(html)
		## check for uniqueness of this zone name in the region
		nameExists = checkNameIgnoreCase(regionName, zoneName)
		if nameExists:
			msgAlreadyExists = 'Zone Name ' + zoneName + ' in Region ' + regionName + ' already exists. Hence Add New Zone Operation Stopped'
			transaction.rollback()
			return HttpResponseRedirect(redirectURL + msgAlreadyExists)
		## check whether user has any of the following rights
		## cloudman resource manager privileges
		## If not, then has membership of region admin_group	 
		if not userIsSuperUser:
			userIsAdminOfRegion = isAdminForRegion(regionName, groupsList)
			if not userIsAdminOfRegion:
				message = "You neither have membership of administrative group of region " + regionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone";
				return HttpResponseRedirect(redirectURL + message)
		## Get the Region Object
		try:
			region = Region.objects.get(name=regionName)
		except Region.DoesNotExist:
			errorMessage = 'No Record Found for Region ' + regionName + '. Hence Add New Zone Operation Stopped'
			return HttpResponseRedirect(redirectURL + errorMessage)
		## validate hepspec, memory, storage and bandwidth values
		errorMsg = checkAttributeValues(hepSpecs, memory, storage, bandwidth)
		if (errorMsg != ''):
			return HttpResponseRedirect(redirectURL + errorMsg)
		## validate hepspec over commit and memory over commit values
		if hepSpec_overcommit < 1:
			msgFailure = "Hepspec Over Commit value should be greater than or equal to 1. Hence Add Zone Operation Stopped"
			return HttpResponseRedirect(redirectURL + msgFailure)
		if memory_overcommit < 1:
			msgFailure = "Memory Over Commit value should be greater than or equal to 1. Hence Add Zone Operation Stopped"
			return HttpResponseRedirect(redirectURL + msgFailure)
		## get all the resource types check boxes values (resource type name), 
		## the allowed resource types for this zone will be the ones whose checkbox is selected
		totalResourceTypes = request.POST['totalresourcetypes']
		index = 1
		atleastOneRTsel = False
		resourceTypesList = []
		while index <= int(totalResourceTypes):
			if ('resourcetype'+str(index)) in request.POST.keys() :
				## if checkbox selected
				resourceTypesList.append(request.POST['resourcetype'+str(index)])
				atleastOneRTsel = True
			index = index + 1
		if not atleastOneRTsel:
			message = "No Resource Types selected that are allowed for this Zone. Hence Zone Add Operation Stopped"
			return HttpResponseRedirect(redirectURL + message)
		## get the resource type objects for all those that are selected
		resourceTypesFullInfoList = []
		msgSuccess = ''
		for oneRT in resourceTypesList:
			try:
				resourceType = ResourceType.objects.get(name=oneRT)
				resourceTypesFullInfoList.append(resourceType)
				msgSuccess = msgSuccess + ' ' + oneRT;
			except ResourceType.DoesNotExist:
				errorMessage = 'No Record Found for Resource Type ' + oneRT + '. Hence Add New Zone Operation Stopped'
				return HttpResponseRedirect(redirectURL + errorMessage)
		msgSuccess = 'New Zone ' + zoneName + ' added successfully ' + ' to Region ' + regionName + '. The allowed Resource Types are ' + msgSuccess
		if hepSpecs == '':
			hepSpecs = None
		else:
			hepSpecs = round((float(hepSpecs)), 3)
		if memory == '':
			memory = None
		else:
			memory = round((float(memory)), 3)
		if storage == '':
			storage = None
		else:
			storage = round((float(storage)), 3)
		if bandwidth == '':
			bandwidth = None
		else:
			bandwidth = round((float(bandwidth)), 3)
		## create the zone object and save it
		newzone = Zone(name=zoneName, description=description, region=region, hepspecs=hepSpecs, memory=memory, storage=storage, bandwidth=bandwidth, hepspec_overcommit=hepSpec_overcommit, memory_overcommit=memory_overcommit)
		newzone.save()
		## get the newly added zone object
		## add the allowed resource types for this zone
		addedZone = Zone.objects.get(name=zoneName, region=region)
		for oneRT in resourceTypesFullInfoList:
			zrt = ZoneAllowedResourceType(zone=addedZone, resource_type=oneRT)
			zrt.save()
		##Add the Log for Zone
		if addLog(request,zoneName,comment,addedZone,None,'zone','add',True):
			transaction.commit()
		else:
			transaction.rollback()
			msgSuccess = 'Error in creating new zone' + zoneName
			
		## return the success message to the user
		html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/zone/list/\"></HEAD><body> %s.</body></html>" % msgSuccess
		return HttpResponse(html)
	else:
		## Zone can be added provided you have any of the following privileges
		## cloudman resource manager privileges
		## only to regions for which user has membership of its admin_group
		if not userIsSuperUser:
			userIsAdmin = True
			## if region is not already selected, then check user has membership of atleast one region admin_group
			if selRegionName == '':
				userIsAdmin = isAdminOfAnyRegion(groupsList)
				if not userIsAdmin:
					message = "You neither have membership of administrative groups of any region nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone";
					html = "<html><body> %s.</body></html>" % message
					return HttpResponse(html)
			else:
				userIsAdmin = isAdminForRegion(selRegionName, groupsList)
				if not userIsAdmin:
					message = "You neither have membership of administrative group of region " + selRegionName + " nor possess Cloudman Resource Manager Privileges. Hence you are not authorized to add new Zone";
					html = "<html><body> %s.</body></html>" % message
					return HttpResponse(html)
		form = ZoneForm(userGroups=groupsList, superUserRights=userIsSuperUser)
                resourceForm=ResourceForm
	resourceType = ResourceType.objects.all()
	return render_to_response('zone/addnew.html',locals(),context_instance=RequestContext(request))