Skip to content

Commit 91781f6

Browse files
EshaantheMelonm-strzelczykrsamborski
authored
Adding firewall samples and tests (GoogleCloudPlatform#1538)
Adding firewall samples with tests. Includes: refactored wait_for_operation.php to support Zone, Region and Global operations. Co-authored-by: Maciej Strzelczyk Co-authored-by: Remigiusz Samborski
1 parent c69211b commit 91781f6

9 files changed

+449
-17
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
include_once 'wait_for_operation.php';
27+
28+
# [START compute_firewall_create]
29+
use Google\Cloud\Compute\V1\FirewallsClient;
30+
use Google\Cloud\Compute\V1\Allowed;
31+
use Google\Cloud\Compute\V1\Firewall;
32+
use Google\Cloud\Compute\V1\Firewall\Direction;
33+
34+
/**
35+
* Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.
36+
*
37+
* Example:
38+
* ```
39+
* create_firewall_rule($projectId, $firewallRuleName, $network);
40+
* ```
41+
*
42+
* @param string $projectId Project ID or project number of the Cloud project you want to create a rule for.
43+
* @param string $firewallRuleName Name of the rule that is created.
44+
* @param string $network Name of the network the rule will be applied to. Available name formats:
45+
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
46+
* projects/{project_id}/global/networks/{network}
47+
* global/networks/{network}
48+
*
49+
* @throws \Google\ApiCore\ApiException if the remote call fails.
50+
*/
51+
52+
function create_firewall_rule(string $projectId, string $firewallRuleName, string $network = 'global/networks/default')
53+
{
54+
$firewallsClient = new FirewallsClient();
55+
$allowedPorts = (new Allowed())
56+
->setIPProtocol('tcp')
57+
->setPorts(['80', '443']);
58+
$firewallResource = (new Firewall())
59+
->setName($firewallRuleName)
60+
->setDirection(Direction::INGRESS)
61+
->setAllowed([$allowedPorts])
62+
->setSourceRanges(['0.0.0.0/0'])
63+
->setTargetTags(['web'])
64+
->setNetwork($network)
65+
->setDescription('Allowing TCP traffic on ports 80 and 443 from Internet.');
66+
67+
/**
68+
* Note that the default value of priority for the firewall API is 1000.
69+
* If you check the value of its priority at this point it will be
70+
* equal to 0, however it is not treated as "set" by the library and thus
71+
* the default will be applied to the new rule. If you want to create a rule
72+
* that has priority == 0, you need to explicitly set it so:
73+
*
74+
* $firewallResource->setPriority(0);
75+
*/
76+
77+
//Create the firewall rule using Firewalls Client.
78+
$operation = $firewallsClient->insert($firewallResource, $projectId);
79+
80+
// Wait for the create operation to complete using a custom helper function.
81+
// @see src/wait_for_operation.php
82+
$operation = wait_for_operation($operation, $projectId);
83+
if (empty($operation->getError())) {
84+
printf('Created rule %s.' . PHP_EOL, $firewallRuleName);
85+
} else {
86+
printf('Firewall rule creation failed!' . PHP_EOL);
87+
}
88+
}
89+
# [END compute_firewall_create]
90+
91+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
92+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

compute/cloud-client/instances/src/create_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function create_instance(
8585

8686
// Wait for the create operation to complete using a custom helper function.
8787
// @see src/wait_for_operation.php
88-
$operation = wait_for_operation($operation, $projectId, $zone);
88+
$operation = wait_for_operation($operation, $projectId);
8989
if (empty($operation->getError())) {
9090
printf('Created instance %s' . PHP_EOL, $instanceName);
9191
} else {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
include_once 'wait_for_operation.php';
27+
28+
# [START compute_firewall_delete]
29+
use Google\Cloud\Compute\V1\FirewallsClient;
30+
31+
/**
32+
* Delete a firewall rule from the specified project.
33+
*
34+
* Example:
35+
* ```
36+
* delete_firewall_rule($projectId, $firewallRuleName);
37+
* ```
38+
*
39+
* @param string $projectId Project ID or project number of the Cloud project you want to delete a rule for.
40+
* @param string $firewallRuleName Name of the rule that is deleted.
41+
*
42+
* @throws \Google\ApiCore\ApiException if the remote call fails.
43+
*/
44+
function delete_firewall_rule(string $projectId, string $firewallRuleName)
45+
{
46+
$firewallsClient = new FirewallsClient();
47+
48+
// Delete the firewall rule using Firewalls Client.
49+
$operation = $firewallsClient->delete($firewallRuleName, $projectId);
50+
51+
// Wait for the create operation to complete using a custom helper function.
52+
// @see src/wait_for_operation.php
53+
$operation = wait_for_operation($operation, $projectId);
54+
if (empty($operation->getError())) {
55+
printf('Rule %s deleted successfully!' . PHP_EOL, $firewallRuleName);
56+
} else {
57+
print('Deletion failed!' . PHP_EOL);
58+
}
59+
}
60+
# [END compute_firewall_delete]
61+
62+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
63+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

compute/cloud-client/instances/src/delete_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function delete_instance(
5252

5353
// Wait for the create operation to complete using a custom helper function.
5454
// @see src/wait_for_operation.php
55-
$operation = wait_for_operation($operation, $projectId, $zone);
55+
$operation = wait_for_operation($operation, $projectId);
5656
if (empty($operation->getError())) {
5757
printf('Deleted instance %s' . PHP_EOL, $instanceName);
5858
} else {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
# [START compute_firewall_list]
27+
use Google\Cloud\Compute\V1\FirewallsClient;
28+
29+
/**
30+
* Return a list of all the firewall rules in specified project. Also prints the
31+
* list of firewall names and their descriptions.
32+
* Example:
33+
* ```
34+
* list_firewall_rules($projectId);
35+
* ```
36+
*
37+
* @param string $projectId Project ID or project number of the Cloud project you want to list rules from.
38+
*
39+
* @throws \Google\ApiCore\ApiException if the remote call fails.
40+
*/
41+
function list_firewall_rules(string $projectId)
42+
{
43+
// List all firewall rules defined for the project using Firewalls Client.
44+
$firewallClient = new FirewallsClient();
45+
$firewallList = $firewallClient->list($projectId);
46+
47+
print('--- Firewall Rules ---' . PHP_EOL);
48+
foreach ($firewallList->iterateAllElements() as $firewall) {
49+
printf(' - %s : %s : %s' . PHP_EOL, $firewall->getName(), $firewall->getDescription(), $firewall->getNetwork());
50+
}
51+
}
52+
# [END compute_firewall_list]
53+
54+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
include_once 'wait_for_operation.php';
27+
28+
# [START compute_firewall_patch]
29+
use Google\Cloud\Compute\V1\FirewallsClient;
30+
use Google\Cloud\Compute\V1\Firewall;
31+
32+
/**
33+
* Modifies the priority of a given firewall rule.
34+
*
35+
* Example:
36+
* ```
37+
* patch_firewall_priority($projectId, $firewallRuleName, $priority);
38+
* ```
39+
*
40+
* @param string $projectId Project ID or project number of the Cloud project you want to patch a rule from.
41+
* @param string $firewallRuleName Name of the rule that you want to modify.
42+
* @param int $priority The new priority to be set for the rule.
43+
*
44+
* @throws \Google\ApiCore\ApiException if the remote call fails.
45+
*/
46+
function patch_firewall_priority(string $projectId, string $firewallRuleName, int $priority)
47+
{
48+
$firewallsClient = new FirewallsClient();
49+
$firewallResource = (new Firewall())->setPriority($priority);
50+
51+
// The patch operation doesn't require the full definition of a Firewall object. It will only update
52+
// the values that were set in it, in this case it will only change the priority.
53+
$operation = $firewallsClient->patch($firewallRuleName, $firewallResource, $projectId);
54+
55+
// Wait for the create operation to complete using a custom helper function.
56+
// @see src/wait_for_operation.php
57+
$operation = wait_for_operation($operation, $projectId);
58+
if (empty($operation->getError())) {
59+
printf('Patched %s priority to %d.' . PHP_EOL, $firewallRuleName, $priority);
60+
} else {
61+
print('Patching failed!' . PHP_EOL);
62+
}
63+
}
64+
# [END compute_firewall_patch]
65+
66+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
use Google\Cloud\Compute\V1\FirewallsClient;
27+
use Google\Cloud\Compute\V1\Firewall\Direction;
28+
29+
/**
30+
* Prints details about a particular firewall rule in the specified project
31+
*
32+
* Example:
33+
* ```
34+
* print_firewall_rule($projectId, $firewallRuleName);
35+
* ```
36+
*
37+
* @param string $projectId Project ID or project number of the Cloud project you want to print a rule from.
38+
* @param string $firewallRuleName Unique name for the firewall rule.
39+
*
40+
* @throws \Google\ApiCore\ApiException if the remote call fails.
41+
*/
42+
function print_firewall_rule(string $projectId, string $firewallRuleName)
43+
{
44+
// Get details of a firewall rule defined for the project using Firewalls Client.
45+
$firewallClient = new FirewallsClient();
46+
$response = $firewallClient->get($firewallRuleName, $projectId);
47+
$direction = $response->getDirection();
48+
printf('ID: %s' . PHP_EOL, $response->getID());
49+
printf('Kind: %s' . PHP_EOL, $response->getKind());
50+
printf('Name: %s' . PHP_EOL, $response->getName());
51+
printf('Creation Time: %s' . PHP_EOL, $response->getCreationTimestamp());
52+
if ($direction = Direction::INGRESS) {
53+
print('Direction: INGRESS' . PHP_EOL);
54+
} else {
55+
print('Direction: EGRESS' . PHP_EOL);
56+
}
57+
printf('Network: %s' . PHP_EOL, $response->getNetwork());
58+
printf('Disabled: %s' . PHP_EOL, var_export($response->getDisabled(), true));
59+
printf('Priority: %s' . PHP_EOL, $response->getPriority());
60+
printf('Self Link: %s' . PHP_EOL, $response->getSelfLink());
61+
printf('Logging Enabled: %s' . PHP_EOL, var_export($response->getLogConfig()->getEnable(), true));
62+
print('--Allowed--' . PHP_EOL);
63+
foreach ($response->getAllowed() as $item) {
64+
printf('Protocol: %s' . PHP_EOL, $item->getIPProtocol());
65+
foreach ($item->getPorts()as $ports) {
66+
printf(' - Ports: %s' . PHP_EOL, $ports);
67+
}
68+
}
69+
print('--Source Ranges--' . PHP_EOL);
70+
foreach ($response->getSourceRanges()as $ranges) {
71+
printf(' - Range: %s' . PHP_EOL, $ranges);
72+
}
73+
}
74+
75+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)