Skip to content

Commit

Permalink
Adds 'enable a crypto key version' snippet for doc purposes (#901)
Browse files Browse the repository at this point in the history
* Adds snippet for enabling a key version

* Adds snippet for enabling a key version

* Adds snippet for enabling a key version
  • Loading branch information
WalterHub authored and lesv committed Nov 2, 2017
1 parent bffa74b commit edaf105
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kms/src/main/java/com/example/SnippetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
30 changes: 30 additions & 0 deletions kms/src/main/java/com/example/Snippets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]

/**
Expand Down
23 changes: 23 additions & 0 deletions kms/src/test/java/com/example/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit edaf105

Please sign in to comment.