Search in sources :

Example 36 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-automl by googleapis.

the class TranslateCreateModel method createModel.

// Create a model
static void createModel(String projectId, String datasetId, String displayName) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, "us-central1");
        TranslationModelMetadata translationModelMetadata = TranslationModelMetadata.newBuilder().build();
        Model model = Model.newBuilder().setDisplayName(displayName).setDatasetId(datasetId).setTranslationModelMetadata(translationModelMetadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> future = client.createModelAsync(projectLocation, model);
        // OperationFuture.get() will block until the model is created, which may take several hours.
        // You can use OperationFuture.getInitialFuture to get a future representing the initial
        // response to the request, which contains information while the operation is in progress.
        System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
        System.out.println("Training started...");
    }
}
Also used : Model(com.google.cloud.automl.v1.Model) TranslationModelMetadata(com.google.cloud.automl.v1.TranslationModelMetadata) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName)

Example 37 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-automl by googleapis.

the class ListOperationStatusTest method setUp.

@Before
public void setUp() throws IOException, InterruptedException {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);
    // if the LRO status count more than 300, delete half of operations.
    try (AutoMlClient client = AutoMlClient.create()) {
        OperationsClient operationsClient = client.getOperationsClient();
        LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
        ListOperationsRequest listRequest = ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
        List<String> operationFullPathsToBeDeleted = new ArrayList<>();
        for (Operation operation : operationsClient.listOperations(listRequest).iterateAll()) {
            // Filter: deleting already done operations.
            if (operation.getDone() && !operation.hasError()) {
                operationFullPathsToBeDeleted.add(operation.getName());
            }
        }
        if (operationFullPathsToBeDeleted.size() > 300) {
            System.out.println("Cleaning up...");
            for (String operationFullPath : operationFullPathsToBeDeleted.subList(0, operationFullPathsToBeDeleted.size() / 2)) {
                // retry_interval * (random value in range [1 - rand_factor, 1 + rand_factor])
                ExponentialBackOff exponentialBackOff = new ExponentialBackOff.Builder().setInitialIntervalMillis(60000).setMaxElapsedTimeMillis(300000).setRandomizationFactor(0.5).setMultiplier(1.1).setMaxIntervalMillis(80000).build();
                // delete unused operations.
                try {
                    operationsClient.deleteOperation(operationFullPath);
                } catch (ResourceExhaustedException ex) {
                    // exponential back off and retry.
                    long backOffInMillis = exponentialBackOff.nextBackOffMillis();
                    System.out.printf("Backing off for %d milliseconds " + "due to Resource exhaustion.\n", backOffInMillis);
                    if (backOffInMillis < 0) {
                        break;
                    }
                    System.out.println("Backing off" + backOffInMillis);
                    TimeUnit.MILLISECONDS.sleep(backOffInMillis);
                } catch (Exception ex) {
                    throw ex;
                }
            }
        } else {
            // Clear the list since we wont anything with the list.
            operationFullPathsToBeDeleted.clear();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) OperationsClient(com.google.longrunning.OperationsClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(com.google.longrunning.Operation) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) IOException(java.io.IOException) ResourceExhaustedException(com.google.api.gax.rpc.ResourceExhaustedException) LocationName(com.google.cloud.automl.v1.LocationName) ResourceExhaustedException(com.google.api.gax.rpc.ResourceExhaustedException) ListOperationsRequest(com.google.longrunning.ListOperationsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) Before(org.junit.Before)

Example 38 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-dlp by googleapis.

the class DlpServiceClientTest method listInspectTemplatesTest.

@Test
public void listInspectTemplatesTest() throws Exception {
    InspectTemplate responsesElement = InspectTemplate.newBuilder().build();
    ListInspectTemplatesResponse expectedResponse = ListInspectTemplatesResponse.newBuilder().setNextPageToken("").addAllInspectTemplates(Arrays.asList(responsesElement)).build();
    mockDlpService.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    ListInspectTemplatesPagedResponse pagedListResponse = client.listInspectTemplates(parent);
    List<InspectTemplate> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getInspectTemplatesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockDlpService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListInspectTemplatesRequest actualRequest = ((ListInspectTemplatesRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListInspectTemplatesResponse(com.google.privacy.dlp.v2.ListInspectTemplatesResponse) AbstractMessage(com.google.protobuf.AbstractMessage) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) ListInspectTemplatesRequest(com.google.privacy.dlp.v2.ListInspectTemplatesRequest) OrganizationLocationName(com.google.privacy.dlp.v2.OrganizationLocationName) LocationName(com.google.privacy.dlp.v2.LocationName) ListInspectTemplatesPagedResponse(com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPagedResponse) Test(org.junit.Test)

Example 39 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-dlp by googleapis.

the class DlpServiceClientTest method createInspectTemplateTest.

@Test
public void createInspectTemplateTest() throws Exception {
    InspectTemplate expectedResponse = InspectTemplate.newBuilder().setName(InspectTemplateName.ofOrganizationInspectTemplateName("[ORGANIZATION]", "[INSPECT_TEMPLATE]").toString()).setDisplayName("displayName1714148973").setDescription("description-1724546052").setCreateTime(Timestamp.newBuilder().build()).setUpdateTime(Timestamp.newBuilder().build()).setInspectConfig(InspectConfig.newBuilder().build()).build();
    mockDlpService.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    InspectTemplate inspectTemplate = InspectTemplate.newBuilder().build();
    InspectTemplate actualResponse = client.createInspectTemplate(parent, inspectTemplate);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockDlpService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateInspectTemplateRequest actualRequest = ((CreateInspectTemplateRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(inspectTemplate, actualRequest.getInspectTemplate());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) OrganizationLocationName(com.google.privacy.dlp.v2.OrganizationLocationName) LocationName(com.google.privacy.dlp.v2.LocationName) CreateInspectTemplateRequest(com.google.privacy.dlp.v2.CreateInspectTemplateRequest) Test(org.junit.Test)

Example 40 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-dlp by googleapis.

the class DlpServiceClientTest method createStoredInfoTypeTest.

@Test
public void createStoredInfoTypeTest() throws Exception {
    StoredInfoType expectedResponse = StoredInfoType.newBuilder().setName(StoredInfoTypeName.ofOrganizationStoredInfoTypeName("[ORGANIZATION]", "[STORED_INFO_TYPE]").toString()).setCurrentVersion(StoredInfoTypeVersion.newBuilder().build()).addAllPendingVersions(new ArrayList<StoredInfoTypeVersion>()).build();
    mockDlpService.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    StoredInfoTypeConfig config = StoredInfoTypeConfig.newBuilder().build();
    StoredInfoType actualResponse = client.createStoredInfoType(parent, config);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockDlpService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateStoredInfoTypeRequest actualRequest = ((CreateStoredInfoTypeRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) CreateStoredInfoTypeRequest(com.google.privacy.dlp.v2.CreateStoredInfoTypeRequest) ArrayList(java.util.ArrayList) StoredInfoType(com.google.privacy.dlp.v2.StoredInfoType) StoredInfoTypeConfig(com.google.privacy.dlp.v2.StoredInfoTypeConfig) OrganizationLocationName(com.google.privacy.dlp.v2.OrganizationLocationName) LocationName(com.google.privacy.dlp.v2.LocationName) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AbstractMessage (com.google.protobuf.AbstractMessage)18 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)16 StatusRuntimeException (io.grpc.StatusRuntimeException)14 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)13 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)12 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 LocationName (com.google.cloud.automl.v1beta1.LocationName)12 LocationName (com.google.cloud.translate.v3beta1.LocationName)10 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)10 Model (com.google.cloud.automl.v1.Model)8 LocationName (com.google.cloud.bigquery.connection.v1.LocationName)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)5