def test_validate_project_json_missing_composer_environment(): json = { 'project_id': 'mock_project_id', 'location': 'mock_gcp_location', } with pytest.raises(ValueError): api_validator.validate_project_json(json)
def get_composer_experimental_apì(): """Gets the configuration information of the Cloud Composer experimental API""" logger.log( logging.INFO, f"Entered get_composer_experimental_apì -- {API_BASE_PATH_V1}/composer/api api GET method" ) req_data = request.get_json() if req_data: logger.log(logging.DEBUG, f"Request contains a json payload, validating: {req_data}") api_validator.validate_project_json(req_data) project_id, location, composer_environment = api_service.get_gcp_composer_details( req_data) else: logger.log( logging.DEBUG, f"Request does not contain a json payload, validating implicitly") project_id, location, composer_environment = api_service.get_gcp_composer_details( None) authenticated_session = auth_service.get_authenticated_session() airflow_uri, client_id = airflow_service.AirflowService( authenticated_session, project_id, location, composer_environment).get_airflow_experimental_api() return jsonify(airflow_uri=airflow_uri, client_id=client_id, next_actions=api_service.get_next_actions_experimental_api( airflow_uri, client_id))
def get_composer_config(): """Gets the configuration information of the Cloud Composer environment""" logger.log( logging.INFO, f"Entered get_composer_config -- {API_BASE_PATH_V1}/composer/config api GET method" ) req_data = request.get_json() if req_data: logger.log(logging.DEBUG, f"Request contains a json payload, validating: {req_data}") api_validator.validate_project_json(req_data) project_id, location, composer_environment = api_service.get_gcp_composer_details( req_data) else: logger.log( logging.DEBUG, f"Request does not contain a json payload, validating implicitly") project_id, location, composer_environment = api_service.get_gcp_composer_details( None) authenticated_session = auth_service.get_authenticated_session() airflow_config = airflow_service.AirflowService( authenticated_session, project_id, location, composer_environment).get_airflow_config() next_actions = { 'list': f'{API_BASE_PATH_V1}/dag/list', 'validate': f'{API_BASE_PATH_V1}/dag/validate', 'deploy': f'{API_BASE_PATH_V1}/dag/deploy' } return jsonify(airflow_config=airflow_config, next_actions=next_actions)
def trigger_dag(dag_name): """Triggers a specific, existing dag within a Cloud Composer environment""" logger.log( logging.INFO, f"Entered trigger_dag -- {API_BASE_PATH_V1}/dag/trigger/{dag_name} api PUT method" ) req_data = request.get_json() if req_data: if 'conf' not in req_data or len(req_data['conf']) == 0: return { 'error': "JSON payload provided but conf element is missing or empty" }, 500 if 'project_id' in req_data: api_validator.validate_project_json(req_data) project_id, location, composer_environment = api_service.get_gcp_composer_details( req_data) else: project_id, location, composer_environment = api_service.get_gcp_composer_details( None) try: res = api_service.trigger_dag(project_id, location, composer_environment, dag_name) except: return {'error': traceback.print_exc()}, 500 authenticated_session = auth_service.get_authenticated_session() airflow_uri, client_id = airflow_service.AirflowService( authenticated_session, project_id, location, composer_environment).get_airflow_experimental_api() return jsonify(api_response=res, next_actions=api_service.get_next_actions_experimental_api( airflow_uri, client_id))
def list_dags(): """Lists the dags stored in the Cloud Composer GCS bucket""" logger.log( logging.INFO, f"Entered list_dags -- {API_BASE_PATH_V1}/dag/list api GET method") req_data = request.get_json() if req_data: logger.log(logging.DEBUG, f"Request contains a json payload, validating: {req_data}") api_validator.validate_project_json(req_data) project_id, location, composer_environment = api_service.get_gcp_composer_details( req_data) bucket_name = api_service.get_dag_bucket(project_id, location, composer_environment) else: logger.log( logging.DEBUG, f"Request does not contain a json payload, validating implicitly") project_id, location, composer_environment = api_service.get_gcp_composer_details( None) bucket_name = api_service.get_dag_bucket(project_id, location, composer_environment) dag_list = api_service.list_dags(project_id, bucket_name) next_actions = { 'validate': f'{API_BASE_PATH_V1}/dag/validate', 'deploy': f'{API_BASE_PATH_V1}/dag/deploy' } return jsonify(dag_list=dag_list, next_actions=next_actions)
def test_validate_project_json_valid(): json = { 'project_id': 'mock_project_id', 'location': 'mock_gcp_location', 'composer_environment': 'mock_composer_environment' } is_valid = api_validator.validate_project_json(json) assert is_valid