|
| 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_instances_verify_default_value] |
| 27 | +# [START compute_usage_report_set] |
| 28 | +# [START compute_usage_report_get] |
| 29 | +# [START compute_usage_report_disable] |
| 30 | +use Google\Cloud\Compute\V1\ProjectsClient; |
| 31 | +use Google\Cloud\Compute\V1\UsageExportLocation; |
| 32 | + |
| 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 | +/** |
| 39 | + * Set Compute Engine usage export bucket for the Cloud Project. |
| 40 | + * This sample presents how to interpret the default value for the report name prefix parameter. |
| 41 | + * Example: |
| 42 | + * ``` |
| 43 | + * set_usage_export_bucket($projectId, $bucketName, $reportPrefixName); |
| 44 | + * ``` |
| 45 | + * |
| 46 | + * @param string $projectId Your Google Cloud project ID. |
| 47 | + * @param string $bucketName Google Cloud Storage Bucket used to store Compute Engine usage reports. |
| 48 | + * An existing Google Cloud Storage bucket is required. |
| 49 | + * @param string $reportPrefixName Prefix of the usage report name which defaults to an empty string |
| 50 | + * to showcase default values behavior. |
| 51 | + * |
| 52 | + * @return \Google\Cloud\Compute\V1\Operation |
| 53 | + * |
| 54 | + * @throws \Google\ApiCore\ApiException if the remote call fails. |
| 55 | + */ |
| 56 | +function set_usage_export_bucket( |
| 57 | + string $projectId, |
| 58 | + string $bucketName, |
| 59 | + string $reportPrefixName = '' |
| 60 | +) { |
| 61 | + // Initialize UsageExportLocation object with provided bucket name and no report name prefix. |
| 62 | + $usageExportLocation = new UsageExportLocation(array( |
| 63 | + "bucket_name" => $bucketName, |
| 64 | + "report_name_prefix" => $reportPrefixName |
| 65 | + )); |
| 66 | + |
| 67 | + if (strlen($reportPrefixName) == 0) { |
| 68 | + // Sending empty value for report_name_prefix results in the next usage report |
| 69 | + // being generated with the default prefix value "usage_gce". |
| 70 | + // See https://cloud.google.com/compute/docs/reference/rest/v1/projects/setUsageExportBucket |
| 71 | + print("Setting report_name_prefix to empty value causes the " . |
| 72 | + "report to have the default value of `usage_gce`."); |
| 73 | + } |
| 74 | + |
| 75 | + // Set the usage export location. |
| 76 | + $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 setting for the project from the server. |
| 100 | + $projectsClient = new ProjectsClient(); |
| 101 | + $projectResponse = $projectsClient->get($projectId); |
| 102 | + |
| 103 | + // Construct proper values to be displayed, taking into account default values behavior. |
| 104 | + if ($projectResponse->hasUsageExportLocation()) { |
| 105 | + $responseUsageExportLocation = $projectResponse->getUsageExportLocation(); |
| 106 | + |
| 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; |
| 122 | + } |
| 123 | +} |
| 124 | +# [END compute_usage_report_get] |
| 125 | +# [END compute_instances_verify_default_value] |
| 126 | + |
| 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); |
| 146 | +} |
| 147 | +# [END compute_usage_report_disable] |
| 148 | + |
| 149 | +require_once __DIR__ . '/../../../../testing/sample_helpers.php'; |
| 150 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments