Search in sources :

Example 21 with ProjectName

use of com.google.storage.v2.ProjectName in project java-docs-samples by GoogleCloudPlatform.

the class Quickstart method quickstart.

public void quickstart(String projectId, String secretId) throws Exception {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Create the parent secret.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        // Add a secret version.
        SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello world!")).build();
        SecretVersion addedVersion = client.addSecretVersion(createdSecret.getName(), payload);
        // Access the secret version.
        AccessSecretVersionResponse response = client.accessSecretVersion(addedVersion.getName());
        // Print the secret payload.
        // 
        // WARNING: Do not print the secret in a production environment - this
        // snippet is showing how to access the secret material.
        String data = response.getPayload().getData().toStringUtf8();
        System.out.printf("Plaintext: %s\n", data);
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ByteString(com.google.protobuf.ByteString) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Example 22 with ProjectName

use of com.google.storage.v2.ProjectName in project divolte-collector by divolte.

the class GoogleCloudPubSubSinkConfiguration method createTopic.

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);
    }
}
Also used : ProjectName(com.google.pubsub.v1.ProjectName) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Topic(com.google.pubsub.v1.Topic)

Example 23 with ProjectName

use of com.google.storage.v2.ProjectName in project java-phishingprotection by googleapis.

the class PhishingProtectionServiceV1Beta1ClientTest method reportPhishingTest.

@Test
public void reportPhishingTest() throws Exception {
    ReportPhishingResponse expectedResponse = ReportPhishingResponse.newBuilder().build();
    mockPhishingProtectionServiceV1Beta1.addResponse(expectedResponse);
    ProjectName parent = ProjectName.of("[PROJECT]");
    String uri = "uri116076";
    ReportPhishingResponse actualResponse = client.reportPhishing(parent, uri);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockPhishingProtectionServiceV1Beta1.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ReportPhishingRequest actualRequest = ((ReportPhishingRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(uri, actualRequest.getUri());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ProjectName(com.google.phishingprotection.v1beta1.ProjectName) ReportPhishingResponse(com.google.phishingprotection.v1beta1.ReportPhishingResponse) ReportPhishingRequest(com.google.phishingprotection.v1beta1.ReportPhishingRequest) Test(org.junit.Test)

Example 24 with ProjectName

use of com.google.storage.v2.ProjectName in project java-storage by googleapis.

the class StorageClientTest method listNotificationsTest.

@Test
public void listNotificationsTest() throws Exception {
    Notification responsesElement = Notification.newBuilder().build();
    ListNotificationsResponse expectedResponse = ListNotificationsResponse.newBuilder().setNextPageToken("").addAllNotifications(Arrays.asList(responsesElement)).build();
    mockStorage.addResponse(expectedResponse);
    ProjectName parent = ProjectName.of("[PROJECT]");
    ListNotificationsPagedResponse pagedListResponse = client.listNotifications(parent);
    List<Notification> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getNotificationsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockStorage.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListNotificationsRequest actualRequest = ((ListNotificationsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListNotificationsPagedResponse(com.google.storage.v2.StorageClient.ListNotificationsPagedResponse) Test(org.junit.Test)

Example 25 with ProjectName

use of com.google.storage.v2.ProjectName in project java-storage by googleapis.

the class StorageClientTest method listBucketsTest.

@Test
public void listBucketsTest() throws Exception {
    Bucket responsesElement = Bucket.newBuilder().build();
    ListBucketsResponse expectedResponse = ListBucketsResponse.newBuilder().setNextPageToken("").addAllBuckets(Arrays.asList(responsesElement)).build();
    mockStorage.addResponse(expectedResponse);
    ProjectName parent = ProjectName.of("[PROJECT]");
    ListBucketsPagedResponse pagedListResponse = client.listBuckets(parent);
    List<Bucket> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getBucketsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockStorage.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListBucketsRequest actualRequest = ((ListBucketsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListBucketsPagedResponse(com.google.storage.v2.StorageClient.ListBucketsPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)186 StatusRuntimeException (io.grpc.StatusRuntimeException)89 AbstractMessage (com.google.protobuf.AbstractMessage)84 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)74 ProjectName (com.google.monitoring.v3.ProjectName)62 ProjectName (com.google.pubsub.v1.ProjectName)44 ProjectName (com.google.logging.v2.ProjectName)31 ArrayList (java.util.ArrayList)31 ProjectName (com.google.privacy.dlp.v2.ProjectName)22 ByteString (com.google.protobuf.ByteString)20 DataTransferServiceClient (com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient)17 ProjectName (com.google.cloud.bigquery.datatransfer.v1.ProjectName)17 IOException (java.io.IOException)17 CreateTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest)16 TransferConfig (com.google.cloud.bigquery.datatransfer.v1.TransferConfig)16 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)16 ProjectName (com.google.containeranalysis.v1beta1.ProjectName)16 ApiException (com.google.api.gax.rpc.ApiException)15 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)15 TimeSeries (com.google.monitoring.v3.TimeSeries)15