com.google.cloud.NoCredentials Java Examples

The following examples show how to use com.google.cloud.NoCredentials. 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: EmulatedPubSubSinkTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = Exception.class)
public void testPubSubSinkThrowsExceptionOnFailure() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.enableCheckpointing(100);
	env.setParallelism(1);
	env.setRestartStrategy(RestartStrategies.noRestart());

	// Create test stream
	//use source function to prevent the job from shutting down before a checkpoint has been made
	env.addSource(new SingleInputSourceFunction())

		.map((MapFunction<String, String>) StringUtils::reverse)
		.addSink(PubSubSink.newBuilder()
							.withSerializationSchema(new SimpleStringSchema())
							.withProjectName(PROJECT_NAME)
							.withTopicName(TOPIC_NAME)
							// Specific for emulator
							.withHostAndPortForEmulator("unknown-host-to-force-sink-crash:1234")
							.withCredentials(NoCredentials.getInstance())
							.build()).name("PubSub sink");

	// Run
	env.execute();
}
 
Example #2
Source File: EmulatedPubSubSinkTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = Exception.class)
public void testPubSubSinkThrowsExceptionOnFailure() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.enableCheckpointing(100);
	env.setParallelism(1);
	env.setRestartStrategy(RestartStrategies.noRestart());

	// Create test stream
	//use source function to prevent the job from shutting down before a checkpoint has been made
	env.addSource(new SingleInputSourceFunction())

		.map((MapFunction<String, String>) StringUtils::reverse)
		.addSink(PubSubSink.newBuilder()
							.withSerializationSchema(new SimpleStringSchema())
							.withProjectName(PROJECT_NAME)
							.withTopicName(TOPIC_NAME)
							// Specific for emulator
							.withHostAndPortForEmulator("unknown-host-to-force-sink-crash:1234")
							.withCredentials(NoCredentials.getInstance())
							.build()).name("PubSub sink");

	// Run
	env.execute();
}
 
Example #3
Source File: EmulatedPubSubSinkTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlinkSink() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	List<String> input = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten");

	// Create test stream
	DataStream<String> theData = env
		.fromCollection(input)
		.name("Test input")
		.map((MapFunction<String, String>) StringUtils::reverse);

	// Sink into pubsub
	theData
		.addSink(PubSubSink.newBuilder()
							.withSerializationSchema(new SimpleStringSchema())
							.withProjectName(PROJECT_NAME)
							.withTopicName(TOPIC_NAME)
						   // Specific for emulator
						.withHostAndPortForEmulator(getPubSubHostPort())
						.withCredentials(NoCredentials.getInstance())
						.build())
		.name("PubSub sink");

	// Run
	env.execute();

	// Now get the result from PubSub and verify if everything is there
	List<ReceivedMessage> receivedMessages = pubsubHelper.pullMessages(PROJECT_NAME, SUBSCRIPTION_NAME, 100);

	assertEquals("Wrong number of elements", input.size(), receivedMessages.size());

	// Check output strings
	List<String> output = new ArrayList<>();
	receivedMessages.forEach(msg -> output.add(msg.getMessage().getData().toStringUtf8()));

	for (String test : input) {
		assertTrue("Missing " + test, output.contains(StringUtils.reverse(test)));
	}
}
 
Example #4
Source File: CloudSpannerDriver.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private Spanner createSpanner(SpannerKey key) {
  Builder builder = SpannerOptions.newBuilder();
  if (key.projectId != null)
    builder.setProjectId(key.projectId);
  if (key.credentials != null)
    builder.setCredentials(key.credentials);
  else if (!hasDefaultCredentials())
    builder.setCredentials(NoCredentials.getInstance());
  if (key.host != null)
    builder.setHost(key.host);
  SpannerOptions options = builder.build();
  return options.getService();
}
 
Example #5
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 #6
Source File: GcpSpannerEmulatorAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SpannerOptions spannerOptions() {
	Assert.notNull(this.properties.getEmulatorHost(), "`spring.cloud.gcp.spanner.emulator-host` must be set.");
	return SpannerOptions.newBuilder()
			.setProjectId(this.properties.getProjectId())
			.setCredentials(NoCredentials.getInstance())
			.setEmulatorHost(this.properties.getEmulatorHost()).build();
}
 
Example #7
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDatastoreEmulatorCredentialsConfig() {
	this.contextRunner.run((context) -> {
		CredentialsProvider defaultCredentialsProvider = context.getBean(CredentialsProvider.class);
		assertThat(defaultCredentialsProvider).isNotInstanceOf(NoCredentialsProvider.class);

		DatastoreOptions datastoreOptions = getDatastoreBean(context).getOptions();
		assertThat(datastoreOptions.getCredentials()).isInstanceOf(NoCredentials.class);
	});
}
 
Example #8
Source File: EntityManagerFactory.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns the credentials from the given connection parameters.
 * 
 * @param parameters
 *          the connection parameters
 * @return the credentials for authenticating with the Datastore service.
 * @throws IOException
 *           if any error occurs such as not able to read the credentials file.
 */
private static Credentials getCredentials(ConnectionParameters parameters) throws IOException {
  if (parameters.isEmulator()) {
    return NoCredentials.getInstance();
  }
  InputStream jsonCredentialsStream = parameters.getJsonCredentialsStream();
  if (jsonCredentialsStream != null) {
    return ServiceAccountCredentials.fromStream(jsonCredentialsStream);
  }
  File jsonCredentialsFile = parameters.getJsonCredentialsFile();
  if (jsonCredentialsFile != null) {
    return ServiceAccountCredentials.fromStream(new FileInputStream(jsonCredentialsFile));
  }
  return ServiceAccountCredentials.getApplicationDefault();
}
 
Example #9
Source File: EmulatedPubSubSinkTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlinkSink() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	List<String> input = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten");

	// Create test stream
	DataStream<String> theData = env
		.fromCollection(input)
		.name("Test input")
		.map((MapFunction<String, String>) StringUtils::reverse);

	// Sink into pubsub
	theData
		.addSink(PubSubSink.newBuilder()
							.withSerializationSchema(new SimpleStringSchema())
							.withProjectName(PROJECT_NAME)
							.withTopicName(TOPIC_NAME)
						   // Specific for emulator
						.withHostAndPortForEmulator(getPubSubHostPort())
						.withCredentials(NoCredentials.getInstance())
						.build())
		.name("PubSub sink");

	// Run
	env.execute();

	// Now get the result from PubSub and verify if everything is there
	List<ReceivedMessage> receivedMessages = pubsubHelper.pullMessages(PROJECT_NAME, SUBSCRIPTION_NAME, 100);

	assertEquals("Wrong number of elements", input.size(), receivedMessages.size());

	// Check output strings
	List<String> output = new ArrayList<>();
	receivedMessages.forEach(msg -> output.add(msg.getMessage().getData().toStringUtf8()));

	for (String test : input) {
		assertTrue("Missing " + test, output.contains(StringUtils.reverse(test)));
	}
}
 
Example #10
Source File: EmulatedPubSubSourceTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testFlinkSource() throws Exception {
	// Create some messages and put them into pubsub
	List<String> input = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");

	List<String> messagesToSend = new ArrayList<>(input);
	messagesToSend.add("End");

	// Publish the messages into PubSub
	Publisher publisher = pubsubHelper.createPublisher(PROJECT_NAME, TOPIC_NAME);
	messagesToSend.forEach(s -> {
		try {
			publisher
				.publish(PubsubMessage.newBuilder()
					.setData(ByteString.copyFromUtf8(s))
					.build())
				.get();
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	});

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.enableCheckpointing(1000);
	env.setParallelism(1);
	env.setRestartStrategy(RestartStrategies.noRestart());

	DataStream<String> fromPubSub = env
		.addSource(PubSubSource.newBuilder()
							.withDeserializationSchema(new BoundedStringDeserializer(10))
							.withProjectName(PROJECT_NAME)
							.withSubscriptionName(SUBSCRIPTION_NAME)
							.withCredentials(NoCredentials.getInstance())
							.withPubSubSubscriberFactory(new PubSubSubscriberFactoryForEmulator(getPubSubHostPort(), PROJECT_NAME, SUBSCRIPTION_NAME, 10, Duration.ofSeconds(15), 100))
							.build())
		.name("PubSub source");

	List<String> output = new ArrayList<>();
	fromPubSub.writeUsingOutputFormat(new LocalCollectionOutputFormat<>(output));

	env.execute();

	assertEquals("Wrong number of elements", input.size(), output.size());
	for (String test : input) {
		assertTrue("Missing " + test, output.contains(test));
	}
}
 
Example #11
Source File: CloudSpannerDriverTest.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Test
public void testCredentials() throws Exception {
  CloudSpannerDriver driver = CloudSpannerDriver.getDriver();
  assertNotNull(driver);

  // get connection without any credentials
  CloudSpannerConnection connection = (CloudSpannerConnection) DriverManager.getConnection(
      "jdbc:cloudspanner://localhost;Project=adroit-hall-123;Instance=test-instance;Database=testdb2");
  // allow ComputeEngineCredentials as this is the default when running on Travis
  GoogleCredentials def = null;
  try {
    def = GoogleCredentials.getApplicationDefault();
  } catch (IOException e) {
    // ignore
  }
  assertTrue(NoCredentials.getInstance()
      .equals(connection.getSpanner().getOptions().getCredentials())
      || connection.getSpanner().getOptions().getCredentials().getClass()
          .equals(ComputeEngineCredentials.class)
      || (def != null && connection.getSpanner().getOptions().getCredentials().equals(def)));
  EnvironmentVariablesUtil.clearCachedDefaultCredentials();

  // get connection with application default credentials
  env.set("GOOGLE_APPLICATION_CREDENTIALS", "cloudspanner-emulator-key.json");
  connection = (CloudSpannerConnection) DriverManager.getConnection(
      "jdbc:cloudspanner://localhost;Project=adroit-hall-123;Instance=test-instance;Database=testdb2");
  assertEquals(
      GoogleCredentials.fromStream(new FileInputStream("cloudspanner-emulator-key.json")),
      connection.getSpanner().getOptions().getCredentials());
  EnvironmentVariablesUtil.clearCachedDefaultCredentials();

  // get connection without any credentials again
  env.clear("GOOGLE_APPLICATION_CREDENTIALS");
  connection = (CloudSpannerConnection) DriverManager.getConnection(
      "jdbc:cloudspanner://localhost;Project=adroit-hall-123;Instance=test-instance;Database=testdb2");
  // allow ComputeEngineCredentials as this is the default when running on Travis
  assertTrue(
      NoCredentials.getInstance().equals(connection.getSpanner().getOptions().getCredentials())
          || connection.getSpanner().getOptions().getCredentials().getClass()
              .equals(ComputeEngineCredentials.class));
  EnvironmentVariablesUtil.clearCachedDefaultCredentials();
}
 
Example #12
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public CredentialsProvider credentialsProvider() {
	return () -> NoCredentials.getInstance();
}
 
Example #13
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public CredentialsProvider credentialsProvider() {
	return () -> NoCredentials.getInstance();
}
 
Example #14
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public CredentialsProvider credentialsProvider() {
	return () -> NoCredentials.getInstance();
}
 
Example #15
Source File: EmulatedPubSubSourceTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testFlinkSource() throws Exception {
	// Create some messages and put them into pubsub
	List<String> input = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");

	List<String> messagesToSend = new ArrayList<>(input);
	messagesToSend.add("End");

	// Publish the messages into PubSub
	Publisher publisher = pubsubHelper.createPublisher(PROJECT_NAME, TOPIC_NAME);
	messagesToSend.forEach(s -> {
		try {
			publisher
				.publish(PubsubMessage.newBuilder()
					.setData(ByteString.copyFromUtf8(s))
					.build())
				.get();
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	});

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.enableCheckpointing(1000);
	env.setParallelism(1);
	env.setRestartStrategy(RestartStrategies.noRestart());

	DataStream<String> fromPubSub = env
		.addSource(PubSubSource.newBuilder()
							.withDeserializationSchema(new BoundedStringDeserializer(10))
							.withProjectName(PROJECT_NAME)
							.withSubscriptionName(SUBSCRIPTION_NAME)
							.withCredentials(NoCredentials.getInstance())
							.withPubSubSubscriberFactory(new PubSubSubscriberFactoryForEmulator(getPubSubHostPort(), PROJECT_NAME, SUBSCRIPTION_NAME, 10, Duration.ofSeconds(15), 100))
							.build())
		.name("PubSub source");

	List<String> output = new ArrayList<>();
	fromPubSub.writeUsingOutputFormat(new LocalCollectionOutputFormat<>(output));

	env.execute();

	assertEquals("Wrong number of elements", input.size(), output.size());
	for (String test : input) {
		assertTrue("Missing " + test, output.contains(test));
	}
}