0

The problem I'm trying to solve, is this:

I want to be able to deploy a new API Gateway configuration-file, that is the OpenAPI 2.0 (Swagger) specification, which API Gateway uses to set up the endpoints. I want to do this through code and not the CLI because I require multiple members of a team to be able to deploy new revisions at high speed, and preferably automatically whenever, say, pushing to a main-branch.

I've looked at the following documentation so far: https://github.com/googleapis/nodejs-api-gateway/tree/main/samples/generated/v1

Mainly, all their examples look more or less the same, i.e. as an example - getting an API Config:

import { ApiGatewayServiceClient } from '@google-cloud/api-gateway';

function main() {
  // [START apigateway_v1_generated_ApiGatewayService_GetApiConfig_async]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. Resource name of the form:
   *  `projects/* /locations/global/apis/* /configs/*`
   */
  const name = ''projects/<my_project_id>/locations/global/apis/<my_API_name>/configs/<API_config_name>''
  /**
   *  Specifies which fields of the API Config are returned in the response.
   *  Defaults to `BASIC` view.
   */
  const view = {}

  // Instantiates a client
  const apigatewayClient = new ApiGatewayServiceClient();

  async function callGetApiConfig() {
    // Construct request
    const request = {
      name,
    };

    // Run request
    const response = await apigatewayClient.getApiConfig(request);
    console.log(response);
  }

  callGetApiConfig();
  // [END apigateway_v1_generated_ApiGatewayService_GetApiConfig_async]
}

The result then looks like this:

Output from apiConfig request

Now what I don't understand, is why many of the fields of the response are empty (grcpServices, openapiDocuments, managedServiceConfigs), especially openapiDocuments, because I know for a fact that an OpenAPI YAML spec is present on GCP. For context, I was trying to see what the response would look like in order to find a solution to the main problem.

That leads me to my second question. As I mentioned, I'm trying to deploy/add a new OpenAPI spec (since you have to add a new one if you want to add new stuff) with the same library. The example on how to create an API Config, given from the documentation, is the following:

import { ApiGatewayServiceClient } from '@google-cloud/api-gateway';

function main() {
  // [START apigateway_v1_generated_ApiGatewayService_CreateApiConfig_async]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. Parent resource of the API Config, of the form:
   *  `projects/* /locations/global/apis/*`
   */
  const parent = 'projects/<project_id>/locations/global/apis/<my_API_name>'
  /**
   *  Required. Identifier to assign to the API Config. Must be unique within scope of
   *  the parent resource.
   */
  const apiConfigId = 'unique-api-config-id-123'
  /**
   *  Required. API resource.
   */
  const apiConfig = {} // What to put here???

  // Instantiates a client
  const apigatewayClient = new ApiGatewayServiceClient();

  async function callCreateApiConfig() {
    // Construct request
    const request = {
      parent,
      apiConfigId,
      apiConfig,
    };

    // Run request
    const [operation] = await apigatewayClient.createApiConfig(request);
    const [response] = await operation.promise();
    console.log(response);
  }

  callCreateApiConfig();
  // [END apigateway_v1_generated_ApiGatewayService_CreateApiConfig_async]
}

Now if you try to run this code out of the box, you get the following error: Error: 3 INVALID_ARGUMENT: Please specify one of: (service_rollout.rollout_id, openapi_documents, grpc_services)

I understand that the variable apiConfig shouldn't be empty, but I cannot find any living examples of what this is actually supposed to contain. I've dived into the source code of the library to try to find an answer, but to no prevail. Also where exactly to fulfill the requirements of specify one of service_rollout.rollout_id, openapi_documents, grpc_services is completely beyond me.

Any responses and help here would be greatly appreciated.

EDIT: I found the answer to the first question. I had to edit the request-parameter, namely by the following: import { google } from '@google-cloud/api-gateway/build/protos/protos'; const request = {name, google.cloud.apigateway.v1.GetApiConfigRequest.ConfigView.FULL}. The second question remains open, but perhaps this first solution will guide me to the last.

Matty
  • 31
  • 7

1 Answers1

1

Lo and behold, the answer to the first question lead me to the answer to the second.

The solution was to simply include the following in the request:

const apiConfigId = 'test-api-gateway-new-config-1';

const openApiDocument: google.cloud.apigateway.v1.ApiConfig.IOpenApiDocument = {
    document: {
      path: 'name_of_file.yaml',
      contents: fs.readFileSync('path/to/file/you/want/to/upload'),
    }
  }
  /**
   *  Required. API resource.
   */
  const apiConfig: google.cloud.apigateway.v1.IApiConfig = {
    openapiDocuments: [
      openApiDocument,
    ]
  }

After running this code, a new OpenAPI spec will have been uploaded to GCP's API Gateway with the name "test-api-gateway-new-config-1".

Matty
  • 31
  • 7