* Set Compute Engine usage export bucket for the Cloud project. * This sample presents how to interpret the default value for the report name prefix parameter. * * @param {string} projectId - ID or number of the project you want to use. * @param {string} bucketName - Google Cloud Storage Bucket
(projectId, bucketName, reportNamePrefix = '')
| 21 | * @param {string} reportNamePrefix - Report Name Prefix which defaults to an empty string to showcase default values behaviour. |
| 22 | */ |
| 23 | function main(projectId, bucketName, reportNamePrefix = '') { |
| 24 | // [START compute_usage_report_set] |
| 25 | /** |
| 26 | * TODO(developer): Uncomment and replace these variables before running the sample. |
| 27 | */ |
| 28 | // const projectId = 'YOUR_PROJECT_ID'; |
| 29 | // const bucketName = 'YOUR_BUCKET_NAME'; |
| 30 | |
| 31 | const compute = require('@google-cloud/compute'); |
| 32 | const computeProtos = compute.protos.google.cloud.compute.v1; |
| 33 | |
| 34 | async function setUsageExportBucket() { |
| 35 | const usageExportLocationResource = new computeProtos.UsageExportLocation(); |
| 36 | usageExportLocationResource.bucketName = bucketName; |
| 37 | usageExportLocationResource.reportNamePrefix = reportNamePrefix; |
| 38 | |
| 39 | if (!reportNamePrefix) { |
| 40 | // Sending an empty value for reportNamePrefix results in the next usage report being generated with the default prefix value "usage_gce". |
| 41 | // (see: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get) |
| 42 | console.log( |
| 43 | 'Setting reportNamePrefix to empty value causes the report to have the default prefix value `usage_gce`.' |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | // Set the usage export location. |
| 48 | const projectsClient = new compute.ProjectsClient(); |
| 49 | const operationsClient = new compute.GlobalOperationsClient(); |
| 50 | |
| 51 | let [operation] = await projectsClient.setUsageExportBucket({ |
| 52 | project: projectId, |
| 53 | usageExportLocationResource, |
| 54 | }); |
| 55 | |
| 56 | while (operation.status !== 'DONE') { |
| 57 | [operation] = await operationsClient.wait({ |
| 58 | operation: operation.name, |
| 59 | project: projectId, |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | setUsageExportBucket(); |
| 65 | // [END compute_usage_report_set] |
| 66 | } |
| 67 | |
| 68 | main(...process.argv.slice(2)); |
no test coverage detected