Skip to content

Instantly share code, notes, and snippets.

@stephenplusplus
Created November 30, 2020 23:37
Show Gist options
  • Save stephenplusplus/87b66993a0627e114dc296bf6ea132f5 to your computer and use it in GitHub Desktop.
Save stephenplusplus/87b66993a0627e114dc296bf6ea132f5 to your computer and use it in GitHub Desktop.
compute-pr-522
diff --git a/system-test/compute.js b/system-test/compute.js
index 5dbc948..daefd4b 100644
--- a/system-test/compute.js
+++ b/system-test/compute.js
@@ -20,7 +20,6 @@ const uuid = require('uuid');
const {promisify} = require('util');
const Compute = require('../');
-const {execSync} = require('child_process');
describe('Compute', () => {
// Since the Compute Engine API is rather large and involves long-running
@@ -692,46 +691,88 @@ describe('Compute', () => {
});
});
- describe.skip('rules', () => {
+ describe('rules', () => {
const RULE_NAME = generateName('rule');
const NETWORK_NAME = generateName('network');
const SUBNETWORK_NAME = generateName('subnetwork');
const BACKEND_SERVICE_NAME = generateName('backend-service');
+ let RULE;
+ let SUBNETWORK;
+
before(async () => {
- execSync(
- `gcloud compute networks create ${NETWORK_NAME} --subnet-mode=custom`
- );
- execSync(
- `gcloud compute networks subnets create ${SUBNETWORK_NAME} --network=${NETWORK_NAME} --range=10.0.1.0/24 --region=us-central1`
- );
- execSync(
- `gcloud compute backend-services create ${BACKEND_SERVICE_NAME} --load-balancing-scheme=INTERNAL --region=us-central1`
+ const projectId = await compute.authClient.getProjectId();
+ const resourceUrlPrefix = `https://www.googleapis.com/compute/v1/projects/${projectId}`;
+
+ const [network, networkOperation] = await compute.createNetwork(
+ NETWORK_NAME,
+ {
+ autoCreateSubnetworks: false,
+ }
);
- execSync(
- `gcloud compute forwarding-rules create ${RULE_NAME} --load-balancing-scheme=INTERNAL --backend-service=${BACKEND_SERVICE_NAME} --subnet=${SUBNETWORK_NAME} --network=${NETWORK_NAME} --region=us-central1 --ports=80-82`
+ await networkOperation.promise();
+
+ const [subnetwork, subnetworkOperation] = await network.createSubnetwork(
+ SUBNETWORK_NAME,
+ {
+ region: 'us-central1',
+ range: '10.0.1.0/24',
+ }
);
+ SUBNETWORK = subnetwork;
+ await subnetworkOperation.promise();
+
+ const region = compute.region('us-central1');
+
+ const resp = await computeRequest({
+ method: 'POST',
+ uri: '/regions/us-central1/backendServices',
+ json: {
+ name: BACKEND_SERVICE_NAME,
+ loadBalancingScheme: 'INTERNAL',
+ },
+ });
+ await region.operation(resp.name).promise();
+
+ const [rule, ruleOperation] = await region.createRule(RULE_NAME, {
+ loadBalancingScheme: 'INTERNAL',
+ backendService: `${resourceUrlPrefix}/regions/us-central1/backendServices/${BACKEND_SERVICE_NAME}`,
+ subnetwork: `${resourceUrlPrefix}/regions/us-central1/subnetworks/${SUBNETWORK_NAME}`,
+ network: `${resourceUrlPrefix}/global/networks/${NETWORK_NAME}`,
+ ports: ['80', '81', '82'],
+ });
+ RULE = rule;
+ await ruleOperation.promise();
});
after(async () => {
const [firewalls] = await compute.getFirewalls();
- const firewallsToDelete = firewalls
- .filter(x => x.name.includes('network-name'))
- .map(y => y.name);
+ const firewallsToDelete = firewalls.filter(x =>
+ x.name.startsWith(TESTS_PREFIX)
+ );
+
for (const firewall of firewallsToDelete) {
- await deleteFirewallRule(firewall);
+ await firewall.delete();
}
- await deleteForwardingRules(RULE_NAME);
- await deleteBackendService(BACKEND_SERVICE_NAME);
- await deleteSubnetworks(SUBNETWORK_NAME);
+
+ const [ruleOperation] = await RULE.delete();
+ await ruleOperation.promise();
+
+ await computeRequest({
+ method: 'DELETE',
+ uri: 'regions/us-central1/backendServices/' + BACKEND_SERVICE_NAME,
+ });
+
+ const [subnetworkOperation] = await SUBNETWORK.delete();
+ await subnetworkOperation.promise();
});
it('should have created the right rule', async () => {
- const rule = await getForwardingRules(RULE_NAME);
- assert.strictEqual(rule.name, RULE_NAME);
- assert.strictEqual(rule.IPProtocol, 'TCP');
- assert.deepStrictEqual(rule.ports, ['80', '81', '82']);
- assert.strictEqual(rule.loadBalancingScheme, 'INTERNAL');
+ const [metadata] = await RULE.getMetadata();
+ assert.strictEqual(metadata.name, RULE_NAME);
+ assert.strictEqual(metadata.IPProtocol, 'TCP');
+ assert.deepStrictEqual(metadata.ports, ['80', '81', '82']);
+ assert.strictEqual(metadata.loadBalancingScheme, 'INTERNAL');
});
});
@@ -1145,12 +1186,18 @@ describe('Compute', () => {
if (objects.length === 0) {
return Promise.resolve();
}
- console.log(`deleting ${objects.length} ${type}...`);
+ console.log(`Deleting ${objects.length} ${type}...`);
await Promise.all(
objects.map(async o => {
- console.log(`Deleting ${type} '${o.name}'...`);
- await awaitResult(o.delete());
- console.log(`${type} '${o.name}' deleted!`);
+ try {
+ await awaitResult(o.delete());
+ } catch (e) {
+ // Ignore the error, but log.
+ console.log(`\nError!\nError deleting '${o.name}' ${e.message} \n`);
+ return Promise.resolve();
+ }
+
+ console.log(`'${o.name || o.id}' deleted!`);
})
);
}
@@ -1223,27 +1270,6 @@ describe('Compute', () => {
await operation.promise();
}
- async function getForwardingRules(name) {
- return await computeRequest({
- method: 'GET',
- uri: 'regions/us-central1/forwardingRules/' + name,
- });
- }
-
- async function deleteBackendService(name) {
- await computeRequest({
- method: 'DELETE',
- uri: 'regions/us-central1/backendServices/' + name,
- });
- }
-
- async function deleteFirewallRule(name) {
- await computeRequest({
- method: 'DELETE',
- uri: 'global/firewalls/' + name,
- });
- }
-
async function deleteTargetProxies(opts) {
const resp = await computeRequest({
uri: '/global/targetHttpProxies',
@@ -1258,21 +1284,6 @@ describe('Compute', () => {
);
}
- async function deleteSubnetworks(subnetwork) {
- await computeRequest({
- method: 'DELETE',
- uri: 'regions/us-central1/subnetworks/' + subnetwork,
- });
- }
-
- async function deleteForwardingRules(rule) {
- await computeRequest({
- method: 'DELETE',
- uri: 'regions/us-central1/forwardingRules/' + rule,
- });
- console.log(`${rule} deleted!`);
- }
-
async function deleteTargetProxy(name) {
const resp = await computeRequest({
method: 'DELETE',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment