Search in sources :

Example 6 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-retail by googleapis.

the class RemoveTestResources method deleteAllProducts.

public static void deleteAllProducts(String branchName) throws IOException {
    System.out.println("Deleting products in process, please wait...");
    try (ProductServiceClient productServiceClient = ProductServiceClient.create()) {
        ListProductsRequest listRequest = ListProductsRequest.newBuilder().setParent(branchName).build();
        ListProductsPagedResponse products = productServiceClient.listProducts(listRequest);
        int deleteCount = 0;
        for (Product product : products.iterateAll()) {
            DeleteProductRequest deleteRequest = DeleteProductRequest.newBuilder().setName(product.getName()).build();
            try {
                productServiceClient.deleteProduct(deleteRequest);
                deleteCount++;
            } catch (PermissionDeniedException e) {
                System.out.println("Ignore PermissionDenied in case the product does not exist " + "at time of deletion.");
            }
        }
        System.out.printf("%s products were deleted from %s%n", deleteCount, branchName);
    }
}
Also used : DeleteProductRequest(com.google.cloud.retail.v2.DeleteProductRequest) ListProductsRequest(com.google.cloud.retail.v2.ListProductsRequest) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) Product(com.google.cloud.retail.v2.Product) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListProductsPagedResponse(com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)

Example 7 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-docs-samples by GoogleCloudPlatform.

the class CreateFilesetEntry method createEntry.

// Create Fileset Entry.
public static void createEntry(String projectId, String entryGroupId, String entryId) {
    // Currently, Data Catalog stores metadata in the us-central1 region.
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
        // that will be used in step 3.
        try {
            dataCatalogClient.deleteEntry(EntryName.of(projectId, location, entryGroupId, entryId).toString());
        } catch (PermissionDeniedException | NotFoundException e) {
            // PermissionDeniedException or NotFoundException are thrown if
            // Entry does not exist.
            System.out.println("Entry does not exist.");
        }
        // that will be used in step 2.
        try {
            dataCatalogClient.deleteEntryGroup(EntryGroupName.of(projectId, location, entryGroupId).toString());
        } catch (PermissionDeniedException | NotFoundException e) {
            // PermissionDeniedException or NotFoundException are thrown if
            // Entry Group does not exist.
            System.out.println("Entry Group does not exist.");
        }
        // 2. Create an Entry Group.
        // Construct the EntryGroup for the EntryGroup request.
        EntryGroup entryGroup = EntryGroup.newBuilder().setDisplayName("My Fileset Entry Group").setDescription("This Entry Group consists of ....").build();
        // Construct the EntryGroup request to be sent by the client.
        CreateEntryGroupRequest entryGroupRequest = CreateEntryGroupRequest.newBuilder().setParent(LocationName.of(projectId, location).toString()).setEntryGroupId(entryGroupId).setEntryGroup(entryGroup).build();
        // Use the client to send the API request.
        EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest);
        System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName());
        // 3. Create a Fileset Entry.
        // Construct the Entry for the Entry request.
        Entry entry = Entry.newBuilder().setDisplayName("My Fileset").setDescription("This fileset consists of ....").setGcsFilesetSpec(GcsFilesetSpec.newBuilder().addFilePatterns("gs://cloud-samples-data/*").build()).setSchema(Schema.newBuilder().addColumns(ColumnSchema.newBuilder().setColumn("first_name").setDescription("First name").setMode("REQUIRED").setType("STRING").build()).addColumns(ColumnSchema.newBuilder().setColumn("last_name").setDescription("Last name").setMode("REQUIRED").setType("STRING").build()).addColumns(ColumnSchema.newBuilder().setColumn("addresses").setDescription("Addresses").setMode("REPEATED").setType("RECORD").addSubcolumns(ColumnSchema.newBuilder().setColumn("city").setDescription("City").setMode("NULLABLE").setType("STRING").build()).addSubcolumns(ColumnSchema.newBuilder().setColumn("state").setDescription("State").setMode("NULLABLE").setType("STRING").build()).build()).build()).setType(EntryType.FILESET).build();
        // Construct the Entry request to be sent by the client.
        CreateEntryRequest entryRequest = CreateEntryRequest.newBuilder().setParent(entryGroupResponse.getName()).setEntryId(entryId).setEntry(entry).build();
        // Use the client to send the API request.
        Entry entryResponse = dataCatalogClient.createEntry(entryRequest);
        System.out.printf("\nEntry created with name: %s\n", entryResponse.getName());
    } catch (AlreadyExistsException | IOException e) {
        // AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
        // IOException's are thrown when unable to create the DataCatalogClient,
        // for example an invalid Service Account path.
        System.out.println("Error in create entry process:\n" + e.toString());
    }
}
Also used : Entry(com.google.cloud.datacatalog.v1.Entry) AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) CreateEntryGroupRequest(com.google.cloud.datacatalog.v1.CreateEntryGroupRequest) EntryGroup(com.google.cloud.datacatalog.v1.EntryGroup) DataCatalogClient(com.google.cloud.datacatalog.v1.DataCatalogClient) NotFoundException(com.google.api.gax.rpc.NotFoundException) CreateEntryRequest(com.google.cloud.datacatalog.v1.CreateEntryRequest) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) IOException(java.io.IOException)

Example 8 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-spanner by googleapis.

the class ITVPCNegativeTest method deniedListBackupOperations.

@Test
public void deniedListBackupOperations() throws IOException {
    try (OperationsClient client = OperationsClient.create(OperationsSettings.newBuilder().setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build()).setEndpoint("spanner.googleapis.com:443").setCredentialsProvider(FixedCredentialsProvider.create(GoogleCredentials.fromStream(new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"))))).build())) {
        client.listOperations(backupId.getName() + "/operations", "");
        fail("Expected PermissionDeniedException");
    } catch (PermissionDeniedException e) {
        assertThat(e.getMessage()).contains("Request is prohibited by organization's policy");
    }
}
Also used : OperationsClient(com.google.longrunning.OperationsClient) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) SerialIntegrationTest(com.google.cloud.spanner.SerialIntegrationTest)

Aggregations

PermissionDeniedException (com.google.api.gax.rpc.PermissionDeniedException)8 Test (org.junit.Test)3 DeleteProductRequest (com.google.cloud.retail.v2.DeleteProductRequest)2 ListProductsRequest (com.google.cloud.retail.v2.ListProductsRequest)2 Product (com.google.cloud.retail.v2.Product)2 ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)2 ListProductsPagedResponse (com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)2 WebSecurityScannerClient (com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient)2 WebSecurityScannerSettings (com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings)2 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 CreateEntryGroupRequest (com.google.cloud.datacatalog.v1.CreateEntryGroupRequest)1 CreateEntryRequest (com.google.cloud.datacatalog.v1.CreateEntryRequest)1 DataCatalogClient (com.google.cloud.datacatalog.v1.DataCatalogClient)1 Entry (com.google.cloud.datacatalog.v1.Entry)1 EntryGroup (com.google.cloud.datacatalog.v1.EntryGroup)1 ListRecommendationsRequest (com.google.cloud.recommender.v1beta1.ListRecommendationsRequest)1 Recommendation (com.google.cloud.recommender.v1beta1.Recommendation)1