23
23
24
24
namespace Google \Cloud \Samples \Compute ;
25
25
26
- # [START compute_instances_verify_default_value]
27
26
# [START compute_usage_report_set]
28
- # [START compute_usage_report_get]
29
- # [START compute_usage_report_disable]
30
27
use Google \Cloud \Compute \V1 \ProjectsClient ;
31
28
use Google \Cloud \Compute \V1 \UsageExportLocation ;
29
+ use Google \Cloud \Compute \V1 \Operation ;
30
+ use Google \Cloud \Compute \V1 \GlobalOperationsClient ;
32
31
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]
38
32
/**
39
33
* Set Compute Engine usage export bucket for the Cloud project.
40
34
* This sample presents how to interpret the default value for the report name prefix parameter.
49
43
* @param string $reportNamePrefix Prefix of the usage report name which defaults to an empty string
50
44
* to showcase default values behavior.
51
45
*
52
- * @return \Google\Cloud\Compute\V1\Operation
53
- *
54
46
* @throws \Google\ApiCore\ApiException if the remote call fails.
55
47
*/
56
48
function set_usage_export_bucket (
@@ -69,82 +61,28 @@ function set_usage_export_bucket(
69
61
// being generated with the default prefix value "usage_gce".
70
62
// See https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket
71
63
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 );
73
65
}
74
66
75
67
// Set the usage export location.
76
68
$ 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 );
106
70
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 );
122
75
}
123
- }
124
- # [END compute_usage_report_get]
125
- # [END compute_instances_verify_default_value]
126
76
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
+ );
146
84
}
147
- # [END compute_usage_report_disable ]
85
+ # [END compute_usage_report_set ]
148
86
149
87
require_once __DIR__ . '/../../../../testing/sample_helpers.php ' ;
150
88
\Google \Cloud \Samples \execute_sample (__FILE__ , __NAMESPACE__ , $ argv );
0 commit comments