This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
sample_default_values.py
134 lines (107 loc) · 4.75 KB
/
sample_default_values.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# Copyright 2021 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
#
# http://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.
"""
A sample script showing how to handle default values when communicating
with the Compute Engine API.
"""
# [START compute_instances_verify_default_value]
# [START compute_usage_report_set]
# [START compute_usage_report_get]
# [START compute_usage_report_disable]
from google.cloud import compute_v1
# [END compute_usage_report_disable]
# [END compute_usage_report_get]
# [END compute_usage_report_set]
# [START compute_usage_report_set]
def set_usage_export_bucket(
project_id: str, bucket_name: str, report_name_prefix: str = ""
) -> None:
"""
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.
Args:
project_id: project ID or project number of the project to update.
bucket_name: Google Cloud Storage bucket used to store Compute Engine
usage reports. An existing Google Cloud Storage bucket is required.
report_name_prefix: Prefix of the usage report name which defaults to an empty string
to showcase default values behaviour.
"""
usage_export_location = compute_v1.UsageExportLocation(
bucket_name=bucket_name, report_name_prefix=report_name_prefix
)
if not report_name_prefix:
# Sending an empty value for report_name_prefix results in the
# next usage report being generated with the default prefix value
# "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket)
print(
"Setting report_name_prefix to empty value causes the report "
"to have the default prefix of `usage_gce`."
)
projects_client = compute_v1.ProjectsClient()
operation = projects_client.set_usage_export_bucket(
project=project_id, usage_export_location_resource=usage_export_location
)
op_client = compute_v1.GlobalOperationsClient()
while operation.status != compute_v1.Operation.Status.DONE:
operation = op_client.wait(operation=operation.name, project=project_id)
# [END compute_usage_report_set]
# [START compute_usage_report_get]
def get_usage_export_bucket(project_id: str) -> compute_v1.UsageExportLocation:
"""
Retrieve Compute Engine usage export bucket for the Cloud project.
Replaces the empty value returned by the API with the default value used
to generate report file names.
Args:
project_id: project ID or project number of the project to update.
Returns:
UsageExportLocation object describing the current usage export settings
for project project_id.
"""
projects_client = compute_v1.ProjectsClient()
project_data = projects_client.get(project=project_id)
uel = project_data.usage_export_location
if not uel.bucket_name:
# The usage reports are disabled.
return uel
if not uel.report_name_prefix:
# Although the server sent the empty string value, the next usage report
# generated with these settings still has the default prefix value
# "usage_gce". (see https://cloud.google.com/compute/docs/reference/rest/v1/projects/get)
print(
"Report name prefix not set, replacing with default value of "
"`usage_gce`."
)
uel.report_name_prefix = "usage_gce"
return uel
# [END compute_usage_report_get]
# [END compute_instances_verify_default_value]
# [START compute_usage_report_disable]
def disable_usage_export(project_id: str) -> None:
"""
Disable Compute Engine usage export bucket for the Cloud Project.
Args:
project_id: project ID or project number of the project to update.
"""
projects_client = compute_v1.ProjectsClient()
# Setting `usage_export_location_resource` to an
# empty object will disable the usage report generation.
operation = projects_client.set_usage_export_bucket(
project=project_id, usage_export_location_resource={}
)
op_client = compute_v1.GlobalOperationsClient()
while operation.status != compute_v1.Operation.Status.DONE:
operation = op_client.wait(operation=operation.name, project=project_id)
# [END compute_usage_report_disable]