Skip to content

Commit 32d42fc

Browse files
authored
chore: upgrade kms samples to new client surface (#1950)
1 parent 94129eb commit 32d42fc

35 files changed

+321
-100
lines changed

kms/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-kms": "^1.12.0"
3+
"google/cloud-kms": "^1.20"
44
}
55
}

kms/src/create_key_asymmetric_decrypt.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_asymmetric_decrypt]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829
use Google\Protobuf\Duration;
2930

3031
function create_key_asymmetric_decrypt(
@@ -52,7 +53,11 @@ function create_key_asymmetric_decrypt(
5253
);
5354

5455
// Call the API.
55-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
56+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
57+
->setParent($keyRingName)
58+
->setCryptoKeyId($id)
59+
->setCryptoKey($key);
60+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5661
printf('Created asymmetric decryption key: %s' . PHP_EOL, $createdKey->getName());
5762

5863
return $createdKey;

kms/src/create_key_asymmetric_sign.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_asymmetric_sign]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829
use Google\Protobuf\Duration;
2930

3031
function create_key_asymmetric_sign(
@@ -52,7 +53,11 @@ function create_key_asymmetric_sign(
5253
);
5354

5455
// Call the API.
55-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
56+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
57+
->setParent($keyRingName)
58+
->setCryptoKeyId($id)
59+
->setCryptoKey($key);
60+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5661
printf('Created asymmetric signing key: %s' . PHP_EOL, $createdKey->getName());
5762

5863
return $createdKey;

kms/src/create_key_hsm.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_hsm]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829
use Google\Cloud\Kms\V1\ProtectionLevel;
2930
use Google\Protobuf\Duration;
3031

@@ -54,7 +55,11 @@ function create_key_hsm(
5455
);
5556

5657
// Call the API.
57-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
58+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
59+
->setParent($keyRingName)
60+
->setCryptoKeyId($id)
61+
->setCryptoKey($key);
62+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5863
printf('Created hsm key: %s' . PHP_EOL, $createdKey->getName());
5964

6065
return $createdKey;

kms/src/create_key_labels.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_labels]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829

2930
function create_key_labels(
3031
string $projectId = 'my-project',
@@ -50,7 +51,11 @@ function create_key_labels(
5051
]);
5152

5253
// Call the API.
53-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
54+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
55+
->setParent($keyRingName)
56+
->setCryptoKeyId($id)
57+
->setCryptoKey($key);
58+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5459
printf('Created labeled key: %s' . PHP_EOL, $createdKey->getName());
5560

5661
return $createdKey;

kms/src/create_key_mac.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_mac]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829
use Google\Protobuf\Duration;
2930

3031
function create_key_mac(
@@ -52,7 +53,11 @@ function create_key_mac(
5253
);
5354

5455
// Call the API.
55-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
56+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
57+
->setParent($keyRingName)
58+
->setCryptoKeyId($id)
59+
->setCryptoKey($key);
60+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5661
printf('Created mac key: %s' . PHP_EOL, $createdKey->getName());
5762

5863
return $createdKey;

kms/src/create_key_ring.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_ring]
23-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateKeyRingRequest;
2425
use Google\Cloud\Kms\V1\KeyRing;
2526

2627
function create_key_ring(
@@ -38,7 +39,11 @@ function create_key_ring(
3839
$keyRing = new KeyRing();
3940

4041
// Call the API.
41-
$createdKeyRing = $client->createKeyRing($locationName, $id, $keyRing);
42+
$createKeyRingRequest = (new CreateKeyRingRequest())
43+
->setParent($locationName)
44+
->setKeyRingId($id)
45+
->setKeyRing($keyRing);
46+
$createdKeyRing = $client->createKeyRing($createKeyRingRequest);
4247
printf('Created key ring: %s' . PHP_EOL, $createdKeyRing->getName());
4348

4449
return $createdKeyRing;

kms/src/create_key_rotation_schedule.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_rotation_schedule]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829
use Google\Protobuf\Duration;
2930
use Google\Protobuf\Timestamp;
3031

@@ -57,7 +58,11 @@ function create_key_rotation_schedule(
5758
);
5859

5960
// Call the API.
60-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
61+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
62+
->setParent($keyRingName)
63+
->setCryptoKeyId($id)
64+
->setCryptoKey($key);
65+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
6166
printf('Created key with rotation: %s' . PHP_EOL, $createdKey->getName());
6267

6368
return $createdKey;

kms/src/create_key_symmetric_encrypt_decrypt.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_symmetric_encrypt_decrypt]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
2325
use Google\Cloud\Kms\V1\CryptoKey;
2426
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2527
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2628
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
27-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2829

2930
function create_key_symmetric_encrypt_decrypt(
3031
string $projectId = 'my-project',
@@ -46,7 +47,11 @@ function create_key_symmetric_encrypt_decrypt(
4647
);
4748

4849
// Call the API.
49-
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
50+
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
51+
->setParent($keyRingName)
52+
->setCryptoKeyId($id)
53+
->setCryptoKey($key);
54+
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
5055
printf('Created symmetric key: %s' . PHP_EOL, $createdKey->getName());
5156

5257
return $createdKey;

kms/src/create_key_version.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
namespace Google\Cloud\Samples\Kms;
2121

2222
// [START kms_create_key_version]
23+
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
24+
use Google\Cloud\Kms\V1\CreateCryptoKeyVersionRequest;
2325
use Google\Cloud\Kms\V1\CryptoKeyVersion;
24-
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2526

2627
function create_key_version(
2728
string $projectId = 'my-project',
@@ -39,7 +40,10 @@ function create_key_version(
3940
$version = new CryptoKeyVersion();
4041

4142
// Call the API.
42-
$createdVersion = $client->createCryptoKeyVersion($keyName, $version);
43+
$createCryptoKeyVersionRequest = (new CreateCryptoKeyVersionRequest())
44+
->setParent($keyName)
45+
->setCryptoKeyVersion($version);
46+
$createdVersion = $client->createCryptoKeyVersion($createCryptoKeyVersionRequest);
4347
printf('Created key version: %s' . PHP_EOL, $createdVersion->getName());
4448

4549
return $createdVersion;

0 commit comments

Comments
 (0)