|
| 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)); |
0 commit comments