From edaf1057d87c72daad79a6ba63b245a83b4c6810 Mon Sep 17 00:00:00 2001 From: Walter Poupore Date: Thu, 2 Nov 2017 15:17:02 -0700 Subject: [PATCH] Adds 'enable a crypto key version' snippet for doc purposes (#901) * Adds snippet for enabling a key version * Adds snippet for enabling a key version * Adds snippet for enabling a key version --- .../java/com/example/SnippetCommands.java | 7 +++++ kms/src/main/java/com/example/Snippets.java | 30 +++++++++++++++++++ kms/src/test/java/com/example/SnippetsIT.java | 23 ++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/kms/src/main/java/com/example/SnippetCommands.java b/kms/src/main/java/com/example/SnippetCommands.java index a69b769c8f9..2f472934399 100644 --- a/kms/src/main/java/com/example/SnippetCommands.java +++ b/kms/src/main/java/com/example/SnippetCommands.java @@ -103,6 +103,12 @@ public void run() throws IOException { } } + public static class EnableCryptoKeyVersionCommand extends KeyVersionArgs implements Command { + public void run() throws IOException { + Snippets.enableCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version); + } + } + public static class DestroyCryptoKeyVersionCommand extends KeyVersionArgs implements Command { public void run() throws IOException { Snippets.destroyCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version); @@ -211,6 +217,7 @@ public void run() throws IOException { @SubCommand(name = "listCryptoKeys", impl = ListCryptoKeysCommand.class), @SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class), @SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class), + @SubCommand(name = "enableCryptoKeyVersion", impl = EnableCryptoKeyVersionCommand.class), @SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class), @SubCommand(name = "restoreCryptoKeyVersion", impl = RestoreCryptoKeyVersionCommand.class), @SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class), diff --git a/kms/src/main/java/com/example/Snippets.java b/kms/src/main/java/com/example/Snippets.java index 8e93f4c6fb9..44f05be45e8 100644 --- a/kms/src/main/java/com/example/Snippets.java +++ b/kms/src/main/java/com/example/Snippets.java @@ -179,6 +179,36 @@ public static CryptoKeyVersion disableCryptoKeyVersion( } // [END kms_disable_cryptokey_version] + // [START kms_enable_cryptokey_version] + + /** + * Enables the given version of the crypto key. + */ + public static CryptoKeyVersion enableCryptoKeyVersion( + String projectId, String locationId, String keyRingId, String cryptoKeyId, String version) + throws IOException { + // Create the Cloud KMS client. + CloudKMS kms = createAuthorizedClient(); + + // The resource name of the cryptoKey version + String cryptoKeyVersion = String.format( + "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s", + projectId, locationId, keyRingId, cryptoKeyId, version); + + CryptoKeyVersion newVersionState = new CryptoKeyVersion() + .setState("ENABLED"); + + CryptoKeyVersion response = kms.projects().locations().keyRings().cryptoKeys() + .cryptoKeyVersions() + .patch(cryptoKeyVersion, newVersionState) + .setUpdateMask("state") + .execute(); + + System.out.println(response); + return response; + } + // [END kms_enable_cryptokey_version] + // [START kms_destroy_cryptokey_version] /** diff --git a/kms/src/test/java/com/example/SnippetsIT.java b/kms/src/test/java/com/example/SnippetsIT.java index b0577a8817a..60a037f9673 100644 --- a/kms/src/test/java/com/example/SnippetsIT.java +++ b/kms/src/test/java/com/example/SnippetsIT.java @@ -187,6 +187,29 @@ public void disableCryptoKeyVersion_disables() throws Exception { KEY_RING_ID, CRYPTO_KEY_ID, version)); } + @Test + public void enableCryptoKeyVersion_enables() throws Exception { + Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID); + + Matcher matcher = Pattern.compile(".*cryptoKeyVersions/(\\d+)\",\"state\":\"ENABLED\".*", + Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim()); + assertTrue(matcher.matches()); + String version = matcher.group(1); + + // Disable the new key version + Snippets.disableCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version); + assertThat(bout.toString()).containsMatch(String.format( + "keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DISABLED\"", + KEY_RING_ID, CRYPTO_KEY_ID, version)); + + // Enable the now-disabled key version + Snippets.enableCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version); + assertThat(bout.toString()).containsMatch(String.format( + "keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"ENABLED\"", + KEY_RING_ID, CRYPTO_KEY_ID, version)); + + } + @Test public void destroyCryptoKeyVersion_destroys() throws Exception { Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);