|
| 1 | +// Copyright 2022 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 | + * Gets a list of preemption operations from given zone in a project. |
| 17 | + * Optionally limit the results to instance name. |
| 18 | + * |
| 19 | + * @param {string} projectId - ID or number of the project you want to use. |
| 20 | + * @param {string} zone - Name of the zone you want to check, for example: us-west3-b. |
| 21 | + * @param {string} instanceName - Name of the virtual machine to look for. |
| 22 | + * @param {string} customFilter - Filter string to be used for this listing operation. |
| 23 | + */ |
| 24 | +function main(projectId, zone, instanceName = '', customFilter = '') { |
| 25 | + // [START compute_preemptible_history] |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment and replace these variables before running the sample. |
| 28 | + */ |
| 29 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 30 | + // const zone = 'europe-central2-b'; |
| 31 | + // const instanceName = 'YOUR_INSTANCE_NAME'; |
| 32 | + // const customFilter = 'operationType="compute.instances.preempted"'; |
| 33 | + |
| 34 | + const compute = require('@google-cloud/compute'); |
| 35 | + |
| 36 | + async function preemptionHistory() { |
| 37 | + const zoneOperationsClient = new compute.ZoneOperationsClient(); |
| 38 | + |
| 39 | + let filter; |
| 40 | + |
| 41 | + if (customFilter !== '') { |
| 42 | + filter = customFilter; |
| 43 | + } else { |
| 44 | + filter = 'operationType="compute.instances.preempted"'; |
| 45 | + |
| 46 | + if (instanceName !== '') { |
| 47 | + filter += ` AND targetLink="https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instances/${instanceName}"`; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const [operationsList] = await zoneOperationsClient.list({ |
| 52 | + project: projectId, |
| 53 | + zone, |
| 54 | + filter, |
| 55 | + }); |
| 56 | + |
| 57 | + for (const operation of operationsList) { |
| 58 | + const thisInstanceName = operation.targetLink.split('/').pop(); |
| 59 | + if (thisInstanceName === instanceName) { |
| 60 | + // The filter used is not 100% accurate, it's `contains` not `equals` |
| 61 | + // So we need to check the name to make sure it's the one we want. |
| 62 | + console.log(`- ${instanceName} ${operation.insertTime}`); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + preemptionHistory(); |
| 68 | + // [END compute_preemptible_history] |
| 69 | +} |
| 70 | + |
| 71 | +process.on('unhandledRejection', err => { |
| 72 | + console.error(err.message); |
| 73 | + process.exitCode = 1; |
| 74 | +}); |
| 75 | + |
| 76 | +main(...process.argv.slice(2)); |
0 commit comments