Search in sources :

Example 76 with ProjectName

use of com.google.logging.v2.ProjectName in project spring-cloud-gcp by spring-cloud.

the class PubSubSampleApplicationTests method getMessagesFromSubscription.

private List<String> getMessagesFromSubscription(String subscriptionName) {
    String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
    PullRequest pullRequest = PullRequest.newBuilder().setReturnImmediately(true).setMaxMessages(10).setSubscription(projectSubscriptionName).build();
    PullResponse pullResponse = subscriptionAdminClient.getStub().pullCallable().call(pullRequest);
    return pullResponse.getReceivedMessagesList().stream().map((message) -> message.getMessage().getData().toStringUtf8()).collect(Collectors.toList());
}
Also used : Topic(com.google.pubsub.v1.Topic) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ListTopicsPagedResponse(com.google.cloud.pubsub.v1.TopicAdminClient.ListTopicsPagedResponse) ServiceOptions(com.google.cloud.ServiceOptions) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) StreamSupport(java.util.stream.StreamSupport) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) SpringRunner(org.springframework.test.context.junit4.SpringRunner) PullResponse(com.google.pubsub.v1.PullResponse) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Awaitility.await(org.awaitility.Awaitility.await) IOException(java.io.IOException) Test(org.junit.Test) PullRequest(com.google.pubsub.v1.PullRequest) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) LocalServerPort(org.springframework.boot.web.server.LocalServerPort) List(java.util.List) Rule(org.junit.Rule) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) OutputCaptureRule(org.springframework.boot.test.system.OutputCaptureRule) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ListSubscriptionsPagedResponse(com.google.cloud.pubsub.v1.SubscriptionAdminClient.ListSubscriptionsPagedResponse) Subscription(com.google.pubsub.v1.Subscription) ResponseEntity(org.springframework.http.ResponseEntity) Matchers.is(org.hamcrest.Matchers.is) PushConfig(com.google.pubsub.v1.PushConfig) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ProjectName(com.google.pubsub.v1.ProjectName) PullResponse(com.google.pubsub.v1.PullResponse) PullRequest(com.google.pubsub.v1.PullRequest)

Example 77 with ProjectName

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

the class JobSearchListTenants method listTenants.

// List Tenants.
public static void listTenants(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
        ProjectName parent = ProjectName.of(projectId);
        ListTenantsRequest request = ListTenantsRequest.newBuilder().setParent(parent.toString()).build();
        for (Tenant responseItem : tenantServiceClient.listTenants(request).iterateAll()) {
            System.out.format("Tenant Name: %s%n", responseItem.getName());
            System.out.format("External ID: %s%n", responseItem.getExternalId());
        }
    }
}
Also used : TenantServiceClient(com.google.cloud.talent.v4beta1.TenantServiceClient) Tenant(com.google.cloud.talent.v4beta1.Tenant) ProjectName(com.google.cloud.talent.v4beta1.ProjectName) ListTenantsRequest(com.google.cloud.talent.v4beta1.ListTenantsRequest)

Example 78 with ProjectName

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    if (projectId == null) {
        System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
        return;
    }
    // Instantiates a client
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    // Prepares an individual data point
    TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    TypedValue value = TypedValue.newBuilder().setDoubleValue(3.14).build();
    Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
    List<Point> pointList = new ArrayList<>();
    pointList.add(point);
    ProjectName name = ProjectName.of(projectId);
    // Prepares the metric descriptor
    Map<String, String> metricLabels = new HashMap<String, String>();
    metricLabels.put("store_id", "Pittsburg");
    Metric metric = Metric.newBuilder().setType("custom.googleapis.com/my_metric").putAllLabels(metricLabels).build();
    // Prepares the monitored resource descriptor
    Map<String, String> resourceLabels = new HashMap<String, String>();
    resourceLabels.put("instance_id", "1234567890123456789");
    resourceLabels.put("zone", "us-central1-f");
    MonitoredResource resource = MonitoredResource.newBuilder().setType("gce_instance").putAllLabels(resourceLabels).build();
    // Prepares the time series request
    TimeSeries timeSeries = TimeSeries.newBuilder().setMetric(metric).setResource(resource).addAllPoints(pointList).build();
    List<TimeSeries> timeSeriesList = new ArrayList<>();
    timeSeriesList.add(timeSeries);
    CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(name.toString()).addAllTimeSeries(timeSeriesList).build();
    // Writes time series data
    metricServiceClient.createTimeSeries(request);
    System.out.printf("Done writing time series data.%n");
    metricServiceClient.close();
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) HashMap(java.util.HashMap) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) Point(com.google.monitoring.v3.Point) Metric(com.google.api.Metric) TypedValue(com.google.monitoring.v3.TypedValue)

Example 79 with ProjectName

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

the class Snippets method listMetricDescriptors.

/**
 * Returns the first page of all metric descriptors.
 */
void listMetricDescriptors() throws IOException {
    // [START monitoring_list_descriptors]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    final MetricServiceClient client = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder().setName(name.toString()).build();
    ListMetricDescriptorsPagedResponse response = client.listMetricDescriptors(request);
    System.out.println("Listing descriptors: ");
    for (MetricDescriptor d : response.iterateAll()) {
        System.out.println(d.getName() + " " + d.getDisplayName());
    }
// [END monitoring_list_descriptors]
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)

Example 80 with ProjectName

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

the class Snippets method listTimeSeriesHeaders.

// CHECKSTYLE ON: VariableDeclarationUsageDistance
/**
 * Demonstrates listing time series headers.
 */
void listTimeSeriesHeaders() throws IOException {
    // [START monitoring_read_timeseries_fields]
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    String projectId = System.getProperty("projectId");
    ProjectName name = ProjectName.of(projectId);
    // Restrict time to last 20 minutes
    long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000);
    TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter("metric.type=\"compute.googleapis.com/instance/cpu/utilization\"").setInterval(interval).setView(ListTimeSeriesRequest.TimeSeriesView.HEADERS);
    ListTimeSeriesRequest request = requestBuilder.build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    System.out.println("Got timeseries headers: ");
    for (TimeSeries ts : response.iterateAll()) {
        System.out.println(ts);
    }
// [END monitoring_read_timeseries_fields]
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Aggregations

Test (org.junit.Test)70 StatusRuntimeException (io.grpc.StatusRuntimeException)34 ProjectName (com.google.privacy.dlp.v2.ProjectName)22 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)21 ProjectName (com.google.monitoring.v3.ProjectName)21 AbstractMessage (com.google.protobuf.AbstractMessage)21 DataTransferServiceClient (com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient)17 ProjectName (com.google.cloud.bigquery.datatransfer.v1.ProjectName)17 CreateTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest)16 TransferConfig (com.google.cloud.bigquery.datatransfer.v1.TransferConfig)16 ApiException (com.google.api.gax.rpc.ApiException)15 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)14 ProjectName (com.google.logging.v2.ProjectName)14 ApiException (com.google.api.gax.grpc.ApiException)13 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)13 ProjectName (com.google.devtools.clouderrorreporting.v1beta1.ProjectName)11 ArrayList (java.util.ArrayList)11 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)10 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)9 TimeSeries (com.google.monitoring.v3.TimeSeries)9