ETag가 포함된 보안 비밀 삭제

지정된 이름, ETag, 모든 버전이 포함된 보안 비밀을 삭제하는 방법을 알려줍니다.

코드 샘플

Go

Secret Manager용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Secret Manager 클라이언트 라이브러리를 참조하세요.

Secret Manager에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import (
	"context"
	"fmt"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
)

// deleteSecretWithEtag deletes the secret with the given name and all of its versions.
func deleteSecretWithEtag(name, etag string) error {
	// name := "projects/my-project/secrets/my-secret"
	// etag := `"123"`

	// Create the client.
	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create secretmanager client: %w", err)
	}
	defer client.Close()

	// Build the request.
	req := &secretmanagerpb.DeleteSecretRequest{
		Name: name,
		Etag: etag,
	}

	// Call the API.
	if err := client.DeleteSecret(ctx, req); err != nil {
		return fmt.Errorf("failed to delete secret: %w", err)
	}
	return nil
}

Java

Secret Manager용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Secret Manager 클라이언트 라이브러리를 참조하세요.

Secret Manager에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import com.google.cloud.secretmanager.v1.DeleteSecretRequest;
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
import com.google.cloud.secretmanager.v1.SecretName;
import java.io.IOException;

public class DeleteSecretWithEtag {

  public static void deleteSecret() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String secretId = "your-secret-id";
    // Including the quotes is important.
    String etag = "\"1234\"";
    deleteSecret(projectId, secretId, etag);
  }

  // Delete an existing secret with the given name and etag.
  public static void deleteSecret(String projectId, String secretId, String etag)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
      // Build the secret name.
      SecretName secretName = SecretName.of(projectId, secretId);

      // Construct the request.
      DeleteSecretRequest request =
          DeleteSecretRequest.newBuilder()
              .setName(secretName.toString())
              .setEtag(etag)
              .build();

      // Delete the secret.
      client.deleteSecret(request);
      System.out.printf("Deleted secret %s\n", secretId);
    }
  }
}

Python

Secret Manager용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Secret Manager 클라이언트 라이브러리를 참조하세요.

Secret Manager에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

def delete_secret_with_etag(project_id: str, secret_id: str, etag: str) -> None:
    """
    Delete the secret with the given name, etag, and all of its versions.
    """

    # Import the Secret Manager client library and types.
    from google.cloud import secretmanager
    from google.cloud.secretmanager_v1.types import service

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret.
    name = client.secret_path(project_id, secret_id)

    # Build the request
    request = service.DeleteSecretRequest()
    request.name = name
    request.etag = etag

    # Delete the secret.
    client.delete_secret(request=request)

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.