Skip to content

Commit 05be6e7

Browse files
authored
Gapic firewall samples (GoogleCloudPlatform#5995)
* docs(samples): added samples to demonstrate GCE default values with firewall * fix: increasing the image list count to 100 * refactor: lint fix * refactor(samples): lint fix * docs(samples): added/modified comments * docs(samples): lint fix
1 parent 137cf8f commit 05be6e7

File tree

7 files changed

+350
-3
lines changed

7 files changed

+350
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_delete]
20+
21+
import com.google.cloud.compute.v1.FirewallsClient;
22+
import com.google.cloud.compute.v1.GlobalOperationsClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import java.io.IOException;
25+
import java.util.UUID;
26+
27+
public class DeleteFirewallRule {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample
31+
// project: project ID or project number of the Cloud project you want to use.
32+
// firewallRuleName: name of the firewall rule you want to delete.
33+
String project = "your-project-id";
34+
String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
35+
deleteFirewallRule(project, firewallRuleName);
36+
}
37+
38+
39+
// Deletes a firewall rule from the project.
40+
public static void deleteFirewallRule(String project, String firewallRuleName)
41+
throws IOException {
42+
/* Initialize client that will be used to send requests. This client only needs to be created
43+
once, and can be reused for multiple requests. After completing all of your requests, call
44+
the `firewallsClient.close()` method on the client to safely
45+
clean up any remaining background resources. */
46+
try (FirewallsClient firewallsClient = FirewallsClient.create();
47+
GlobalOperationsClient operationsClient = GlobalOperationsClient.create()) {
48+
49+
Operation operation = firewallsClient.delete(project, firewallRuleName);
50+
operationsClient.wait(project, operation.getName());
51+
}
52+
}
53+
}
54+
// [END compute_firewall_delete]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import com.google.cloud.compute.v1.Firewall;
20+
import com.google.cloud.compute.v1.FirewallsClient;
21+
import java.io.IOException;
22+
import java.util.UUID;
23+
24+
public class GetFirewallRule {
25+
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample
29+
// project: project ID or project number of the Cloud project you want to use.
30+
// firewallRuleName: name of the rule that is created.
31+
String project = "your-project-id";
32+
String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
33+
getFirewallRule(project, firewallRuleName);
34+
}
35+
36+
37+
// Retrieves the firewall rule given by the firewallRuleName if present.
38+
public static void getFirewallRule(String project, String firewallRuleName)
39+
throws IOException {
40+
/* Initialize client that will be used to send requests. This client only needs to be created
41+
once, and can be reused for multiple requests. After completing all of your requests, call
42+
the `firewallsClient.close()` method on the client to safely
43+
clean up any remaining background resources. */
44+
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
45+
Firewall response = firewallsClient.get(project, firewallRuleName);
46+
System.out.println(response);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_list]
20+
21+
import com.google.cloud.compute.v1.Firewall;
22+
import com.google.cloud.compute.v1.FirewallsClient;
23+
import com.google.cloud.compute.v1.FirewallsClient.ListPagedResponse;
24+
import java.io.IOException;
25+
26+
public class ListFirewallRules {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample
30+
// project: project ID or project number of the Cloud project you want to use.
31+
String project = "your-project-id";
32+
listFirewallRules(project);
33+
}
34+
35+
// Return a list of all the firewall rules in specified project.
36+
// Also prints the list of firewall names and their descriptions.
37+
public static ListPagedResponse listFirewallRules(String project)
38+
throws IOException {
39+
/* Initialize client that will be used to send requests. This client only needs to be created
40+
once, and can be reused for multiple requests. After completing all of your requests, call
41+
the `firewallsClient.close()` method on the client to safely
42+
clean up any remaining background resources. */
43+
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
44+
ListPagedResponse firewallResponse = firewallsClient.list(project);
45+
for (Firewall firewall : firewallResponse.iterateAll()) {
46+
System.out.println(firewall.getName());
47+
}
48+
return firewallResponse;
49+
}
50+
}
51+
}
52+
// [END compute_firewall_list]

compute/cloud-client/src/main/java/compute/ListImages.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void main(String[] args) throws IOException {
3636
listImages(project);
3737

3838
// page_size: size of the pages you want the API to return on each call.
39-
int pageSize = 2;
39+
int pageSize = 100;
4040
listImagesByPage(project, pageSize);
4141
}
4242

@@ -52,7 +52,7 @@ public static void listImages(String project) throws IOException {
5252
// Listing only non-deprecated images to reduce the size of the reply.
5353
ListImagesRequest imagesRequest = ListImagesRequest.newBuilder()
5454
.setProject(project)
55-
.setMaxResults(2)
55+
.setMaxResults(100)
5656
.setFilter("deprecated.state != DEPRECATED")
5757
.build();
5858

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_patch]
20+
21+
import com.google.cloud.compute.v1.Firewall;
22+
import com.google.cloud.compute.v1.FirewallsClient;
23+
import com.google.cloud.compute.v1.GlobalOperationsClient;
24+
import com.google.cloud.compute.v1.Operation;
25+
import com.google.cloud.compute.v1.PatchFirewallRequest;
26+
import java.io.IOException;
27+
import java.util.UUID;
28+
29+
public class PatchFirewallRule {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample
33+
// project: project ID or project number of the Cloud project you want to use.
34+
// firewallRuleName: name of the rule you want to modify.
35+
// priority: the new priority to be set for the rule.
36+
String project = "your-project-id";
37+
String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
38+
int priority = 10;
39+
40+
patchFirewallPriority(project, firewallRuleName, priority);
41+
}
42+
43+
// Modifies the priority of a given firewall rule.
44+
public static void patchFirewallPriority(String project, String firewallRuleName, int priority)
45+
throws IOException {
46+
/* Initialize client that will be used to send requests. This client only needs to be created
47+
once, and can be reused for multiple requests. After completing all of your requests, call
48+
the `firewallsClient.close()` method on the client to safely
49+
clean up any remaining background resources. */
50+
try (FirewallsClient firewallsClient = FirewallsClient.create();
51+
GlobalOperationsClient operationsClient = GlobalOperationsClient.create()) {
52+
53+
/* The patch operation doesn't require the full definition of a Firewall object. It will only
54+
update the values that were set in it, in this case it will only change the priority. */
55+
Firewall firewall = Firewall.newBuilder()
56+
.setPriority(priority).build();
57+
58+
PatchFirewallRequest patchFirewallRequest = PatchFirewallRequest.newBuilder()
59+
.setProject(project)
60+
.setFirewall(firewallRuleName)
61+
.setFirewallResource(firewall).build();
62+
63+
Operation operation = firewallsClient.patch(patchFirewallRequest);
64+
operationsClient.wait(project, operation.getName());
65+
System.out.println("Firewall Patch applied successfully ! ");
66+
}
67+
}
68+
}
69+
// [END compute_firewall_patch]

0 commit comments

Comments
 (0)