pulumi / pulumi-aws

An Amazon Web Services (AWS) Pulumi resource package, providing multi-language access to AWS
Apache License 2.0
466 stars 158 forks source link

Apigateway name input never reads from openapi config #2299

Open tscully49 opened 1 year ago

tscully49 commented 1 year ago

What happened?

I tried to not provide the input name for API gateway but have the name provided in the info.title field in the openapi config. According to the docs (https://www.pulumi.com/registry/packages/aws/api-docs/apigateway/restapi/#name_nodejs) if the name is not provided it will read from the openapi config. What seems to happen is it uses the pulumi resource name and will never read from the openapi config.

Steps to reproduce

import * as aws from '@pulumi/aws'

const apigatewayBody = {
  openapi: "3.0.3",
  info: {
    title: "the-name-i-want",
    version: version
  },
}

new aws.apigateway.RestApi('restApiGateway', {
  body: JSON.stringify(apigatewayBody),
  // no name input provided
});

run pulumi up

Expected Behavior

I expect the name of the api gateway to be read from the openapi config and will be "the-name-i-want"

Actual Behavior

The API-gateway will be named "restApiGateway-xxxxx" with hexadecimal characters at the end, seeming to come from the pulumi name.

Output of pulumi about

CLI
Version 3.50.0 Go Version go1.19.4 Go Compiler gc

Plugins NAME VERSION aws 5.26.0 aws-apigateway 1.0.1 awsx 1.0.1 docker 3.6.1 nodejs unknown

Host
OS darwin Version 12.6.1 Arch arm64

This project is written in nodejs: executable='/Users/me/.asdf/shims/node' version='v16.19.0'

Dependencies: NAME VERSION @pulumi/aws 5.26.0 @pulumi/aws-apigateway 1.0.1 @pulumi/awsx 1.0.1 @pulumi/pulumi 3.51.0 @types/jest 29.2.5 @types/node 14.18.36 eslint 8.31.0 jest 29.3.1 ts-jest 28.0.8 typescript 4.9.4

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

thomas11 commented 1 year ago

Hi @tscully49, thank you for reporting this. This seems like an unfortunate case of Pulumi's auto-naming interfering with API Gateway's handling of the body's title.

I will add this to our backlog. In the meantime, a workaround might be to parse out info.title and use it as the name of the RestApi:

new aws.apigateway.RestApi('restApiGateway', {
  body: JSON.stringify(apigatewayBody),
  name: parsedTitle
});
tscully49 commented 1 year ago

Good idea! The problem I run into there is that I have the body in an apply so I can use the invokeArn from a lambda being created. Since the body can be an input string, I stringify it before it gets assigned to a variable as an output so I can pass it in. After it's assigned it becomes hard, if possible, to parse. Currently, I set the name to a variable and use it in both the body and the resource input for the name. Any thoughts on how I could clean that up?

Example:

const apigatewayName = 'myRestApi'
const apigatewayBody = lambda.invokeArn.apply((invokeArn) => JSON.stringify({
    openapi: '3.0.3',
    info: {
        title: apigatewayName,
        version,
    },
    paths: {
        '/test': {
            post: {
                responses: {
                    200: {
                        description: '200 response',
                        content: {
                            'application/json': {
                                schema: {
                                    $ref: '#/components/schemas/Empty',
                                },  
                            },  
                        },  
                    },  
                },   
                'x-amazon-apigateway-integration': {
                    httpMethod: 'POST',
                    uri: invokeArn,
                    responses: {
                        default: {
                            statusCode: '200',
                        },
                    },
                    passthroughBehavior: 'when_no_match',
                    contentHandling: 'CONVERT_TO_TEXT',
                    type: 'aws',
                },
            },
        },
    },
}))

new aws.apigateway.RestApi('restApiGateway', {
    body: JSON.stringify(apigatewayBody),
    endpointConfiguration: {
        types: 'EDGE',
    },
    tags,
    name: apigatewayName
}, {
    dependsOn: lambda,
})