Skip to content

Commit dc9256b

Browse files
authored
chore: upgrade KMS to new sample format (GoogleCloudPlatform#1649)
1 parent 30dc341 commit dc9256b

37 files changed

+280
-414
lines changed

kms/src/create_key_asymmetric_decrypt.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_asymmetric_decrypt]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
@@ -25,7 +27,7 @@
2527
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2628
use Google\Protobuf\Duration;
2729

28-
function create_key_asymmetric_decrypt_sample(
30+
function create_key_asymmetric_decrypt(
2931
string $projectId = 'my-project',
3032
string $locationId = 'us-east1',
3133
string $keyRingId = 'my-key-ring',
@@ -52,16 +54,11 @@ function create_key_asymmetric_decrypt_sample(
5254
// Call the API.
5355
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5456
printf('Created asymmetric decryption key: %s' . PHP_EOL, $createdKey->getName());
57+
5558
return $createdKey;
5659
}
5760
// [END kms_create_key_asymmetric_decrypt]
5861

59-
if (isset($argv)) {
60-
if (count($argv) === 0) {
61-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
62-
}
63-
64-
require_once __DIR__ . '/../vendor/autoload.php';
65-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
66-
create_key_asymmetric_decrypt_sample($projectId, $locationId, $keyRingId, $id);
67-
}
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_asymmetric_sign.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_asymmetric_sign]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
@@ -25,7 +27,7 @@
2527
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2628
use Google\Protobuf\Duration;
2729

28-
function create_key_asymmetric_sign_sample(
30+
function create_key_asymmetric_sign(
2931
string $projectId = 'my-project',
3032
string $locationId = 'us-east1',
3133
string $keyRingId = 'my-key-ring',
@@ -52,16 +54,11 @@ function create_key_asymmetric_sign_sample(
5254
// Call the API.
5355
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5456
printf('Created asymmetric signing key: %s' . PHP_EOL, $createdKey->getName());
57+
5558
return $createdKey;
5659
}
5760
// [END kms_create_key_asymmetric_sign]
5861

59-
if (isset($argv)) {
60-
if (count($argv) === 0) {
61-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
62-
}
63-
64-
require_once __DIR__ . '/../vendor/autoload.php';
65-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
66-
create_key_asymmetric_sign_sample($projectId, $locationId, $keyRingId, $id);
67-
}
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_hsm.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_hsm]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
@@ -26,7 +28,7 @@
2628
use Google\Cloud\Kms\V1\ProtectionLevel;
2729
use Google\Protobuf\Duration;
2830

29-
function create_key_hsm_sample(
31+
function create_key_hsm(
3032
string $projectId = 'my-project',
3133
string $locationId = 'us-east1',
3234
string $keyRingId = 'my-key-ring',
@@ -54,16 +56,11 @@ function create_key_hsm_sample(
5456
// Call the API.
5557
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5658
printf('Created hsm key: %s' . PHP_EOL, $createdKey->getName());
59+
5760
return $createdKey;
5861
}
5962
// [END kms_create_key_hsm]
6063

61-
if (isset($argv)) {
62-
if (count($argv) === 0) {
63-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
64-
}
65-
66-
require_once __DIR__ . '/../vendor/autoload.php';
67-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
68-
create_key_hsm_sample($projectId, $locationId, $keyRingId, $id);
69-
}
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_labels.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_labels]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2325
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2426
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
2527
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2628

27-
function create_key_labels_sample(
29+
function create_key_labels(
2830
string $projectId = 'my-project',
2931
string $locationId = 'us-east1',
3032
string $keyRingId = 'my-key-ring',
@@ -50,16 +52,11 @@ function create_key_labels_sample(
5052
// Call the API.
5153
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5254
printf('Created labeled key: %s' . PHP_EOL, $createdKey->getName());
55+
5356
return $createdKey;
5457
}
5558
// [END kms_create_key_labels]
5659

57-
if (isset($argv)) {
58-
if (count($argv) === 0) {
59-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
60-
}
61-
62-
require_once __DIR__ . '/../vendor/autoload.php';
63-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
64-
create_key_labels_sample($projectId, $locationId, $keyRingId, $id);
65-
}
60+
// The following 2 lines are only needed to run the samples
61+
require_once __DIR__ . '/../../testing/sample_helpers.php';
62+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_mac.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_mac]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
@@ -25,7 +27,7 @@
2527
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2628
use Google\Protobuf\Duration;
2729

28-
function create_key_mac_sample(
30+
function create_key_mac(
2931
string $projectId = 'my-project',
3032
string $locationId = 'us-east1',
3133
string $keyRingId = 'my-key-ring',
@@ -52,16 +54,11 @@ function create_key_mac_sample(
5254
// Call the API.
5355
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5456
printf('Created mac key: %s' . PHP_EOL, $createdKey->getName());
57+
5558
return $createdKey;
5659
}
5760
// [END kms_create_key_mac]
5861

59-
if (isset($argv)) {
60-
if (count($argv) === 0) {
61-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
62-
}
63-
64-
require_once __DIR__ . '/../vendor/autoload.php';
65-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
66-
create_key_mac_sample($projectId, $locationId, $keyRingId, $id);
67-
}
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_ring.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_ring]
2123
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2224
use Google\Cloud\Kms\V1\KeyRing;
2325

24-
function create_key_ring_sample(
26+
function create_key_ring(
2527
string $projectId = 'my-project',
2628
string $locationId = 'us-east1',
2729
string $id = 'my-key-ring'
@@ -38,16 +40,11 @@ function create_key_ring_sample(
3840
// Call the API.
3941
$createdKeyRing = $client->createKeyRing($locationName, $id, $keyRing);
4042
printf('Created key ring: %s' . PHP_EOL, $createdKeyRing->getName());
43+
4144
return $createdKeyRing;
4245
}
4346
// [END kms_create_key_ring]
4447

45-
if (isset($argv)) {
46-
if (count($argv) === 0) {
47-
return printf("Usage: php %s PROJECT_ID LOCATION_ID ID\n", basename(__FILE__));
48-
}
49-
50-
require_once __DIR__ . '/../vendor/autoload.php';
51-
list($_, $projectId, $locationId, $id) = $argv;
52-
create_key_ring_sample($projectId, $locationId, $id);
53-
}
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_rotation_schedule.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_rotation_schedule]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
@@ -26,7 +28,7 @@
2628
use Google\Protobuf\Duration;
2729
use Google\Protobuf\Timestamp;
2830

29-
function create_key_rotation_schedule_sample(
31+
function create_key_rotation_schedule(
3032
string $projectId = 'my-project',
3133
string $locationId = 'us-east1',
3234
string $keyRingId = 'my-key-ring',
@@ -57,16 +59,11 @@ function create_key_rotation_schedule_sample(
5759
// Call the API.
5860
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
5961
printf('Created key with rotation: %s' . PHP_EOL, $createdKey->getName());
62+
6063
return $createdKey;
6164
}
6265
// [END kms_create_key_rotation_schedule]
6366

64-
if (isset($argv)) {
65-
if (count($argv) === 0) {
66-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
67-
}
68-
69-
require_once __DIR__ . '/../vendor/autoload.php';
70-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
71-
create_key_rotation_schedule_sample($projectId, $locationId, $keyRingId, $id);
72-
}
67+
// The following 2 lines are only needed to run the samples
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_symmetric_encrypt_decrypt.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_symmetric_encrypt_decrypt]
2123
use Google\Cloud\Kms\V1\CryptoKey;
2224
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
2325
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
2426
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
2527
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2628

27-
function create_key_symmetric_encrypt_decrypt_sample(
29+
function create_key_symmetric_encrypt_decrypt(
2830
string $projectId = 'my-project',
2931
string $locationId = 'us-east1',
3032
string $keyRingId = 'my-key-ring',
@@ -46,16 +48,11 @@ function create_key_symmetric_encrypt_decrypt_sample(
4648
// Call the API.
4749
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
4850
printf('Created symmetric key: %s' . PHP_EOL, $createdKey->getName());
51+
4952
return $createdKey;
5053
}
5154
// [END kms_create_key_symmetric_encrypt_decrypt]
5255

53-
if (isset($argv)) {
54-
if (count($argv) === 0) {
55-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID ID\n", basename(__FILE__));
56-
}
57-
58-
require_once __DIR__ . '/../vendor/autoload.php';
59-
list($_, $projectId, $locationId, $keyRingId, $id) = $argv;
60-
create_key_symmetric_encrypt_decrypt_sample($projectId, $locationId, $keyRingId, $id);
61-
}
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/create_key_version.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_create_key_version]
2123
use Google\Cloud\Kms\V1\CryptoKeyVersion;
2224
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2325

24-
function create_key_version_sample(
26+
function create_key_version(
2527
string $projectId = 'my-project',
2628
string $locationId = 'us-east1',
2729
string $keyRingId = 'my-key-ring',
@@ -39,16 +41,11 @@ function create_key_version_sample(
3941
// Call the API.
4042
$createdVersion = $client->createCryptoKeyVersion($keyName, $version);
4143
printf('Created key version: %s' . PHP_EOL, $createdVersion->getName());
44+
4245
return $createdVersion;
4346
}
4447
// [END kms_create_key_version]
4548

46-
if (isset($argv)) {
47-
if (count($argv) === 0) {
48-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID KEY_ID\n", basename(__FILE__));
49-
}
50-
51-
require_once __DIR__ . '/../vendor/autoload.php';
52-
list($_, $projectId, $locationId, $keyRingId, $keyId) = $argv;
53-
create_key_version_sample($projectId, $locationId, $keyRingId, $keyId);
54-
}
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

kms/src/decrypt_asymmetric.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
declare(strict_types=1);
1919

20+
namespace Google\Cloud\Samples\Kms;
21+
2022
// [START kms_decrypt_asymmetric]
2123
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
2224

23-
function decrypt_asymmetric_sample(
25+
function decrypt_asymmetric(
2426
string $projectId = 'my-project',
2527
string $locationId = 'us-east1',
2628
string $keyRingId = 'my-key-ring',
@@ -37,16 +39,11 @@ function decrypt_asymmetric_sample(
3739
// Call the API.
3840
$decryptResponse = $client->asymmetricDecrypt($keyVersionName, $ciphertext);
3941
printf('Plaintext: %s' . PHP_EOL, $decryptResponse->getPlaintext());
42+
4043
return $decryptResponse;
4144
}
4245
// [END kms_decrypt_asymmetric]
4346

44-
if (isset($argv)) {
45-
if (count($argv) === 0) {
46-
return printf("Usage: php %s PROJECT_ID LOCATION_ID KEY_RING_ID KEY_ID VERSION_ID CIPHERTEXT\n", basename(__FILE__));
47-
}
48-
49-
require_once __DIR__ . '/../vendor/autoload.php';
50-
list($_, $projectId, $locationId, $keyRingId, $keyId, $versionId, $ciphertext) = $argv;
51-
decrypt_asymmetric_sample($projectId, $locationId, $keyRingId, $keyId, $versionId, $ciphertext);
52-
}
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)