Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion parametermanager/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"require": {
"google/cloud-kms": "^2.3",
"google/cloud-secret-manager": "^2.0.0",
"google/cloud-parametermanager": "^0.5.0"
"google/cloud-parametermanager": "^0.7.0",
"google/cloud-resource-manager": "^1.2"
}
}
90 changes: 90 additions & 0 deletions parametermanager/src/bind_tags_to_param.php
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);
95 changes: 95 additions & 0 deletions parametermanager/src/bind_tags_to_regional_param.php
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);
69 changes: 69 additions & 0 deletions parametermanager/src/create_param_template.php
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));
Comment on lines +50 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Robustness: If $format is passed in lowercase or mixed case (e.g., 'json'), TemplateFormat::value() will return null or throw an exception because the enum values are uppercase. Converting $format to uppercase ensures the function is robust against different input casings.

    $template = (new Template())
        ->setFormat(TemplateFormat::value('TEMPLATE_FORMAT_' . strtoupper($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);
70 changes: 70 additions & 0 deletions parametermanager/src/create_param_template_version.php
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);
Loading
Loading