스키마 버전 롤백

이 문서에서는 Pub/Sub 주제의 스키마를 롤백하는 방법을 보여줍니다.

롤백 작업을 수행하면 지정된 이전 버전과 정확히 동일한 스키마 정의를 사용하여 다른 스키마 버전을 만들 수 있습니다.

시작하기 전에

필수 역할 및 권한

스키마를 롤백하고 관리하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 Pub/Sub 편집자(roles/pubsub.editor) IAM 역할을 부여해 달라고 요청하세요. 역할 부여에 대한 자세한 내용은 액세스 관리를 참조하세요.

이 사전 정의된 역할에는 스키마를 롤백하고 관리하는 데 필요한 권한이 포함되어 있습니다. 필요한 정확한 권한을 보려면 필수 권한 섹션을 확장하세요.

필수 권한

스키마를 롤백하고 관리하려면 다음 권한이 필요합니다.

  • 스키마 만들기: pubsub.schemas.create
  • 주제에 스키마 연결: pubsub.schemas.attach
  • 스키마 버전 커밋: pubsub.schemas.commit
  • 스키마 또는 스키마 버전 삭제: pubsub.schemas.delete
  • 스키마 또는 스키마 버전 가져오기: pubsub.schemas.get
  • 스키마 나열: pubsub.schemas.list
  • 스키마 버전 나열: pubsub.schemas.listRevisions
  • 스키마 롤백: pubsub.schemas.rollback
  • 메시지 유효성 검사: pubsub.schemas.validate
  • 스키마의 IAM 정책 가져오기: pubsub.schemas.getIamPolicy
  • 스키마의 IAM 정책 구성: pubsub.schemas.setIamPolicy

커스텀 역할이나 다른 사전 정의된 역할을 사용하여 이 권한을 부여받을 수도 있습니다.

사용자, 그룹, 도메인, 서비스 계정과 같은 주 구성원에 역할 및 권한을 부여할 수 있습니다. 한 프로젝트에서 스키마를 만들고 이를 다른 프로젝트에 있는 주제에 연결할 수 있습니다. 각 프로젝트에 필요한 권한이 있는지 확인합니다.

스키마 버전 롤백

Google Cloud 콘솔, gcloud CLI, Pub/Sub API, Cloud 클라이언트 라이브러리를 사용하여 스키마를 롤백할 수 있습니다. 다음 단계를 따르세요.

콘솔

  1. Google Cloud 콘솔에서 Pub/Sub 스키마 페이지로 이동합니다.

    스키마로 이동

  2. 기존 스키마의 이름을 클릭합니다.

    스키마의 스키마 세부정보 페이지가 열립니다.

  3. 롤백을 클릭합니다.

    스키마 롤백 대화상자가 열립니다.

  4. 스키마를 롤백하려는 버전을 선택합니다.

  5. 확인을 클릭하여 롤백 작업을 저장합니다.

    롤백 작업에 지정된 스키마를 사용하여 새 버전이 생성됩니다.

  6. 스키마 세부정보 페이지에서 스키마의 최신 버전과 롤백 작업의 소스로 선택한 버전을 선택합니다.

  7. 차이점 보기를 클릭합니다.

    두 스키마가 동일한지 확인할 수 있습니다.

    허용된 마지막 버전 필드를 업데이트하여 바로 전에 만든 스키마 버전을 주제 유효성을 검사하기 위한 마지막 버전으로 사용할 수 있습니다.

gcloud

gcloud pubsub schemas rollback SCHEMA_ID \
        --revision-id=REVISION_ID

각 항목의 의미는 다음과 같습니다.

  • REVISION_ID는 롤백할 버전입니다.

REST

스키마를 롤백하려면 다음과 같이 POST 요청을 보냅니다.

POST https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/SCHEMA_ID:rollback
Authorization: Bearer $(gcloud auth application-default print-access-token)
Content-Type: application/json --data @response-body.json

요청 본문에 다음 필드를 지정합니다.

{
  "revisionId": REVISION_KD
}

각 항목의 의미는 다음과 같습니다.

  • REVISION_KD는 롤백할 버전의 ID입니다.

응답 본문에는 스키마 리소스의 JSON 표현이 포함되어야 합니다.

C++

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 C++ 설정 안내를 따르세요. 자세한 내용은 Pub/Sub C++ API 참고 문서를 확인하세요.

namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
   std::string const& schema_id, std::string const& revision_id) {
  google::pubsub::v1::RollbackSchemaRequest request;
  request.set_name(pubsub::Schema(project_id, schema_id).FullName());
  request.set_revision_id(revision_id);
  auto schema = client.RollbackSchema(request);
  if (!schema) throw std::move(schema).status();

  std::cout << "Rolledback schema. Created a new schema and its metadata is:"
            << "\n"
            << schema->DebugString() << "\n";
}

Go

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 Go 설정 안내를 따르세요. 자세한 내용은 Pub/Sub Go API 참고 문서를 참조하세요.

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsub"
)

// rollbackSchema creates a new schema revision that is a copy of the provided revisionID.
func rollbackSchema(w io.Writer, projectID, schemaID, revisionID string) error {
	// projectID := "my-project-id"
	// schemaID := "my-schema"
	// revisionID := "a1b2c3d4"
	ctx := context.Background()
	client, err := pubsub.NewSchemaClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
	}
	defer client.Close()

	s, err := client.RollbackSchema(ctx, schemaID, revisionID)
	if err != nil {
		return fmt.Errorf("RollbackSchema: %w", err)
	}
	fmt.Fprintf(w, "Rolled back a schema: %#v\n", s)
	return nil
}

Java

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 자바 설정 안내를 따르세요. 자세한 내용은 Pub/Sub Java API 참고 문서를 참조하세요.


import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaName;
import java.io.IOException;

public class RollbackSchemaExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project";
    String schemaId = "your-schema";
    String revisionId = "your-revision";

    rollbackSchemaExample(projectId, schemaId, revisionId);
  }

  public static void rollbackSchemaExample(String projectId, String schemaId, String revisionId)
      throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);

    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {

      Schema schema = schemaServiceClient.rollbackSchema(schemaName, revisionId);

      System.out.println("Rolled back a schema:" + schema);

    } catch (NotFoundException e) {
      System.out.println(schemaName + "not found.");
    }
  }
}

Python

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 Python 설정 안내를 따르세요. 자세한 내용은 Pub/Sub Python API 참고 문서를 참조하세요.

from google.api_core.exceptions import NotFound
from google.cloud.pubsub import SchemaServiceClient

# TODO(developer): Replace these variables before running the sample.
# project_id = "your-project-id"
# schema_id = "your-schema-id"
# schema_revision_id = "your-schema-revision-id"

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id)

try:
    result = schema_client.rollback_schema(
        request={"name": schema_path, "revision_id": schema_revision_id}
    )
    print(f"Rolled back a schema revision:\n{result}")
except NotFound:
    print(f"{schema_id} not found.")

Node.js

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 Node.js 설정 안내를 따르세요. 자세한 내용은 Pub/Sub Node.js API 참고 문서를 참조하세요.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function rollbackSchema(schemaNameOrId, revisionId) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to roll back the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.rollbackSchema({
    name,
    revisionId,
  });

  console.log(`Schema ${name} revision ${revisionId} rolled back.`);
}

Node.js

이 샘플을 시도하기 전에 빠른 시작: 클라이언트 라이브러리 사용의 Node.js 설정 안내를 따르세요. 자세한 내용은 Pub/Sub Node.js API 참고 문서를 참조하세요.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
import {PubSub} from '@google-cloud/pubsub';

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function rollbackSchema(schemaNameOrId: string, revisionId: string) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to roll back the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.rollbackSchema({
    name,
    revisionId,
  });

  console.log(`Schema ${name} revision ${revisionId} rolled back.`);
}

다음 단계