Skip to content

Commit

Permalink
Fix support for apiKeySource (#112)
Browse files Browse the repository at this point in the history
Ports the fix from pulumi/pulumi-awsx#1159 into this package.

Requires an awkward workaround for #111 (the workaround also takes a dependency on the currently broken behaviour, so this test will start failing when #111 is fixed).

Fixes #110.
  • Loading branch information
Luke Hoban authored Nov 24, 2023
1 parent 70746d1 commit 1b96058
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 4 deletions.
3 changes: 3 additions & 0 deletions examples/apikeysource/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: apigateway-apikeysource
runtime: nodejs
description: Testing the `akikeysource for `API`.
15 changes: 15 additions & 0 deletions examples/apikeysource/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export const apikeysource = "HEADER";
48 changes: 48 additions & 0 deletions examples/apikeysource/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as apigateway from "@pulumi/aws-apigateway";
import * as data from "./data";

const f = new aws.lambda.CallbackFunction("f", {
callback: async (ev, ctx) => {
console.log(JSON.stringify(ev));
return {
statusCode: 200,
body: "goodbye",
};
},
});

const api = new apigateway.RestAPI("authorizer-api", {
apiKeySource: data.apikeysource,
routes: [{
path: "/",
method: "GET",
eventHandler: f,
}],
}, { version: "" });


// TODO[pulumi/pulumi-aws-apigateway#111]: The `api` output is (incorrectly) a string not an object
// so we cannot read it's output. Workaround this by looking up the API by name.
var apiRead: any = {};
if (!pulumi.runtime.isDryRun()) {
apiRead = aws.apigateway.getRestApiOutput({
name: api.url.apply(_ => "authorizer-api"),
});
}
export const apiKeySource = apiRead.apiKeySource;
16 changes: 16 additions & 0 deletions examples/apikeysource/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "api",
"version": "0.0.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@pulumi/aws": "^6.0.0",
"@pulumi/aws-apigateway": "1.1.0-alpha.3",
"@pulumi/pulumi": "^3.3.1"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
15 changes: 15 additions & 0 deletions examples/apikeysource/step2/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export const apikeysource = "AUTHORIZER";
18 changes: 18 additions & 0 deletions examples/apikeysource/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
26 changes: 23 additions & 3 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
//go:build nodejs || all
// +build nodejs all

package examples

import (
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"path"
"path/filepath"
"testing"

"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/stretchr/testify/assert"
)

func TestSimpleTs(t *testing.T) {
Expand All @@ -20,6 +21,25 @@ func TestSimpleTs(t *testing.T) {
integration.ProgramTest(t, &test)
}

func TestAccApiKeySource(t *testing.T) {
test := getBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "./apikeysource"),
EditDirs: []integration.EditDir{
{
Dir: "./apikeysource/step2",
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
apiKeySource := stack.Outputs["apiKeySource"].(string)
assert.Equal(t, "AUTHORIZER", apiKeySource)
},
},
},
})

integration.ProgramTest(t, &test)
}

func getJSBaseOptions(t *testing.T) integration.ProgramTestOptions {
base := getBaseOptions(t)
baseJS := base.With(integration.ProgramTestOptions{
Expand Down
5 changes: 4 additions & 1 deletion examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func getBaseOptions(t *testing.T) integration.ProgramTestOptions {
awsRegion := getRegion(t)
return integration.ProgramTestOptions{
SkipRefresh: true,
Quick: true,
Quick: true,
Dependencies: []string{
"@pulumi/aws-apigateway",
},
Config: map[string]string{
"aws:region": awsRegion,
},
Expand Down
3 changes: 3 additions & 0 deletions provider/cmd/pulumi-resource-aws-apigateway/apigateway/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ export function createAPI(
name: ifUndefined(restApiArgs.name, title),
binaryMediaTypes: ifUndefined(args.binaryMediaTypes, ["*/*"]),
body: swaggerString,
// We pass this in directly, because setting it in the Swagger doesn't cause
// it to take affect, it must be passed directly to the RestAPI constructor as well.
apiKeySource: args.apiKeySource,
},
{ parent }
);
Expand Down

0 comments on commit 1b96058

Please sign in to comment.