Skip to content

Commit a9f9eb6

Browse files
authored
feat: adding samples for default values (#64)
* feat: adding samples for default values Featuring the usage report API.
1 parent dbb71b9 commit a9f9eb6

File tree

4 files changed

+186
-3
lines changed

4 files changed

+186
-3
lines changed

compute/compute/snippets/noxfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4949
# build specific Cloud project. You can also use your own string
5050
# to use your own Cloud project.
51-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
52-
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
51+
# 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
52+
'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5353
# If you need to use a specific version of pip,
5454
# change pip_version_override to the string representation
5555
# of the version number, for example, "20.2.4"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pytest==6.2.4
1+
pytest==6.2.4
2+
google-cloud-storage==1.39.0
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2021 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+
import typing
15+
import uuid
16+
17+
import google.auth
18+
import google.cloud.storage as storage
19+
import pytest
20+
21+
from sample_default_values import \
22+
disable_usage_export, get_usage_export_bucket, set_usage_export_bucket
23+
24+
PROJECT = google.auth.default()[1]
25+
BUCKET_NAME = "test" + uuid.uuid4().hex[:10]
26+
TEST_PREFIX = 'some-prefix'
27+
28+
29+
@pytest.fixture
30+
def temp_bucket():
31+
storage_client = storage.Client()
32+
bucket = storage_client.create_bucket(BUCKET_NAME)
33+
yield bucket
34+
bucket.delete(force=True)
35+
36+
37+
def test_set_usage_export_bucket_default(capsys: typing.Any,
38+
temp_bucket: storage.Bucket) -> None:
39+
set_usage_export_bucket(project_id=PROJECT, bucket_name=temp_bucket.name)
40+
uel = get_usage_export_bucket(project_id=PROJECT)
41+
assert(uel.bucket_name == temp_bucket.name)
42+
assert(uel.report_name_prefix == 'usage_gce')
43+
out, _ = capsys.readouterr()
44+
assert('default prefix of `usage_gce`.' in out)
45+
46+
disable_usage_export(project_id=PROJECT)
47+
uel = get_usage_export_bucket(project_id=PROJECT)
48+
assert(uel.bucket_name == '')
49+
assert(uel.report_name_prefix == '')
50+
51+
52+
def test_set_usage_export_bucket_custom(capsys: typing.Any,
53+
temp_bucket: storage.Bucket) -> None:
54+
set_usage_export_bucket(project_id=PROJECT, bucket_name=temp_bucket.name,
55+
report_name_prefix=TEST_PREFIX)
56+
uel = get_usage_export_bucket(project_id=PROJECT)
57+
assert(uel.bucket_name == temp_bucket.name)
58+
assert(uel.report_name_prefix == TEST_PREFIX)
59+
out, _ = capsys.readouterr()
60+
assert('usage_gce' not in out)
61+
62+
disable_usage_export(project_id=PROJECT)
63+
uel = get_usage_export_bucket(project_id=PROJECT)
64+
assert(uel.bucket_name == '')
65+
assert(uel.report_name_prefix == '')

0 commit comments

Comments
 (0)