diff --git a/secretmanager/src/create_regional_secret_with_delayed_destroy.php b/secretmanager/src/create_regional_secret_with_delayed_destroy.php new file mode 100644 index 000000000..7ebd4606a --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_delayed_destroy.php @@ -0,0 +1,72 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + // Build the secret. + $secret = new Secret([ + 'version_destroy_ttl' => new Duration([ + 'seconds' => $versionDestroyTtl + ]) + ]); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret: %s', $newSecret->getName()); +} +// [END secretmanager_create_regional_secret_with_delayed_destroy] + +// 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); diff --git a/secretmanager/src/create_secret_with_delayed_destroy.php b/secretmanager/src/create_secret_with_delayed_destroy.php new file mode 100644 index 000000000..a7e40764d --- /dev/null +++ b/secretmanager/src/create_secret_with_delayed_destroy.php @@ -0,0 +1,73 @@ +projectName($projectId); + + // Build the secret. + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + 'version_destroy_ttl' => new Duration([ + 'seconds' => $versionDestroyTtl + ]) + ]); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret: %s', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_delayed_destroy] + +// 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); diff --git a/secretmanager/src/disable_regional_secret_delayed_destroy.php b/secretmanager/src/disable_regional_secret_delayed_destroy.php new file mode 100644 index 000000000..d6ed2dc52 --- /dev/null +++ b/secretmanager/src/disable_regional_secret_delayed_destroy.php @@ -0,0 +1,75 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the secret. + $secret = new Secret([ + 'name' => $name, + ]); + + // Set the field mask. + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['version_destroy_ttl']); + + // Build the request. + $request = new UpdateSecretRequest(); + $request->setSecret($secret); + $request->setUpdateMask($fieldMask); + + // Update the secret. + $newSecret = $client->updateSecret($request); + + // Print the new secret name. + printf('Updated secret: %s', $newSecret->getName()); +} +// [END secretmanager_disable_regional_secret_delayed_destroy] + +// 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); diff --git a/secretmanager/src/disable_secret_delayed_destroy.php b/secretmanager/src/disable_secret_delayed_destroy.php new file mode 100644 index 000000000..2e01612a9 --- /dev/null +++ b/secretmanager/src/disable_secret_delayed_destroy.php @@ -0,0 +1,71 @@ +secretName($projectId, $secretId); + + // Build the secret. + $secret = new Secret([ + 'name' => $name + ]); + + // Set the field mask. + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['version_destroy_ttl']); + + // Build the request. + $request = new UpdateSecretRequest(); + $request->setSecret($secret); + $request->setUpdateMask($fieldMask); + + // Update the secret. + $newSecret = $client->updateSecret($request); + + // Print the new secret name. + printf('Updated secret: %s', $newSecret->getName()); +} +// [END secretmanager_disable_secret_delayed_destroy] + +// 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); diff --git a/secretmanager/src/update_regional_secret_with_delayed_destroy.php b/secretmanager/src/update_regional_secret_with_delayed_destroy.php new file mode 100644 index 000000000..98e0d114e --- /dev/null +++ b/secretmanager/src/update_regional_secret_with_delayed_destroy.php @@ -0,0 +1,80 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the secret. + $secret = new Secret([ + 'name' => $name, + 'version_destroy_ttl' => new Duration([ + 'seconds' => $versionDestroyTtl, + ]) + ]); + + // Set the field mask. + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['version_destroy_ttl']); + + // Build the request. + $request = new UpdateSecretRequest(); + $request->setSecret($secret); + $request->setUpdateMask($fieldMask); + + // Update the secret. + $newSecret = $client->updateSecret($request); + + // Print the new secret name. + printf('Updated secret: %s', $newSecret->getName()); +} +// [END secretmanager_update_regional_secret_with_delayed_destroy] + +// 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); diff --git a/secretmanager/src/update_secret_with_delayed_destroy.php b/secretmanager/src/update_secret_with_delayed_destroy.php new file mode 100644 index 000000000..b487ff955 --- /dev/null +++ b/secretmanager/src/update_secret_with_delayed_destroy.php @@ -0,0 +1,76 @@ +secretName($projectId, $secretId); + + // Build the secret. + $secret = new Secret([ + 'name' => $name, + 'version_destroy_ttl' => new Duration([ + 'seconds' => $versionDestroyTtl, + ]) + ]); + + // Set the field mask. + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['version_destroy_ttl']); + + // Build the request. + $request = new UpdateSecretRequest(); + $request->setSecret($secret); + $request->setUpdateMask($fieldMask); + + // Update the secret. + $newSecret = $client->updateSecret($request); + + // Print the new secret name. + printf('Updated secret: %s', $newSecret->getName()); +} +// [END secretmanager_update_secret_with_delayed_destroy] + +// 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); diff --git a/secretmanager/test/regionalsecretmanagerTest.php b/secretmanager/test/regionalsecretmanagerTest.php index 01d5c7b48..18c9c97ac 100644 --- a/secretmanager/test/regionalsecretmanagerTest.php +++ b/secretmanager/test/regionalsecretmanagerTest.php @@ -33,6 +33,7 @@ use Google\Cloud\SecretManager\V1\CreateSecretRequest; use Google\Cloud\SecretManager\V1\DeleteSecretRequest; use Google\Cloud\SecretManager\V1\DisableSecretVersionRequest; +use Google\Cloud\SecretManager\V1\GetSecretRequest; use Google\Cloud\SecretManager\V1\Secret; use Google\Cloud\SecretManager\V1\SecretPayload; use Google\Cloud\SecretManager\V1\SecretVersion; @@ -59,6 +60,7 @@ class regionalsecretmanagerTest extends TestCase private static $testSecretBindTagToCreateName; private static $testSecretWithLabelsToCreateName; private static $testSecretWithAnnotationsToCreateName; + private static $testSecretWithDelayedDestroyToCreateName; private static $iamUser = 'user:kapishsingh@google.com'; private static $locationId = 'us-central1'; @@ -68,6 +70,7 @@ class regionalsecretmanagerTest extends TestCase private static $testAnnotationKey = 'test-annotation-key'; private static $testAnnotationValue = 'test-annotation-value'; private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; + private static $testDelayedDestroyTime = 86400; private static $testTagKey; private static $testTagValue; @@ -91,6 +94,7 @@ public static function setUpBeforeClass(): void self::$testSecretBindTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::$testSecretWithLabelsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::$testSecretWithAnnotationsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretWithDelayedDestroyToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::disableSecretVersion(self::$testSecretVersionToEnable); self::$testTagKey = self::createTagKey(self::randomSecretId()); @@ -110,6 +114,7 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretBindTagToCreateName); self::deleteSecret(self::$testSecretWithLabelsToCreateName); self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); + self::deleteSecret(self::$testSecretWithDelayedDestroyToCreateName); sleep(15); // Added a sleep to wait for the tag unbinding self::deleteTagValue(); self::deleteTagKey(); @@ -162,6 +167,13 @@ private static function deleteSecret(string $name) } } + private static function getSecret(string $projectId, string $locationId, string $secretId): Secret + { + $name = self::$client->projectLocationSecretName($projectId, $locationId, $secretId); + $getSecretRequest = (new GetSecretRequest())->setName($name); + return self::$client->getSecret($getSecretRequest); + } + private static function createTagKey(string $short_name): string { $parent = self::$client->projectName(self::$projectId); @@ -588,4 +600,53 @@ public function testDeleteSecretAnnotation() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithDelayedDestroyed() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_delayed_destroy', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testDelayedDestroyTime + ]); + + $this->assertStringContainsString('Created secret', $output); + + $secret = self::getSecret($name['project'], $name['location'], $name['secret']); + $this->assertEquals(self::$testDelayedDestroyTime, $secret->getVersionDestroyTtl()->getSeconds()); + } + + public function testDisableSecretDelayedDestroy() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('disable_regional_secret_delayed_destroy', [ + $name['project'], + $name['location'], + $name['secret'] + ]); + + $this->assertStringContainsString('Updated secret', $output); + + $secret = self::getSecret($name['project'], $name['location'], $name['secret']); + $this->assertNull($secret->getVersionDestroyTtl()); + } + + public function testUpdateSecretWithDelayedDestroyed() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('update_regional_secret_with_delayed_destroy', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testDelayedDestroyTime + ]); + + $this->assertStringContainsString('Updated secret', $output); + $secret = self::getSecret($name['project'], $name['location'], $name['secret']); + $this->assertEquals(self::$testDelayedDestroyTime, $secret->getVersionDestroyTtl()->getSeconds()); + } } diff --git a/secretmanager/test/secretmanagerTest.php b/secretmanager/test/secretmanagerTest.php index a67d8cfa7..11b9dd3bd 100644 --- a/secretmanager/test/secretmanagerTest.php +++ b/secretmanager/test/secretmanagerTest.php @@ -33,6 +33,7 @@ use Google\Cloud\SecretManager\V1\CreateSecretRequest; use Google\Cloud\SecretManager\V1\DeleteSecretRequest; use Google\Cloud\SecretManager\V1\DisableSecretVersionRequest; +use Google\Cloud\SecretManager\V1\GetSecretRequest; use Google\Cloud\SecretManager\V1\Replication; use Google\Cloud\SecretManager\V1\Replication\Automatic; use Google\Cloud\SecretManager\V1\Secret; @@ -62,6 +63,7 @@ class secretmanagerTest extends TestCase private static $testSecretBindTagToCreateName; private static $testSecretWithLabelsToCreateName; private static $testSecretWithAnnotationsToCreateName; + private static $testSecretWithDelayedDestroyToCreateName; private static $iamUser = 'user:sethvargo@google.com'; private static $testLabelKey = 'test-label-key'; @@ -70,6 +72,7 @@ class secretmanagerTest extends TestCase private static $testAnnotationKey = 'test-annotation-key'; private static $testAnnotationValue = 'test-annotation-value'; private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; + private static $testDelayedDestroyTime = 86400; private static $testTagKey; private static $testTagValue; @@ -89,6 +92,7 @@ public static function setUpBeforeClass(): void self::$testSecretBindTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretWithLabelsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretWithAnnotationsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithDelayedDestroyToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretVersion = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); @@ -111,6 +115,7 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretBindTagToCreateName); self::deleteSecret(self::$testSecretWithLabelsToCreateName); self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); + self::deleteSecret(self::$testSecretWithDelayedDestroyToCreateName); sleep(15); // Added a sleep to wait for the tag unbinding self::deleteTagValue(); self::deleteTagKey(); @@ -167,6 +172,14 @@ private static function deleteSecret(string $name) } } + private static function getSecret(string $projectId, string $secretId): Secret + { + $name = self::$client->secretName($projectId, $secretId); + $getSecretRequest = (new GetSecretRequest()) + ->setName($name); + return self::$client->getSecret($getSecretRequest); + } + private static function createTagKey(string $short_name): string { $parent = self::$client->projectName(self::$projectId); @@ -582,4 +595,51 @@ public function testDeleteSecretAnnotation() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithDelayedDestroyed() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_delayed_destroy', [ + $name['project'], + $name['secret'], + self::$testDelayedDestroyTime + ]); + + $this->assertStringContainsString('Created secret', $output); + + $secret = self::getSecret($name['project'], $name['secret']); + $this->assertEquals(self::$testDelayedDestroyTime, $secret->getVersionDestroyTtl()->getSeconds()); + } + + public function testDisableSecretDelayedDestroy() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('disable_secret_delayed_destroy', [ + $name['project'], + $name['secret'], + ]); + + $this->assertStringContainsString('Updated secret', $output); + + $secret = self::getSecret($name['project'], $name['secret']); + $this->assertNull($secret->getVersionDestroyTtl()); + } + + public function testUpdateSecretWithDelayedDestroyed() + { + $name = self::$client->parseName(self::$testSecretWithDelayedDestroyToCreateName); + + $output = $this->runFunctionSnippet('update_secret_with_delayed_destroy', [ + $name['project'], + $name['secret'], + self::$testDelayedDestroyTime + ]); + + $this->assertStringContainsString('Updated secret', $output); + + $secret = self::getSecret($name['project'], $name['secret']); + $this->assertEquals(self::$testDelayedDestroyTime, $secret->getVersionDestroyTtl()->getSeconds()); + } }