Search in sources :

Example 1 with GetCryptoKeyRequest

use of com.google.cloud.kms.v1.GetCryptoKeyRequest in project java-storage by googleapis.

the class ITStorageTest method ensureKmsKeyExistsForTests.

private static String ensureKmsKeyExistsForTests(KeyManagementServiceBlockingStub kmsStub, String projectId, String location, String keyRingName, String keyName) throws StatusRuntimeException {
    String kmsKeyResourcePath = CryptoKeyName.of(projectId, location, keyRingName, keyName).toString();
    try {
        // Attempt to Get CryptoKey
        requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyResourcePath);
        GetCryptoKeyRequest getCryptoKeyRequest = GetCryptoKeyRequest.newBuilder().setName(kmsKeyResourcePath).build();
        KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetCryptoKey = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader);
        stubForGetCryptoKey.getCryptoKey(getCryptoKeyRequest);
    } catch (StatusRuntimeException ex) {
        if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) {
            String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString();
            CryptoKey cryptoKey = CryptoKey.newBuilder().setPurpose(CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT).build();
            CreateCryptoKeyRequest createCryptoKeyRequest = CreateCryptoKeyRequest.newBuilder().setCryptoKeyId(keyName).setParent(kmsKeyRingResourcePath).setCryptoKey(cryptoKey).build();
            requestParamsHeader.put(requestParamsKey, "parent=" + kmsKeyRingResourcePath);
            KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForCreateCryptoKey = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader);
            stubForCreateCryptoKey.createCryptoKey(createCryptoKeyRequest);
        } else {
            throw ex;
        }
    }
    return kmsKeyResourcePath;
}
Also used : GetCryptoKeyRequest(com.google.cloud.kms.v1.GetCryptoKeyRequest) KeyManagementServiceBlockingStub(com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoKey(com.google.cloud.kms.v1.CryptoKey) KeyManagementServiceGrpc(com.google.cloud.kms.v1.KeyManagementServiceGrpc) CreateCryptoKeyRequest(com.google.cloud.kms.v1.CreateCryptoKeyRequest) KeyManagementServiceBlockingStub(com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)

Example 2 with GetCryptoKeyRequest

use of com.google.cloud.kms.v1.GetCryptoKeyRequest in project gapic-generator-java by googleapis.

the class SyncGetCryptoKey method syncGetCryptoKey.

public static void syncGetCryptoKey() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        GetCryptoKeyRequest request = GetCryptoKeyRequest.newBuilder().setName(CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString()).build();
        CryptoKey response = keyManagementServiceClient.getCryptoKey(request);
    }
}
Also used : GetCryptoKeyRequest(com.google.cloud.kms.v1.GetCryptoKeyRequest) CryptoKey(com.google.cloud.kms.v1.CryptoKey) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 3 with GetCryptoKeyRequest

use of com.google.cloud.kms.v1.GetCryptoKeyRequest in project gapic-generator-java by googleapis.

the class AsyncGetCryptoKey method asyncGetCryptoKey.

public static void asyncGetCryptoKey() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        GetCryptoKeyRequest request = GetCryptoKeyRequest.newBuilder().setName(CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString()).build();
        ApiFuture<CryptoKey> future = keyManagementServiceClient.getCryptoKeyCallable().futureCall(request);
        // Do something.
        CryptoKey response = future.get();
    }
}
Also used : GetCryptoKeyRequest(com.google.cloud.kms.v1.GetCryptoKeyRequest) CryptoKey(com.google.cloud.kms.v1.CryptoKey) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Aggregations

CryptoKey (com.google.cloud.kms.v1.CryptoKey)3 GetCryptoKeyRequest (com.google.cloud.kms.v1.GetCryptoKeyRequest)3 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)2 CreateCryptoKeyRequest (com.google.cloud.kms.v1.CreateCryptoKeyRequest)1 KeyManagementServiceGrpc (com.google.cloud.kms.v1.KeyManagementServiceGrpc)1 KeyManagementServiceBlockingStub (com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1