Example #1
0
def validate_request_create_args(request_args, config, *args, **kwargs):
    """
    *arg and **kwargs are only for the interface
    validate post request
    1. read data from body
    2. validate using spec validation
    3. convert data from body to arguments (spec instance, argument with default setting) 
    TODO: rasie right kind of error with clear message 
    """
    
    initialize_request_args(request_args, config)
    #check the permission for creating the request
    permission = getWritePermission(request_args)
    authz_match(permission['role'], permission['group'])
    
    # set default values for teh request_args
    specClass = loadSpecClassByType(request_args["RequestType"])
    setArgumentsWithDefault(request_args, specClass.getWorkloadArguments())
    
    # get the spec type and validate arguments
    spec = loadSpecByType(request_args["RequestType"])
    if request_args["RequestType"] == "Resubmission":
        request_args["OriginalRequestCouchURL"] = '%s/%s' % (config.couch_host, 
                                                             config.couch_reqmgr_db)
    workload = spec.factoryWorkloadConstruction(request_args["RequestName"], 
                                                request_args)
    return workload, request_args
Example #2
0
def validate_clone_create_args(request_args, config, reqmgr_db_service, *args, **kwargs):
    """
    Handle clone workflows through the clone API, by loading the spec arguments
    definition (and chain definition, if needed) and inheriting all arguments defined
    in the spec.
    *arg and **kwargs are only for the interface
    """
    response = reqmgr_db_service.getRequestByNames(request_args.pop("OriginalRequestName"))
    originalArgs = response.values()[0]

    chainArgs = None
    specClass = loadSpecClassByType(originalArgs["RequestType"])
    if originalArgs["RequestType"] == 'Resubmission':
        # cloning an ACDC, nothing that we can validate
        # simply copy the whole original dictionary over and accept all args
        createArgs = originalArgs
    else:
        # load arguments definition from the proper/original spec factory
        createArgs = specClass.getWorkloadCreateArgs()
        if originalArgs["RequestType"] in ('StepChain', 'TaskChain'):
            chainArgs = specClass.getChainCreateArgs()

    cloned_args = initialize_clone(request_args, originalArgs, createArgs, chainArgs)
    initialize_request_args(cloned_args, config)

    permission = getWritePermission(cloned_args)
    authz_match(permission['role'], permission['group'])

    spec = specClass()
    workload = spec.factoryWorkloadConstruction(cloned_args["RequestName"], cloned_args)

    return workload, cloned_args
Example #3
0
def validate_request_create_args(request_args, config, reqmgr_db_service, *args, **kwargs):
    """
    *arg and **kwargs are only for the interface
    validate post request
    1. read data from body
    2. validate using spec validation
    3. convert data from body to arguments (spec instance, argument with default setting)
    TODO: raise right kind of error with clear message
    """
    if request_args["RequestType"] == "Resubmission":
        # do not set default values for Resubmission since it will be inherited from parent
        # both create & assign args are accepted for Resubmission creation
        workload, request_args = validate_resubmission_create_args(request_args, config, reqmgr_db_service)
    else:
        initialize_request_args(request_args, config)
        # check the permission for creating the request
        permission = getWritePermission(request_args)
        authz_match(permission['role'], permission['group'])

        # load the correct class in order to validate the arguments
        specClass = loadSpecClassByType(request_args["RequestType"])
        # set default values for the request_args
        setArgumentsWithDefault(request_args, specClass.getWorkloadCreateArgs())
        spec = specClass()
        workload = spec.factoryWorkloadConstruction(request_args["RequestName"],
                                                    request_args)

    return workload, request_args
Example #4
0
def validate_request_update_args(request_args, config, reqmgr_db_service, param):
    """
    param and safe structure is RESTArgs structure: named tuple
    RESTArgs(args=[], kwargs={})
    
    validate post/put request
    1. read data from body
    2. validate the permission (authentication)
    3. validate state transition (against previous state from couchdb)
    2. validate using workload validation
    3. convert data from body to arguments (spec instance, argument with default setting)
    
    TODO: rasie right kind of error with clear message 
    """
    # this needs to be deleted for validation
    request_name = request_args.pop("RequestName")
    couchurl =  '%s/%s' % (config.couch_host, config.couch_reqmgr_db)
    workload = WMWorkloadHelper()
    workload.loadSpecFromCouch(couchurl, request_name)
    
    # first validate the permission by status and request type.
    # if the status is not set only ReqMgr Admin can change the values
    # TODO for each step, assigned, approved, announce find out what other values
    # can be set
    request_args["RequestType"] = workload.requestType()
    permission = getWritePermission(request_args)
    authz_match(permission['role'], permission['group'])
    del request_args["RequestType"]

    #validate the status
    if "RequestStatus" in request_args:
        validate_state_transition(reqmgr_db_service, request_name, request_args["RequestStatus"])
        # delete request_args since it is not part of spec argument and validation
        args_without_status = {}
        args_without_status.update(request_args)
        del args_without_status["RequestStatus"]
    else:
        args_without_status = request_args

    if len(args_without_status) == 1:
        if 'RequestPriority' in args_without_status:
            args_without_status['RequestPriority'] = int(args_without_status['RequestPriority'])
            if (lambda x: (x >= 0 and x < 1e6))(args_without_status['RequestPriority']) is False:
                raise InvalidSpecParameterValue("RequestPriority must be an integer between 0 and 1e6")
            return workload, args_without_status
        elif 'cascade' in args_without_status:
            # status was already validated
            return workload, request_args
    elif len(args_without_status) > 0 and not workqueue_stat_validation(args_without_status):
        # validate the arguments against the spec argumentSpecdefinition
        #TODO: currently only assigned status allows any update other then Status update
        workload.validateArgumentForAssignment(args_without_status)

    # to update request_args with type conversion
    request_args.update(args_without_status)
    return workload, request_args
Example #5
0
def validate_request_update_args(request_args, config, reqmgr_db_service, param):
    """
    param and safe structure is RESTArgs structure: named tuple
    RESTArgs(args=[], kwargs={})

    validate post/put request
    1. read data from body
    2. validate the permission (authentication)
    3. validate state transition (against previous state from couchdb)
    2. validate using workload validation
    3. convert data from body to arguments (spec instance, argument with default setting)

    TODO: raise right kind of error with clear message
    """
    # this needs to be deleted for validation
    request_name = request_args.pop("RequestName")
    couchurl = '%s/%s' % (config.couch_host, config.couch_reqmgr_db)
    workload = WMWorkloadHelper()
    workload.loadSpecFromCouch(couchurl, request_name)

    # first validate the permission by status and request type.
    # if the status is not set only ReqMgr Admin can change the values
    # TODO for each step, assigned, approved, announce find out what other values
    # can be set
    request_args["RequestType"] = workload.requestType()
    permission = getWritePermission(request_args)
    authz_match(permission['role'], permission['group'])
    del request_args["RequestType"]

    # validate the status
    if "RequestStatus" in request_args:
        validate_state_transition(reqmgr_db_service, request_name, request_args["RequestStatus"])
        if request_args["RequestStatus"] in STATES_ALLOW_ONLY_STATE_TRANSITION:
            # if state change doesn't allow other transition nothing else to validate
            args_only_status = {}
            args_only_status["RequestStatus"] = request_args["RequestStatus"]
            args_only_status["cascade"] = request_args.get("cascade", False)
            return workload, args_only_status
        elif request_args["RequestStatus"] == 'assigned':
            workload.validateArgumentForAssignment(request_args)

    # TODO: fetch it from the assignment arg definition
    if 'RequestPriority' in request_args:
        request_args['RequestPriority'] = int(request_args['RequestPriority'])
        if (lambda x: (x >= 0 and x < 1e6))(request_args['RequestPriority']) is False:
            raise InvalidSpecParameterValue("RequestPriority must be an integer between 0 and 1e6")

    return workload, request_args
Example #6
0
def validate_resubmission_create_args(request_args, config, reqmgr_db_service, *args, **kwargs):
    """
    Handle resubmission workflows, loading the spec arguments definition (and chain
    definition, if needed) and inheriting all arguments defined in the spec.
    User can also override arguments, since it uses the same mechanism as a clone.
    *arg and **kwargs are only for the interface
    """
    response = reqmgr_db_service.getRequestByNames(request_args["OriginalRequestName"])
    originalArgs = response.values()[0]

    ### not a nice fix for #8245, but we cannot inherit the CollectionName attr
    originalArgs.pop("CollectionName", None)

    chainArgs = None
    if originalArgs["RequestType"] == 'Resubmission':
        # ACDC of ACDC, we can't validate this case
        # simply copy the whole original dictionary over and accept all args
        createArgs = originalArgs
        request_args["OriginalRequestType"] = originalArgs["OriginalRequestType"]
        request_args["ResubmissionCount"] = originalArgs.get("ResubmissionCount", 1) + 1
    else:
        # load arguments definition from the proper/original spec factory
        parentClass = loadSpecClassByType(originalArgs["RequestType"])
        createArgs = parentClass.getWorkloadAssignArgs()
        if originalArgs["RequestType"] in ('StepChain', 'TaskChain'):
            chainArgs = parentClass.getChainCreateArgs()
        createArgs.update(parentClass.getWorkloadCreateArgs())
        request_args["OriginalRequestType"] = originalArgs["RequestType"]

    cloned_args = initialize_clone(request_args, originalArgs, createArgs, chainArgs)
    initialize_request_args(cloned_args, config)

    permission = getWritePermission(cloned_args)
    authz_match(permission['role'], permission['group'])

    specClass = loadSpecClassByType(request_args["RequestType"])
    spec = specClass()
    workload = spec.factoryWorkloadConstruction(cloned_args["RequestName"], cloned_args)

    return workload, cloned_args
Example #7
0
from __future__ import print_function
import os
import unittest

#from WMCore_t.ReqMgr_t.Config import Config
from WMCore_t.ReqMgr_t.TestConfig import config
from WMCore.Wrappers import JsonWrapper
from WMCore.Services.ReqMgr.ReqMgr import ReqMgr
from WMCore.WMBase import getWMBASE
from WMQuality.REST.RESTBaseUnitTestWithDBBackend import RESTBaseUnitTestWithDBBackend
from WMCore.ReqMgr.Auth import getWritePermission
from WMCore.REST.Test import fake_authz_headers

req_args = {"RequestType": "ReReco", "RequestStatus": None}
ADMIN_PERMISSION = getWritePermission(req_args) 

req_args = {"RequestType": "ReReco", "RequestStatus": "completed"}
DEFAULT_STATUS_PERMISSION = getWritePermission(req_args)

req_args = {"RequestType": "ReReco", "RequestStatus": "new"}
CREATE_PERMISSION = getWritePermission(req_args)

DEFAULT_PERMISSION = DEFAULT_STATUS_PERMISSION

req_args = {"RequestType": "ReReco", "RequestStatus": "assinged"}
ASSIGN_PERMISSION = getWritePermission(req_args)


# this needs to move in better location
def insertDataToCouch(couchUrl, couchDBName, data):
Example #8
0
from __future__ import print_function

import json
import os
import unittest

from WMCore_t.ReqMgr_t.TestConfig import config

from WMCore.REST.Test import fake_authz_headers
from WMCore.ReqMgr.Auth import getWritePermission
from WMCore.Services.ReqMgr.ReqMgr import ReqMgr
from WMQuality.REST.RESTBaseUnitTestWithDBBackend import RESTBaseUnitTestWithDBBackend

req_args = {"RequestType": "ReReco", "RequestStatus": None}
ADMIN_PERMISSION = getWritePermission(req_args)

req_args = {"RequestType": "ReReco", "RequestStatus": "completed"}
DEFAULT_STATUS_PERMISSION = getWritePermission(req_args)

req_args = {"RequestType": "ReReco", "RequestStatus": "new"}
CREATE_PERMISSION = getWritePermission(req_args)

DEFAULT_PERMISSION = DEFAULT_STATUS_PERMISSION

req_args = {"RequestType": "ReReco", "RequestStatus": "assinged"}
ASSIGN_PERMISSION = getWritePermission(req_args)


# this needs to move in better location
def insertDataToCouch(couchUrl, couchDBName, data):