Skip to content

Commit

Permalink
feat: compute_disk_start/stop_replication
Browse files Browse the repository at this point in the history
  • Loading branch information
Joanna Grycz committed Dec 4, 2024
1 parent a250786 commit ded92ee
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 9 deletions.
15 changes: 11 additions & 4 deletions compute/disks/createCustomSecondaryDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function main(
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// If you want to create regional disk, you should use: RegionDisksClient and RegionOperationsClient.
// Instantiate a diskClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zoneOperationsClient
Expand All @@ -38,17 +39,19 @@ async function main(
// The project for the secondary disk.
const secondaryProjectId = await disksClient.getProjectId();

// The zone for the secondary disk. The primary and secondary disks must be in different regions.
// secondaryLocation = 'europe-west4-a';
// The zone or region for the secondary disk. The primary and secondary disks must be in different regions.
// If you use RegionDisksClient- define region, if DisksClient- define zone.
// secondaryLocation = 'us-central1-a';

// The name of the secondary disk.
// secondaryDiskName = 'secondary-disk-name';

// The project that contains the primary disk.
const primaryProjectId = await disksClient.getProjectId();

// The zone for the primary disk.
// primaryLocation = 'europe-central2-b';
// The zone or region for the primary disk.
// If you use RegionDisksClient- define region, if DisksClient- define zone.
// primaryLocation = 'us-central1-b';

// The name of the primary disk that the secondary disk receives data from.
// primaryDiskName = 'primary-disk-name';
Expand All @@ -65,6 +68,7 @@ async function main(
const disk = new compute.Disk({
sizeGb: diskSizeGb,
name: secondaryDiskName,
// If you use RegionDisksClient, pass region as an argument instead of zone
zone: secondaryLocation,
type: diskType,
asyncPrimaryDisk: new compute.DiskAsyncReplication({
Expand All @@ -73,6 +77,7 @@ async function main(
disk: `projects/${primaryProjectId}/zones/${primaryLocation}/disks/${primaryDiskName}`,
}),
// Specify additional guest OS features.
// To learn more about OS features, open: `https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#secondary2`.
// You don't need to include the guest OS features of the primary disk.
// The secondary disk automatically inherits the guest OS features of the primary disk.
guestOsFeatures: [
Expand All @@ -90,6 +95,7 @@ async function main(

const [response] = await disksClient.insert({
project: secondaryProjectId,
// If you use RegionDisksClient, pass region as an argument instead of zone
zone: secondaryLocation,
diskResource: disk,
});
Expand All @@ -101,6 +107,7 @@ async function main(
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: secondaryProjectId,
// If you use RegionOperationsClient, pass region as an argument instead of zone
zone: operation.zone.split('/').pop(),
});
}
Expand Down
4 changes: 2 additions & 2 deletions compute/disks/createRegionalSecondaryDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main(
const secondaryProjectId = await regionDisksClient.getProjectId();

// The region for the secondary disk.
// secondaryLocation = 'europe-west4';
// secondaryLocation = 'us-central1';

// The name of the secondary disk.
// secondaryDiskName = 'secondary-disk-name';
Expand All @@ -48,7 +48,7 @@ async function main(
const primaryProjectId = await regionDisksClient.getProjectId();

// The region for the primary disk.
// primaryLocation = 'europe-central2';
// primaryLocation = 'us-central2';

// The name of the primary disk that the secondary disk receives data from.
// primaryDiskName = 'primary-disk-name';
Expand Down
4 changes: 2 additions & 2 deletions compute/disks/createZonalSecondaryDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main(
const secondaryProjectId = await disksClient.getProjectId();

// The zone for the secondary disk. The primary and secondary disks must be in different regions.
// secondaryLocation = 'europe-west4-a';
// secondaryLocation = 'us-central1-a';

// The name of the secondary disk.
// secondaryDiskName = 'secondary-disk-name';
Expand All @@ -48,7 +48,7 @@ async function main(
const primaryProjectId = await disksClient.getProjectId();

// The zone for the primary disk.
// primaryLocation = 'europe-central2-b';
// primaryLocation = 'us-central1-b';

// The name of the primary disk that the secondary disk receives data from.
// primaryDiskName = 'primary-disk-name';
Expand Down
91 changes: 91 additions & 0 deletions compute/disks/startReplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

async function main(
secondaryDiskName,
secondaryLocation,
primaryDiskName,
primaryLocation
) {
// [START compute_disk_start_replication]
// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// Instantiate a diskClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zoneOperationsClient
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

/**
* TODO(developer): Update/uncomment these variables before running the sample.
*/
// The project of the secondary disk.
const secondaryProjectId = await disksClient.getProjectId();

// The zone of the secondary disk.
// secondaryLocation = 'us-central1-a';

// The name of the secondary disk.
// secondaryDiskName = 'secondary-disk-name';

// The project of the primary disk.
const primaryProjectId = await disksClient.getProjectId();

// The zone of the primary disk.
// primaryLocation = 'us-central1-a';

// The name of the primary disk.
// primaryDiskName = 'primary-disk-name';

// Start replication
async function callStartReplication() {
const [response] = await disksClient.startAsyncReplication({
project: secondaryProjectId,
zone: primaryLocation,
disk: primaryDiskName,
disksStartAsyncReplicationRequestResource:
new compute.DisksStartAsyncReplicationRequest({
asyncSecondaryDisk: `projects/${primaryProjectId}/zones/${secondaryLocation}/disks/${secondaryDiskName}`,
}),
});

let operation = response.latestResponse;

// Wait for the operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: secondaryProjectId,
zone: operation.zone.split('/').pop(),
});
}

console.log(
`Data replication from primary disk: ${primaryDiskName} to secondary disk: ${secondaryDiskName} started.`
);
}

await callStartReplication();
// [END compute_disk_start_replication]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
70 changes: 70 additions & 0 deletions compute/disks/stopReplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

async function main(primaryDiskName, primaryLocation) {
// [START compute_disk_stop_replication]
// Import the Compute library
const computeLib = require('@google-cloud/compute');

// Instantiate a diskClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zoneOperationsClient
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

/**
* TODO(developer): Update/uncomment these variables before running the sample.
*/
// The project that contains the primary disk.
const primaryProjectId = await disksClient.getProjectId();

// The zone of the primary disk.
// primaryLocation = 'us-central1-a';

// The name of the primary disk.
// primaryDiskName = 'primary-disk-name';

// Stop replication
async function callStopReplication() {
const [response] = await disksClient.stopAsyncReplication({
project: primaryProjectId,
zone: primaryLocation,
disk: primaryDiskName,
});

let operation = response.latestResponse;

// Wait for the operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: primaryProjectId,
zone: operation.zone.split('/').pop(),
});
}

console.log(`Replication for primary disk: ${primaryDiskName} stopped.`);
}

await callStopReplication();
// [END compute_disk_stop_replication]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function createDisk(diskName, zone) {
console.log(`Disk: ${diskName} created.`);
}

describe('Create compute zonal secondary disk', async () => {
describe('Compute zonal secondary disk', async () => {
const prefix = 'zonal-disk';
const secondaryDiskName = `${prefix}-secondary-${uuid.v4()}`;
const primaryDiskName = `${prefix}-primary-${uuid.v4()}`;
Expand Down Expand Up @@ -86,4 +86,34 @@ describe('Create compute zonal secondary disk', async () => {

assert(response.includes(`Secondary disk: ${secondaryDiskName} created.`));
});

it('should start replication', () => {
const response = execSync(
`node ./disks/startReplication.js ${secondaryDiskName} ${secondaryZone} ${primaryDiskName} ${primaryZone}`,
{
cwd,
}
);

assert(
response.includes(
`Data replication from primary disk: ${primaryDiskName} to secondary disk: ${secondaryDiskName} started.`
)
);
});

it('should stop replication', () => {
const response = execSync(
`node ./disks/stopReplication.js ${primaryDiskName} ${primaryZone}`,
{
cwd,
}
);

assert(
response.includes(
`Replication for primary disk: ${primaryDiskName} stopped.`
)
);
});
});

0 comments on commit ded92ee

Please sign in to comment.