This repository was archived by the owner on Dec 31, 2023. It is now read-only.
Repository navigation
feat: adding samples for default values #64
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a36d7d6
feat: adding samples for default values
m-strzelczyk 1b9ef96
chore: Applying changes from review
m-strzelczyk 7e66749
feat: Adding tests to default values samples.
m-strzelczyk 0fa1d4d
chore: Applying changes after tech-writer review.
m-strzelczyk cacc823
Merge remote-tracking branch 'origin/samples-default-values' into sam…
m-strzelczyk ecd8c4a
Merge branch 'master' into samples-default-values
m-strzelczyk a977b36
fix: Make the tests run in different projects
m-strzelczyk d9d327d
chore: Review suggestions applied.
m-strzelczyk 985c40e
Merge remote-tracking branch 'origin/samples-default-values' into sam…
m-strzelczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: adding samples for default values
Featuring the usage report API.
- Loading branch information
commit a36d7d6122d7fb6b16bec4a014e74ad394705f50
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| #!/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 default value for the name prefix | ||
| parameter. | ||
|
|
||
| Params: | ||
| 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: Report Name Prefix 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 empty value for report_name_prefix, will result with the | ||
| # next usage report generated will have the default prefix value | ||
|
m-strzelczyk marked this conversation as resolved.
Outdated
|
||
| # "usage_gce". (ref: https://cloud.google.com/compute/docs/reference/rest/v1/projects/get) | ||
|
m-strzelczyk marked this conversation as resolved.
Outdated
|
||
| print("Setting report_name_prefix to empty value will cause the report " | ||
| "to have the default prefix of `usage_gce`.") | ||
|
|
||
| projects_client = compute_v1.ProjectsClient() | ||
| projects_client.set_usage_export_bucket( | ||
| project=project_id, usage_export_location_resource=usage_export_location) | ||
| return | ||
|
busunkim96 marked this conversation as resolved.
Outdated
|
||
| # [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. | ||
|
|
||
| Params: | ||
|
busunkim96 marked this conversation as resolved.
Outdated
|
||
| 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 value, the next usage report | ||
| # generated with these settings still has the default prefix value | ||
| # "usage_gce". (ref: 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. | ||
|
|
||
| Params: | ||
| project_id: project ID or project number of the project to update. | ||
| """ | ||
| projects_client = compute_v1.ProjectsClient() | ||
| export_location = compute_v1.UsageExportLocation( | ||
| {'bucket_name': '', 'report_name_prefix': ''}) | ||
|
|
||
| # Updating the setting with empty bucket name will disable the | ||
| # usage report generation. | ||
| projects_client.set_usage_export_bucket( | ||
| project=project_id, usage_export_location_resource=export_location) | ||
|
m-strzelczyk marked this conversation as resolved.
Outdated
|
||
| return | ||
| # [END compute_usage_report_disable] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.