Search in sources :

Example 1 with CreateCryptoKeyRequest

use of com.google.cloud.kms.v1.CreateCryptoKeyRequest 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 CreateCryptoKeyRequest

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

the class AsyncCreateCryptoKey method asyncCreateCryptoKey.

public static void asyncCreateCryptoKey() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        CreateCryptoKeyRequest request = CreateCryptoKeyRequest.newBuilder().setParent(KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]").toString()).setCryptoKeyId("cryptoKeyId-1643185255").setCryptoKey(CryptoKey.newBuilder().build()).setSkipInitialVersionCreation(true).build();
        ApiFuture<CryptoKey> future = keyManagementServiceClient.createCryptoKeyCallable().futureCall(request);
        // Do something.
        CryptoKey response = future.get();
    }
}
Also used : CryptoKey(com.google.cloud.kms.v1.CryptoKey) CreateCryptoKeyRequest(com.google.cloud.kms.v1.CreateCryptoKeyRequest) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 3 with CreateCryptoKeyRequest

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

the class SyncCreateCryptoKey method syncCreateCryptoKey.

public static void syncCreateCryptoKey() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        CreateCryptoKeyRequest request = CreateCryptoKeyRequest.newBuilder().setParent(KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]").toString()).setCryptoKeyId("cryptoKeyId-1643185255").setCryptoKey(CryptoKey.newBuilder().build()).setSkipInitialVersionCreation(true).build();
        CryptoKey response = keyManagementServiceClient.createCryptoKey(request);
    }
}
Also used : CryptoKey(com.google.cloud.kms.v1.CryptoKey) CreateCryptoKeyRequest(com.google.cloud.kms.v1.CreateCryptoKeyRequest) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Aggregations

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