Skip to content

Commit 4fdfe56

Browse files
feat(parametermanager): added cmek key related snippets (#2077)
1 parent 04a5c01 commit 4fdfe56

10 files changed

+844
-47
lines changed

parametermanager/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": {
3+
"google/cloud-kms": "^2.3",
34
"google/cloud-secret-manager": "^1.15.2",
4-
"google/cloud-parametermanager": "^0.1.1"
5+
"google/cloud-parametermanager": "^0.2.0"
56
}
67
}

parametermanager/phpunit.xml.dist

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
limitations under the License.
1616
-->
1717
<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>
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>
3737
</phpunit>
38-
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_with_kms_key]
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" with provided KMS key 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+
* @param string $kmsKey The KMS key used to encrypt the parameter (e.g. 'projects/my-project/locations/global/keyRings/test/cryptoKeys/test-key')
40+
*/
41+
function create_param_with_kms_key(string $projectId, string $parameterId, string $kmsKey): void
42+
{
43+
// Create a client for the Parameter Manager service.
44+
$client = new ParameterManagerClient();
45+
46+
// Build the resource name of the parent object.
47+
$parent = $client->locationName($projectId, 'global');
48+
49+
// Create a new Parameter object.
50+
$parameter = (new Parameter())
51+
->setKmsKey($kmsKey);
52+
53+
// Prepare the request with the parent, parameter ID, and the parameter object.
54+
$request = (new CreateParameterRequest())
55+
->setParent($parent)
56+
->setParameterId($parameterId)
57+
->setParameter($parameter);
58+
59+
// Crete the parameter.
60+
$newParameter = $client->createParameter($request);
61+
62+
// Print the new parameter name
63+
printf('Created parameter %s with kms key %s' . PHP_EOL, $newParameter->getName(), $newParameter->getKmsKey());
64+
65+
}
66+
// [END parametermanager_create_param_with_kms_key]
67+
68+
// The following 2 lines are only needed to execute the samples on the CLI
69+
require_once __DIR__ . '/../../testing/sample_helpers.php';
70+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_regional_param_with_kms_key]
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 regional parameter of type "unformatted" with provided KMS key using the Parameter Manager SDK for GCP.
36+
*
37+
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $locationId The Parameter Location (e.g. 'us-central1')
39+
* @param string $parameterId The Parameter ID (e.g. 'my-param')
40+
* @param string $kmsKey The KMS key used to encrypt the parameter (e.g. 'projects/my-project/locations/us-central1/keyRings/test/cryptoKeys/test-key')
41+
*/
42+
function create_regional_param_with_kms_key(string $projectId, string $locationId, string $parameterId, string $kmsKey): void
43+
{
44+
// Specify regional endpoint.
45+
$options = ['apiEndpoint' => "parametermanager.$locationId.rep.googleapis.com"];
46+
47+
// Create a client for the Parameter Manager service.
48+
$client = new ParameterManagerClient($options);
49+
50+
// Build the resource name of the parent object.
51+
$parent = $client->locationName($projectId, $locationId);
52+
53+
// Create a new Parameter object.
54+
$parameter = (new Parameter())
55+
->setKmsKey($kmsKey);
56+
57+
// Prepare the request with the parent, parameter ID, and the parameter object.
58+
$request = (new CreateParameterRequest())
59+
->setParent($parent)
60+
->setParameterId($parameterId)
61+
->setParameter($parameter);
62+
63+
// Crete the parameter.
64+
$newParameter = $client->createParameter($request);
65+
66+
// Print the new parameter name
67+
printf('Created regional parameter %s with kms key %s' . PHP_EOL, $newParameter->getName(), $newParameter->getKmsKey());
68+
69+
}
70+
// [END parametermanager_create_regional_param_with_kms_key]
71+
72+
// The following 2 lines are only needed to execute the samples on the CLI
73+
require_once __DIR__ . '/../../testing/sample_helpers.php';
74+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_remove_param_kms_key]
29+
// Import necessary classes for updating a parameter.
30+
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
31+
use Google\Cloud\ParameterManager\V1\GetParameterRequest;
32+
use Google\Cloud\ParameterManager\V1\UpdateParameterRequest;
33+
use Google\Protobuf\FieldMask;
34+
35+
/**
36+
* Update a parameter by removing kms key using the Parameter Manager SDK for GCP.
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+
*/
41+
function remove_param_kms_key(string $projectId, string $parameterId): void
42+
{
43+
// Create a client for the Parameter Manager service.
44+
$client = new ParameterManagerClient();
45+
46+
// Build the resource name of the parameter.
47+
$parameterName = $client->parameterName($projectId, 'global', $parameterId);
48+
49+
// Prepare the request to get the parameter.
50+
$request = (new GetParameterRequest())
51+
->setName($parameterName);
52+
53+
// Retrieve the parameter using the client.
54+
$parameter = $client->getParameter($request);
55+
56+
$parameter->clearKmsKey();
57+
58+
$updateMask = (new FieldMask())
59+
->setPaths(['kms_key']);
60+
61+
// Prepare the request to update the parameter.
62+
$request = (new UpdateParameterRequest())
63+
->setUpdateMask($updateMask)
64+
->setParameter($parameter);
65+
66+
// Update the parameter using the client.
67+
$updatedParameter = $client->updateParameter($request);
68+
69+
// Print the parameter details.
70+
printf('Removed kms key for parameter %s' . PHP_EOL, $updatedParameter->getName());
71+
}
72+
// [END parametermanager_remove_param_kms_key]
73+
74+
// The following 2 lines are only needed to execute the samples on the CLI
75+
require_once __DIR__ . '/../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_remove_regional_param_kms_key]
29+
// Import necessary classes for updating a parameter.
30+
use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient;
31+
use Google\Cloud\ParameterManager\V1\GetParameterRequest;
32+
use Google\Cloud\ParameterManager\V1\UpdateParameterRequest;
33+
use Google\Protobuf\FieldMask;
34+
35+
/**
36+
* Update a regional parameter by removing kms key using the Parameter Manager SDK for GCP.
37+
*
38+
* @param string $projectId The Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $locationId The Parameter Location (e.g. 'us-central1')
40+
* @param string $parameterId The Parameter ID (e.g. 'my-param')
41+
*/
42+
function remove_regional_param_kms_key(string $projectId, string $locationId, string $parameterId): void
43+
{
44+
// Specify regional endpoint.
45+
$options = ['apiEndpoint' => "parametermanager.$locationId.rep.googleapis.com"];
46+
47+
// Create a client for the Parameter Manager service.
48+
$client = new ParameterManagerClient($options);
49+
50+
// Build the resource name of the parameter.
51+
$parameterName = $client->parameterName($projectId, $locationId, $parameterId);
52+
53+
// Prepare the request to get the parameter.
54+
$request = (new GetParameterRequest())
55+
->setName($parameterName);
56+
57+
// Retrieve the parameter using the client.
58+
$parameter = $client->getParameter($request);
59+
60+
$parameter->clearKmsKey();
61+
62+
$updateMask = (new FieldMask())
63+
->setPaths(['kms_key']);
64+
65+
// Prepare the request to update the parameter.
66+
$request = (new UpdateParameterRequest())
67+
->setUpdateMask($updateMask)
68+
->setParameter($parameter);
69+
70+
// Update the parameter using the client.
71+
$updatedParameter = $client->updateParameter($request);
72+
73+
// Print the parameter details.
74+
printf('Removed kms key for regional parameter %s' . PHP_EOL, $updatedParameter->getName());
75+
}
76+
// [END parametermanager_remove_regional_param_kms_key]
77+
78+
// The following 2 lines are only needed to execute the samples on the CLI
79+
require_once __DIR__ . '/../../testing/sample_helpers.php';
80+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)