Kanan Mahammadli
Kanan Mahammadli

Reputation: 147

Retrive endpoint url of deployed app from google cloud run with Python

I want to send requests to a deployed app on a cloud run with python, but inside the test file, I don't want to hardcode the endpoint; how can I get the URL of the deployed app with python script inside the test file so that I can send requests to that URL?

Upvotes: 1

Views: 1028

Answers (2)

DazWilkin
DazWilkin

Reputation: 40061

In a pure Python way, you can use Google's API Client Library for Run.

To my knowledge, there isn't a Cloud Client Library

The method is namespaces.services.get and it is documented by APIs Explorer namespaces.services.get.

One important fact with Cloud Run is that the API endpoint differs by Cloud Run region.

See service endpoint. You will need to override the client configuration (using ClientOptions) with the correct (region-specific) api_endpoint.

The following is from-memory! I've not run this code but it should be (nearly) correct:

import google.auth
import os

from googleapiclient import discovery
from google.api_core.client_options import ClientOptions

creds, project = google.auth.default()

REGION = os.getenv("REGION")
SERVICE = os.getenv("SERVICE")

# Must override the default run.googleapis.com endpoint
# with region-specific endpoint
api_endpoint = "https://{region}-run.googleapis.com".format(
    region=REGION
)

options = ClientOptions(
    api_endpoint=api_endpoint
)

service = discovery.build("run", "v1", 
    client_options=options,
    credentials=creds
)

name = "namespaces/{namespace}/services/{service}".format(
    namespace=project,
    service=SERVICE
)
rqst = service.namespaces().services().get(name=name)
resp = rqst.execute()

The resp will be Service and you can grab its ServiceStatus url.

Upvotes: 4

boredabdel
boredabdel

Reputation: 2120

You can use gcloud to fetch the url of the service like this

gcloud run services describe SERVICE_NAME
--format="value(status.url)"

Upvotes: 3

Related Questions