-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Closed
vatsal-vora-crestdata
wants to merge
3
commits into
GoogleCloudPlatform:main
from
vatsal-vora-crestdata:parametermanager-all-samples
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
## Initial placeholder README file for folder creation | ||
# 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 |
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,6 @@ | ||
{ | ||
"require": { | ||
"google/cloud-secret-manager": "^1.15.2", | ||
"google/cloud-parametermanager": "^0.1.1" | ||
} | ||
} |
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,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> | ||
|
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,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); |
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,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); |
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,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); |
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.
This would be better off using
json_encode
: