* Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet. * * @param {string} projectId - project ID or project number of the Cloud project you want to use. * @param {string} firewallRuleName - name of the rule that is created. * @param {string} netwo
( projectId, firewallRuleName, networkName = 'global/networks/default' )
| 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 computeProtos = 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 computeProtos.Firewall(); |
| 46 | firewallRule.name = firewallRuleName; |
| 47 | firewallRule.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 | } |
no test coverage detected