Пример #1
0
def validateParameters1(rtName, resourceClass, hepspecs,memory, storage, bandwidth):
    errorMessage = ''
    if ( (rtName == None) or (rtName == '') ):
        errorMessage = 'Resource Type Name field cannot be blank. ';
    if ( (resourceClass == None) or (resourceClass == '') ):
        errorMessage = errorMessage + 'Resource Class field cannot be blank. ';
    errorMessage = errorMessage + checkAttributeValues(hepspec, memory, storage, bandwidth)
    return errorMessage
Пример #2
0
def update(request):
    rtName = request.REQUEST.get("name", "")
    redirectURL = '/cloudman/message/?msg='
    groups = request.META.get('ADFS_GROUP','')
    groupsList = groups.split(';') ;
    userIsSuperUser = isSuperUser(groupsList)
    ## Check - Logged in user has administrative privileges
    if not userIsSuperUser:
        message = "You don't have cloudman resource manager privileges. Hence you are not authorized to update Resource Type " + rtName;
        html = "<html><body> %s.</body></html>" % message
        return HttpResponse(html)
    ## Get the Resource Type Object
    resourceTypeObject = None
    try:
        resourceTypeObject = ResourceType.objects.get(name=rtName)
    except ResourceType.DoesNotExist:
        failureMessage = "Resource Type with Name " + rtName + " could not be found"
        return HttpResponseRedirect(redirectURL+failureMessage)
    oldRTInfo = getResourceTypeInfo(resourceTypeObject)
    ## if this is a form submission, then do the update or else display the form for update
    if request.method == 'POST':
        ## Existing values
        currName = resourceTypeObject.name
        currResourceClass = resourceTypeObject.resource_class
        currHepSpecs = resourceTypeObject.hepspecs
        currMemory = resourceTypeObject.memory
        currStorage = resourceTypeObject.storage
        currBandwidth = resourceTypeObject.bandwidth
        ## Get the new values
        newName = request.POST['name']
        newResourceClass = request.POST['resource_class']
        newHepSpecs = request.POST['hepspecs']
        newMemory = request.POST['memory']
        newStorage = request.POST['storage']
        newBandwidth = request.POST['bandwidth']
        comment  = request.POST['comment']
        try:
            validate_name(newName)
            validate_name(newResourceClass)
            validate_float(newHepSpecs)
            validate_float(newMemory)
            validate_float(newStorage)
            validate_float(newBandwidth)
            validate_comment(comment)
        except ValidationError as e:
            message ='Edit Resource Type Form  '+', '.join(e.messages)
            html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/resourcetype/list/\"></HEAD><body> %s.</body></html>" % message
            return HttpResponse(html)
        ## If resource parameters are changed, then validate them
        errorMsg = checkAttributeValues(newHepSpecs, newMemory, newStorage, newBandwidth)
        if (errorMsg != ''):
            return HttpResponseRedirect(redirectURL + errorMsg)
        ## convert empty values for the following fields into None i.e NULL and if not empty, then round off to 3 decimals 
        if (newHepSpecs == ''):
            newHepSpecs = None
        else:
            newHepSpecs = round((float(newHepSpecs)), 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 atleast one parameter is changed from its existing value
        if ( (currName == newName) and (currResourceClass == newResourceClass) and (currHepSpecs== newHepSpecs) and (currMemory == newMemory) and (currStorage == newStorage) and (currBandwidth == newBandwidth) ):
            message = 'No New Value provided for any field to perform Edit Operation. Hence Edit Resource Type ' + rtName + ' aborted'
            return HttpResponseRedirect(redirectURL + message)
        ## If name is changed, then validate it and if success, then assign the new name to the object
        if (currName != newName):
            if (newName == ''):
                errorMsg = 'Name field cannot be left blank. So Edit Resource Type operation stopped'
                return HttpResponseRedirect(redirectURL + errorMsg)
            nameExists = checkNameIgnoreCase(newName)
            if nameExists:
                msgAlreadyExists = 'Resource Type ' + newName + ' already exists. Hence Edit Resource Type Operation Stopped'
                return HttpResponseRedirect(redirectURL + msgAlreadyExists);
            resourceTypeObject.name = newName
        ## check the remaining parameters and if changed, then assign to the object
        if (currResourceClass != newResourceClass):
            resourceTypeObject.resource_class = newResourceClass
        if (currHepSpecs != newHepSpecs):
            resourceTypeObject.hepspecs = newHepSpecs
        if (currMemory != newMemory):
            resourceTypeObject.memory = newMemory
        if (currStorage != newStorage):
            resourceTypeObject.storage = newStorage
        if (currBandwidth != newBandwidth):
            resourceTypeObject.bandwidth = newBandwidth
        ## update the object and return the success message to the user
        resourceTypeObject.save()
        newRTInfo = getResourceTypeInfo(resourceTypeObject)
        objectId = resourceTypeObject.id 
        if addUpdateLog(request,newName,objectId,comment,oldRTInfo,newRTInfo,'resourcetype',True):
            message = 'Resource Type ' + rtName + ' Successfully Updated'
            transaction.commit()
        else:
            transaction.rollback()
            message = 'Error in Updating Resource Type ' + rtName
        html = "<html><HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"4; url=/cloudman/resourcetype/list/\"></HEAD><body> %s.</body></html>" % message
        return HttpResponse(html)
    ## if it is not form submission, then call the template to display the update form 
    else:
        form = ResourceTypeForm()
        #resourceTypeList=list(resourceTypeObject)
        #json_object=simplejson.dumps(resourceTypeObject)
        return render_to_response('resourcetype/update.html',locals(),context_instance=RequestContext(request))