2323
2424namespace 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]
3027use Google \Cloud \Compute \V1 \ProjectsClient ;
3128use 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.
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 */
5648function 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
14987require_once __DIR__ . '/../../../../testing/sample_helpers.php ' ;
15088\Google \Cloud \Samples \execute_sample (__FILE__ , __NAMESPACE__ , $ argv );
0 commit comments