|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +A sample script showing how to handle default values when communicating |
| 18 | +with the Compute Engine API. |
| 19 | +""" |
| 20 | +# [START compute_instances_verify_default_value] |
| 21 | +# [START compute_usage_report_set] |
| 22 | +# [START compute_usage_report_get] |
| 23 | +# [START compute_usage_report_disable] |
| 24 | +from google.cloud import compute_v1 |
| 25 | +# [END compute_usage_report_disable] |
| 26 | +# [END compute_usage_report_get] |
| 27 | +# [END compute_usage_report_set] |
| 28 | + |
| 29 | + |
| 30 | +# [START compute_usage_report_set] |
| 31 | +def set_usage_export_bucket(project_id: str, bucket_name: str, |
| 32 | + report_name_prefix: str = "") -> None: |
| 33 | + """ |
| 34 | + Set Compute Engine usage export bucket for the Cloud project. |
| 35 | + This sample presents how to interpret the default value for the |
| 36 | + report name prefix parameter. |
| 37 | +
|
| 38 | + Args: |
| 39 | + project_id: project ID or project number of the project to update. |
| 40 | + bucket_name: Google Cloud Storage bucket used to store Compute Engine |
| 41 | + usage reports. An existing Google Cloud Storage bucket is required. |
| 42 | + report_name_prefix: Prefix of the usage report name which defaults to an empty string |
| 43 | + to showcase default values behaviour. |
| 44 | + """ |
| 45 | + usage_export_location = compute_v1.UsageExportLocation( |
| 46 | + bucket_name=bucket_name, |
| 47 | + report_name_prefix=report_name_prefix |
| 48 | + ) |
| 49 | + |
| 50 | + if not report_name_prefix: |
| 51 | + # Sending an empty value for report_name_prefix results in the |
| 52 | + # next usage report being generated with the default prefix value |
| 53 | + # "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket) |
| 54 | + print("Setting report_name_prefix to empty value causes the report " |
| 55 | + "to have the default prefix of `usage_gce`.") |
| 56 | + |
| 57 | + projects_client = compute_v1.ProjectsClient() |
| 58 | + operation = projects_client.set_usage_export_bucket( |
| 59 | + project=project_id, usage_export_location_resource=usage_export_location) |
| 60 | + |
| 61 | + op_client = compute_v1.GlobalOperationsClient() |
| 62 | + op_client.wait(project=project_id, operation=operation.name) |
| 63 | +# [END compute_usage_report_set] |
| 64 | + |
| 65 | + |
| 66 | +# [START compute_usage_report_get] |
| 67 | +def get_usage_export_bucket(project_id: str) -> compute_v1.UsageExportLocation: |
| 68 | + """ |
| 69 | + Retrieve Compute Engine usage export bucket for the Cloud project. |
| 70 | + Replaces the empty value returned by the API with the default value used |
| 71 | + to generate report file names. |
| 72 | +
|
| 73 | + Args: |
| 74 | + project_id: project ID or project number of the project to update. |
| 75 | + Returns: |
| 76 | + UsageExportLocation object describing the current usage export settings |
| 77 | + for project project_id. |
| 78 | + """ |
| 79 | + projects_client = compute_v1.ProjectsClient() |
| 80 | + project_data = projects_client.get(project=project_id) |
| 81 | + |
| 82 | + uel = project_data.usage_export_location |
| 83 | + |
| 84 | + if not uel.bucket_name: |
| 85 | + # The usage reports are disabled. |
| 86 | + return uel |
| 87 | + |
| 88 | + if not uel.report_name_prefix: |
| 89 | + # Although the server sent the empty string value, the next usage report |
| 90 | + # generated with these settings still has the default prefix value |
| 91 | + # "usage_gce". (see https://cloud.google.com/compute/docs/reference/rest/v1/projects/get) |
| 92 | + print('Report name prefix not set, replacing with default value of ' |
| 93 | + '`usage_gce`.') |
| 94 | + uel.report_name_prefix = 'usage_gce' |
| 95 | + return uel |
| 96 | +# [END compute_usage_report_get] |
| 97 | +# [END compute_instances_verify_default_value] |
| 98 | + |
| 99 | + |
| 100 | +# [START compute_usage_report_disable] |
| 101 | +def disable_usage_export(project_id: str) -> None: |
| 102 | + """ |
| 103 | + Disable Compute Engine usage export bucket for the Cloud Project. |
| 104 | +
|
| 105 | + Args: |
| 106 | + project_id: project ID or project number of the project to update. |
| 107 | + """ |
| 108 | + projects_client = compute_v1.ProjectsClient() |
| 109 | + |
| 110 | + # Updating the setting with None will disable the |
| 111 | + # usage report generation. |
| 112 | + operation = projects_client.set_usage_export_bucket( |
| 113 | + project=project_id, usage_export_location_resource=None) |
| 114 | + |
| 115 | + op_client = compute_v1.GlobalOperationsClient() |
| 116 | + op_client.wait(project=project_id, operation=operation.name) |
| 117 | +# [END compute_usage_report_disable] |
0 commit comments