com.google.cloud.pubsub.v1.TopicAdminClient Java Examples

The following examples show how to use com.google.cloud.pubsub.v1.TopicAdminClient. 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: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatchEs() throws Exception {
  final String deviceName = "patchme-device-es";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  try {
    DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
    DeviceRegistryExample.createDeviceWithNoAuth(
        deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.patchEs256ForAuth(
        deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

    String got = bout.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(got.contains("Created device: {"));
  } finally {
    DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  }

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #2
Source File: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void deleteTopic(ProjectTopicName topicName) throws IOException {
	TopicAdminClient adminClient = getTopicAdminClient();
	try {
		adminClient.getTopic(topicName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
		return;
	}

	// If it exists we delete all subscriptions and the topic itself.
	LOG.info("DeleteTopic {} first delete old subscriptions.", topicName);
	adminClient
		.listTopicSubscriptions(topicName)
		.iterateAllAsProjectSubscriptionName()
		.forEach(subscriptionAdminClient::deleteSubscription);
	LOG.info("DeleteTopic {}", topicName);
	adminClient
		.deleteTopic(topicName);
}
 
Example #3
Source File: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void deleteTopic(ProjectTopicName topicName) throws IOException {
	TopicAdminClient adminClient = getTopicAdminClient();
	try {
		adminClient.getTopic(topicName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
		return;
	}

	// If it exists we delete all subscriptions and the topic itself.
	LOG.info("DeleteTopic {} first delete old subscriptions.", topicName);
	adminClient
		.listTopicSubscriptions(topicName)
		.iterateAllAsProjectSubscriptionName()
		.forEach(subscriptionAdminClient::deleteSubscription);
	LOG.info("DeleteTopic {}", topicName);
	adminClient
		.deleteTopic(topicName);
}
 
Example #4
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatchRsa() throws Exception {
  final String deviceName = "patchme-device-rsa";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  try {
    DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
    DeviceRegistryExample.createDeviceWithNoAuth(
        deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.patchRsa256ForAuth(
        deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

    String got = bout.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(got.contains("Created device: {"));
  } finally {
    DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  }

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #5
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Creates a topic and grants the IoT service account access. */
protected static Topic createIotTopic(String projectId, String topicId) throws Exception {
  // Create a new topic
  final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    final Topic topic = topicAdminClient.createTopic(topicName);
    final String topicString = topicName.toString();
    // add role -> members binding
    // create updated policy
    topicAdminClient.setIamPolicy(
        topicString,
        com.google.iam.v1.Policy.newBuilder(topicAdminClient.getIamPolicy(topicString))
            .addBindings(
                Binding.newBuilder()
                    .addMembers("serviceAccount:[email protected]")
                    .setRole(Role.owner().toString())
                    .build())
            .build());

    System.out.println("Setup topic / policy for: " + topic.getName());
    return topic;
  }
}
 
Example #6
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteUnauthDevice() throws Exception {
  final String deviceName = "noauth-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithNoAuth(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #7
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteEsDevice() throws Exception {
  final String deviceName = "es-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithEs256(
      deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #8
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteRsaDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #9
Source File: TopicAdmin.java    From ffwd with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
public TopicAdminClient getClient() throws IOException {
  final TopicAdminSettings.Builder adminSetting = TopicAdminSettings.newBuilder();

  final String emulatorHost = System.getenv("PUBSUB_EMULATOR_HOST");
  if (emulatorHost != null) {
    ManagedChannel channel =
      ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build();
    TransportChannelProvider channelProvider =
      FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));

    adminSetting.setTransportChannelProvider(channelProvider);
    adminSetting.setCredentialsProvider(NoCredentialsProvider.create());
  }

  return TopicAdminClient.create(adminSetting.build());
}
 
Example #10
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateGetDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Retrieving device"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #11
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateConfigureDevice() throws Exception {
  final String deviceName = "rsa-device-config";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.setDeviceConfiguration(
      deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID, "some-test-data", 0L);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Updated: 2"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #12
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateListDevices() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Found"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #13
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetIam() throws Exception {
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.setIamPermissions(PROJECT_ID, CLOUD_REGION, REGISTRY_ID, MEMBER, ROLE);
  DeviceRegistryExample.getIamPermissions(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("ETAG"));

  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #14
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateGetRegistry() throws Exception {
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.getRegistry(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertFalse(got.contains("eventNotificationConfigs"));

  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #15
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetIam() throws Exception {
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.getIamPermissions(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("ETAG"));

  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #16
Source File: UsePubSubEmulatorSnippet.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
  // [START pubsub_use_emulator]
  String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
  ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
  try {
    TransportChannelProvider channelProvider =
        FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
    CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

    // Set the channel and credentials provider when creating a `TopicAdminClient`.
    // Similarly for SubscriptionAdminClient
    TopicAdminClient topicClient =
        TopicAdminClient.create(
            TopicAdminSettings.newBuilder()
                .setTransportChannelProvider(channelProvider)
                .setCredentialsProvider(credentialsProvider)
                .build());

    TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
    // Set the channel and credentials provider when creating a `Publisher`.
    // Similarly for Subscriber
    Publisher publisher =
        Publisher.newBuilder(topicName)
            .setChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build();
  } finally {
    channel.shutdown();
  }
  // [END pubsub_use_emulator]
}
 
Example #17
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpDeviceEvent() throws Exception {
  final String deviceName = "rsa-device-http-event";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-num_messages=1",
    "-message_type=event",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.HttpExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("200"));
  Assert.assertTrue(got.contains("OK"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #18
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpDeviceState() throws Exception {
  final String deviceName = "rsa-device-http-state";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-num_messages=1",
    "-message_type=state",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.HttpExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("200"));
  Assert.assertTrue(got.contains("OK"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #19
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpDeviceConfig() throws Exception {
  final String deviceName = "rsa-device-http-state";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-num_messages=1",
    "-message_type=event",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.HttpExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("200"));
  Assert.assertTrue(got.contains("OK"));
  Assert.assertTrue(got.contains("\"binaryData\": \"\""));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #20
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMqttDeviceConfig() throws Exception {
  final String deviceName = "rsa-device-mqtt-config";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-message_type=events",
    "-num_messages=1",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.MqttExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  System.out.println(got);
  Assert.assertTrue(got.contains("Payload :"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #21
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMqttDeviceEvents() throws Exception {
  final String deviceName = "rsa-device-mqtt-events";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-message_type=events",
    "-num_messages=1",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.MqttExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  //
  // Finished loop successfully. Goodbye!

  Assert.assertTrue(got.contains("Publishing events message 1"));
  Assert.assertTrue(got.contains("Finished loop successfully. Goodbye!"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #22
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMqttDeviceState() throws Exception {
  final String deviceName = "rsa-device-mqtt-state";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-message_type=state",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.MqttExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Publishing state message 1"));
  Assert.assertTrue(got.contains("Finished loop successfully. Goodbye!"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #23
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public TopicAdminClient getTopicAdminClient() throws IOException {
	if (topicClient == null) {
		TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
			.setTransportChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
		topicClient = TopicAdminClient.create(topicAdminSettings);
	}
	return topicClient;
}
 
Example #24
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
Example #25
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public TopicAdminClient getTopicAdminClient() throws IOException {
	if (topicClient == null) {
		TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
			.setTransportChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
		topicClient = TopicAdminClient.create(topicAdminSettings);
	}
	return topicClient;
}
 
Example #26
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepare() throws IOException {
	assumeThat(
			"PUB/SUB-sample integration tests are disabled. Please use '-Dit.pubsub=true' "
					+ "to enable them. ",
			System.getProperty("it.pubsub"), is("true"));

	projectName = ProjectName.of(ServiceOptions.getDefaultProjectId()).getProject();
	topicAdminClient = TopicAdminClient.create();
	subscriptionAdminClient = SubscriptionAdminClient.create();

	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC));
	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2));

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION1),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC),
			PushConfig.getDefaultInstance(),
			10);

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION2),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION3),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
}
 
Example #27
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
Example #28
Source File: PubSubManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private static TopicAdminClient buildTopicAdminClient(final PubSubConfig config) {
    final TopicAdminSettings.Builder topicAdminSettingsBuilder = TopicAdminSettings.newBuilder();

    buildCredentialsProvider(config).ifPresent(topicAdminSettingsBuilder::setCredentialsProvider);
    buildTransportChannelProvider(config).ifPresent(topicAdminSettingsBuilder::setTransportChannelProvider);

    try {
        return TopicAdminClient.create(topicAdminSettingsBuilder.build());
    } catch (final IOException e) {
        throw ex.illegalStateUnableToBuildTopicAdminClient(e);
    }
}
 
Example #29
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void createTopic(final PubSubConfig config) {
    final TopicAdminClient topicAdminClient = pubSubManager.topicAdminClient(config);

    final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

    try {
        topicAdminClient.getTopic(topicName);
    } catch (final NotFoundException nf) {
        try {
            topicAdminClient.createTopic(topicName);
        } catch (final AlreadyExistsException ae) {
            log.topicExistAlready(topicName, ae);
        }
    }
}
 
Example #30
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a Pub/Sub topic and subscription.
 *
 * @throws IOException if Pub/Sub is unavailable
 */
@Before
public void initializePubsubResources() throws IOException {
  projectId = ServiceOptions.getDefaultProjectId();
  topicId = "test-topic-" + UUID.randomUUID().toString();
  subscriptionId = "test-subscription-" + UUID.randomUUID().toString();
  topicName = ProjectTopicName.of(projectId, topicId);
  subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);

  TopicAdminClient.create().createTopic(topicName);
  SubscriptionAdminClient.create().createSubscription(subscriptionName, topicName,
      PushConfig.getDefaultInstance(), 0);
}