def _get_v35_ovdc_url_data(method: str, tokens: list): """Parse tokens from url and http method to get v35 ovdc specific data. Returns a dictionary with operation and url data. :param shared_constants.RequestMethod method: http verb :param str[] tokens: http url :rtype: dict """ num_tokens = len(tokens) if num_tokens == 5: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.V35_OVDC_LIST} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 6: if method == shared_constants.RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.V35_OVDC_UPDATE, shared_constants.RequestKey.OVDC_ID: tokens[5] } if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.V35_OVDC_INFO, shared_constants.RequestKey.OVDC_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError()
def _get_pks_url_data(method, url): """Parse url and http method to get desired PKS operation and url data. Returns a data dictionary with 'operation' key and also any relevant url data. :param RequestMethod method: :param str url: :rtype: dict """ tokens = url.split('/') num_tokens = len(tokens) operation_type = tokens[3].lower() if operation_type.endswith('s'): operation_type = operation_type[:-1] if operation_type == OperationType.CLUSTER: if num_tokens == 4: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.PKS_CLUSTER_LIST} if method == RequestMethod.POST: return {_OPERATION_KEY: CseOperation.PKS_CLUSTER_CREATE} raise e.MethodNotAllowedRequestError() if num_tokens == 5: if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.PKS_CLUSTER_INFO, RequestKey.CLUSTER_NAME: tokens[4] } if method == RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.PKS_CLUSTER_RESIZE, RequestKey.CLUSTER_NAME: tokens[4] } if method == RequestMethod.DELETE: return { _OPERATION_KEY: CseOperation.PKS_CLUSTER_DELETE, RequestKey.CLUSTER_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() if num_tokens == 6: if method == RequestMethod.GET: if tokens[5] == 'config': return { _OPERATION_KEY: CseOperation.PKS_CLUSTER_CONFIG, RequestKey.CLUSTER_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() raise e.MethodNotAllowedRequestError()
def _get_url_data(method, url): """Parse url and http method to get desired CSE operation and url data. Url is processed like a tree to find the desired operation as fast as possible. These explicit checks allow any invalid urls or http methods to fall through and trigger the appropriate exception. Returns a data dictionary with 'operation' key and also any relevant url data. :param RequestMethod method: :param str url: :rtype: dict """ tokens = url.split('/') num_tokens = len(tokens) if num_tokens < 4: raise e.NotFoundRequestError() if tokens[2] == PKS_SERVICE_NAME: return _get_pks_url_data(method, url) operation_type = tokens[3].lower() if operation_type.endswith('s'): operation_type = operation_type[:-1] if operation_type == OperationType.CLUSTER: if num_tokens == 4: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.CLUSTER_LIST} if method == RequestMethod.POST: return {_OPERATION_KEY: CseOperation.CLUSTER_CREATE} raise e.MethodNotAllowedRequestError() if num_tokens == 5: if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.CLUSTER_INFO, RequestKey.CLUSTER_NAME: tokens[4] } if method == RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.CLUSTER_RESIZE, RequestKey.CLUSTER_NAME: tokens[4] } if method == RequestMethod.DELETE: return { _OPERATION_KEY: CseOperation.CLUSTER_DELETE, RequestKey.CLUSTER_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() if num_tokens == 6: if method == RequestMethod.GET: if tokens[5] == 'config': return { _OPERATION_KEY: CseOperation.CLUSTER_CONFIG, RequestKey.CLUSTER_NAME: tokens[4] } if tokens[5] == 'upgrade-plan': return { _OPERATION_KEY: CseOperation.CLUSTER_UPGRADE_PLAN, RequestKey.CLUSTER_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() if num_tokens == 7: if method == RequestMethod.POST: if tokens[5] == 'action' and tokens[6] == 'upgrade': return { _OPERATION_KEY: CseOperation.CLUSTER_UPGRADE, RequestKey.CLUSTER_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() elif operation_type == OperationType.NODE: if num_tokens == 4: if method == RequestMethod.POST: return {_OPERATION_KEY: CseOperation.NODE_CREATE} if method == RequestMethod.DELETE: return {_OPERATION_KEY: CseOperation.NODE_DELETE} raise e.MethodNotAllowedRequestError() if num_tokens == 5: if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.NODE_INFO, RequestKey.NODE_NAME: tokens[4] } raise e.MethodNotAllowedRequestError() elif operation_type == OperationType.OVDC: if num_tokens == 4: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.OVDC_LIST} raise e.MethodNotAllowedRequestError() if num_tokens == 5: if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.OVDC_INFO, RequestKey.OVDC_ID: tokens[4] } if method == RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.OVDC_UPDATE, RequestKey.OVDC_ID: tokens[4] } raise e.MethodNotAllowedRequestError() if num_tokens == 6 and tokens[5] == 'compute-policies': if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.OVDC_COMPUTE_POLICY_LIST, RequestKey.OVDC_ID: tokens[4] } if method == RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.OVDC_COMPUTE_POLICY_UPDATE, RequestKey.OVDC_ID: tokens[4] } raise e.MethodNotAllowedRequestError() elif operation_type == OperationType.SYSTEM: if num_tokens == 4: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.SYSTEM_INFO} if method == RequestMethod.PUT: return {_OPERATION_KEY: CseOperation.SYSTEM_UPDATE} raise e.MethodNotAllowedRequestError() elif operation_type == OperationType.TEMPLATE: if num_tokens == 4: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.TEMPLATE_LIST} raise e.MethodNotAllowedRequestError() raise e.NotFoundRequestError()
def _get_legacy_url_data(method: str, url: str, api_version: str): tokens = url.split('/') num_tokens = len(tokens) operation_type = tokens[3].lower() if operation_type.endswith('s'): operation_type = operation_type[:-1] if operation_type == shared_constants.OperationType.CLUSTER: if api_version not in (VcdApiVersion.VERSION_33.value, VcdApiVersion.VERSION_34.value): raise cse_exception.NotFoundRequestError() if num_tokens == 4: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.CLUSTER_LIST} if method == shared_constants.RequestMethod.POST: return {_OPERATION_KEY: CseOperation.CLUSTER_CREATE} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 5: if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.CLUSTER_INFO, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } if method == shared_constants.RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.CLUSTER_RESIZE, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } if method == shared_constants.RequestMethod.DELETE: return { _OPERATION_KEY: CseOperation.CLUSTER_DELETE, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 6: if method == shared_constants.RequestMethod.GET: if tokens[5] == 'config': return { _OPERATION_KEY: CseOperation.CLUSTER_CONFIG, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } if tokens[5] == 'upgrade-plan': return { _OPERATION_KEY: CseOperation.CLUSTER_UPGRADE_PLAN, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 7: if method == shared_constants.RequestMethod.POST: if tokens[5] == 'action' and tokens[6] == 'upgrade': return { _OPERATION_KEY: CseOperation.CLUSTER_UPGRADE, shared_constants.RequestKey.CLUSTER_NAME: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() elif operation_type == shared_constants.OperationType.NODE: if api_version not in (VcdApiVersion.VERSION_33.value, VcdApiVersion.VERSION_34.value): raise cse_exception.NotFoundRequestError() if num_tokens == 4: if method == shared_constants.RequestMethod.POST: return {_OPERATION_KEY: CseOperation.NODE_CREATE} if method == shared_constants.RequestMethod.DELETE: return {_OPERATION_KEY: CseOperation.NODE_DELETE} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 5: if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.NODE_INFO, shared_constants.RequestKey.NODE_NAME: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() elif operation_type == shared_constants.OperationType.OVDC: if api_version not in (VcdApiVersion.VERSION_33.value, VcdApiVersion.VERSION_34.value): raise cse_exception.NotFoundRequestError() if num_tokens == 4: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.OVDC_LIST} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 5: if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.OVDC_INFO, shared_constants.RequestKey.OVDC_ID: tokens[4] } if method == shared_constants.RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.OVDC_UPDATE, shared_constants.RequestKey.OVDC_ID: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 6 and tokens[5] == 'compute-policies': if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.OVDC_COMPUTE_POLICY_LIST, shared_constants.RequestKey.OVDC_ID: tokens[4] } if method == shared_constants.RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.OVDC_COMPUTE_POLICY_UPDATE, shared_constants.RequestKey.OVDC_ID: tokens[4] } raise cse_exception.MethodNotAllowedRequestError() elif operation_type == shared_constants.OperationType.SYSTEM: if num_tokens == 4: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.SYSTEM_INFO} if method == shared_constants.RequestMethod.PUT: return {_OPERATION_KEY: CseOperation.SYSTEM_UPDATE} raise cse_exception.MethodNotAllowedRequestError() elif operation_type == shared_constants.OperationType.TEMPLATE: if num_tokens == 4: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.TEMPLATE_LIST} raise cse_exception.MethodNotAllowedRequestError() raise cse_exception.NotFoundRequestError()
def _get_v35_cluster_url_data(method: str, tokens: list): """Parse tokens from url and http method to get v35 cluster specific data. Returns a dictionary with operation and url data. :param RequestMethod method: http verb :param str[] tokens: http url :rtype: dict """ num_tokens = len(tokens) if num_tokens == 5: if method == shared_constants.RequestMethod.GET: return {_OPERATION_KEY: CseOperation.V35_CLUSTER_LIST} if method == shared_constants.RequestMethod.POST: return {_OPERATION_KEY: CseOperation.V35_CLUSTER_CREATE} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 6: if method == shared_constants.RequestMethod.GET: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_INFO, shared_constants.RequestKey.CLUSTER_ID: tokens[5] } if method == shared_constants.RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_RESIZE, shared_constants.RequestKey.CLUSTER_ID: tokens[5] } if method == shared_constants.RequestMethod.DELETE: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_DELETE, shared_constants.RequestKey.CLUSTER_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 7: if method == shared_constants.RequestMethod.GET: if tokens[6] == 'config': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_CONFIG, shared_constants.RequestKey.CLUSTER_ID: tokens[5] } if tokens[6] == 'upgrade-plan': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_UPGRADE_PLAN, # noqa: E501 shared_constants.RequestKey.CLUSTER_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 8: if method == shared_constants.RequestMethod.POST: if tokens[6] == 'action' and tokens[7] == 'upgrade': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_UPGRADE, shared_constants.RequestKey.CLUSTER_ID: tokens[5] } if method == shared_constants.RequestMethod.DELETE: if tokens[6] == 'nfs': return { _OPERATION_KEY: CseOperation.V35_NODE_DELETE, shared_constants.RequestKey.CLUSTER_ID: tokens[5], shared_constants.RequestKey.NODE_NAME: tokens[7] } raise cse_exception.MethodNotAllowedRequestError()
def _get_v35_cluster_url_data(method: str, url: str): """Parse url and http method to get v35 cluster specific data. Returns a dictionary with operation and url data. :param RequestMethod method: http verb :param str url: http url :rtype: dict """ tokens = url.split('/') num_tokens = len(tokens) if num_tokens < 5: raise cse_exception.NotFoundRequestError() operation_type = tokens[4].lower() if operation_type.endswith('s'): operation_type = operation_type[:-1] if operation_type == OperationType.CLUSTER: if num_tokens == 5: if method == RequestMethod.GET: return {_OPERATION_KEY: CseOperation.V35_CLUSTER_LIST} if method == RequestMethod.POST: return {_OPERATION_KEY: CseOperation.V35_CLUSTER_CREATE} raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 6: if method == RequestMethod.GET: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_INFO, RequestKey.CLUSTER_ID: tokens[5] } if method == RequestMethod.PUT: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_RESIZE, RequestKey.CLUSTER_ID: tokens[5] } if method == RequestMethod.DELETE: return { _OPERATION_KEY: CseOperation.V35_CLUSTER_DELETE, RequestKey.CLUSTER_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 7: if method == RequestMethod.GET: if tokens[6] == 'config': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_CONFIG, RequestKey.CLUSTER_ID: tokens[5] } if tokens[6] == 'upgrade-plan': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_UPGRADE_PLAN, # noqa: E501 RequestKey.CLUSTER_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError() if num_tokens == 8: if method == RequestMethod.POST: if tokens[6] == 'action' and tokens[7] == 'upgrade': return { _OPERATION_KEY: CseOperation.V35_CLUSTER_UPGRADE, RequestKey.CLUSTER_ID: tokens[5] } raise cse_exception.MethodNotAllowedRequestError()