org.springframework.cloud.gcp.core.GcpProjectIdProvider Java Examples

The following examples show how to use org.springframework.cloud.gcp.core.GcpProjectIdProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
public GcpPubSubAutoConfiguration(GcpPubSubProperties gcpPubSubProperties,
		GcpProjectIdProvider gcpProjectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this.gcpPubSubProperties = gcpPubSubProperties;
	this.finalProjectIdProvider = (gcpPubSubProperties.getProjectId() != null)
			? gcpPubSubProperties::getProjectId
			: gcpProjectIdProvider;

	if (gcpPubSubProperties.getEmulatorHost() == null
			|| "false".equals(gcpPubSubProperties.getEmulatorHost())) {
		this.finalCredentialsProvider = gcpPubSubProperties.getCredentials().hasKey()
				? new DefaultCredentialsProvider(gcpPubSubProperties)
				: credentialsProvider;
	}
	else {
		// Since we cannot create a general NoCredentialsProvider if the emulator host is enabled
		// (because it would also be used for the other components), we have to create one here
		// for this particular case.
		this.finalCredentialsProvider = NoCredentialsProvider.create();
	}
}
 
Example #2
Source File: GoogleConfigPropertySourceLocator.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
public GoogleConfigPropertySourceLocator(GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider,
		GcpConfigProperties gcpConfigProperties) throws IOException {
	Assert.notNull(gcpConfigProperties, "Google Config properties must not be null");

	if (gcpConfigProperties.isEnabled()) {
		Assert.notNull(credentialsProvider, "Credentials provider cannot be null");
		Assert.notNull(projectIdProvider, "Project ID provider cannot be null");
		this.credentials = gcpConfigProperties.getCredentials().hasKey()
				? new DefaultCredentialsProvider(gcpConfigProperties).getCredentials()
				: credentialsProvider.getCredentials();
		this.projectId = (gcpConfigProperties.getProjectId() != null)
				? gcpConfigProperties.getProjectId()
				: projectIdProvider.getProjectId();
		Assert.notNull(this.credentials, "Credentials must not be null");

		Assert.notNull(this.projectId, "Project ID must not be null");

		this.timeout = gcpConfigProperties.getTimeoutMillis();
		this.name = gcpConfigProperties.getName();
		this.profile = gcpConfigProperties.getProfile();
		this.enabled = gcpConfigProperties.isEnabled();
		Assert.notNull(this.name, "Config name must not be null");
		Assert.notNull(this.profile, "Config profile must not be null");
	}
}
 
Example #3
Source File: GcpSpannerAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
CoreSpannerAutoConfiguration(GcpSpannerProperties gcpSpannerProperties,
		GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this.credentials = (gcpSpannerProperties.getCredentials().hasKey()
			? new DefaultCredentialsProvider(gcpSpannerProperties)
			: credentialsProvider).getCredentials();
	this.projectId = (gcpSpannerProperties.getProjectId() != null)
			? gcpSpannerProperties.getProjectId()
			: projectIdProvider.getProjectId();
	this.instanceId = gcpSpannerProperties.getInstanceId();
	this.databaseName = gcpSpannerProperties.getDatabase();
	this.numRpcChannels = gcpSpannerProperties.getNumRpcChannels();
	this.prefetchChunks = gcpSpannerProperties.getPrefetchChunks();
	this.minSessions = gcpSpannerProperties.getMinSessions();
	this.maxSessions = gcpSpannerProperties.getMaxSessions();
	this.maxIdleSessions = gcpSpannerProperties.getMaxIdleSessions();
	this.writeSessionsFraction = gcpSpannerProperties.getWriteSessionsFraction();
	this.keepAliveIntervalMinutes = gcpSpannerProperties
			.getKeepAliveIntervalMinutes();
	this.createInterleavedTableDdlOnDeleteCascade = gcpSpannerProperties
			.isCreateInterleavedTableDdlOnDeleteCascade();
	this.failIfPoolExhausted = gcpSpannerProperties.isFailIfPoolExhausted();
}
 
Example #4
Source File: StackdriverTraceAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public StackdriverTraceAutoConfiguration(GcpProjectIdProvider gcpProjectIdProvider,
		CredentialsProvider credentialsProvider,
		GcpTraceProperties gcpTraceProperties) throws IOException {
	this.finalProjectIdProvider = (gcpTraceProperties.getProjectId() != null)
			? gcpTraceProperties::getProjectId
			: gcpProjectIdProvider;
	this.finalCredentialsProvider =
			gcpTraceProperties.getCredentials().hasKey()
					? new DefaultCredentialsProvider(gcpTraceProperties)
					: credentialsProvider;
}
 
Example #5
Source File: GoogleStorageIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
public static Storage storage(CredentialsProvider credentialsProvider,
		GcpProjectIdProvider projectIdProvider) throws IOException {
	return StorageOptions.newBuilder()
			.setCredentials(credentialsProvider.getCredentials())
			.setProjectId(projectIdProvider.getProjectId()).build().getService();
}
 
Example #6
Source File: GcpContextAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Get a GCP project ID provider.
 * @return a {@link GcpProjectIdProvider} that returns the project ID in the properties or, if
 * none, the project ID from the GOOGLE_CLOUD_PROJECT envvar and Metadata Server
 */
@Bean
@ConditionalOnMissingBean
public GcpProjectIdProvider gcpProjectIdProvider() {
	GcpProjectIdProvider projectIdProvider =
			(this.gcpProperties.getProjectId() != null)
					? () -> this.gcpProperties.getProjectId()
					: new DefaultGcpProjectIdProvider();

	if (LOGGER.isInfoEnabled()) {
		LOGGER.info("The default project ID is " + projectIdProvider.getProjectId());
	}

	return projectIdProvider;
}
 
Example #7
Source File: FirebaseAuthenticationAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "firebaseJwtDelegatingValidator")
public DelegatingOAuth2TokenValidator<Jwt> firebaseJwtDelegatingValidator(JwtIssuerValidator jwtIssuerValidator, GcpProjectIdProvider gcpProjectIdProvider) {
	List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
	validators.add(new JwtTimestampValidator());
	validators.add(jwtIssuerValidator);
	validators.add(new FirebaseTokenValidator(projectId));
	return new DelegatingOAuth2TokenValidator<>(validators);
}
 
Example #8
Source File: GcpStackdriverMetricsAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public GcpStackdriverMetricsAutoConfiguration(GcpMetricsProperties gcpMetricsProperties,
		StackdriverProperties stackdriverProperties, GcpProjectIdProvider gcpProjectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this.stackdriverProperties = stackdriverProperties;
	this.projectId = (gcpMetricsProperties.getProjectId() != null)
			? gcpMetricsProperties.getProjectId() : gcpProjectIdProvider.getProjectId();
	this.credentialsProvider = gcpMetricsProperties.getCredentials().hasKey()
			? new DefaultCredentialsProvider(gcpMetricsProperties) : credentialsProvider;
}
 
Example #9
Source File: GcpBigQueryAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
GcpBigQueryAutoConfiguration(
		GcpBigQueryProperties gcpBigQueryProperties,
		GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {

	this.projectId = (gcpBigQueryProperties.getProjectId() != null)
			? gcpBigQueryProperties.getProjectId()
			: projectIdProvider.getProjectId();

	this.credentialsProvider = (gcpBigQueryProperties.getCredentials().hasKey()
			? new DefaultCredentialsProvider(gcpBigQueryProperties)
			: credentialsProvider);

	this.datasetName = gcpBigQueryProperties.getDatasetName();
}
 
Example #10
Source File: GcpDatastoreAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
GcpDatastoreAutoConfiguration(GcpDatastoreProperties gcpDatastoreProperties,
		GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {

	this.projectId = (gcpDatastoreProperties.getProjectId() != null)
			? gcpDatastoreProperties.getProjectId()
			: projectIdProvider.getProjectId();
	this.namespace = gcpDatastoreProperties.getNamespace();

	String hostToConnect = gcpDatastoreProperties.getHost();
	if (gcpDatastoreProperties.getEmulator().isEnabled()) {
		hostToConnect = "localhost:" + gcpDatastoreProperties.getEmulator().getPort();
		LOGGER.info("Connecting to a local datastore emulator.");
	}

	if (hostToConnect == null) {
		this.credentials = (gcpDatastoreProperties.getCredentials().hasKey()
				? new DefaultCredentialsProvider(gcpDatastoreProperties)
				: credentialsProvider).getCredentials();
	}
	else {
		// Use empty credentials with Datastore Emulator.
		this.credentials = NoCredentials.getInstance();
	}

	this.host = hostToConnect;
}
 
Example #11
Source File: GcpFirestoreAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
GcpFirestoreAutoConfiguration(GcpFirestoreProperties gcpFirestoreProperties,
		GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {

	this.projectId = (gcpFirestoreProperties.getProjectId() != null)
			? gcpFirestoreProperties.getProjectId()
			: projectIdProvider.getProjectId();

	this.credentialsProvider = (gcpFirestoreProperties.getCredentials().hasKey()
			? new DefaultCredentialsProvider(gcpFirestoreProperties)
			: credentialsProvider);

	this.hostPort = gcpFirestoreProperties.getHostPort();
	this.firestoreRootPath = String.format(ROOT_PATH_FORMAT, this.projectId);
}
 
Example #12
Source File: SecretManagerPropertySource.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public SecretManagerPropertySource(
		String propertySourceName,
		SecretManagerTemplate secretManagerTemplate,
		GcpProjectIdProvider projectIdProvider) {
	super(propertySourceName, secretManagerTemplate);

	this.projectIdProvider = projectIdProvider;
}
 
Example #13
Source File: StackdriverJsonLayout.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
	super.start();

	// If no Project ID set, then attempt to resolve it with the default project ID provider
	if (StringUtils.isEmpty(this.projectId) || this.projectId.endsWith("_IS_UNDEFINED")) {
		GcpProjectIdProvider projectIdProvider = new DefaultGcpProjectIdProvider();
		this.projectId = projectIdProvider.getProjectId();
	}
}
 
Example #14
Source File: LoggingWebMvcConfigurer.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor that accepts an {@link TraceIdLoggingWebMvcInterceptor}. If the given
 * interceptor is null, then a default {@link XCloudTraceIdExtractor} is used.
 * @param interceptor the interceptor to use with this configurer. If not provided a
 * {@link TraceIdLoggingWebMvcInterceptor} is used with the trace ID extractor
 * described above.
 * @param projectIdProvider the project ID provider to use
 */
public LoggingWebMvcConfigurer(
		@Autowired(required = false) TraceIdLoggingWebMvcInterceptor interceptor,
		GcpProjectIdProvider projectIdProvider) {
	if (interceptor != null) {
		this.interceptor = interceptor;
	}
	else {
		this.interceptor = new TraceIdLoggingWebMvcInterceptor(
				new XCloudTraceIdExtractor());
	}
}
 
Example #15
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public PubSubAdmin(GcpProjectIdProvider projectIdProvider, TopicAdminClient topicAdminClient,
		SubscriptionAdminClient subscriptionAdminClient) {
	Assert.notNull(projectIdProvider, "The project ID provider can't be null.");
	Assert.notNull(topicAdminClient, "The topic administration client can't be null");
	Assert.notNull(subscriptionAdminClient,
			"The subscription administration client can't be null");

	this.projectId = projectIdProvider.getProjectId();
	Assert.hasText(this.projectId, "The project ID can't be null or empty.");
	this.topicAdminClient = topicAdminClient;
	this.subscriptionAdminClient = subscriptionAdminClient;
}
 
Example #16
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * This constructor instantiates TopicAdminClient and SubscriptionAdminClient with all their
 * defaults and the provided credentials provider.
 * @param projectIdProvider the project id provider to use
 * @param credentialsProvider the credentials provider to use
 * @throws IOException thrown when there are errors in contacting Google Cloud Pub/Sub
 */
public PubSubAdmin(GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this(projectIdProvider,
			TopicAdminClient.create(
					TopicAdminSettings.newBuilder()
							.setCredentialsProvider(credentialsProvider)
							.build()),
			SubscriptionAdminClient.create(
					SubscriptionAdminSettings.newBuilder()
					.setCredentialsProvider(credentialsProvider)
					.build()));
}
 
Example #17
Source File: DefaultSubscriberFactory.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Default {@link DefaultSubscriberFactory} constructor.
 * @param projectIdProvider provides the default GCP project ID for selecting the subscriptions
 */
public DefaultSubscriberFactory(GcpProjectIdProvider projectIdProvider) {
	Assert.notNull(projectIdProvider, "The project ID provider can't be null.");

	this.projectId = projectIdProvider.getProjectId();
	Assert.hasText(this.projectId, "The project ID can't be null or empty.");
}
 
Example #18
Source File: DefaultPublisherFactory.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Create {@link DefaultPublisherFactory} instance based on the provided {@link GcpProjectIdProvider}.
 * <p>The {@link GcpProjectIdProvider} must not be null, neither provide an empty {@code projectId}.
 * @param projectIdProvider provides the default GCP project ID for selecting the topic
 */
public DefaultPublisherFactory(GcpProjectIdProvider projectIdProvider) {
	Assert.notNull(projectIdProvider, "The project ID provider can't be null.");

	this.projectId = projectIdProvider.getProjectId();
	Assert.hasText(this.projectId, "The project ID can't be null or empty.");
}
 
Example #19
Source File: GcpContextAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProjectIdProvider_withGcpProperties() {
	this.contextRunner.withPropertyValues("spring.cloud.gcp.projectId=tonberry")
			.run((context) -> {
				GcpProjectIdProvider projectIdProvider =
						context.getBean(GcpProjectIdProvider.class);
				assertThat(projectIdProvider.getProjectId()).isEqualTo("tonberry");
			});
}
 
Example #20
Source File: GcpContextAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProjectIdProvider_withoutGcpProperties() {
	this.contextRunner.run((context) -> {
		GcpProjectIdProvider projectIdProvider =
				context.getBean(GcpProjectIdProvider.class);
		assertThat(projectIdProvider).isInstanceOf(DefaultGcpProjectIdProvider.class);
	});
}
 
Example #21
Source File: GcpStorageAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public GcpStorageAutoConfiguration(
		GcpProjectIdProvider coreProjectIdProvider,
		CredentialsProvider credentialsProvider,
		GcpStorageProperties gcpStorageProperties) throws IOException {

	this.gcpProjectIdProvider =
			gcpStorageProperties.getProjectId() != null
					? gcpStorageProperties::getProjectId
					: coreProjectIdProvider;

	this.credentialsProvider =
			gcpStorageProperties.getCredentials().hasKey()
					? new DefaultCredentialsProvider(gcpStorageProperties)
					: credentialsProvider;
}
 
Example #22
Source File: GcpPubSubReactiveAutoConfigurationTest.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public GcpProjectIdProvider projectIdProvider() {
	return () -> "fake project";
}
 
Example #23
Source File: IapAuthenticationAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
static GcpProjectIdProvider mockProjectIdProvider() {
	return mockProjectIdProvider;
}
 
Example #24
Source File: FirebaseAuthenticationAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public GcpProjectIdProvider projectIdProvider() {
	return () -> "spring-firebase-test-project";
}
 
Example #25
Source File: IapAuthenticationAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
@ConditionalOnGcpEnvironment({GcpEnvironment.APP_ENGINE_FLEXIBLE, GcpEnvironment.APP_ENGINE_STANDARD})
public AudienceProvider appEngineBasedAudienceProvider(GcpProjectIdProvider projectIdProvider) {
	return new AppEngineAudienceProvider(projectIdProvider);
}
 
Example #26
Source File: GcpPubSubAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public GcpProjectIdProvider projectIdProvider() {
	return () -> "fake project";
}
 
Example #27
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public GcpProjectIdProvider gcpProjectIdProvider() {
	return () -> "project123";
}
 
Example #28
Source File: GcpStorageAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public static GcpProjectIdProvider gcpProjectIdProvider() {
	return () -> "default-project";
}
 
Example #29
Source File: GcpContextAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private ContextConsumer<AssertableApplicationContext> checkNumberOfBeansOfTypeGcpProjectIdProvider(int count) {
	return context -> assertThat(context
			.getBeansOfType(GcpProjectIdProvider.class).size())
					.isEqualTo(count);
}
 
Example #30
Source File: LoggingWebMvcConfigurer.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public LoggingWebMvcConfigurer(TraceIdLoggingWebMvcInterceptor interceptor,
		GcpProjectIdProvider projectIdProvider) {
	super(interceptor, projectIdProvider);
}