use of com.google.api.gax.rpc.PermissionDeniedException in project java-monitoring by googleapis.
the class ITVPCServiceControlTest method postJSON.
private static void postJSON(URL url, String payload) throws Exception {
GoogleCredentials adc = GoogleCredentials.getApplicationDefault().createScoped(ImmutableList.of("https://www.googleapis.com/auth/monitoring", "https://www.googleapis.com/auth/monitoring.write", "https://www.googleapis.com/auth/cloud-platform"));
adc.refresh();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + adc.getAccessToken().getTokenValue());
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json, */*");
conn.setDoOutput(true);
OutputStream body = conn.getOutputStream();
body.write(payload.getBytes("UTF-8"));
body.close();
conn.getResponseMessage();
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) conn.getErrorStream()));
StringBuilder response = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
response.append(line).append("\n");
}
if (conn.getResponseCode() == 403) {
throw new PermissionDeniedException(response.toString(), null, GrpcStatusCode.of(io.grpc.Status.Code.PERMISSION_DENIED), false);
} else if (conn.getResponseCode() != 200) {
throw new RuntimeException(response.toString());
}
return;
}
use of com.google.api.gax.rpc.PermissionDeniedException in project java-recommender by googleapis.
the class ListRecommendations method listRecommendations.
// List recommendations for a specified project, location, and recommender
public static void listRecommendations(String projectId, String location, String recommender) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (RecommenderClient recommenderClient = RecommenderClient.create()) {
// / Build the request
String parent = String.format("projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(parent).build();
try {
// Send the request
ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request);
// Print out each recommendation
for (Recommendation responseItem : response.iterateAll()) {
Recommendation recommendation = responseItem;
System.out.println("Recommendation name: " + recommendation.getName());
System.out.println("- description: " + recommendation.getDescription());
System.out.println("- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory());
System.out.println("- state_info.state: " + recommendation.getStateInfo().getState());
System.out.println();
}
// Indicate the request was successful
System.out.println("List recommendations successful");
} catch (PermissionDeniedException e) {
System.out.println("Permission denied for project '" + projectId + "'. Ensure you have the appropriate permissions to list recommendations: \n" + e.toString());
} catch (InvalidArgumentException e) {
System.out.println(("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n" + e.toString()));
}
}
}
use of com.google.api.gax.rpc.PermissionDeniedException in project java-retail by googleapis.
the class RemoveTestResources method deleteAllProducts.
public static void deleteAllProducts() throws IOException {
System.out.println("Deleting products in process, please wait...");
try (ProductServiceClient productServiceClient = ProductServiceClient.create()) {
ListProductsRequest listRequest = ListProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).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, DEFAULT_CATALOG);
}
}
use of com.google.api.gax.rpc.PermissionDeniedException in project java-websecurityscanner by googleapis.
the class VPCServiceControlNegativeTest method testScanConfigList.
@Test
public void testScanConfigList() throws IOException {
// try to get the list of scan configs - it must fail
WebSecurityScannerSettings wssSetting = getWssSettingWithCredentials();
final String formattedParent = ProjectName.format(OUT_VPCSC_PROJECT);
try (WebSecurityScannerClient wssClient = WebSecurityScannerClient.create(wssSetting)) {
ListScanConfigsRequest lscRequest = ListScanConfigsRequest.newBuilder().setParent(formattedParent).build();
ListScanConfigsPagedResponse scanConfigsList = wssClient.listScanConfigs(lscRequest);
fail("Exception must occur for non-acceessible project scan-configs");
} catch (PermissionDeniedException expected) {
}
}
use of com.google.api.gax.rpc.PermissionDeniedException in project java-websecurityscanner by googleapis.
the class VPCServiceControlNegativeTest method testScanConfigCreation.
@Test
public void testScanConfigCreation() throws IOException {
WebSecurityScannerSettings wssSetting = getWssSettingWithCredentials();
final String formattedParent = ProjectName.format(OUT_VPCSC_PROJECT);
try (WebSecurityScannerClient wssClient = WebSecurityScannerClient.create(wssSetting)) {
ScanConfig scanConfig = ScanConfig.newBuilder().addAllStartingUrls(Lists.newArrayList(OUT_VPCSC_HOSTNAME)).setDisplayName(testScanConfigCreationDisplayName).build();
CreateScanConfigRequest request = CreateScanConfigRequest.newBuilder().setParent(formattedParent).setScanConfig(scanConfig).build();
ScanConfig responseScanConfig = wssClient.createScanConfig(request);
fail("Exception must occur for non-accessible project");
} catch (PermissionDeniedException expected) {
}
}
Aggregations