Skip to content

feat(parametermanager): Added global and regional samples for parameter manager #2071

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 65 additions & 0 deletions parametermanager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Google Parameter Manager PHP Sample Application

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=parametermanager

## Description

This simple command-line application demonstrates how to invoke
[Google Parameter Manager][parametermanager] from PHP.

## Build and Run

1. **Enable APIs** - [Enable the Parameter Manager
API](https://console.cloud.google.com/apis/enableflow?apiid=parametermanager.googleapis.com)
and create a new project or select an existing project.

1. **Download The Credentials** - Click "Go to credentials" after enabling the
APIs. Click "New Credentials" and select "Service Account Key". Create a new
service account, use the JSON key type, and select "Create". Once
downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to
the path of the JSON key that was downloaded.

1. **Clone the repo** and cd into this directory

```text
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/parametermanager
```

1. **Install dependencies** via [Composer][install-composer]. If composer is
installed locally:


```text
$ php composer.phar install
```

If composer is installed globally:

```text
$ composer install
```

1. Execute the snippets in the [src/](src/) directory by running:

```text
$ php src/SNIPPET_NAME.php
```

The usage will print for each if no arguments are provided.

See the [Parameter Manager Documentation](https://cloud.google.com/secret-manager/parameter-manager/docs/overview) for more information.

## Contributing changes

* See [CONTRIBUTING.md](../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../LICENSE)

[install-composer]: http://getcomposer.org/doc/00-intro.md
[parametermanager]: https://cloud.google.com/secret-manager/parameter-manager/docs/overview
6 changes: 6 additions & 0 deletions parametermanager/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"google/cloud-secret-manager": "^1.15.2",
"google/cloud-parametermanager": "^0.1.1"
}
}
38 changes: 38 additions & 0 deletions parametermanager/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<phpunit bootstrap="../testing/bootstrap.php">
<testsuites>
<testsuite name="PHP Parameter Manager test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>

68 changes: 68 additions & 0 deletions parametermanager/src/create_param.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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]
// Import necessary classes for creating a parameter.
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
use Google\Cloud\ParameterManager\V1\CreateParameterRequest;
use Google\Cloud\ParameterManager\V1\Parameter;

/**
* Creates a parameter of type "unformatted" using the Parameter Manager SDK for GCP.
*
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
* @param string $parameterId The Parameter ID (e.g. 'my-param')
*/
function create_param(string $projectId, string $parameterId): 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');

// Create a new Parameter object.
$parameter = new Parameter();

// Prepare the request with the parent, parameter ID, and the parameter object.
$request = (new CreateParameterRequest())
->setParent($parent)
->setParameterId($parameterId)
->setParameter($parameter);

// Crete the parameter.
$newParameter = $client->createParameter($request);

// Print the new parameter name
printf('Created parameter: %s' . PHP_EOL, $newParameter->getName());

}
// [END parametermanager_create_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);
73 changes: 73 additions & 0 deletions parametermanager/src/create_param_version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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_version]
// Import necessary classes for creating a parameter version.
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
use Google\Cloud\ParameterManager\V1\CreateParameterVersionRequest;
use Google\Cloud\ParameterManager\V1\ParameterVersion;
use Google\Cloud\ParameterManager\V1\ParameterVersionPayload;

/**
* Creates a parameter version with an unformatted payload.
*
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
* @param string $parameterId The Parameter ID (e.g. 'my-param')
* @param string $versionId The Version ID (e.g. 'my-param-version')
* @param string $payload The unformatted string payload (e.g. 'test123')
*/
function create_param_version(string $projectId, string $parameterId, string $versionId, string $payload): void
{
// Create a client for the Parameter Manager service.
$client = new ParameterManagerClient();

// Build the resource name of the parent object.
$parent = $client->parameterName($projectId, 'global', $parameterId);

// Create a new ParameterVersionPayload object and set the unformatted data.
$parameterVersionPayload = new ParameterVersionPayload();
$parameterVersionPayload->setData($payload);

// Create a new ParameterVersion object and set the payload.
$parameterVersion = new ParameterVersion();
$parameterVersion->setPayload($parameterVersionPayload);

// Prepare the request with the parent and parameter version object.
$request = (new CreateParameterVersionRequest())
->setParent($parent)
->setParameterVersionId($versionId)
->setParameterVersion($parameterVersion);

// Call the API to create the parameter version.
$newParameterVersion = $client->createParameterVersion($request);
printf('Created parameter version: %s' . PHP_EOL, $newParameterVersion->getName());
}
// [END parametermanager_create_param_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);
76 changes: 76 additions & 0 deletions parametermanager/src/create_param_version_with_secret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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_version_with_secret]
// Import necessary classes for creating a parameter version.
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
use Google\Cloud\ParameterManager\V1\CreateParameterVersionRequest;
use Google\Cloud\ParameterManager\V1\ParameterVersion;
use Google\Cloud\ParameterManager\V1\ParameterVersionPayload;

/**
* Creates a parameter version with an secret reference.
*
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
* @param string $parameterId The Parameter ID (e.g. 'my-param')
* @param string $versionId The Version ID (e.g. 'my-param-version')
* @param string $secretId The ID of the secret to be referenced (e.g. 'projects/my-project/secrets/my-secret/versions/latest')
*/
function create_param_version_with_secret(string $projectId, string $parameterId, string $versionId, string $secretId): void
{
// Create a client for the Parameter Manager service.
$client = new ParameterManagerClient();

// Build the resource name of the parent object.
$parent = $client->parameterName($projectId, 'global', $parameterId);

// Build payload.
$payload = sprintf('{"username": "test-user", "password": "__REF__(//secretmanager.googleapis.com/%s)"}', $secretId);

// Create a new ParameterVersionPayload object and set the payload with secret reference.
$parameterVersionPayload = new ParameterVersionPayload();
$parameterVersionPayload->setData($payload);

// Create a new ParameterVersion object and set the payload.
$parameterVersion = new ParameterVersion();
$parameterVersion->setPayload($parameterVersionPayload);

// Prepare the request with the parent and parameter version object.
$request = (new CreateParameterVersionRequest())
->setParent($parent)
->setParameterVersionId($versionId)
->setParameterVersion($parameterVersion);

// Call the API to create the parameter version.
$newParameterVersion = $client->createParameterVersion($request);
printf('Created parameter version: %s' . PHP_EOL, $newParameterVersion->getName());
}
// [END parametermanager_create_param_version_with_secret]

// 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