Search in sources :

Example 1 with FirewallsClient

use of com.google.cloud.compute.v1.FirewallsClient in project java-compute by googleapis.

the class ITSmokeInstancesTest method testCapitalLetterField.

@Test
public void testCapitalLetterField() throws IOException, ExecutionException, InterruptedException {
    // we want to test a field like "IPProtocol"
    String name = generateRandomName("fw-rule");
    FirewallsClient firewallsClient = FirewallsClient.create();
    Firewall firewall = Firewall.newBuilder().setName(name).addAllowed(Allowed.newBuilder().setIPProtocol("tcp").addPorts("80").build()).addSourceRanges("0.0.0.0/0").build();
    try {
        firewallsClient.insertAsync(DEFAULT_PROJECT, firewall).get();
        Firewall fetched = firewallsClient.get(DEFAULT_PROJECT, name);
        Assert.assertEquals(name, fetched.getName());
        Assert.assertEquals("tcp", fetched.getAllowed(0).getIPProtocol());
    } finally {
        firewallsClient.deleteAsync(DEFAULT_PROJECT, name).get();
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) Firewall(com.google.cloud.compute.v1.Firewall) Test(org.junit.Test)

Example 2 with FirewallsClient

use of com.google.cloud.compute.v1.FirewallsClient in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testPatchFirewallRule.

public static void testPatchFirewallRule() throws IOException, InterruptedException, ExecutionException {
    final PrintStream out = System.out;
    ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(stdOut));
    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)

Example 3 with FirewallsClient

use of com.google.cloud.compute.v1.FirewallsClient in project java-docs-samples by GoogleCloudPlatform.

the class DeleteFirewallRule method deleteFirewallRule.

// Deletes a firewall rule from the project.
public static void deleteFirewallRule(String project, String firewallRuleName) 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()) {
        OperationFuture<Operation, Operation> operation = firewallsClient.deleteAsync(project, firewallRuleName);
        operation.get(3, TimeUnit.MINUTES);
        System.out.println("Deleted firewall rule -> " + firewallRuleName);
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) Operation(com.google.cloud.compute.v1.Operation)

Example 4 with FirewallsClient

use of com.google.cloud.compute.v1.FirewallsClient in project java-docs-samples by GoogleCloudPlatform.

the class GetFirewallRule method getFirewallRule.

// Retrieves the firewall rule given by the firewallRuleName if present.
public static void getFirewallRule(String project, String firewallRuleName) throws IOException {
    /* 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()) {
        Firewall response = firewallsClient.get(project, firewallRuleName);
        System.out.print(response.getName());
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) Firewall(com.google.cloud.compute.v1.Firewall)

Example 5 with FirewallsClient

use of com.google.cloud.compute.v1.FirewallsClient in project java-docs-samples by GoogleCloudPlatform.

the class CreateFirewallRule method createFirewall.

// Creates a simple firewall rule allowing for incoming HTTP and
// HTTPS access from the entire Internet.
public static void createFirewall(String project, String firewallRuleName, String network) 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 below firewall rule is created in the default network.
        Firewall firewallRule = Firewall.newBuilder().setName(firewallRuleName).setDirection(Direction.INGRESS.toString()).addAllowed(Allowed.newBuilder().addPorts("80").addPorts("443").setIPProtocol("tcp").build()).addSourceRanges("0.0.0.0/0").setNetwork(network).addTargetTags("web").setDescription("Allowing TCP traffic on port 80 and 443 from Internet.").build();
        /* Note that the default value of priority for the firewall API is 1000.
         If you check the value of `firewallRule.getPriority()` at this point it
         will be equal to 0, however it is not treated as "set" by the library and thus
         the default will be applied to the new rule. If you want to create a rule that
         has priority == 0, you'll need to explicitly set it so: setPriority(0) */
        InsertFirewallRequest insertFirewallRequest = InsertFirewallRequest.newBuilder().setFirewallResource(firewallRule).setProject(project).build();
        firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES);
        System.out.println("Firewall rule created successfully -> " + firewallRuleName);
    }
}
Also used : FirewallsClient(com.google.cloud.compute.v1.FirewallsClient) InsertFirewallRequest(com.google.cloud.compute.v1.InsertFirewallRequest) Firewall(com.google.cloud.compute.v1.Firewall)

Aggregations

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