Search in sources :

Example 11 with Secret

use of com.google.cloud.secretmanager.v1beta1.Secret in project java-secretmanager by googleapis.

the class SecretManagerServiceClientTest method listSecretVersionsTest.

@Test
public void listSecretVersionsTest() throws Exception {
    SecretVersion responsesElement = SecretVersion.newBuilder().build();
    ListSecretVersionsResponse expectedResponse = ListSecretVersionsResponse.newBuilder().setNextPageToken("").addAllVersions(Arrays.asList(responsesElement)).build();
    mockSecretManagerService.addResponse(expectedResponse);
    SecretName parent = SecretName.of("[PROJECT]", "[SECRET]");
    ListSecretVersionsPagedResponse pagedListResponse = client.listSecretVersions(parent);
    List<SecretVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getVersionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockSecretManagerService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListSecretVersionsRequest actualRequest = ((ListSecretVersionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListSecretVersionsPagedResponse(com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient.ListSecretVersionsPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 12 with Secret

use of com.google.cloud.secretmanager.v1beta1.Secret in project java-secretmanager by googleapis.

the class ITSystemTest method tearDown.

@AfterClass
public static void tearDown() {
    /* destroy secret version */
    DestroySecretVersionRequest versionRequest = DestroySecretVersionRequest.newBuilder().setName(formattedSecretVersion).build();
    SecretVersion actualSecretVersion = client.destroySecretVersion(versionRequest);
    assertEquals(secretVersion.getCreateTime(), actualSecretVersion.getCreateTime());
    assertEquals(SecretVersion.State.DESTROYED, actualSecretVersion.getState());
    /* delete secret */
    DeleteSecretRequest secretRequest = DeleteSecretRequest.newBuilder().setName(formattedSecretName).build();
    client.deleteSecret(secretRequest);
    client.close();
}
Also used : SecretVersion(com.google.cloud.secretmanager.v1beta1.SecretVersion) DestroySecretVersionRequest(com.google.cloud.secretmanager.v1beta1.DestroySecretVersionRequest) DeleteSecretRequest(com.google.cloud.secretmanager.v1beta1.DeleteSecretRequest) AfterClass(org.junit.AfterClass)

Example 13 with Secret

use of com.google.cloud.secretmanager.v1beta1.Secret in project java-secretmanager by googleapis.

the class ITSystemTest method listSecretsTest.

@Test
public void listSecretsTest() {
    ListSecretsRequest request = ListSecretsRequest.newBuilder().setParent(PROJECT_NAME).build();
    for (Secret actualSecret : client.listSecrets(request).iterateAll()) {
        if (secret.getName().equals(actualSecret.getName())) {
            assertEquals(secret.getReplication(), actualSecret.getReplication());
            assertEquals(secret.getCreateTime(), actualSecret.getCreateTime());
        }
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1beta1.Secret) ListSecretsRequest(com.google.cloud.secretmanager.v1beta1.ListSecretsRequest) Test(org.junit.Test)

Example 14 with Secret

use of com.google.cloud.secretmanager.v1beta1.Secret in project java-secretmanager by googleapis.

the class ITSystemTest method getSecretTest.

@Test
public void getSecretTest() {
    GetSecretRequest request = GetSecretRequest.newBuilder().setName(formattedSecretName).build();
    Secret actualSecret = client.getSecret(request);
    assertEquals(secret.getName(), actualSecret.getName());
    assertEquals(secret.getReplication(), actualSecret.getReplication());
    assertEquals(secret.getCreateTime(), actualSecret.getCreateTime());
}
Also used : Secret(com.google.cloud.secretmanager.v1beta1.Secret) GetSecretRequest(com.google.cloud.secretmanager.v1beta1.GetSecretRequest) Test(org.junit.Test)

Example 15 with Secret

use of com.google.cloud.secretmanager.v1beta1.Secret in project java-secretmanager by googleapis.

the class Quickstart method quickstart.

public void quickstart(String projectId, String secretId) throws Exception {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Create the parent secret.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        // Add a secret version.
        SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello world!")).build();
        SecretVersion addedVersion = client.addSecretVersion(createdSecret.getName(), payload);
        // Access the secret version.
        AccessSecretVersionResponse response = client.accessSecretVersion(addedVersion.getName());
        // Print the secret payload.
        // 
        // WARNING: Do not print the secret in a production environment - this
        // snippet is showing how to access the secret material.
        String data = response.getPayload().getData().toStringUtf8();
        System.out.printf("Plaintext: %s\n", data);
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ByteString(com.google.protobuf.ByteString) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Aggregations

Secret (com.google.cloud.secretmanager.v1.Secret)21 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)11 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)11 Test (org.junit.Test)6 SecretName (com.google.cloud.secretmanager.v1.SecretName)5 ByteString (com.google.protobuf.ByteString)5 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)4 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 FieldMask (com.google.protobuf.FieldMask)3 Secret (com.marcnuri.yakc.model.io.k8s.api.core.v1.Secret)3 Test (org.junit.jupiter.api.Test)3 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)2 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)2 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)2 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)2 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)2 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient.ListSecretsPagedResponse)2 Configuration (com.marcnuri.yakc.config.Configuration)2 Node (com.marcnuri.yakc.model.io.k8s.api.core.v1.Node)2