|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 Google LLC. |
| 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/main/parametermanager/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +declare(strict_types=1); |
| 25 | + |
| 26 | +namespace Google\Cloud\Samples\ParameterManager; |
| 27 | + |
| 28 | +// [START parametermanager_remove_regional_param_kms_key] |
| 29 | +// Import necessary classes for updating a parameter. |
| 30 | +use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; |
| 31 | +use Google\Cloud\ParameterManager\V1\GetParameterRequest; |
| 32 | +use Google\Cloud\ParameterManager\V1\UpdateParameterRequest; |
| 33 | +use Google\Protobuf\FieldMask; |
| 34 | + |
| 35 | +/** |
| 36 | + * Update a regional parameter by removing kms key using the Parameter Manager SDK for GCP. |
| 37 | + * |
| 38 | + * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') |
| 39 | + * @param string $locationId The Parameter Location (e.g. 'us-central1') |
| 40 | + * @param string $parameterId The Parameter ID (e.g. 'my-param') |
| 41 | + */ |
| 42 | +function remove_regional_param_kms_key(string $projectId, string $locationId, string $parameterId): void |
| 43 | +{ |
| 44 | + // Specify regional endpoint. |
| 45 | + $options = ['apiEndpoint' => "parametermanager.$locationId.rep.googleapis.com"]; |
| 46 | + |
| 47 | + // Create a client for the Parameter Manager service. |
| 48 | + $client = new ParameterManagerClient($options); |
| 49 | + |
| 50 | + // Build the resource name of the parameter. |
| 51 | + $parameterName = $client->parameterName($projectId, $locationId, $parameterId); |
| 52 | + |
| 53 | + // Prepare the request to get the parameter. |
| 54 | + $request = (new GetParameterRequest()) |
| 55 | + ->setName($parameterName); |
| 56 | + |
| 57 | + // Retrieve the parameter using the client. |
| 58 | + $parameter = $client->getParameter($request); |
| 59 | + |
| 60 | + $parameter->clearKmsKey(); |
| 61 | + |
| 62 | + $updateMask = (new FieldMask()) |
| 63 | + ->setPaths(['kms_key']); |
| 64 | + |
| 65 | + // Prepare the request to update the parameter. |
| 66 | + $request = (new UpdateParameterRequest()) |
| 67 | + ->setUpdateMask($updateMask) |
| 68 | + ->setParameter($parameter); |
| 69 | + |
| 70 | + // Update the parameter using the client. |
| 71 | + $updatedParameter = $client->updateParameter($request); |
| 72 | + |
| 73 | + // Print the parameter details. |
| 74 | + printf('Removed kms key for regional parameter %s' . PHP_EOL, $updatedParameter->getName()); |
| 75 | +} |
| 76 | +// [END parametermanager_remove_regional_param_kms_key] |
| 77 | + |
| 78 | +// The following 2 lines are only needed to execute the samples on the CLI |
| 79 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 80 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments