Пример #1
0
def homepanel(request):
    anyRolePresent = False
    groupUserRolePresent = False
    groupManagerRolePresent = False
    projectManagerRolePresent = False
    regionManagerRolePresent = False

    fullName = request.META.get('ADFS_FULLNAME','')
    groups = request.META.get('ADFS_GROUP','')
    groupsList = groups.split(';') ;
    rolesList = getUserRoles(groupsList)
    for entry in rolesList:
        if entry[0] == 'group' and entry[1] == 'user':
            groupUserRoles = entry[2]
            if len(groupUserRoles) > 0:
                groupUserRolePresent = True
        if entry[0] == 'group' and entry[1] == 'manager':
            groupManagerRoles = entry[2]
            if len(groupManagerRoles) > 0:
                groupManagerRolePresent = True
        if entry[0] == 'project' and entry[1] == 'manager':
            projectManagerRoles = entry[2]
            if len(projectManagerRoles) > 0:
                projectManagerRolePresent = True
        if entry[0] == 'region' and entry[1] == 'manager':
            regionManagerRoles = entry[2]
            if len(regionManagerRoles) > 0:
                regionManagerRolePresent = True
    if groupUserRolePresent or groupManagerRolePresent or projectManagerRolePresent or regionManagerRolePresent:
        anyRolePresent = True
    egroupsList = getAllEGroups()
    userIsSuperUser = isSuperUser(groupsList)
    usersGroupsSet = set(groupsList)
    SUSet = set(SUPER_USER_GROUPS)

    intersectionValue = usersGroupsSet.isdisjoint(SUSet)
    intersectionSet = usersGroupsSet.intersection(SUSet)
    regionsCount = getRegionCount()
    zonesCount = getZoneCount()
    groupsCount = getGroupsCount()

    return render_to_response('homepanel.html',locals(),context_instance=RequestContext(request))
Пример #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))