use of com.google.cloud.compute.v1.Firewall in project java-docs-samples by GoogleCloudPlatform.
the class PatchFirewallRule method patchFirewallPriority.
// Modifies the priority of a given firewall rule.
public static void patchFirewallPriority(String project, String firewallRuleName, int priority) throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `firewallsClient.close()` method on the client to safely
clean up any remaining background resources. */
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
/* The patch operation doesn't require the full definition of a Firewall object. It will only
update the values that were set in it, in this case it will only change the priority. */
Firewall firewall = Firewall.newBuilder().setPriority(priority).build();
PatchFirewallRequest patchFirewallRequest = PatchFirewallRequest.newBuilder().setProject(project).setFirewall(firewallRuleName).setFirewallResource(firewall).build();
OperationFuture<Operation, Operation> operation = firewallsClient.patchAsync(patchFirewallRequest);
operation.get(3, TimeUnit.MINUTES);
System.out.println("Firewall Patch applied successfully ! ");
}
}
use of com.google.cloud.compute.v1.Firewall in project java-docs-samples by GoogleCloudPlatform.
the class FirewallIT method testPatchFirewallRule.
@Test
public void testPatchFirewallRule() throws IOException, InterruptedException, ExecutionException, TimeoutException {
final PrintStream out = System.out;
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
// Firewall rule is auto-deleted by GCE Enforcer within a few minutes.
if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) {
try (FirewallsClient client = FirewallsClient.create()) {
Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500);
TimeUnit.SECONDS.sleep(5);
Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
}
}
// Clear system output to not affect other tests.
// Refrain from setting out to null as it will throw NullPointer in the subsequent tests.
stdOut.close();
System.setOut(out);
}
use of com.google.cloud.compute.v1.Firewall in project java-compute by googleapis.
the class FirewallsClientTest method listTest.
@Test
public void listTest() throws Exception {
Firewall responsesElement = Firewall.newBuilder().build();
FirewallList expectedResponse = FirewallList.newBuilder().setNextPageToken("").addAllItems(Arrays.asList(responsesElement)).build();
mockService.addResponse(expectedResponse);
String project = "project-6911";
ListPagedResponse pagedListResponse = client.list(project);
List<Firewall> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
List<String> actualRequests = mockService.getRequestPaths();
Assert.assertEquals(1, actualRequests.size());
String apiClientHeaderKey = mockService.getRequestHeaders().get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()).iterator().next();
Assert.assertTrue(GaxHttpJsonProperties.getDefaultApiClientHeaderPattern().matcher(apiClientHeaderKey).matches());
}
Aggregations