Skip to content

Commit 792790d

Browse files
authored
chore(docs): firewall samples
1 parent 1b88fca commit 792790d

File tree

5 files changed

+319
-0
lines changed

5 files changed

+319
-0
lines changed
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.
17+
*
18+
* @param {string} projectId - project ID or project number of the Cloud project you want to use.
19+
* @param {string} firewallRuleName - name of the rule that is created.
20+
* @param {string} network - name of the network the rule will be applied to. Available name formats:
21+
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
22+
* projects/{project_id}/global/networks/{network}
23+
* global/networks/{network}
24+
*/
25+
function main(
26+
projectId,
27+
firewallRuleName,
28+
networkName = 'global/networks/default'
29+
) {
30+
// [START compute_firewall_create]
31+
/**
32+
* TODO(developer): Uncomment and replace these variables before running the sample.
33+
*/
34+
// const projectId = 'YOUR_PROJECT_ID';
35+
// const firewallRuleName = 'YOUR_FIREWALL_RULE_NAME'
36+
// const networkName = 'global/networks/default'
37+
38+
const compute = require('@google-cloud/compute');
39+
const compute_protos = compute.protos.google.cloud.compute.v1;
40+
41+
async function createFirewallRule() {
42+
const firewallsClient = new compute.FirewallsClient();
43+
const operationsClient = new compute.GlobalOperationsClient();
44+
45+
const firewallRule = new compute_protos.Firewall();
46+
firewallRule.name = firewallRuleName;
47+
firewallRule.direction = compute_protos.Firewall.Direction.INGRESS;
48+
firewallRule.allowed = [
49+
{
50+
IPProtocol: 'tcp',
51+
ports: ['80', '443'],
52+
},
53+
];
54+
firewallRule.targetTags = ['web'];
55+
firewallRule.network = networkName;
56+
firewallRule.description =
57+
'Allowing TCP traffic on port 80 and 443 from Internet.';
58+
59+
// Note that the default value of priority for the firewall API is 1000.
60+
// If you check the value of `firewallRule.priority` at this point it
61+
// will be equal to null, however it is not treated as "set" by the library and thus
62+
// the default will be applied to the new rule. If you want to create a rule that
63+
// has priority == 0, you need to explicitly set it so:
64+
65+
// firewallRule.priority = 0
66+
67+
const [response] = await firewallsClient.insert({
68+
project: projectId,
69+
firewallResource: firewallRule,
70+
});
71+
let operation = response.latestResponse;
72+
73+
// Wait for the create operation to complete.
74+
while (operation.status !== 'DONE') {
75+
[operation] = await operationsClient.wait({
76+
operation: operation.name,
77+
project: projectId,
78+
});
79+
}
80+
81+
console.log('Firewall rule created');
82+
}
83+
84+
createFirewallRule();
85+
// [END compute_firewall_create]
86+
}
87+
88+
main(...process.argv.slice(2));
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Deletes a firewall rule from the project.
17+
*
18+
* @param {string} projectId - project ID or project number of the Cloud project you want to use.
19+
* @param {string} firewallRuleName - name of the rule you want to modify.
20+
*/
21+
function main(projectId, firewallRuleName) {
22+
// [START compute_firewall_delete]
23+
/**
24+
* TODO(developer): Uncomment and replace these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const firewallRuleName = 'FIREWALL_RULE_NAME';
28+
29+
const compute = require('@google-cloud/compute');
30+
31+
async function deleteFirewallRule() {
32+
const firewallsClient = new compute.FirewallsClient();
33+
const operationsClient = new compute.GlobalOperationsClient();
34+
35+
const [response] = await firewallsClient.delete({
36+
project: projectId,
37+
firewall: firewallRuleName,
38+
});
39+
let operation = response.latestResponse;
40+
41+
// Wait for the create operation to complete.
42+
while (operation.status !== 'DONE') {
43+
[operation] = await operationsClient.wait({
44+
operation: operation.name,
45+
project: projectId,
46+
});
47+
}
48+
49+
console.log('Firewall rule deleted');
50+
}
51+
52+
deleteFirewallRule();
53+
// [END compute_firewall_delete]
54+
}
55+
56+
main(...process.argv.slice(2));

compute/firewall/listFirewallRules.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Prints a list of all firewall rules in specified project.
17+
*
18+
* @param {string} projectId - project ID or project number of the Cloud project you want to use.
19+
*/
20+
function main(projectId) {
21+
// [START compute_firewall_list]
22+
/**
23+
* TODO(developer): Uncomment and replace these variables before running the sample.
24+
*/
25+
// const projectId = 'YOUR_PROJECT_ID';
26+
27+
const compute = require('@google-cloud/compute');
28+
29+
async function listFirewallRules() {
30+
const firewallsClient = new compute.FirewallsClient();
31+
32+
const [firewallRules] = await firewallsClient.list({
33+
project: projectId,
34+
});
35+
36+
for (const rule of firewallRules) {
37+
console.log(` - ${rule.name}: ${rule.description}`);
38+
}
39+
}
40+
41+
listFirewallRules();
42+
// [END compute_firewall_list]
43+
}
44+
45+
main(...process.argv.slice(2));
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Modifies the priority of a given firewall rule.
17+
*
18+
* @param {string} projectId - project ID or project number of the Cloud project you want to use.
19+
* @param {string} firewallRuleName - name of the rule you want to modify.
20+
* @param {number} priority - the new priority to be set for the rule.
21+
*/
22+
function main(projectId, firewallRuleName, priority = 10) {
23+
// [START compute_firewall_patch]
24+
/**
25+
* TODO(developer): Uncomment and replace these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const firewallRuleName = 'FIREWALL_RULE_NAME';
29+
// const priority = 10;
30+
31+
const compute = require('@google-cloud/compute');
32+
const compute_protos = compute.protos.google.cloud.compute.v1;
33+
34+
async function patchFirewallPriority() {
35+
const firewallsClient = new compute.FirewallsClient();
36+
const operationsClient = new compute.GlobalOperationsClient();
37+
38+
const firewallRule = new compute_protos.Firewall();
39+
firewallRule.priority = priority;
40+
41+
// The patch operation doesn't require the full definition of a Firewall object. It will only update
42+
// the values that were set in it, in this case it will only change the priority.
43+
const [response] = await firewallsClient.patch({
44+
project: projectId,
45+
firewall: firewallRuleName,
46+
firewallResource: firewallRule,
47+
});
48+
let operation = response.latestResponse;
49+
50+
// Wait for the create operation to complete.
51+
while (operation.status !== 'DONE') {
52+
[operation] = await operationsClient.wait({
53+
operation: operation.name,
54+
project: projectId,
55+
});
56+
}
57+
58+
console.log('Firewall rule updated');
59+
}
60+
61+
patchFirewallPriority();
62+
// [END compute_firewall_patch]
63+
}
64+
65+
main(...process.argv.slice(2));

compute/test/samples.test.js

+65
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {assert} = require('chai');
2525

2626
const instancesClient = new compute.InstancesClient({fallback: 'rest'});
2727
const projectsClient = new compute.ProjectsClient({fallback: 'rest'});
28+
const firewallsClient = new compute.FirewallsClient({fallback: 'rest'});
2829

2930
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3031

@@ -313,4 +314,68 @@ describe('samples', () => {
313314
execSync(`node deleteInstance ${projectId} ${zone} ${newInstanceName}`);
314315
});
315316
});
317+
318+
describe('firewall', () => {
319+
it('should create and delete firewall rule', async () => {
320+
const projectId = await instancesClient.getProjectId();
321+
const firewallRuleName = `test-firewall-rule-${uuid.v4().split('-')[0]}`;
322+
323+
let output = execSync(
324+
`node firewall/createFirewallRule ${projectId} ${firewallRuleName}`
325+
);
326+
assert.match(output, /Firewall rule created/);
327+
328+
output = execSync(
329+
`node firewall/deleteFirewallRule ${projectId} ${firewallRuleName}`
330+
);
331+
assert.match(output, /Firewall rule deleted/);
332+
});
333+
334+
it('should list firewall rules', async () => {
335+
const projectId = await instancesClient.getProjectId();
336+
const firewallRuleName = `test-firewall-rule-${uuid.v4().split('-')[0]}`;
337+
338+
execSync(
339+
`node firewall/createFirewallRule ${projectId} ${firewallRuleName}`
340+
);
341+
const output = execSync(`node firewall/listFirewallRules ${projectId}`);
342+
assert.isTrue(output.includes(`- ${firewallRuleName}:`));
343+
344+
execSync(
345+
`node firewall/deleteFirewallRule ${projectId} ${firewallRuleName}`
346+
);
347+
});
348+
349+
it('should patch firewall rule', async () => {
350+
const projectId = await instancesClient.getProjectId();
351+
const firewallRuleName = `test-firewall-rule-${uuid.v4().split('-')[0]}`;
352+
353+
execSync(
354+
`node firewall/createFirewallRule ${projectId} ${firewallRuleName}`
355+
);
356+
357+
let [firewallRule] = await firewallsClient.get({
358+
project: projectId,
359+
firewall: firewallRuleName,
360+
});
361+
362+
assert.equal(firewallRule.priority, 1000);
363+
364+
const output = execSync(
365+
`node firewall/patchFirewallPriority ${projectId} ${firewallRuleName} 500`
366+
);
367+
assert.match(output, /Firewall rule updated/);
368+
369+
[firewallRule] = await firewallsClient.get({
370+
project: projectId,
371+
firewall: firewallRuleName,
372+
});
373+
374+
assert.equal(firewallRule.priority, 500);
375+
376+
execSync(
377+
`node firewall/deleteFirewallRule ${projectId} ${firewallRuleName}`
378+
);
379+
});
380+
});
316381
});

0 commit comments

Comments
 (0)