1

我正在 GCP 中使用 Cloudrun,并想总结使用 API Gateway 创建的 API。这需要创建一个Swagger/OpenAPI v2文档,其中包含 Google 为 Cloudrun 服务生成的 URL。

如何将这些从 Pulumi 输出中获取到 swagger 文件中以作为 API Config 上传?

我最初希望使用 Moustache。但现在我正在尝试使用原始 JSON,但仍然没有成功。我已经尝试过apply()interpolate[object Object]从来没有尝试过部署服务的 url。

"x-google-backend": {
  "address": {
    "[object Object]":null
  },
  "path_translation":"APPEND_PATH_TO_ADDRESS"
}

我错过了什么?

是否有可能直接使用小胡子进行此工作,而无需使用反序列化的 Swagger?

重新创建的代码:

一个基本的招摇文件my-spec.yaml

swagger: '2.0'

info:
  title: Demo for URL Replacement
  version: 0.0.1

schemes:
  - https

paths:
  /users:
      post:
        summary: Registers a new user to the service
        operationId: createUser
        x-google-backend:
          address: {{{apiUsersUrl}}} # Output URL from CloudRun, in moustache format
          path_translation: APPEND_PATH_TO_ADDRESS
        responses:
          201:
            description: Success
        security:
        - api_key: []

securityDefinitions:
  api_key:
    type: apiKey
    name: x-api-key
    in: header

我的文件用于编写新的 API 配置。

import * as pulumi from '@pulumi/pulumi';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import { Spec } from '../types/Swagger';

export function buildApiSpecJS(usersUrl: pulumi.Output<string>) {
  const specFile = fs.readFileSync(`${__dirname}/my-spec.yaml`, 'utf8');
  const specTemplate= yaml.load(specFile) as Spec;

  usersUrl.apply((url) => {
    let pathName: keyof typeof specTemplate.paths;
    // eslint-disable-next-line guard-for-in
    for (pathName in specTemplate.paths) {
      const path = specTemplate.paths[pathName];
      let operation: keyof typeof path;
      for (operation in path) {
        if (path[operation] !== '$ref') {
          const address: string = path[operation]['x-google-backend']?.address;
          if (address === '{{{apiUsersUrl}}}') {
            path[operation]['x-google-backend'].address = pulumi.interpolate`${url}`;
          }
        }
      }
    }

    fs.writeFileSync(`${__dirname}/testfile.json`, JSON.stringify(specTemplate));
  });
}

Specfrom类型是从DefinitelyTyped定义中复制和修改的

// Changes:
//  - Operation is edited to add the Google Backend

export interface GoogleBackend {
  address: string;
  jwt_audience?: string;
  disable_auth?: boolean;
  path_translation?: string; // [ APPEND_PATH_TO_ADDRESS | CONSTANT_ADDRESS ]
  deadline?: string;
  protocol?: string; // [ http/1.1 | h2 ]
};

//
///// .... Copied Types ....
//

// ----------------------------- Operation -----------------------------------
export interface Operation {
  responses: { [responseName: string]: Response | Reference };
  summary?: string | undefined;
  description?: string | undefined;
  externalDocs?: ExternalDocs | undefined;
  operationId?: string | undefined;
  produces?: string[] | undefined;
  consumes?: string[] | undefined;
  parameters?: Array<Parameter | Reference> | undefined;
  schemes?: string[] | undefined;
  deprecated?: boolean | undefined;
  security?: Array<{ [securityDefinitionName: string]: string[] }> | undefined;
  tags?: string[] | undefined;
  'x-google-backend': GoogleBackend | undefined;  // Added
}

//
///// Copied Types continue....
//

4

1 回答 1

1

The solution I landed on was DomainMapping and DNS. I used DomainMapping to create a predictable name in each environment for the Cloud Run service. e.g. api-users.dev.example.com.

Then as I know that name ahead of time, I can use it for the Moustache replacements in the API Spec.

于 2021-08-08T09:19:36.253 回答