删除主题的架构修订版本

本文档介绍如何删除 Pub/Sub 主题的架构修订版本。对架构的删除操作会同时删除与该架构关联的所有修订版本。

准备工作

所需的角色和权限

如需获取删除和管理架构修订版本所需的权限,请让管理员授予您项目的 Pub/Sub Editor (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. 确认删除操作。

gcloud

gcloud pubsub schemas delete-revision SCHEMA_NAME@REVISION_ID

其中:

  • REVISION_ID 是您要回滚到的修订版本。

REST

如需删除架构修订版本,请发送如下所示的 DELETE 请求:

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

响应正文应包含已删除架构资源的 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) {
  std::string const schema_id_with_revision = schema_id + "@" + revision_id;

  google::pubsub::v1::DeleteSchemaRevisionRequest request;
  request.set_name(
      pubsub::Schema(project_id, schema_id_with_revision).FullName());
  auto schema = client.DeleteSchemaRevision(request);
  if (!schema) throw std::move(schema).status();

  std::cout << "Deleted schema. Its metadata is:" << "\n"
            << schema->DebugString() << "\n";
}

Go

在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Go API 参考文档

import (
	"context"
	"fmt"
	"io"

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

func deleteSchemaRevision(w io.Writer, projectID, schemaID, revisionID string) error {
	// projectID := "my-project-id"
	// schemaID := "my-schema-id"
	// revisionID := "my-revision-id"
	ctx := context.Background()
	client, err := pubsub.NewSchemaClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
	}
	defer client.Close()

	if _, err := client.DeleteSchemaRevision(ctx, schemaID, revisionID); err != nil {
		return fmt.Errorf("client.DeleteSchema revision: %w", err)
	}
	fmt.Fprintf(w, "Deleted a schema revision: %s@%s", schemaID, revisionID)
	return nil
}

Java

在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Java 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Java API 参考文档


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

public class DeleteSchemaRevisionExample {

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

    deleteSchemaRevisionExample(projectId, schemaId);
  }

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

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

      DeleteSchemaRevisionRequest request =
          DeleteSchemaRevisionRequest.newBuilder().setName(schemaName.toString()).build();

      schemaServiceClient.deleteSchemaRevision(request);

      System.out.println("Deleted a schema revision:" + schemaName);

    } 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"
# revision_id = "your-revision-id"

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id + "@" + revision_id)

try:
    schema_client.delete_schema_revision(request={"name": schema_path})
    print(f"Deleted a schema revision:\n{schema_path}")
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 deleteSchemaRevision(schemaNameOrId, revisionId) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

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

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 deleteSchemaRevision(
  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 delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

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

后续步骤