VladaZakharova commented on code in PR #40008: URL: https://github.com/apache/airflow/pull/40008#discussion_r1639576552
########## tests/system/providers/google/cloud/cloud_run/example_cloud_run_service.py: ########## @@ -0,0 +1,93 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Example Airflow DAG that uses Google Cloud Run Service Operators. +""" + +from __future__ import annotations + +import os +from datetime import datetime + +from google.cloud.run_v2 import Service +from google.cloud.run_v2.types import k8s_min + +from airflow.models.dag import DAG +from airflow.providers.google.cloud.operators.cloud_run import ( + CloudRunCreateServiceOperator, +) + +PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT", "default") +DAG_ID = "example_cloud_run_serivce" + +region = "us-central1" +service_name_prefix = "cloudrun-system-test-service" Review Comment: Such names of objects should be provided with DAG_ID and ENV_ID (check other system tests for examples), to be sure that every name will be unique ########## airflow/providers/google/cloud/operators/cloud_run.py: ########## @@ -353,3 +353,57 @@ def _wait_for_operation(self, operation: operation.Operation): except Exception: error = operation.exception(timeout=self.timeout_seconds) raise AirflowException(error) + + +class CloudRunCreateServiceOperator(GoogleCloudBaseOperator): + """ + Creates a Service without executing it. Pushes the created service to xcom. + + :param project_id: Required. The ID of the Google Cloud project that the service belongs to. + :param region: Required. The ID of the Google Cloud region that the service belongs to. + :param service_name: Required. The name of the service to create. + :param service: Required. The service descriptor containing the configuration of the service to submit. + :param gcp_conn_id: The connection ID used to connect to Google Cloud. + :param impersonation_chain: Optional service account to impersonate using short-term + credentials, or chained list of accounts required to get the access_token + of the last account in the list, which will be impersonated in the request. + If set as a string, the account must grant the originating account + the Service Account Token Creator IAM role. + If set as a sequence, the identities from the list must grant + Service Account Token Creator IAM role to the directly preceding identity, with first + account from the list granting this role to the originating account (templated). + """ + + template_fields = ("project_id", "region", "gcp_conn_id", "impersonation_chain", "service_name") + + def __init__( + self, + project_id: str, + region: str, + service_name: str, + service: dict | Service, + gcp_conn_id: str = "google_cloud_default", + impersonation_chain: str | Sequence[str] | None = None, + **kwargs, + ): + super().__init__(**kwargs) + self.project_id = project_id + self.region = region + self.service_name = service_name + self.service = service + self.gcp_conn_id = gcp_conn_id + self.impersonation_chain = impersonation_chain + + def execute(self, context: Context): + hook: CloudRunServiceHook = CloudRunServiceHook( + gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain + ) + + service = hook.create_service( Review Comment: Usually in operators to create an object we are adding blocks to catch errors, like 409(Already exists), or "Required parameter was not provided" ########## tests/system/providers/google/cloud/cloud_run/example_cloud_run_service.py: ########## @@ -0,0 +1,93 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Example Airflow DAG that uses Google Cloud Run Service Operators. +""" + +from __future__ import annotations + +import os +from datetime import datetime + +from google.cloud.run_v2 import Service +from google.cloud.run_v2.types import k8s_min + +from airflow.models.dag import DAG +from airflow.providers.google.cloud.operators.cloud_run import ( + CloudRunCreateServiceOperator, +) + +PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT", "default") +DAG_ID = "example_cloud_run_serivce" + +region = "us-central1" +service_name_prefix = "cloudrun-system-test-service" +service1_name = f"{service_name_prefix}1" +service2_name = f"{service_name_prefix}2" +service3_name = f"{service_name_prefix}3" + +create1_task_name = "create-service1" + + +def _assert_created_services_xcom(ti): + service1_dicts = ti.xcom_pull(task_ids=[create1_task_name], key="return_value") + assert service1_name in service1_dicts[0]["name"] + + +# [START howto_create_service_instance] +def _create_service(): + service = Service() + container = k8s_min.Container() + container.image = "us-docker.pkg.dev/cloudrun/container/service:latest" + service.template.containers.append(container) + return service + + +# [END howto_create_service_instance] + + +with DAG( Review Comment: If you are adding new operator and test to create this service, there should be also an operator to be able to delete it, so that every test, if creates something, also deletes everything after the execution. I would suggest to add additional operator to delete service and add a specific task in the system test here -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org