调用 REST API

在本指南中,您将学习如何调用 Private Catalog Producer REST API。

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Deployment Manager and Compute Engine APIs.

    Enable the APIs

配置身份验证

作为云管理员,您可以调用 REST 对 Private Catalog 执行操作。首先,设置您的访问凭据:

  1. 获取您的客户端 ID 和密钥
  2. 获取访问令牌
  3. 请务必在范围参数中发送 https://www.googleapis.com/auth/cloud-platform

创建目录

如需创建目录,请运行以下命令,并将 <token> 替换为您的 OAuth2 访问令牌,并将 <organization-ID> 替换为您的组织 ID:

curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d "{parent:"//cloudresourcemanager.googleapis.com/organizations/<organization-ID>", display_name:"Mobile catalog", description: "description"}" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs

响应正文包含类似于以下输出的数据:

{
  "name": "operations/ccb.515502c5-81ad-4226-8037-1b802eed8f69"
}

这是操作名称。您可以通过移除前缀 operation/ 来从操作名称派生操作 ID。在上一个示例响应中,操作 ID 为 ccb.515502c5-81ad-4226-8037-1b802eed8f69。您可以使用操作 ID 检查目录创建状态。

检查目录创建操作的状态

如需创建目录,请运行以下命令,并将 <operation-ID> 替换为上一个响应中的操作 ID:

curl -s -H "Authorization: Bearer <token>" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/operations/<operation-ID>

列出您组织的目录

如需列出您有权访问的目录,请在组织范围内运行以下命令并将 <organization-number> 替换为组织编号:

curl -s -H "Authorization: Bearer <token>" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs?parent=//cloudresourcemanager.googleapis.com/organizations/<organization-number>

响应正文包含类似于以下输出的数据:

{
  "catalogs": [
    {
      "name": "catalogs/<catalog-ID>",
      "parent": "//cloudresourcemanager.googleapis.com/organizations/<organization-ID>",
      "displayName": "Mobile catalog",
      "description": "Mobile-related applications",
      "createTime": "2019-01-01T22:05:19.541Z",
      "updateTime": "2019-01-01T22:05:26.097Z"
    },
    {
      "name": "catalogs/<catalog ID>",
      "parent": "//cloudresourcemanager.googleapis.com/organizations/<organization-ID>",
      "displayName": "Development tools",
      "description": "Applications and tools for developers",
      "createTime": "2019-01-06T00:08:06.090Z",
      "updateTime": "2019-01-06T00:08:08.069Z"
    },
    {
      "name": "catalogs/<catalog ID>",
      "parent": "//cloudresourcemanager.googleapis.com/organizations/<organization-ID>",
      "displayName": "Data science catalog",
      "description": "Applications for data scientists",
      "createTime": "2019-01-06T00:09:45.319Z",
      "updateTime": "2019-01-06T00:09:48.050Z"
    },
    {
      "name": "catalogs/<catalog ID>",
      "parent": "//cloudresourcemanager.googleapis.com/organizations/<organization-ID>",
      "displayName": "Training catalog",
      "description": "Training applications and software",
      "createTime": "2019-01-06T22:13:38.261Z",
      "updateTime": "2019-01-06T22:13:46.291Z"
    },
    {
      "name": "catalogs/<catalog-ID>",
      "parent": "//cloudresourcemanager.googleapis.com/organizations/<organization-ID>",
      "displayName": "Media catalog",
      "description": "Applications for handling audio and visual media",
      "createTime": "2019-01-07T00:15:11.949Z",
      "updateTime": "2019-01-07T00:15:15.426Z"
    }
  ]
}

获取有关特定目录的信息

如需获取有关目录的信息,请运行以下命令,并将 <catalog-ID> 替换为目录 ID,以便列出组织的目录:

curl -s -H "Authorization: Bearer <token>" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-ID>

响应正文包含类似于以下输出的数据:

{
  "name": "catalogs/<catalog-ID>",
  "parent": "//cloudresourcemanager.googleapis.com/organizations/<organizaton-ID>",
  "displayName": "Data science catalog",
  "description": "Applications for data scientists",
  "createTime": "2019-01-01T22:05:19.541Z",
  "updateTime": "2019-01-01T22:05:26.097Z"
}

创建产品

如需创建产品,请运行以下命令,并将 <catalog-ID> 替换为 API 调用的响应中提供的目录 ID,以列出您组织的目录:

curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d @/tmp/rest_create_product_request https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-ID>/products

以下示例展示了 /tmp/rest_create_product_request 文件的内容:

{
  name:"catalogs/<catalog-ID>/products/<product-name>",
  asset_type:"google.deploymentmanager.Template",
  display_metadata:{
    name:"<product-name>",
    description:"Sample Description"
  }
}

列出目录中的产品

如需列出目录中的产品,请运行以下命令,并将 <catalog-ID> 替换为 API 调用的响应中的目录 ID,以列出您的组织的目录:

curl -s -H "Authorization: Bearer <token>" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-ID>/products

响应正文包含类似于以下输出的数据:

{
  "products": [
    {
      "name": "catalogs/<catalog-ID>/products/dm-product",
      "assetType": "google.deploymentmanager.Template",
      "displayMetadata": {
        "name": "sample-metadata-name",
        "description": "sample-metadata-description",
        "tagline": "sample-metadata-tagline",
        "support_info": "sample-metadata-support-info"
      },
      "createTime": "2019-01-04T19:36:02.573Z"
    },
    {
      "name": "catalogs/<catalog-ID>/products/listingonly",
      "assetType": "google.private-catalog.ListingOnly",
      "displayMetadata": {
        "name": "listing-based product",
        "description": "description",
        "tagline": "tagline",
        "support_info": "support_info",
        "signup_url": "https://cloud.google.com/launcher",
        "documentations": [
          {
            "url": "https://cloud.google.com/launcher"
          }
        ]
      },
      "createTime": "2019-01-06T00:25:44.722Z"
    }
  ]
}

创建产品版本

如需创建产品版本,请运行以下命令,将 <catalog-ID> 替换为目录 ID,并将 <product-name> 替换为要为其创建版本的产品的名称:

curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d @/tmp/rest_create_version_request https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-ID>/products/<product-name>/versions

以下示例展示了 /tmp/rest_create_version_request 文件的内容:

{
  name: "catalogs/<catalog-ID>/products/<product-name>/versions/v1",
  description: "v1",
  original_asset: {
        imports: [
                {
                  name: "my-template",
                  content: "resources:\n name: \'awesome template\'\n  properties:\n",
                },
                {
                  name: "my-template.schema",
                  content: ""
                }
        ],
        mainTemplate: "my-template",
        templateFileType: "JINJA"
  }
}

响应正文包含有关生成的操作的数据,类似于以下输出:

{
  "name": "operations/cvb.efcd831d-257d-43f7-8650-f7016c7469cb"
}

列出产品版本

如需列出产品的版本,请运行以下命令,将 <catalog-ID> 替换为目录 ID,并将 <product-name> 替换为要为其列出版本的产品的名称:

curl -s -H "Authorization: Bearer <token>" https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-ID>/products/<product-name>/versions

响应正文包含类似于以下输出的数据:

{
  "versions": [
    {
      "name": "catalogs/<catalog-ID>/products/<product-name>/versions/v1",
      "createTime": "2019-01-04T19:37:47.519Z",
      "updateTime": "2019-01-04T19:37:47.519Z"
    }
  ]
}

上传产品图标

要上传产品图标,请运行以下命令,将 <catalog-ID> 替换为目录 ID,并将 <product-name> 替换为您要为其上传图标的产品的名称:

curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d @/tmp/rest_upload_icon_request https://cloudprivatecatalogproducer.googleapis.com/v1beta1/catalogs/<catalog-id>/products/<product-name>/icons:upload

以下示例展示了 /tmp/rest_upload_icon_request 文件的内容:

{
  icon_bytes: "iVBORw0KGgoA...Jggg=="
}