Skip to content

Commit c862427

Browse files
authored
Refactoring set, get and disable usage export bucket samples (GoogleCloudPlatform#1433)
Refactoring set, get and disable usage export bucket samples by putting them in separate files. Removal of compute_instances_verify_default_value region tag as we decided not to use it.
1 parent b1a7862 commit c862427

File tree

5 files changed

+220
-130
lines changed

5 files changed

+220
-130
lines changed

compute/cloud-client/instances/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ $ php src/delete_instance.php $YOUR_PROJECT_ID "us-central1-a" "my-new-instance-
101101
Deleted instance my-new-instance-name
102102
```
103103

104+
### Set usage export bucket
105+
106+
```
107+
$ php src/set_usage_export_bucket.php $YOUR_PROJECT_ID "my-gcs-bucket-name" "my-report-name-prefix"
108+
```
109+
110+
### Get usage export bucket
111+
112+
```
113+
$ php src/get_usage_export_bucket.php $YOUR_PROJECT_ID
114+
```
115+
116+
### Disable usage export bucket
117+
118+
```
119+
$ php src/disable_usage_export_bucket.php $YOUR_PROJECT_ID
120+
```
121+
104122
## Troubleshooting
105123

106124
If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
# [START compute_usage_report_disable]
27+
use Google\Cloud\Compute\V1\ProjectsClient;
28+
use Google\Cloud\Compute\V1\GlobalOperationsClient;
29+
use Google\Cloud\Compute\V1\Operation;
30+
31+
/**
32+
* Disable Compute Engine usage export bucket for the Cloud Project.
33+
* Example:
34+
* ```
35+
* disable_usage_export_bucket($projectId);
36+
* ```
37+
*
38+
* @param string $projectId Your Google Cloud project ID.
39+
*
40+
* @throws \Google\ApiCore\ApiException if the remote call fails.
41+
*/
42+
function disable_usage_export_bucket(string $projectId)
43+
{
44+
// Disable the usage export location by sending null as usageExportLocationResource.
45+
$projectsClient = new ProjectsClient();
46+
$operation = $projectsClient->setUsageExportBucket($projectId, null);
47+
48+
// Wait for the set operation to complete.
49+
if ($operation->getStatus() === Operation\Status::RUNNING) {
50+
$operationClient = new GlobalOperationsClient();
51+
$operationClient->wait($operation->getName(), $projectId);
52+
}
53+
54+
printf("Compute Engine usage export bucket for project `%s` disabled.", $projectId);
55+
}
56+
# [END compute_usage_report_disable]
57+
58+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
# [START compute_usage_report_get]
27+
use Google\Cloud\Compute\V1\ProjectsClient;
28+
29+
/**
30+
* Retrieve Compute Engine usage export bucket for the Cloud project.
31+
* Replaces the empty value returned by the API with the default value used
32+
* to generate report file names.
33+
* Example:
34+
* ```
35+
* get_usage_export_bucket($projectId);
36+
* ```
37+
*
38+
* @param string $projectId Your Google Cloud project ID.
39+
*
40+
* @throws \Google\ApiCore\ApiException if the remote call fails.
41+
*/
42+
function get_usage_export_bucket(string $projectId)
43+
{
44+
// Get the usage export location for the project from the server.
45+
$projectsClient = new ProjectsClient();
46+
$projectResponse = $projectsClient->get($projectId);
47+
48+
// Replace the empty value returned by the API with the default value used to generate report file names.
49+
if ($projectResponse->hasUsageExportLocation()) {
50+
$responseUsageExportLocation = $projectResponse->getUsageExportLocation();
51+
52+
// Verify that the server explicitly sent the optional field.
53+
if ($responseUsageExportLocation->hasReportNamePrefix()) {
54+
if ($responseUsageExportLocation->getReportNamePrefix() == '') {
55+
// Although the server explicitly sent the empty string value, the next usage
56+
// report generated with these settings still has the default prefix value "usage_gce".
57+
// See https://cloud.google.com/compute/docs/reference/rest/v1/projects/get
58+
print("Report name prefix not set, replacing with default value of `usage_gce`." . PHP_EOL);
59+
$responseUsageExportLocation->setReportNamePrefix('usage_gce');
60+
}
61+
}
62+
63+
printf(
64+
"Compute Engine usage export bucket for project `%s` is bucket_name = `%s` with " .
65+
"report_name_prefix = `%s`." . PHP_EOL,
66+
$projectId,
67+
$responseUsageExportLocation->getBucketName(),
68+
$responseUsageExportLocation->getReportNamePrefix()
69+
);
70+
} else {
71+
// The usage reports are disabled.
72+
printf("Compute Engine usage export bucket for project `%s` is disabled.", $projectId);
73+
}
74+
}
75+
# [END compute_usage_report_get]
76+
77+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
78+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

compute/cloud-client/instances/src/set_usage_export_bucket.php

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@
2323

2424
namespace Google\Cloud\Samples\Compute;
2525

26-
# [START compute_instances_verify_default_value]
2726
# [START compute_usage_report_set]
28-
# [START compute_usage_report_get]
29-
# [START compute_usage_report_disable]
3027
use Google\Cloud\Compute\V1\ProjectsClient;
3128
use Google\Cloud\Compute\V1\UsageExportLocation;
29+
use Google\Cloud\Compute\V1\Operation;
30+
use Google\Cloud\Compute\V1\GlobalOperationsClient;
3231

33-
# [END compute_usage_report_disable]
34-
# [END compute_usage_report_get]
35-
# [END compute_usage_report_set]
36-
37-
# [START compute_usage_report_set]
3832
/**
3933
* Set Compute Engine usage export bucket for the Cloud project.
4034
* This sample presents how to interpret the default value for the report name prefix parameter.
@@ -49,8 +43,6 @@
4943
* @param string $reportNamePrefix Prefix of the usage report name which defaults to an empty string
5044
* to showcase default values behavior.
5145
*
52-
* @return \Google\Cloud\Compute\V1\Operation
53-
*
5446
* @throws \Google\ApiCore\ApiException if the remote call fails.
5547
*/
5648
function set_usage_export_bucket(
@@ -69,82 +61,28 @@ function set_usage_export_bucket(
6961
// being generated with the default prefix value "usage_gce".
7062
// See https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket
7163
print("Setting report_name_prefix to empty value causes the " .
72-
"report to have the default value of `usage_gce`.");
64+
"report to have the default value of `usage_gce`." . PHP_EOL);
7365
}
7466

7567
// Set the usage export location.
7668
$projectsClient = new ProjectsClient();
77-
return $projectsClient->setUsageExportBucket($projectId, $usageExportLocation);
78-
}
79-
# [END compute_usage_report_set]
80-
81-
# [START compute_usage_report_get]
82-
/**
83-
* Retrieve Compute Engine usage export bucket for the Cloud project.
84-
* Replaces the empty value returned by the API with the default value used
85-
* to generate report file names.
86-
* Example:
87-
* ```
88-
* get_usage_export_bucket($projectId);
89-
* ```
90-
*
91-
* @param string $projectId Your Google Cloud project ID.
92-
* @return UsageExportLocation|null UsageExportLocation object describing the current usage
93-
* export settings for project $projectId.
94-
*
95-
* @throws \Google\ApiCore\ApiException if the remote call fails.
96-
*/
97-
function get_usage_export_bucket(string $projectId)
98-
{
99-
// Get the usage export location for the project from the server.
100-
$projectsClient = new ProjectsClient();
101-
$projectResponse = $projectsClient->get($projectId);
102-
103-
// Replace the empty value returned by the API with the default value used to generate report file names.
104-
if ($projectResponse->hasUsageExportLocation()) {
105-
$responseUsageExportLocation = $projectResponse->getUsageExportLocation();
69+
$operation = $projectsClient->setUsageExportBucket($projectId, $usageExportLocation);
10670

107-
// Verify that the server explicitly sent the optional field.
108-
if ($responseUsageExportLocation->hasReportNamePrefix()) {
109-
if ($responseUsageExportLocation->getReportNamePrefix() == '') {
110-
// Although the server explicitly sent the empty string value, the next usage
111-
// report generated with these settings still has the default prefix value "usage_gce".
112-
// See https://cloud.google.com/compute/docs/reference/rest/v1/projects/get
113-
print("Report name prefix not set, replacing with default value of `usage_gce`.");
114-
$responseUsageExportLocation->setReportNamePrefix('usage_gce');
115-
}
116-
}
117-
118-
return $responseUsageExportLocation;
119-
} else {
120-
// The usage reports are disabled.
121-
return null;
71+
// Wait for the set operation to complete.
72+
if ($operation->getStatus() === Operation\Status::RUNNING) {
73+
$operationClient = new GlobalOperationsClient();
74+
$operationClient->wait($operation->getName(), $projectId);
12275
}
123-
}
124-
# [END compute_usage_report_get]
125-
# [END compute_instances_verify_default_value]
12676

127-
# [START compute_usage_report_disable]
128-
/**
129-
* Disable Compute Engine usage export bucket for the Cloud Project.
130-
* Example:
131-
* ```
132-
* disable_usage_export_bucket($projectId);
133-
* ```
134-
*
135-
* @param string $projectId Your Google Cloud project ID.
136-
*
137-
* @return \Google\Cloud\Compute\V1\Operation
138-
*
139-
* @throws \Google\ApiCore\ApiException if the remote call fails.
140-
*/
141-
function disable_usage_export_bucket(string $projectId)
142-
{
143-
// Disable the usage export location by sending null as usageExportLocationResource.
144-
$projectsClient = new ProjectsClient();
145-
return $projectsClient->setUsageExportBucket($projectId, null);
77+
printf(
78+
"Compute Engine usage export bucket for project `%s` set to bucket_name = `%s` with " .
79+
"report_name_prefix = `%s`." . PHP_EOL,
80+
$projectId,
81+
$usageExportLocation->getBucketName(),
82+
(strlen($reportNamePrefix) == 0) ? 'usage_gce' : $usageExportLocation->getReportNamePrefix()
83+
);
14684
}
147-
# [END compute_usage_report_disable]
85+
# [END compute_usage_report_set]
14886

14987
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
15088
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)