Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanner does not work against emulator host #400

Open
snazy opened this issue Mar 27, 2023 · 1 comment
Open

Spanner does not work against emulator host #400

snazy opened this issue Mar 27, 2023 · 1 comment

Comments

@snazy
Copy link
Contributor

snazy commented Mar 27, 2023

The current state of the Google Cloud Spanner Quarkus extension does not work at all using the Spanner emulator. The reason is actually pretty simple: it requires valid credentials and there is no way around it, even the documented approach does not work.

A way to make it work properly with an emulator is pretty simple, and does not require the hack from the docs described in the link above.

The current code is pretty straight forward, but makes using an emulator practically impossible, because it has a hard dependency via @Inject GoogleCredentials googleCredentials;.

A way around that and the hack is to programmatically request the GoogleCredentialy and only bark, it the emulator host is not present, like in this piece of code:

@ApplicationScope
public class SpannerProducer {

    @Inject
    GcpConfigHolder gcpConfigHolder;

    @Inject
    SpannerConfiguration spannerConfiguration;

    @Produces
    @Singleton
    @Default
    public Spanner storage() throws IOException {
        GoogleCredentials googleCredentials = null;
        Exception googleCredentialsException = null;
        try {
            googleCredentials = Arc.container().instance(GoogleCredentials.class).get();
        } catch (Exception e) {
            googleCredentialsException = e;
        }
    
        GcpBootstrapConfiguration gcpConfiguration = gcpConfigHolder.getBootstrapConfig();
        SpannerOptions.Builder builder =
            SpannerOptions.newBuilder().setProjectId(gcpConfiguration.projectId.orElse(null));
        if (!spannerConfiguration.emulatorHost().isPresent()) {
            builder.setEmulatorHost(spannerConfiguration.emulatorHost().get());
        } else {
            if (googleCredentials == null) {
                if (googleCredentialsException != null) {
                    throw new RuntimeException("No GoogleCredentials available", googleCredentialsException);
                } else {
                    throw new RuntimeException("No GoogleCredentials available");
                }
            }
            builder.setCredentials(googleCredentials);
        }

        return builder.build().getService();
    }
@loicmathieu
Copy link
Collaborator

In fact it can be done more elegantly by injecting an Instance<GoogleCredentials> and use instance.isAvailable(). There is multiple places where this can be done as this issue exists in multiple extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants