'How to obtain Google Cloud Run service URL with Python
I would like to obtain the URL of my Cloud Run service programmatically so I only need to hard code the name of the service and not its URL. This is so I can have the same code deployed in my test and production environments without having to access this value as a secret.
I'm currently trying the cloud run package as:
from google.cloud.run_v2.services.services.client import ServicesClient
def get_service_url(service):
project_number = '123456789'
location = 'europe-west2'
service_name = f'projects/{project_number}/locations/{location}/services/{service}'
ServicesClient.get_service(name=service_name)
get_service_url('my_service')
However this returns
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'name'
>>> run_client.ServicesClient.get_service(name=service_name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get_service() missing 1 required positional argument: 'self'
>>> run_client.ServicesClient().get_service(name=service_name
Traceback (most recent call last):
File "\lib\site-packages\google\api_core\grpc_helpers.py", line 67, in error_remapped_callable
return callable_(*args, **kwargs)
File "\lib\site-packages\grpc\_channel.py", line 946, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "\lib\site-packages\grpc\_channel.py", line 849, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNIMPLEMENTED
details = "Operation is not implemented, or supported, or enabled."
debug_error_string = "{"created":"@1649862914.759000000","description":"Error received from peer ipv4:142.250.184.234:443","file":"src/core/lib/surface/call.cc","file_line":904,"grpc_message":"Operation is not implemented, or supported, or enabled.","grpc_status":12}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "\lib\site-packages\google\cloud\run_v2\services\services\client.py", line 781, in get_service
response = rpc(
File "\lib\site-packages\google\api_core\gapic_v1\method.py", line 145, in __call__
return wrapped_func(*args, **kwargs)
File "\lib\site-packages\google\api_core\retry.py", line 286, in retry_wrapped_func
return retry_target(
File "\lib\site-packages\google\api_core\retry.py", line 189, in retry_target
return target()
File "\lib\site-packages\google\api_core\grpc_helpers.py", line 69, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.MethodNotImplemented: 501 Operation is not implemented, or supported, or enabled.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
