Skip to content

Commit edaf105

Browse files
WalterHublesv
authored andcommitted
Adds 'enable a crypto key version' snippet for doc purposes (GoogleCloudPlatform#901)
* Adds snippet for enabling a key version * Adds snippet for enabling a key version * Adds snippet for enabling a key version
1 parent bffa74b commit edaf105

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

kms/src/main/java/com/example/SnippetCommands.java

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public void run() throws IOException {
103103
}
104104
}
105105

106+
public static class EnableCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
107+
public void run() throws IOException {
108+
Snippets.enableCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
109+
}
110+
}
111+
106112
public static class DestroyCryptoKeyVersionCommand extends KeyVersionArgs implements Command {
107113
public void run() throws IOException {
108114
Snippets.destroyCryptoKeyVersion(projectId, locationId, keyRingId, cryptoKeyId, version);
@@ -211,6 +217,7 @@ public void run() throws IOException {
211217
@SubCommand(name = "listCryptoKeys", impl = ListCryptoKeysCommand.class),
212218
@SubCommand(name = "listCryptoKeyVersions", impl = ListCryptoKeyVersionsCommand.class),
213219
@SubCommand(name = "disableCryptoKeyVersion", impl = DisableCryptoKeyVersionCommand.class),
220+
@SubCommand(name = "enableCryptoKeyVersion", impl = EnableCryptoKeyVersionCommand.class),
214221
@SubCommand(name = "destroyCryptoKeyVersion", impl = DestroyCryptoKeyVersionCommand.class),
215222
@SubCommand(name = "restoreCryptoKeyVersion", impl = RestoreCryptoKeyVersionCommand.class),
216223
@SubCommand(name = "getKeyRingPolicy", impl = GetKeyRingPolicyCommand.class),

kms/src/main/java/com/example/Snippets.java

+30
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ public static CryptoKeyVersion disableCryptoKeyVersion(
179179
}
180180
// [END kms_disable_cryptokey_version]
181181

182+
// [START kms_enable_cryptokey_version]
183+
184+
/**
185+
* Enables the given version of the crypto key.
186+
*/
187+
public static CryptoKeyVersion enableCryptoKeyVersion(
188+
String projectId, String locationId, String keyRingId, String cryptoKeyId, String version)
189+
throws IOException {
190+
// Create the Cloud KMS client.
191+
CloudKMS kms = createAuthorizedClient();
192+
193+
// The resource name of the cryptoKey version
194+
String cryptoKeyVersion = String.format(
195+
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s",
196+
projectId, locationId, keyRingId, cryptoKeyId, version);
197+
198+
CryptoKeyVersion newVersionState = new CryptoKeyVersion()
199+
.setState("ENABLED");
200+
201+
CryptoKeyVersion response = kms.projects().locations().keyRings().cryptoKeys()
202+
.cryptoKeyVersions()
203+
.patch(cryptoKeyVersion, newVersionState)
204+
.setUpdateMask("state")
205+
.execute();
206+
207+
System.out.println(response);
208+
return response;
209+
}
210+
// [END kms_enable_cryptokey_version]
211+
182212
// [START kms_destroy_cryptokey_version]
183213

184214
/**

kms/src/test/java/com/example/SnippetsIT.java

+23
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,29 @@ public void disableCryptoKeyVersion_disables() throws Exception {
187187
KEY_RING_ID, CRYPTO_KEY_ID, version));
188188
}
189189

190+
@Test
191+
public void enableCryptoKeyVersion_enables() throws Exception {
192+
Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);
193+
194+
Matcher matcher = Pattern.compile(".*cryptoKeyVersions/(\\d+)\",\"state\":\"ENABLED\".*",
195+
Pattern.DOTALL | Pattern.MULTILINE).matcher(bout.toString().trim());
196+
assertTrue(matcher.matches());
197+
String version = matcher.group(1);
198+
199+
// Disable the new key version
200+
Snippets.disableCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);
201+
assertThat(bout.toString()).containsMatch(String.format(
202+
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"DISABLED\"",
203+
KEY_RING_ID, CRYPTO_KEY_ID, version));
204+
205+
// Enable the now-disabled key version
206+
Snippets.enableCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID, version);
207+
assertThat(bout.toString()).containsMatch(String.format(
208+
"keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s\",\"state\":\"ENABLED\"",
209+
KEY_RING_ID, CRYPTO_KEY_ID, version));
210+
211+
}
212+
190213
@Test
191214
public void destroyCryptoKeyVersion_destroys() throws Exception {
192215
Snippets.createCryptoKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, CRYPTO_KEY_ID);

0 commit comments

Comments
 (0)