8000 feat: adding samples for default values. · googleapis/python-compute@7254b68 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 7254b68

Browse files
committed
feat: adding samples for default values.
Featuring the usage report API.
1 parent aa5491c commit 7254b68

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 default value for the name prefix
36+
parameter.
37+
38+
Params:
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: Report Name Prefix 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 empty value for report_name_prefix, will result with the
52+
# next usage report generated will have the default prefix value
53+
# "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)
54+
print("Setting report_name_prefix to empty value will cause the report "
55+
"to have the default prefix of `usage_gce`.")
56+
57+
projects_client = compute_v1.ProjectsClient()
58+
projects_client.set_usage_export_bucket(
59+
project=project_id, usage_export_location_resource=usage_export_location)
60+
return
61+
# [END compute_usage_report_set]
62+
63+
64+
# [START compute_usage_report_get]
65+
def get_usage_export_bucket(project_id: str) -> compute_v1.UsageExportLocation:
66+
"""
67+
Retrieve Compute Engine usage export bucket for the Cloud Project.
68+
Replaces the empty value returned by the API with the default value used
69+
to generate report file names.
70+
71+
Params:
72+
project_id: project ID or project number of the project to update.
73+
Returns:
74+
UsageExportLocation object describing the current usage export settings
75+
for project project_id.
76+
"""
77+
projects_client = compute_v1.ProjectsClient()
78+
project_data = projects_client.get(project=project_id)
79+
80+
uel = project_data.usage_export_location
81+
82+
if not uel.bucket_name:
83+
# The Usage Reports are disabled.
84+
return uel
85+
if not uel.report_name_prefix:
86+
# Although the server sent the empty value, the next usage report
87+
# generated with these settings still has the default prefix value
88+
# "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)
89+
print('Report name prefix not set, replacing with default value of '
90+
'`usage_gce`.')
91+
uel.report_name_prefix = 'usage_gce'
92+
return uel
93+
# [END compute_usage_report_get]
94+
# [END compute_instances_verify_default_value]
95+
96+
97+
# [START compute_usage_report_disable]
98+
def disable_usage_export(project_id: str) -> None:
99+
"""
100+
Disable Compute Engine usage export bucket for the Cloud Project.
101+
102+
Params:
103+
project_id: project ID or project number of the project to update.
104+
"""
105+
projects_client = compute_v1.ProjectsClient()
106+
export_location = compute_v1.UsageExportLocation(
107+
{'bucket_name': '', 'report_name_prefix': ''})
108+
109+
# Updating the setting with empty bucket name will disable the
110+
# usage report generation.
111+
projects_client.set_usage_export_bucket(
112+
project=project_id, usage_export_location_resource=export_location)
113+
return
114+
# [END compute_usage_report_disable]

0 commit comments

Comments
 (0)
0