Example Code Implementation Guide
Install dependencies: Ensure you have installed the required Google Cloud libraries. In your terminal, run ’ pip install google-cloud-run’
Adapt to your programming language: If you are using another language (such as Node.js or Go) or directly using the REST API/cURL, the principle remains the same. Do not call the endpoint /v2/projects/…/operations/… Instead, simply check whether the initial response from the endpoint /v2/projects/…/services/…:patch is 200 or 400 more
ETag Handling: Since you mentioned using ETag in your question, ensure you also include the etag field (if using REST) or configure concurrency checks on the Service object if necessary to prevent race conditions.
Code Implementation (Python)
Here is an example implementation using the official Google Cloud Run client library in Python (google-cloud-run_v2).
from google.cloud import run_v2
from google.api_core.exceptions import InvalidArgument, GoogleAPICallError
def validate_cloud_run_service(project_id: str, location: str, service_id: str, new_template) → bool:
“”
Melakukan validasi perubahan konfigurasi layanan Cloud Run tanpa menerapkannya.
Parameter:
-
project_id (str): ID project Google Cloud your.
-
location (str): Region Cloud Run (contoh: ‘asia-northeast1’).
-
service_id (str): Nama layanan Cloud Run you.
-
new_template: Objek template konfigurasi you want
Return:
- bool: True jika validasi sukses, False jika gagal.
“”"
- Inisialisasi klien Cloud Run v2
client = run_v2. ServicesClient()
- Make path all services
service_path = client.service_path(project_id, location, service_id)
- request validate_only=True
request = run_v2. UpdateServiceRequest(
service=run_v2. Service(
name=service_path,
template=new_template
),
update_mask
update_mask={“paths”: [“template”]},
validate_only=True
)
print validasi (dry-run)…"
try:
- Lakukan panggilan PATCH
Proses validasi real-time. We are no chek operations.get()
response = client.update_service(request=request)
- Konfirmasion Sukses
If the code reaches this point (no Exception), then HTTP 200 is OK.
Successful validation and safe configuration for production.
print(“Validation Successful! Configuration is safe to implement.”)
return True
except InvalidArgument as e:
If the configuration is incorrect, the API will immediately reply with HTTP 400
print(f" Validation Failed (Invalid configuration): {e.message}")
return False
except GoogleAPICallError as e:
Catch network errors, IAM permissions, or ETag mismatches
print(f" API error: {e.message}" occurred)
return False
– How to Use the Above Code —
new_template_data = run_v2. RevisionTemplate(…) (Fill in with your template configuration)
is_valid = validate_cloud_run_service(“my-project”, “asia-northeast1”, “my-service”, new_template_data)
if is_valid:
Run the update logic to production (validate_only=False) here
To apply changes to production, we will combine the previous validation logic with the actual update command.
Here is the workflow:
Initial Validation: The code first calls validate_only=True to ensure there are no configuration errors.
Production Update: If validation succeeds (returns True), we create a new request with validate_only=False.
Wait for Completion: Unlike the instant validation process, a production update takes time (downloading container images, configuring network routes, etc.). Therefore, the Cloud Run API returns a valid Operation object. We call .result() on this object. This function makes our program wait patiently until the deployment process is fully complete before proceeding to the next line of code.
Pada Kam, 12 Nov 2026, 00.00, BITT Company <no-reply@discuss.google.dev> menulis: