use of com.google.cloud.compute.v1.InsertFirewallRequest 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);
}
}
use of com.google.cloud.compute.v1.InsertFirewallRequest in project java-docs-samples by GoogleCloudPlatform.
the class CreateFirewallRuleForWindowsActivationHost method createFirewallRuleForWindowsActivationHost.
// Creates a new allow egress firewall rule with the highest priority for host
// kms.windows.googlecloud.com (35.190.247.13) for Windows activation.
public static void createFirewallRuleForWindowsActivationHost(String projectId, String firewallRuleName, String networkName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Instantiates a client.
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
Firewall firewall = Firewall.newBuilder().setName(firewallRuleName).addAllowed(Allowed.newBuilder().setIPProtocol("tcp").addPorts("1688").build()).setDirection("EGRESS").setNetwork(networkName).addDestinationRanges("35.190.247.13/32").setPriority(0).build();
InsertFirewallRequest request = InsertFirewallRequest.newBuilder().setProject(projectId).setFirewallResource(firewall).build();
// Wait for the operation to complete.
Operation operation = firewallsClient.insertAsync(request).get(3, TimeUnit.MINUTES);
if (operation.hasError()) {
System.out.println("Firewall rule creation failed ! ! " + operation.getError());
return;
}
System.out.printf("Firewall rule created %s", firewallRuleName);
}
}
Aggregations