com.google.pubsub.v1.ProjectName Java Examples

The following examples show how to use com.google.pubsub.v1.ProjectName. 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: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
/** Updates the managed Server when a new Topic is created. */
public final com.google.pubsub.v1.Topic createTopic(com.google.pubsub.v1.Topic topic)
    throws ConfigurationAlreadyExistsException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(topic.getName());
  if (getTopicByName(topic.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Topic " + projectTopicName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic.Builder builder = topic.toBuilder();
  if (topic.getLabelsOrDefault(KAFKA_TOPIC, null) == null) {
    builder.putLabels(
        KAFKA_TOPIC,
        String.join(
            KAFKA_TOPIC_SEPARATOR, projectTopicName.getProject(), projectTopicName.getTopic()));
  }

  com.google.pubsub.v1.Topic built = builder.build();
  topicsByProject.put(ProjectName.of(projectTopicName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example #2
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
/** Updates the managed Server when a Subscription is deleted. */
public final void deleteSubscription(String subscriptionName)
    throws ConfigurationNotFoundException {
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscriptionName);
  ProjectName projectName = ProjectName.of(projectSubscriptionName.getProject());

  Set<com.google.pubsub.v1.Subscription> subscriptions =
      subscriptionsByProject.get(projectName.toString());
  getSubscriptionByName(subscriptionName)
      .map(subscriptions::remove)
      .orElseThrow(
          () ->
              new ConfigurationNotFoundException(
                  "Subscription " + projectSubscriptionName.toString() + " does not exist"));
  pubSubRepository.save(getPubSub());
}
 
Example #3
Source File: GoogleCloudPubSubSinkConfiguration.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private static void createTopic(final String hostPort,
                                final TransportChannelProvider channelProvider,
                                final ProjectTopicName topic) {
    final TopicAdminClient topicClient;
    try {
        final TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(NoCredentialsProvider.create())
            .build();
        topicClient = TopicAdminClient.create(topicAdminSettings);
    } catch (final IOException e) {
        throw new UncheckedIOException(String.format("Error creating topic %s for pub/sub emulator %s",
                                                     topic, hostPort), e);
    }
    final ProjectName project = ProjectName.of(topic.getProject());
    if (Streams.stream(topicClient.listTopics(project).iterateAll())
               .map(Topic::getName)
               .map(ProjectTopicName::parse)
               .noneMatch(topic::equals)) {
        logger.info("Initializing Pub/Sub emulator topic: {}", topic);
        topicClient.createTopic(topic);
    }
}
 
Example #4
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Inject
public ConfigurationManager(Server serverConfiguration, PubSubRepository pubSubRepository) {
  Preconditions.checkNotNull(serverConfiguration);
  Preconditions.checkNotNull(pubSubRepository);

  this.serverConfiguration = serverConfiguration;
  this.pubSubRepository = pubSubRepository;
  topicsByProject.clear();
  subscriptionsByProject.clear();
  for (Project p : pubSubRepository.load().getProjectsList()) {
    for (Topic t : p.getTopicsList()) {
      String projectName = ProjectName.format(p.getName());
      String topicName = ProjectTopicName.format(p.getName(), t.getName());
      String kafkaTopic = Optional.ofNullable(t.getKafkaTopic()).orElse(t.getName());
      topicsByProject.put(
          projectName,
          com.google.pubsub.v1.Topic.newBuilder()
              .setName(topicName)
              .putLabels(KAFKA_TOPIC, kafkaTopic)
              .build());

      for (Subscription s : t.getSubscriptionsList()) {
        subscriptionsByProject.put(
            projectName,
            com.google.pubsub.v1.Subscription.newBuilder()
                .setTopic(topicName)
                .setName(ProjectSubscriptionName.format(p.getName(), s.getName()))
                .setAckDeadlineSeconds(s.getAckDeadlineSeconds())
                .setPushConfig(PushConfig.getDefaultInstance())
                .putLabels(KAFKA_TOPIC, kafkaTopic)
                .build());
      }
    }
  }
}
 
Example #5
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a Topic is deleted. */
public final void deleteTopic(String topicName) throws ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(topicName);
  ProjectName projectName = ProjectName.of(projectTopicName.getProject());
  Set<com.google.pubsub.v1.Topic> topics = topicsByProject.get(projectName.toString());
  getTopicByName(topicName)
      .map(topics::remove)
      .orElseThrow(
          () ->
              new ConfigurationNotFoundException(
                  "Topic " + projectTopicName.toString() + " does not exist"));
  pubSubRepository.save(getPubSub());
}
 
Example #6
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a new Subscription is created. */
public final com.google.pubsub.v1.Subscription createSubscription(
    com.google.pubsub.v1.Subscription subscription)
    throws ConfigurationAlreadyExistsException, ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(subscription.getTopic());
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscription.getName());

  if (getSubscriptionByName(subscription.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Subscription " + projectSubscriptionName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic topic =
      getTopicByName(projectTopicName.toString())
          .orElseThrow(
              () ->
                  new ConfigurationNotFoundException(
                      "Topic " + projectTopicName.toString() + " does not exist"));
  com.google.pubsub.v1.Subscription.Builder builder =
      subscription.toBuilder().putLabels(KAFKA_TOPIC, topic.getLabelsOrThrow(KAFKA_TOPIC));
  if (subscription.getAckDeadlineSeconds() == 0) {
    builder.setAckDeadlineSeconds(10);
  }
  builder.setPushConfig(PushConfig.getDefaultInstance()).build();
  com.google.pubsub.v1.Subscription built = builder.build();
  subscriptionsByProject.put(
      ProjectName.of(projectSubscriptionName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example #7
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 #8
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Return every topic in a project.
 * <p>If there are multiple pages, they will all be merged into the same result.
 * @return a list of topics
 */
public List<Topic> listTopics() {
	TopicAdminClient.ListTopicsPagedResponse topicListPage =
			this.topicAdminClient.listTopics(ProjectName.of(this.projectId));

	List<Topic> topics = new ArrayList<>();
	topicListPage.iterateAll().forEach(topics::add);
	return Collections.unmodifiableList(topics);
}
 
Example #9
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Return every subscription in a project.
 * <p>If there are multiple pages, they will all be merged into the same result.
 * @return a list of subscriptions
 */
public List<Subscription> listSubscriptions() {
	SubscriptionAdminClient.ListSubscriptionsPagedResponse subscriptionsPage =
			this.subscriptionAdminClient.listSubscriptions(ProjectName.of(this.projectId));

	List<Subscription> subscriptions = new ArrayList<>();
	subscriptionsPage.iterateAll().forEach(subscriptions::add);
	return Collections.unmodifiableList(subscriptions);
}
 
Example #10
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp()  {

    String projectId = ServiceOptions.getDefaultProjectId();
	try {

		//export GRPC_PROXY_EXP=localhost:3128
		HttpHost proxy = new HttpHost("127.0.0.1",3128);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
					
		httpClient.addRequestInterceptor(new HttpRequestInterceptor(){            
			@Override
			public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException {
					//if (request.getRequestLine().getMethod().equals("CONNECT"))                 
					//   request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE="));
				}
			});
		
		mHttpTransport =  new ApacheHttpTransport(httpClient);		

		HttpTransportFactory hf = new HttpTransportFactory(){
			@Override
			public HttpTransport create() {
				return mHttpTransport;
			}
		};            
		
		credential = GoogleCredentials.getApplicationDefault(hf);

		CredentialsProvider credentialsProvider =  new GoogleCredentialsProvider(){
			public List<String> getScopesToApply(){
				return Arrays.asList("https://www.googleapis.com/auth/pubsub");
			   }

			public Credentials getCredentials()  {
				return credential;
			}
		};

		TopicAdminSettings topicAdminSettings =
		     TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider)
				 .build();
				 
		 TopicAdminClient topicAdminClient =
		     TopicAdminClient.create(topicAdminSettings);
		
		//TopicAdminClient topicAdminClient = TopicAdminClient.create();
		ProjectName project = ProjectName.create(projectId);
		for (Topic element : topicAdminClient.listTopics(project).iterateAll()) 
	  		System.out.println(element.getName());
	
	} catch (Exception ex) 
	{
		System.out.println("ERROR " + ex);
	}
  }
 
Example #11
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
	try
	{
		/*
		// For GoogleAPIs
		HttpTransport httpTransport = new NetHttpTransport();             
		JacksonFactory jsonFactory = new JacksonFactory();
		//ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory).build();	
		GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);				            
		if (credential.createScopedRequired())
		    credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));           				            
		Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
		            .setApplicationName("oauth client")   
		            .build();				            
		Userinfoplus ui = service.userinfo().get().execute();
		System.out.println(ui.getEmail());
		*/

         // Using Google Cloud APIs
	  Storage storage_service = StorageOptions.newBuilder()
		.build()
		.getService();	
	  for (Bucket b : storage_service.list().iterateAll()){
		  System.out.println(b);
	  }

         // String cred_file = "/path/to/cred.json";
	  //GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_file));	
	  GoogleCredentials creds = GoogleCredentials.getApplicationDefault();	  	  
	  FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
	  
	  ///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
         //TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));	
	  
	  TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();

	  TopicAdminClient topicClient =
		  TopicAdminClient.create(
			  TopicAdminSettings.newBuilder()
				  .setTransportChannelProvider(channelProvider)
				  .setCredentialsProvider(credentialsProvider)
				  .build());

	  ListTopicsRequest listTopicsRequest =
						ListTopicsRequest.newBuilder()
							.setProject(ProjectName.format("your_project"))
							.build();
	  ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
	  Iterable<Topic> topics = response.iterateAll();
	  for (Topic topic : topics) 
		 System.out.println(topic);
	 		  

	} 
	catch (Exception ex) {
		System.out.println("Error:  " + ex);
	}
}
 
Example #12
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
		try
		{

			// use env or set the path directly
			String cred_env = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
			cred_env = "/path/to/your/cert.json";
			
/*
			<!--use:
				<dependency>
				<groupId>com.google.api-client</groupId>
				<artifactId>google-api-client</artifactId>
				<version>1.23.0</version>
				</dependency>
				<dependency>
				<groupId>com.google.apis</groupId>
				<artifactId>google-api-services-oauth2</artifactId>
				<version>v2-rev114-1.22.0</version>
				</dependency>
			--> 
			HttpTransport httpTransport = new NetHttpTransport();             
			JacksonFactory jsonFactory = new JacksonFactory();

            // unset GOOGLE_APPLICATION_CREDENTIALS
            //String SERVICE_ACCOUNT_JSON_FILE = "YOUR_SERVICE_ACCOUNT_JSON_FILE.json";
            //FileInputStream inputStream = new FileInputStream(new File(SERVICE_ACCOUNT_JSON_FILE));
            //GoogleCredential credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory);

			// to use application default credentials and a JSON file, set the environment variable first:
            // export GOOGLE_APPLICATION_CREDENTIALS=YOUR_SERVICE_ACCOUNT_JSON_FILE.json        
            GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);

            if (credential.createScopedRequired())
                credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));

			Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
			            .setApplicationName("oauth client")   
			            .build();				            
			Userinfoplus ui = service.userinfo().get().execute();
			System.out.println(ui.getEmail());
*/
/* 
          Using Google Cloud APIs with service account file
		  // You can also just export an export GOOGLE_APPLICATION_CREDENTIALS and use StorageOptions.defaultInstance().service()
		  // see: https://github.com/google/google-auth-library-java#google-auth-library-oauth2-http
		  uncomment the dependencies for google-api-client
		  
			<dependency>
				<groupId>com.google.cloud</groupId>
				<artifactId>google-cloud-storage</artifactId>
				<version>1.35.0</version>
			</dependency>

			<dependency>
				<groupId>com.google.cloud</groupId>
				<artifactId>google-cloud-pubsub</artifactId>
				<version>1.35.0</version>
			</dependency>
*/
		  
		  
		  Storage storage_service = StorageOptions.newBuilder()
			.build()
			.getService();	
		  for (Bucket b : storage_service.list().iterateAll()){
			  System.out.println(b);
		  }

		  //GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_env));	
		  GoogleCredentials creds = GoogleCredentials.getApplicationDefault();	  	  
		  FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
		  
		  ///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
          //TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));	
		  
		  TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();

		  TopicAdminClient topicClient =
			  TopicAdminClient.create(
				  TopicAdminSettings.newBuilder()
					  .setTransportChannelProvider(channelProvider)
					  .setCredentialsProvider(credentialsProvider)
					  .build());

		  ListTopicsRequest listTopicsRequest =
							ListTopicsRequest.newBuilder()
								.setProject(ProjectName.format("your_project"))
								.build();
		  ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
		  Iterable<Topic> topics = response.iterateAll();
		  for (Topic topic : topics) 
			 System.out.println(topic);
		 

		} 
		catch (Exception ex) {
			System.out.println("Error:  " + ex);
		}
	}