/** * 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. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the cryptoKey version String versionName = CryptoKeyVersionName.format( projectId, locationId, keyRingId, cryptoKeyId, version); // Retrieve the current state CryptoKeyVersion current = client.getCryptoKeyVersion(versionName); // Build a copy that updates the state to enabled CryptoKeyVersion updated = CryptoKeyVersion.newBuilder() .setName(current.getName()) .setState(CryptoKeyVersionState.ENABLED) .build(); // Create a FieldMask that only allows 'state' to be updated FieldMask fieldMask = FieldMaskUtil.fromString(CryptoKeyVersion.class, "state"); // Update the version state CryptoKeyVersion response = client.updateCryptoKeyVersion(updated, fieldMask); return response; } } // [END kms_enable_cryptokey_version]
/** * Disables the given version of the crypto key. */ public static CryptoKeyVersion disableCryptoKeyVersion( String projectId, String locationId, String keyRingId, String cryptoKeyId, String version) throws IOException { // Create the Cloud KMS client. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the cryptoKey version String versionName = CryptoKeyVersionName.format( projectId, locationId, keyRingId, cryptoKeyId, version); // Retrieve the current state CryptoKeyVersion current = client.getCryptoKeyVersion(versionName); // Build a copy that updates the state to disabled CryptoKeyVersion updated = CryptoKeyVersion.newBuilder() .setName(current.getName()) .setState(CryptoKeyVersionState.DISABLED) .build(); // Create a FieldMask that only allows 'state' to be updated FieldMask fieldMask = FieldMaskUtil.fromString(CryptoKeyVersion.class, "state"); // Update the version state CryptoKeyVersion response = client.updateCryptoKeyVersion(updated, fieldMask); return response; } } // [END kms_disable_cryptokey_version]