Skip to content

Commit 04a5c01

Browse files
durgesh-ninave-crestbshaffergptSanyam
authored
feat(parametermanager): Added samples for global & regional parameter manager (#2079)
* feat(parametermanager): Added samples for global & regional parameter manager * fix(parametermanager): update json_data to use json_encode and fix linting issue --------- Co-authored-by: Brent Shaffer <[email protected]> Co-authored-by: Sanyam Gupta <[email protected]>
1 parent ae61145 commit 04a5c01

37 files changed

+3267
-1
lines changed

parametermanager/README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
## Initial placeholder README file for folder creation
1+
# Google Parameter Manager PHP Sample Application
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=parametermanager
7+
8+
## Description
9+
10+
This simple command-line application demonstrates how to invoke
11+
[Google Parameter Manager][parametermanager] from PHP.
12+
13+
## Build and Run
14+
15+
1. **Enable APIs** - [Enable the Parameter Manager
16+
API](https://console.cloud.google.com/apis/enableflow?apiid=parametermanager.googleapis.com)
17+
and create a new project or select an existing project.
18+
19+
1. **Download The Credentials** - Click "Go to credentials" after enabling the
20+
APIs. Click "New Credentials" and select "Service Account Key". Create a new
21+
service account, use the JSON key type, and select "Create". Once
22+
downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to
23+
the path of the JSON key that was downloaded.
24+
25+
1. **Clone the repo** and cd into this directory
26+
27+
```text
28+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
29+
$ cd php-docs-samples/parametermanager
30+
```
31+
32+
1. **Install dependencies** via [Composer][install-composer]. If composer is
33+
installed locally:
34+
35+
36+
```text
37+
$ php composer.phar install
38+
```
39+
40+
If composer is installed globally:
41+
42+
```text
43+
$ composer install
44+
```
45+
46+
1. Execute the snippets in the [src/](src/) directory by running:
47+
48+
```text
49+
$ php src/SNIPPET_NAME.php
50+
```
51+
52+
The usage will print for each if no arguments are provided.
53+
54+
See the [Parameter Manager Documentation](https://cloud.google.com/secret-manager/parameter-manager/docs/overview) for more information.
55+
56+
## Contributing changes
57+
58+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
59+
60+
## Licensing
61+
62+
* See [LICENSE](../LICENSE)
63+
64+
[install-composer]: http://getcomposer.org/doc/00-intro.md
65+
[parametermanager]: https://cloud.google.com/secret-manager/parameter-manager/docs/overview

parametermanager/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"google/cloud-secret-manager": "^1.15.2",
4+
"google/cloud-parametermanager": "^0.1.1"
5+
}
6+
}

parametermanager/phpunit.xml.dist

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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+
<phpunit bootstrap="../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP Parameter Manager test">
20+
<directory>test</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
</logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./src</directory>
29+
<exclude>
30+
<directory>./vendor</directory>
31+
</exclude>
32+
</whitelist>
33+
</filter>
34+
<php>
35+
<env name="PHPUNIT_TESTS" value="1"/>
36+
</php>
37+
</phpunit>
38+

parametermanager/src/create_param.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_create_param]
29+
// Import necessary classes for creating a parameter.
30+
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
31+
use Google\Cloud\ParameterManager\V1\CreateParameterRequest;
32+
use Google\Cloud\ParameterManager\V1\Parameter;
33+
34+
/**
35+
* Creates a parameter of type "unformatted" using the Parameter Manager SDK for GCP.
36+
*
37+
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $parameterId The Parameter ID (e.g. 'my-param')
39+
*/
40+
function create_param(string $projectId, string $parameterId): void
41+
{
42+
// Create a client for the Parameter Manager service.
43+
$client = new ParameterManagerClient();
44+
45+
// Build the resource name of the parent object.
46+
$parent = $client->locationName($projectId, 'global');
47+
48+
// Create a new Parameter object.
49+
$parameter = new Parameter();
50+
51+
// Prepare the request with the parent, parameter ID, and the parameter object.
52+
$request = (new CreateParameterRequest())
53+
->setParent($parent)
54+
->setParameterId($parameterId)
55+
->setParameter($parameter);
56+
57+
// Crete the parameter.
58+
$newParameter = $client->createParameter($request);
59+
60+
// Print the new parameter name
61+
printf('Created parameter: %s' . PHP_EOL, $newParameter->getName());
62+
63+
}
64+
// [END parametermanager_create_param]
65+
66+
// The following 2 lines are only needed to execute the samples on the CLI
67+
require_once __DIR__ . '/../../testing/sample_helpers.php';
68+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_create_param_version]
29+
// Import necessary classes for creating a parameter version.
30+
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
31+
use Google\Cloud\ParameterManager\V1\CreateParameterVersionRequest;
32+
use Google\Cloud\ParameterManager\V1\ParameterVersion;
33+
use Google\Cloud\ParameterManager\V1\ParameterVersionPayload;
34+
35+
/**
36+
* Creates a parameter version with an unformatted payload.
37+
*
38+
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $parameterId The Parameter ID (e.g. 'my-param')
40+
* @param string $versionId The Version ID (e.g. 'my-param-version')
41+
* @param string $payload The unformatted string payload (e.g. 'test123')
42+
*/
43+
function create_param_version(string $projectId, string $parameterId, string $versionId, string $payload): void
44+
{
45+
// Create a client for the Parameter Manager service.
46+
$client = new ParameterManagerClient();
47+
48+
// Build the resource name of the parent object.
49+
$parent = $client->parameterName($projectId, 'global', $parameterId);
50+
51+
// Create a new ParameterVersionPayload object and set the unformatted data.
52+
$parameterVersionPayload = new ParameterVersionPayload();
53+
$parameterVersionPayload->setData($payload);
54+
55+
// Create a new ParameterVersion object and set the payload.
56+
$parameterVersion = new ParameterVersion();
57+
$parameterVersion->setPayload($parameterVersionPayload);
58+
59+
// Prepare the request with the parent and parameter version object.
60+
$request = (new CreateParameterVersionRequest())
61+
->setParent($parent)
62+
->setParameterVersionId($versionId)
63+
->setParameterVersion($parameterVersion);
64+
65+
// Call the API to create the parameter version.
66+
$newParameterVersion = $client->createParameterVersion($request);
67+
printf('Created parameter version: %s' . PHP_EOL, $newParameterVersion->getName());
68+
}
69+
// [END parametermanager_create_param_version]
70+
71+
// The following 2 lines are only needed to execute the samples on the CLI
72+
require_once __DIR__ . '/../../testing/sample_helpers.php';
73+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_create_param_version_with_secret]
29+
// Import necessary classes for creating a parameter version.
30+
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
31+
use Google\Cloud\ParameterManager\V1\CreateParameterVersionRequest;
32+
use Google\Cloud\ParameterManager\V1\ParameterVersion;
33+
use Google\Cloud\ParameterManager\V1\ParameterVersionPayload;
34+
35+
/**
36+
* Creates a parameter version with an secret reference.
37+
*
38+
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $parameterId The Parameter ID (e.g. 'my-param')
40+
* @param string $versionId The Version ID (e.g. 'my-param-version')
41+
* @param string $secretId The ID of the secret to be referenced (e.g. 'projects/my-project/secrets/my-secret/versions/latest')
42+
*/
43+
function create_param_version_with_secret(string $projectId, string $parameterId, string $versionId, string $secretId): void
44+
{
45+
// Create a client for the Parameter Manager service.
46+
$client = new ParameterManagerClient();
47+
48+
// Build the resource name of the parent object.
49+
$parent = $client->parameterName($projectId, 'global', $parameterId);
50+
51+
// Build payload.
52+
$payload = json_encode([
53+
'username' => 'test-user',
54+
'password' => sprintf('__REF__(//secretmanager.googleapis.com/%s)', $secretId)
55+
], JSON_UNESCAPED_SLASHES);
56+
57+
// Create a new ParameterVersionPayload object and set the payload with secret reference.
58+
$parameterVersionPayload = new ParameterVersionPayload();
59+
$parameterVersionPayload->setData($payload);
60+
61+
// Create a new ParameterVersion object and set the payload.
62+
$parameterVersion = new ParameterVersion();
63+
$parameterVersion->setPayload($parameterVersionPayload);
64+
65+
// Prepare the request with the parent and parameter version object.
66+
$request = (new CreateParameterVersionRequest())
67+
->setParent($parent)
68+
->setParameterVersionId($versionId)
69+
->setParameterVersion($parameterVersion);
70+
71+
// Call the API to create the parameter version.
72+
$newParameterVersion = $client->createParameterVersion($request);
73+
printf('Created parameter version: %s' . PHP_EOL, $newParameterVersion->getName());
74+
}
75+
// [END parametermanager_create_param_version_with_secret]
76+
77+
// The following 2 lines are only needed to execute the samples on the CLI
78+
require_once __DIR__ . '/../../testing/sample_helpers.php';
79+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)