Repository navigation
feat(parametermanager): add templates, tags and checksum samples #2337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bshaffer
merged 2 commits into
GoogleCloudPlatform:main
from
suvidha-malaviya:feat/parametermanager-templates-tags-checksum-php
Nov 6, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2025 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/parametermanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\ParameterManager; | ||
|
|
||
| // [START parametermanager_bind_tags_to_param] | ||
| use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; | ||
| use Google\Cloud\ParameterManager\V1\CreateParameterRequest; | ||
| use Google\Cloud\ParameterManager\V1\Parameter; | ||
| use Google\Cloud\ResourceManager\V3\Client\TagBindingsClient; | ||
| use Google\Cloud\ResourceManager\V3\CreateTagBindingRequest; | ||
| use Google\Cloud\ResourceManager\V3\TagBinding; | ||
|
|
||
| /** | ||
| * Creates a parameter and binds a tag value to it. | ||
| * | ||
| * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $parameterId The Parameter ID (e.g. 'my-param') | ||
| * @param string $tagValue The tag value (e.g. 'tagValues/1234567890') | ||
| */ | ||
| function bind_tags_to_param(string $projectId, string $parameterId, string $tagValue): void | ||
| { | ||
| // Create a client for the Parameter Manager service. | ||
| $client = new ParameterManagerClient(); | ||
|
|
||
| // Create the Resource Manager tag bindings client. | ||
| $tagBindingsClient = new TagBindingsClient(); | ||
|
|
||
| // Build the resource name of the parent object. | ||
| $parent = $client->locationName($projectId, 'global'); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateParameterRequest()) | ||
| ->setParent($parent) | ||
| ->setParameterId($parameterId) | ||
| ->setParameter(new Parameter()); | ||
|
|
||
| // Create the parameter. | ||
| $newParameter = $client->createParameter($request); | ||
|
|
||
| // Print the new parameter name. | ||
| printf('Created parameter: %s' . PHP_EOL, $newParameter->getName()); | ||
|
|
||
| // Build the tag binding. | ||
| $tagBinding = (new TagBinding()) | ||
| ->setParent('//parametermanager.googleapis.com/' . $newParameter->getName()) | ||
| ->setTagValue($tagValue); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateTagBindingRequest()) | ||
| ->setTagBinding($tagBinding); | ||
|
|
||
| // Create the tag binding and wait for the operation to complete. | ||
| $operationResponse = $tagBindingsClient->createTagBinding($request); | ||
| $operationResponse->pollUntilComplete(); | ||
|
|
||
| // Check if the operation succeeded. | ||
| if ($operationResponse->operationSucceeded()) { | ||
| printf('Created tag binding: %s' . PHP_EOL, $operationResponse->getResult()->getName()); | ||
| } else { | ||
| printf('Error in creating tag binding: %s' . PHP_EOL, $operationResponse->getError()->getMessage()); | ||
| } | ||
| } | ||
| // [END parametermanager_bind_tags_to_param] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2025 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/parametermanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\ParameterManager; | ||
|
|
||
| // [START parametermanager_bind_tags_to_regional_param] | ||
| use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; | ||
| use Google\Cloud\ParameterManager\V1\CreateParameterRequest; | ||
| use Google\Cloud\ParameterManager\V1\Parameter; | ||
| use Google\Cloud\ResourceManager\V3\Client\TagBindingsClient; | ||
| use Google\Cloud\ResourceManager\V3\CreateTagBindingRequest; | ||
| use Google\Cloud\ResourceManager\V3\TagBinding; | ||
|
|
||
| /** | ||
| * Creates a parameter and binds a tag value to it. | ||
| * | ||
| * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $locationId The Parameter Location (e.g. 'us-central1') | ||
| * @param string $parameterId The Parameter ID (e.g. 'my-param') | ||
| * @param string $tagValue The tag value (e.g. 'tagValues/1234567890') | ||
| */ | ||
| function bind_tags_to_regional_param(string $projectId, string $locationId, string $parameterId, string $tagValue): void | ||
| { | ||
| // Specify regional endpoint. | ||
| $options = ['apiEndpoint' => "parametermanager.$locationId.rep.googleapis.com"]; | ||
|
|
||
| // Create a client for the Parameter Manager service. | ||
| $client = new ParameterManagerClient($options); | ||
|
|
||
| // Create the Resource Manager tag bindings client. | ||
| $tagOptions = ['apiEndpoint' => "$locationId-cloudresourcemanager.googleapis.com"]; | ||
| $tagBindingsClient = new TagBindingsClient($tagOptions); | ||
|
|
||
| // Build the resource name of the parent object. | ||
| $parent = $client->locationName($projectId, $locationId); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateParameterRequest()) | ||
| ->setParent($parent) | ||
| ->setParameterId($parameterId) | ||
| ->setParameter(new Parameter()); | ||
|
|
||
| // Create the parameter. | ||
| $newParameter = $client->createParameter($request); | ||
|
|
||
| // Print the new parameter name. | ||
| printf('Created regional parameter: %s' . PHP_EOL, $newParameter->getName()); | ||
|
|
||
| // Build the tag binding. | ||
| $tagBinding = (new TagBinding()) | ||
| ->setParent('//parametermanager.googleapis.com/' . $newParameter->getName()) | ||
| ->setTagValue($tagValue); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateTagBindingRequest()) | ||
| ->setTagBinding($tagBinding); | ||
|
|
||
| // Create the tag binding and wait for the operation to complete. | ||
| $operationResponse = $tagBindingsClient->createTagBinding($request); | ||
| $operationResponse->pollUntilComplete(); | ||
|
|
||
| // Check if the operation succeeded. | ||
| if ($operationResponse->operationSucceeded()) { | ||
| printf('Created tag binding: %s' . PHP_EOL, $operationResponse->getResult()->getName()); | ||
| } else { | ||
| printf('Error in creating tag binding: %s' . PHP_EOL, $operationResponse->getError()->getMessage()); | ||
| } | ||
| } | ||
| // [END parametermanager_bind_tags_to_regional_param] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2025 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/parametermanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\ParameterManager; | ||
|
|
||
| // [START parametermanager_create_param_template] | ||
| use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; | ||
| use Google\Cloud\ParameterManager\V1\CreateTemplateRequest; | ||
| use Google\Cloud\ParameterManager\V1\Template; | ||
| use Google\Cloud\ParameterManager\V1\TemplateFormat; | ||
|
|
||
| /** | ||
| * Creates a template with the specified format. | ||
| * | ||
| * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $templateId The Template ID (e.g. 'my-template') | ||
| * @param string $format The format of the template (YAML, JSON) | ||
| */ | ||
| function create_param_template(string $projectId, string $templateId, string $format): void | ||
| { | ||
| // Create a client for the Parameter Manager service. | ||
| $client = new ParameterManagerClient(); | ||
|
|
||
| // Build the resource name of the parent object. | ||
| $parent = $client->locationName($projectId, 'global'); | ||
|
|
||
| // Build the template. | ||
| $template = (new Template()) | ||
| ->setFormat(TemplateFormat::value('TEMPLATE_FORMAT_' . $format)); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateTemplateRequest()) | ||
| ->setParent($parent) | ||
| ->setTemplateId($templateId) | ||
| ->setTemplate($template); | ||
|
|
||
| // Create the template. | ||
| $newTemplate = $client->createTemplate($request); | ||
|
|
||
| // Print the new template name. | ||
| printf('Created template %s with format %s' . PHP_EOL, $newTemplate->getName(), TemplateFormat::name($newTemplate->getFormat())); | ||
| } | ||
| // [END parametermanager_create_param_template] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2025 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/parametermanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\ParameterManager; | ||
|
|
||
| // [START parametermanager_create_param_template_version] | ||
| use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; | ||
| use Google\Cloud\ParameterManager\V1\CreateTemplateVersionRequest; | ||
| use Google\Cloud\ParameterManager\V1\TemplateVersion; | ||
| use Google\Cloud\ParameterManager\V1\TemplateVersionPayload; | ||
|
|
||
| /** | ||
| * Creates a template version with placeholders in its payload. | ||
| * | ||
| * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $templateId The Template ID (e.g. 'my-template') | ||
| * @param string $versionId The Version ID (e.g. 'my-version') | ||
| */ | ||
| function create_param_template_version(string $projectId, string $templateId, string $versionId): void | ||
| { | ||
| // Create a client for the Parameter Manager service. | ||
| $client = new ParameterManagerClient(); | ||
|
|
||
| // Build the resource name of the template. | ||
| $parent = $client->templateName($projectId, 'global', $templateId); | ||
|
|
||
| // Build the template payload. | ||
| $payload = json_encode(['username' => '{{.username}}', 'host' => '{{.host}}']); | ||
| $templateVersion = (new TemplateVersion()) | ||
| ->setPayload((new TemplateVersionPayload())->setData($payload)); | ||
|
|
||
| // Build the request. | ||
| $request = (new CreateTemplateVersionRequest()) | ||
| ->setParent($parent) | ||
| ->setTemplateVersionId($versionId) | ||
| ->setTemplateVersion($templateVersion); | ||
|
|
||
| // Create the template version. | ||
| $newVersion = $client->createTemplateVersion($request); | ||
|
|
||
| // Print the new template version name. | ||
| printf('Created template version: %s' . PHP_EOL, $newVersion->getName()); | ||
| } | ||
| // [END parametermanager_create_param_template_version] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Robustness: If
$formatis passed in lowercase or mixed case (e.g., 'json'),TemplateFormat::value()will returnnullor throw an exception because the enum values are uppercase. Converting$formatto uppercase ensures the function is robust against different input casings.