Skip to content

Commit

Permalink
Merge branch 'main' into tpu_queued_resources_startup_script
Browse files Browse the repository at this point in the history
  • Loading branch information
iennae authored Nov 21, 2024
2 parents 9a724f9 + 04a1f0d commit 27d5949
Show file tree
Hide file tree
Showing 6 changed files with 556 additions and 0 deletions.
94 changes: 94 additions & 0 deletions compute/disks/consistencyGroups/consistencyGroupAddDisk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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(
consistencyGroupName,
consistencyGroupLocation,
diskName,
diskLocation
) {
// [START compute_consistency_group_add_disk]
// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// If you want to add regional disk,
// you should use: RegionDisksClient and RegionOperationsClient.
// Instantiate a disksClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zone
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

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

// The name of the disk.
// diskName = 'disk-name';

// If you use RegionDisksClient- define region, if DisksClient- define zone.
// The zone or region of the disk.
// diskLocation = 'europe-central2-a';

// The name of the consistency group.
// consistencyGroupName = 'consistency-group-name';

// The region of the consistency group.
// consistencyGroupLocation = 'europe-central2';

async function callAddDiskToConsistencyGroup() {
const [response] = await disksClient.addResourcePolicies({
disk: diskName,
project: projectId,
// If you use RegionDisksClient, pass region as an argument instead of zone.
zone: diskLocation,
disksAddResourcePoliciesRequestResource:
new compute.DisksAddResourcePoliciesRequest({
resourcePolicies: [
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
],
}),
});

let operation = response.latestResponse;

// Wait for the add disk operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: projectId,
// If you use RegionDisksClient, pass region as an argument instead of zone.
zone: operation.zone.split('/').pop(),
});
}

console.log(
`Disk: ${diskName} added to consistency group: ${consistencyGroupName}.`
);
}

await callAddDiskToConsistencyGroup();
// [END compute_consistency_group_add_disk]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
72 changes: 72 additions & 0 deletions compute/disks/consistencyGroups/consistencyGroupDisksList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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(
consistencyGroupName,
consistencyGroupLocation,
disksLocation
) {
// [START compute_consistency_group_disks_list]
// Import the Compute library
const computeLib = require('@google-cloud/compute');

// If you want to get regional disks, you should use: RegionDisksClient.
// Instantiate a disksClient
const disksClient = new computeLib.DisksClient();

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

// If you use RegionDisksClient- define region, if DisksClient- define zone.
// The zone or region of the disks.
// disksLocation = 'europe-central2-a';

// The name of the consistency group.
// consistencyGroupName = 'consistency-group-name';

// The region of the consistency group.
// consistencyGroupLocation = 'europe-central2';

async function callConsistencyGroupDisksList() {
const filter = `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`;

const [response] = await disksClient.list({
project: projectId,
// If you use RegionDisksClient, pass region as an argument instead of zone.
zone: disksLocation,
});

// Filtering must be done manually for now, since list filtering inside disksClient.list is not supported yet.
const filteredDisks = response.filter(disk =>
disk.resourcePolicies.includes(filter)
);

console.log(JSON.stringify(filteredDisks));
}

await callConsistencyGroupDisksList();
// [END compute_consistency_group_disks_list]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
94 changes: 94 additions & 0 deletions compute/disks/consistencyGroups/consistencyGroupRemoveDisk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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(
consistencyGroupName,
consistencyGroupLocation,
diskName,
diskLocation
) {
// [START compute_consistency_group_remove_disk]
// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// If you want to remove regional disk,
// you should use: RegionDisksClient and RegionOperationsClient.
// Instantiate a disksClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zone
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

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

// The name of the disk.
// diskName = 'disk-name';

// If you use RegionDisksClient- define region, if DisksClient- define zone.
// The zone or region of the disk.
// diskLocation = 'europe-central2-a';

// The name of the consistency group.
// consistencyGroupName = 'consistency-group-name';

// The region of the consistency group.
// consistencyGroupLocation = 'europe-central2';

async function callDeleteDiskFromConsistencyGroup() {
const [response] = await disksClient.removeResourcePolicies({
disk: diskName,
project: projectId,
// If you use RegionDisksClient, pass region as an argument instead of zone.
zone: diskLocation,
disksRemoveResourcePoliciesRequestResource:
new compute.DisksRemoveResourcePoliciesRequest({
resourcePolicies: [
`https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${consistencyGroupLocation}/resourcePolicies/${consistencyGroupName}`,
],
}),
});

let operation = response.latestResponse;

// Wait for the delete disk operation to complete.
while (operation.status !== 'DONE') {
[operation] = await zoneOperationsClient.wait({
operation: operation.name,
project: projectId,
// If you use RegionDisksClient, pass region as an argument instead of zone.
zone: operation.zone.split('/').pop(),
});
}

console.log(
`Disk: ${diskName} deleted from consistency group: ${consistencyGroupName}.`
);
}

await callDeleteDiskFromConsistencyGroup();
// [END compute_consistency_group_remove_disk]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
79 changes: 79 additions & 0 deletions compute/disks/consistencyGroups/createConsistencyGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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(consistencyGroupName, region) {
// [START compute_consistency_group_create]
// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// Instantiate a resourcePoliciesClient
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
// Instantiate a regionOperationsClient
const regionOperationsClient = new computeLib.RegionOperationsClient();

/**
* TODO(developer): Update/uncomment these variables before running the sample.
*/
// The project that contains the consistency group.
const projectId = await resourcePoliciesClient.getProjectId();

// The region for the consistency group.
// If you want to add primary disks to consistency group, use the same region as the primary disks.
// If you want to add secondary disks to the consistency group, use the same region as the secondary disks.
// region = 'europe-central2';

// The name for consistency group.
// consistencyGroupName = 'consistency-group-name';

async function callCreateConsistencyGroup() {
// Create a resourcePolicyResource
const resourcePolicyResource = new compute.ResourcePolicy({
diskConsistencyGroupPolicy:
new compute.ResourcePolicyDiskConsistencyGroupPolicy({}),
name: consistencyGroupName,
});

const [response] = await resourcePoliciesClient.insert({
project: projectId,
region,
resourcePolicyResource,
});

let operation = response.latestResponse;

// Wait for the create group operation to complete.
while (operation.status !== 'DONE') {
[operation] = await regionOperationsClient.wait({
operation: operation.name,
project: projectId,
region,
});
}

console.log(`Consistency group: ${consistencyGroupName} created.`);
}

await callCreateConsistencyGroup();
// [END compute_consistency_group_create]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
Loading

0 comments on commit 27d5949

Please sign in to comment.