use of com.google.cloud.compute.v1.Firewall 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();
}
}
use of com.google.cloud.compute.v1.Firewall 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);
}
}
use of com.google.cloud.compute.v1.Firewall 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());
}
}
use of com.google.cloud.compute.v1.Firewall 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.Firewall 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