|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package compute; |
| 18 | + |
| 19 | +// [START compute_firewall_create] |
| 20 | + |
| 21 | +import com.google.cloud.compute.v1.Allowed; |
| 22 | +import com.google.cloud.compute.v1.Firewall; |
| 23 | +import com.google.cloud.compute.v1.Firewall.Direction; |
| 24 | +import com.google.cloud.compute.v1.FirewallsClient; |
| 25 | +import com.google.cloud.compute.v1.GlobalOperationsClient; |
| 26 | +import com.google.cloud.compute.v1.InsertFirewallRequest; |
| 27 | +import com.google.cloud.compute.v1.Operation; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +public class CreateFirewallRule { |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + // TODO(developer): Replace these variables before running the sample |
| 35 | + /* project: project ID or project number of the Cloud project you want to use. |
| 36 | + firewallRuleName: name of the rule that is created. |
| 37 | + network: name of the network the rule will be applied to. Available name formats: |
| 38 | + * https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network} |
| 39 | + * projects/{project_id}/global/networks/{network} |
| 40 | + * global/networks/{network} */ |
| 41 | + String project = "your-project-id"; |
| 42 | + String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID(); |
| 43 | + String network = "global/networks/default"; |
| 44 | + |
| 45 | + // The rule will be created with default priority of 1000. |
| 46 | + createFirewall(project, firewallRuleName, network); |
| 47 | + } |
| 48 | + |
| 49 | + // Creates a simple firewall rule allowing for incoming HTTP and |
| 50 | + // HTTPS access from the entire Internet. |
| 51 | + public static void createFirewall(String project, String firewallRuleName, String network) |
| 52 | + throws IOException { |
| 53 | + /* Initialize client that will be used to send requests. This client only needs to be created |
| 54 | + once, and can be reused for multiple requests. After completing all of your requests, call |
| 55 | + the `firewallsClient.close()` method on the client to safely |
| 56 | + clean up any remaining background resources. */ |
| 57 | + try (FirewallsClient firewallsClient = FirewallsClient.create(); |
| 58 | + GlobalOperationsClient operationsClient = GlobalOperationsClient.create()) { |
| 59 | + |
| 60 | + // The below firewall rule is created in the default network. |
| 61 | + Firewall firewallRule = Firewall.newBuilder() |
| 62 | + .setName(firewallRuleName) |
| 63 | + .setDirection(Direction.INGRESS) |
| 64 | + .addAllowed( |
| 65 | + Allowed.newBuilder().addPorts("80").addPorts("443").setIPProtocol("tcp").build()) |
| 66 | + .addSourceRanges("0.0.0.0/0") |
| 67 | + .setNetwork(network) |
| 68 | + .setDescription("Allowing TCP traffic on port 80 and 443 from Internet.") |
| 69 | + .build(); |
| 70 | + |
| 71 | + /* Note that the default value of priority for the firewall API is 1000. |
| 72 | + If you check the value of `firewallRule.getPriority()` at this point it |
| 73 | + will be equal to 0, however it is not treated as "set" by the library and thus |
| 74 | + the default will be applied to the new rule. If you want to create a rule that |
| 75 | + has priority == 0, you'll need to explicitly set it so: setPriority(0) */ |
| 76 | + |
| 77 | + InsertFirewallRequest insertFirewallRequest = InsertFirewallRequest.newBuilder() |
| 78 | + .setFirewallResource(firewallRule) |
| 79 | + .setProject(project).build(); |
| 80 | + |
| 81 | + Operation operation = firewallsClient.insert(insertFirewallRequest); |
| 82 | + operationsClient.wait(project, operation.getName()); |
| 83 | + |
| 84 | + System.out.println("Firewall rule created successfully ! "); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +// [END compute_firewall_create] |
0 commit comments