Regionale Secret-Version mit Etag aktivieren

Version eines regionalen Secrets mit Etag aktivieren

Codebeispiel

Go

Informationen zum Installieren und Verwenden der Clientbibliothek für Secret Manager finden Sie unter Secret Manager-Clientbibliotheken.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Secret Manager zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import (
	"context"
	"fmt"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
	"google.golang.org/api/option"
)

// enableSecretVersionWithEtag enables the given secret version, enabling it to be
// accessed after previously being disabled. Other secrets versions are
// unaffected.
func EnableRegionalSecretVersionWithEtag(projectId, locationId, secretId, versionId, etag string) error {
	// name := "projects/my-project/locations/my-location/secrets/my-secret/versions/5"
	// etag := `"123"`

	// Create the client.
	ctx := context.Background()
	//Endpoint to send the request to regional server
	endpoint := fmt.Sprintf("secretmanager.%s.rep.googleapis.com:443", locationId)
	client, err := secretmanager.NewClient(ctx, option.WithEndpoint(endpoint))

	if err != nil {
		return fmt.Errorf("failed to create regional secretmanager client: %w", err)
	}
	defer client.Close()

	name := fmt.Sprintf("projects/%s/locations/%s/secrets/%s/versions/%s", projectId, locationId, secretId, versionId)
	// Build the request.
	req := &secretmanagerpb.EnableSecretVersionRequest{
		Name: name,
		Etag: etag,
	}

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

Java

Informationen zum Installieren und Verwenden der Clientbibliothek für Secret Manager finden Sie unter Secret Manager-Clientbibliotheken.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Secret Manager zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import com.google.cloud.secretmanager.v1.EnableSecretVersionRequest;
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
import com.google.cloud.secretmanager.v1.SecretVersion;
import com.google.cloud.secretmanager.v1.SecretVersionName;
import java.io.IOException;

public class EnableRegionalSecretVersionWithEtag {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.

    // Your GCP project ID.
    String projectId = "your-project-id";
    // Location of the secret.
    String locationId = "your-location-id";
    // Resource ID of the secret.
    String secretId = "your-secret-id";
    // Version of the Secret ID you want to enable.
    String versionId = "your-version-id";
    // Etag associated with the secret. Quotes should be included as part of the string.
    String etag = "\"1234\"";
    enableRegionalSecretVersionWithEtag(projectId, locationId, secretId, versionId, etag);
  }

  // Enable an existing secret version.
  public static SecretVersion enableRegionalSecretVersionWithEtag(
      String projectId, String locationId, String secretId, String versionId, String etag)
      throws IOException {

    // Endpoint to call the regional secret manager sever
    String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
    SecretManagerServiceSettings secretManagerServiceSettings =
        SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();

    // Initialize the client that will be used to send requests. This client only needs to be
    // created once, and can be reused for multiple requests.
    try (SecretManagerServiceClient client = 
        SecretManagerServiceClient.create(secretManagerServiceSettings)) {
      // Build the name from the version.
      SecretVersionName secretVersionName = 
          SecretVersionName.ofProjectLocationSecretSecretVersionName(
          projectId, locationId, secretId, versionId);

      // Build the request.
      EnableSecretVersionRequest request =
          EnableSecretVersionRequest.newBuilder()
              .setName(secretVersionName.toString())
              .setEtag(etag)
              .build();

      // Enable the secret version.
      SecretVersion version = client.enableSecretVersion(request);
      System.out.printf("Enabled regional secret version %s\n", version.getName());

      return version;
    }
  }
}

Ruby

Informationen zum Installieren und Verwenden der Clientbibliothek für Secret Manager finden Sie unter Secret Manager-Clientbibliotheken.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Secret Manager zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

# project_id  = "YOUR-GOOGLE-CLOUD-PROJECT"  # (e.g. "my-project")
# location_id = "YOUR-GOOGLE-CLOUD-LOCATION" # (e.g. "us-west1")
# secret_id   = "YOUR-SECRET-ID"             # (e.g. "my-secret")
# version_id  = "YOUR-VERSION"               # (e.g. "5" or "latest")
# etag        = "YOUR-ETAG-ASSOCIATED-WITH-SECRET"  # (e.g. "\"1234\"")

# Require the Secret Manager client library.
require "google/cloud/secret_manager"

# Endpoint for the regional secret manager service.
api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com"

# Create the Secret Manager client.
client = Google::Cloud::SecretManager.secret_manager_service do |config|
  config.endpoint = api_endpoint
end

# Build the resource name of the secret version.
name = client.secret_version_path(
  project:        project_id,
  location:       location_id,
  secret:         secret_id,
  secret_version: version_id
)

# Enable the secret version.
response = client.enable_secret_version name: name, etag: etag

# Print a success message.
puts "Enabled regional secret version: #{response.name}"

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud -Produkte finden Sie im Google Cloud Beispielbrowser.