Search in sources :

Example 6 with Firewall

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 ! ");
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) PatchFirewallRequest(com.google.cloud.compute.v1.PatchFirewallRequest) Operation(com.google.cloud.compute.v1.Operation) Firewall(com.google.cloud.compute.v1.Firewall)

Example 7 with Firewall

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);
}
Also used : PrintStream(java.io.PrintStream) FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 8 with Firewall

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());
}
Also used : ListPagedResponse(com.google.cloud.compute.v1.FirewallsClient.ListPagedResponse) Test(org.junit.Test)

Aggregations

FirewallsClient (com.google.cloud.compute.v1.FirewallsClient)7 Firewall (com.google.cloud.compute.v1.Firewall)5 Operation (com.google.cloud.compute.v1.Operation)3 InsertFirewallRequest (com.google.cloud.compute.v1.InsertFirewallRequest)2 Test (org.junit.Test)2 ListPagedResponse (com.google.cloud.compute.v1.FirewallsClient.ListPagedResponse)1 PatchFirewallRequest (com.google.cloud.compute.v1.PatchFirewallRequest)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Test (org.junit.jupiter.api.Test)1