Search in sources :

Example 1 with ListCryptoKeyVersionsRequest

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

the class KeyManagementServiceClientTest method listCryptoKeyVersionsTest.

@Test
public void listCryptoKeyVersionsTest() throws Exception {
    CryptoKeyVersion responsesElement = CryptoKeyVersion.newBuilder().build();
    ListCryptoKeyVersionsResponse expectedResponse = ListCryptoKeyVersionsResponse.newBuilder().setNextPageToken("").addAllCryptoKeyVersions(Arrays.asList(responsesElement)).build();
    mockKeyManagementService.addResponse(expectedResponse);
    CryptoKeyName parent = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
    ListCryptoKeyVersionsPagedResponse pagedListResponse = client.listCryptoKeyVersions(parent);
    List<CryptoKeyVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCryptoKeyVersionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCryptoKeyVersionsRequest actualRequest = ((ListCryptoKeyVersionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListCryptoKeyVersionsPagedResponse(com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeyVersionsPagedResponse) Test(org.junit.Test)

Example 2 with ListCryptoKeyVersionsRequest

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

the class KeyManagementServiceClientTest method listCryptoKeyVersionsTest2.

@Test
public void listCryptoKeyVersionsTest2() throws Exception {
    CryptoKeyVersion responsesElement = CryptoKeyVersion.newBuilder().build();
    ListCryptoKeyVersionsResponse expectedResponse = ListCryptoKeyVersionsResponse.newBuilder().setNextPageToken("").addAllCryptoKeyVersions(Arrays.asList(responsesElement)).build();
    mockKeyManagementService.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListCryptoKeyVersionsPagedResponse pagedListResponse = client.listCryptoKeyVersions(parent);
    List<CryptoKeyVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCryptoKeyVersionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCryptoKeyVersionsRequest actualRequest = ((ListCryptoKeyVersionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ByteString(com.google.protobuf.ByteString) ListCryptoKeyVersionsPagedResponse(com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeyVersionsPagedResponse) Test(org.junit.Test)

Example 3 with ListCryptoKeyVersionsRequest

use of com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method afterAll.

@AfterClass
public static void afterAll() throws IOException {
    Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
    // Iterate over each key ring's key's crypto key versions and destroy.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
            if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
                CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
                FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
                client.updateCryptoKey(keyWithoutRotation, fieldMask);
            }
            ListCryptoKeyVersionsRequest listVersionsRequest = ListCryptoKeyVersionsRequest.newBuilder().setParent(key.getName()).setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED").build();
            for (CryptoKeyVersion version : client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
                client.destroyCryptoKeyVersion(version.getName());
            }
        }
    }
}
Also used : ListCryptoKeyVersionsRequest(com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest) CryptoKey(com.google.cloud.kms.v1.CryptoKey) CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) FieldMask(com.google.protobuf.FieldMask) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) AfterClass(org.junit.AfterClass)

Example 4 with ListCryptoKeyVersionsRequest

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

the class KeyManagementServiceClientTest method listCryptoKeyVersionsTest.

@Test
public void listCryptoKeyVersionsTest() throws Exception {
    CryptoKeyVersion responsesElement = CryptoKeyVersion.newBuilder().build();
    ListCryptoKeyVersionsResponse expectedResponse = ListCryptoKeyVersionsResponse.newBuilder().setNextPageToken("").addAllCryptoKeyVersions(Arrays.asList(responsesElement)).build();
    mockKeyManagementService.addResponse(expectedResponse);
    CryptoKeyName parent = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
    ListCryptoKeyVersionsPagedResponse pagedListResponse = client.listCryptoKeyVersions(parent);
    List<CryptoKeyVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCryptoKeyVersionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCryptoKeyVersionsRequest actualRequest = ((ListCryptoKeyVersionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListCryptoKeyVersionsPagedResponse(com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeyVersionsPagedResponse) Test(org.junit.Test)

Example 5 with ListCryptoKeyVersionsRequest

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

the class SnippetsIT method afterAll.

@AfterClass
public static void afterAll() throws IOException {
    Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
    // Iterate over each key ring's key's crypto key versions and destroy.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
            if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
                CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
                FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
                client.updateCryptoKey(keyWithoutRotation, fieldMask);
            }
            ListCryptoKeyVersionsRequest listVersionsRequest = ListCryptoKeyVersionsRequest.newBuilder().setParent(key.getName()).setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED").build();
            for (CryptoKeyVersion version : client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
                client.destroyCryptoKeyVersion(version.getName());
            }
        }
    }
}
Also used : ListCryptoKeyVersionsRequest(com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest) CryptoKey(com.google.cloud.kms.v1.CryptoKey) CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) FieldMask(com.google.protobuf.FieldMask) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) AfterClass(org.junit.AfterClass)

Aggregations

CryptoKeyVersion (com.google.cloud.kms.v1.CryptoKeyVersion)4 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)4 ListCryptoKeyVersionsPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeyVersionsPagedResponse)4 ListCryptoKeyVersionsRequest (com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Test (org.junit.Test)4 CryptoKey (com.google.cloud.kms.v1.CryptoKey)2 ByteString (com.google.protobuf.ByteString)2 FieldMask (com.google.protobuf.FieldMask)2 AfterClass (org.junit.AfterClass)2 ListCryptoKeyVersionsResponse (com.google.cloud.kms.v1.ListCryptoKeyVersionsResponse)1