Search in sources :

Example 1 with KeyRingName

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

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

the class KeyManagementServiceClientTest method listImportJobsTest.

@Test
public void listImportJobsTest() throws Exception {
    ImportJob responsesElement = ImportJob.newBuilder().build();
    ListImportJobsResponse expectedResponse = ListImportJobsResponse.newBuilder().setNextPageToken("").addAllImportJobs(Arrays.asList(responsesElement)).build();
    mockKeyManagementService.addResponse(expectedResponse);
    KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
    ListImportJobsPagedResponse pagedListResponse = client.listImportJobs(parent);
    List<ImportJob> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getImportJobsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListImportJobsRequest actualRequest = ((ListImportJobsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListImportJobsPagedResponse(com.google.cloud.kms.v1.KeyManagementServiceClient.ListImportJobsPagedResponse) Test(org.junit.Test)

Example 3 with KeyRingName

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

the class KeyManagementServiceClientTest method listCryptoKeysTest.

@Test
public void listCryptoKeysTest() throws Exception {
    CryptoKey responsesElement = CryptoKey.newBuilder().build();
    ListCryptoKeysResponse expectedResponse = ListCryptoKeysResponse.newBuilder().setNextPageToken("").addAllCryptoKeys(Arrays.asList(responsesElement)).build();
    mockKeyManagementService.addResponse(expectedResponse);
    KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
    ListCryptoKeysPagedResponse pagedListResponse = client.listCryptoKeys(parent);
    List<CryptoKey> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCryptoKeysList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCryptoKeysRequest actualRequest = ((ListCryptoKeysRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListCryptoKeysPagedResponse(com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeysPagedResponse) Test(org.junit.Test)

Example 4 with KeyRingName

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

the class SyncCreateImportJobKeyringnameStringImportjob method syncCreateImportJobKeyringnameStringImportjob.

public static void syncCreateImportJobKeyringnameStringImportjob() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
        String importJobId = "importJobId1449444627";
        ImportJob importJob = ImportJob.newBuilder().build();
        ImportJob response = keyManagementServiceClient.createImportJob(parent, importJobId, importJob);
    }
}
Also used : ImportJob(com.google.cloud.kms.v1.ImportJob) KeyRingName(com.google.cloud.kms.v1.KeyRingName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 5 with KeyRingName

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

the class SyncCreateCryptoKeyKeyringnameStringCryptokey method syncCreateCryptoKeyKeyringnameStringCryptokey.

public static void syncCreateCryptoKeyKeyringnameStringCryptokey() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
        String cryptoKeyId = "cryptoKeyId-1643185255";
        CryptoKey cryptoKey = CryptoKey.newBuilder().build();
        CryptoKey response = keyManagementServiceClient.createCryptoKey(parent, cryptoKeyId, cryptoKey);
    }
}
Also used : CryptoKey(com.google.cloud.kms.v1.CryptoKey) KeyRingName(com.google.cloud.kms.v1.KeyRingName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Aggregations

KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)22 KeyRingName (com.google.cloud.kms.v1.KeyRingName)17 CryptoKey (com.google.cloud.kms.v1.CryptoKey)16 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)7 Binding (com.google.iam.v1.Binding)6 Policy (com.google.iam.v1.Policy)6 AbstractMessage (com.google.protobuf.AbstractMessage)4 Test (org.junit.Test)4 ListCryptoKeysPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeysPagedResponse)2 ListImportJobsPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListImportJobsPagedResponse)2 KeyManagementServiceBlockingStub (com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)2 KeyRing (com.google.cloud.kms.v1.KeyRing)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 CreateCryptoKeyRequest (com.google.cloud.kms.v1.CreateCryptoKeyRequest)1 CreateKeyRingRequest (com.google.cloud.kms.v1.CreateKeyRingRequest)1 GetCryptoKeyRequest (com.google.cloud.kms.v1.GetCryptoKeyRequest)1 GetKeyRingRequest (com.google.cloud.kms.v1.GetKeyRingRequest)1 ImportJob (com.google.cloud.kms.v1.ImportJob)1 KeyManagementServiceGrpc (com.google.cloud.kms.v1.KeyManagementServiceGrpc)1