googleapis / python-firestore

Apache License 2.0
219 stars 75 forks source link

FirestoreAdminClient with emulator #941

Closed pgridharan closed 4 days ago

pgridharan commented 4 months ago

Hi,

I'm not sure if this is a bug or not a feature, but using the FirestoreAdminClient with the emulator does not work 😅

Environment details

Steps to reproduce

  1. Set FIRESTORE_EMULATOR_HOST
  2. Use FirestoreAdminClient instance

Code example

FIRESTORE_EMULATOR_HOST=0.0.0.0:8080
load_dotenv()

print(os.getenv("FIRESTORE_EMULATOR_HOST"))                # Will print `0.0.0.0:8080`
admin_client = FirestoreAdminClient()
request = firestore_admin_v1.types.ListDatabasesRequest(
    parent=f"projects/test-project",
)
client.list_databases(request=request)                     # This should not matter since the emulator only supports one project AFAIK

Stack trace

google.api_core.exceptions.PermissionDenied: 403 Cloud Firestore API has not been used in project test-project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firestore.googleapis.com/overview?project=test-project then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
tom-andersen commented 4 months ago

Thank you for the question. I am going to find someone with more Python knowledge to answer.

pgridharan commented 3 months ago

Is there any update?

daniel-sanche commented 4 days ago

Unfortunately, the admin client is fully auto-generated, and does not support connecting to the emulator using just the FIRESTORE_EMULATOR_HOST env var.

You can still connect to the emulator, but you need to do a but more work to configure the grpc channel yourself. It should look something like this:

from google.auth.credentials import AnonymousCredentials
from google.cloud.firestore_admin_v1.services.firestore_admin.transports.grpc import FirestoreAdminGrpcTransport
from google.cloud.firestore_admin_v1.services.firestore_admin import FirestoreAdminClient
import grpc

FIRESTORE_EMULATOR_HOST="0.0.0.0:8080"

credentials = AnonymousCredentials()
options = [("Authorization", f"Bearer owner")]
channel = grpc.insecure_channel(FIRESTORE_EMULATOR_HOST, options=options)
transport = FirestoreAdminGrpcTransport(host=FIRESTORE_EMULATOR_HOST, channel=channel, credentials=credentials)

admin_client = FirestoreAdminClient(transport=transport)