1919
2020namespace Google \Cloud \Samples \Kms ;
2121
22+ use Google \ApiCore \ApiException ;
23+ use Google \Rpc \Code ;
2224use Google \Cloud \Iam \V1 \Binding ;
2325use Google \Cloud \Iam \V1 \GetIamPolicyRequest ;
2426use Google \Cloud \Iam \V1 \SetIamPolicyRequest ;
4547use Google \Cloud \Kms \V1 \MacVerifyRequest ;
4648use Google \Cloud \Kms \V1 \ProtectionLevel ;
4749use Google \Cloud \Kms \V1 \UpdateCryptoKeyRequest ;
50+ use Google \Cloud \Kms \V1 \DeleteCryptoKeyRequest ;
51+ use Google \Cloud \Kms \V1 \ListRetiredResourcesRequest ;
52+ use Google \Cloud \Kms \V1 \GetCryptoKeyRequest ;
4853use Google \Cloud \TestUtils \TestTrait ;
4954use Google \Protobuf \FieldMask ;
5055use PHPUnit \Framework \TestCase ;
@@ -820,19 +825,19 @@ public function testDeleteCryptoKey()
820825 $ client = new KeyManagementServiceClient ();
821826 $ keyRingName = $ client ->keyRingName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId );
822827 $ keyId = self ::randomId ();
823-
828+
824829 // Create an ASYMMETRIC_SIGN key (no initial version created by default for this purpose).
825830 $ key = (new CryptoKey ())
826831 ->setPurpose (CryptoKeyPurpose::ASYMMETRIC_SIGN )
827832 ->setVersionTemplate ((new CryptoKeyVersionTemplate )
828833 ->setAlgorithm (CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256 ));
829-
834+
830835 $ request = (new CreateCryptoKeyRequest ())
831836 ->setParent ($ keyRingName )
832837 ->setCryptoKeyId ($ keyId )
833838 ->setCryptoKey ($ key )
834839 ->setSkipInitialVersionCreation (true );
835-
840+
836841 $ client ->createCryptoKey ($ request );
837842
838843 // Delete it.
@@ -844,72 +849,70 @@ public function testDeleteCryptoKey()
844849 ]);
845850
846851 $ this ->assertStringContainsString ('Deleted crypto key ' , $ output );
847-
852+
848853 $ keyName = $ client ->cryptoKeyName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId , $ keyId );
849854 try {
850- $ getKeyRequest = (new \Google \Cloud \Kms \V1 \GetCryptoKeyRequest ())->setName ($ keyName );
851- $ deletedKey = $ client ->getCryptoKey ($ getKeyRequest );
852- $ this ->assertEquals (CryptoKey \State::DELETED , $ deletedKey ->getState ());
853- } catch (\Google \ApiCore \ApiException $ e ) {
854- // If the key is not found, it might be due to eventual consistency or it's effectively deleted.
855- // However, typically it SHOULD exist in DELETED state.
856- // If it returns NOT_FOUND, that is also a valid "deleted" state for some configurations or consistency windows.
857- // Let's accept NOT_FOUND as valid for this test.
858- $ this ->assertEquals (\Google \Rpc \Code::NOT_FOUND , $ e ->getCode ());
855+ $ getKeyRequest = (new GetCryptoKeyRequest ())->setName ($ keyName );
856+ $ client ->getCryptoKey ($ getKeyRequest );
857+ $ this ->fail ('Key should be deleted ' );
858+ } catch (ApiException $ e ) {
859+ $ this ->assertEquals (Code::NOT_FOUND , $ e ->getCode ());
859860 }
860861
861862 return $ keyId ;
862863 }
863864
864- /**
865- * @depends testDeleteCryptoKey
866- */
867- public function testListRetiredResources ($ deletedKeyId )
868- {
869- list (, $ output ) = $ this ->runFunctionSnippet ('list_retired_resources ' , [
870- self ::$ projectId ,
871- self ::$ locationId
872- ]);
873-
874- $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
875- $ this ->assertStringContainsString ($ deletedKeyId , $ output );
876- }
877-
878- /**
879- * @depends testDeleteCryptoKey
880- */
881- public function testGetRetiredResource ($ deletedKeyId )
865+ public function testListAndGetRetiredResource ()
882866 {
867+ // Create a key to delete
883868 $ client = new KeyManagementServiceClient ();
869+ $ keyRingName = $ client ->keyRingName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId );
870+ $ keyId = self ::randomId ();
871+ $ key = (new CryptoKey ())
872+ ->setPurpose (CryptoKeyPurpose::ASYMMETRIC_SIGN )
873+ ->setVersionTemplate ((new CryptoKeyVersionTemplate )
874+ ->setAlgorithm (CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256 ));
875+
876+ // Create key (with no initial version)
877+ $ request = (new CreateCryptoKeyRequest ())
878+ ->setParent ($ keyRingName )
879+ ->setCryptoKeyId ($ keyId )
880+ ->setCryptoKey ($ key )
881+ ->setSkipInitialVersionCreation (true );
882+ $ client ->createCryptoKey ($ request );
883+
884+ // Delete it
885+ $ keyName = $ client ->cryptoKeyName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId , $ keyId );
886+ $ deleteRequest = (new DeleteCryptoKeyRequest ())->setName ($ keyName );
887+ $ client ->deleteCryptoKey ($ deleteRequest );
888+
889+ // Find the retired resource ID first (needed for the snippet)
884890 $ parent = $ client ->locationName (self ::$ projectId , self ::$ locationId );
885- $ listRequest = (new \ Google \ Cloud \ Kms \ V1 \ ListRetiredResourcesRequest ())->setParent ($ parent );
886-
891+ $ listRequest = (new ListRetiredResourcesRequest ())->setParent ($ parent );
892+
887893 $ retiredResource = null ;
888894 foreach ($ client ->listRetiredResources ($ listRequest ) as $ res ) {
889- if (strpos ($ res ->getOriginalResource (), $ deletedKeyId ) !== false ) {
895+ if (strpos ($ res ->getOriginalResource (), $ keyId ) !== false ) {
890896 $ retiredResource = $ res ;
891897 break ;
892898 }
893899 }
894-
895- if (!$ retiredResource ) {
896- $ this ->markTestSkipped ('Could not find retired resource for retrieval test. ' );
897- return ;
898- }
900+
901+ $ this ->assertNotNull ($ retiredResource , 'Could not find retired resource for retrieval test. ' );
899902
900903 $ parts = explode ('/ ' , $ retiredResource ->getName ());
901904 $ retiredResourceId = end ($ parts );
902-
903- list (, $ output ) = $ this ->runFunctionSnippet ('get_retired_resource ' , [
905+
906+ list ($ response , $ output ) = $ this ->runFunctionSnippet ('get_retired_resource ' , [
904907 self ::$ projectId ,
905908 self ::$ locationId ,
906909 $ retiredResourceId
907910 ]);
908911
912+ $ this ->assertStringContainsString ($ keyId , $ response ->getOriginalResource ());
909913 $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
910- $ this ->assertStringContainsString ($ deletedKeyId , $ output );
911914 }
912-
915+
913916 public function testDeleteCryptoKeyVersion ()
914917 {
915918 $ this ->markTestSkipped ('Skipping deleteCryptoKeyVersion test due to complexity of destroying a key version. ' );
0 commit comments