@cdktn/provider-generator 0.24.0-pre.9 → 0.24.0-pre.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.spec.swcrc +22 -0
- package/LICENSE +355 -0
- package/README.md +1 -1
- package/build/__tests__/edge-provider-schema/builder.d.ts +9 -3
- package/build/__tests__/edge-provider-schema/builder.js +6 -3
- package/build/__tests__/edge-provider-schema/cli.js +8 -3
- package/build/__tests__/edge-provider-schema/index.js +119 -1
- package/build/__tests__/provider.test.js +4 -1
- package/build/get/__tests__/generator/ephemeral-resources.test.d.ts +2 -0
- package/build/get/__tests__/generator/ephemeral-resources.test.js +49 -0
- package/build/get/__tests__/generator/module-generator.test.js +12 -12
- package/build/get/__tests__/generator/provider-functions.test.d.ts +2 -0
- package/build/get/__tests__/generator/provider-functions.test.js +176 -0
- package/build/get/__tests__/generator/provider-source-matching.test.d.ts +2 -0
- package/build/get/__tests__/generator/provider-source-matching.test.js +45 -0
- package/build/get/__tests__/generator/provider.test.js +9 -1
- package/build/get/__tests__/generator/supported-stored-classes.test.d.ts +2 -0
- package/build/get/__tests__/generator/supported-stored-classes.test.js +137 -0
- package/build/get/__tests__/generator/types.test.js +44 -1
- package/build/get/__tests__/generator/write-only.test.d.ts +2 -0
- package/build/get/__tests__/generator/write-only.test.js +65 -0
- package/build/get/__tests__/target-versions.test.d.ts +2 -0
- package/build/get/__tests__/target-versions.test.js +171 -0
- package/build/get/constructs-maker.d.ts +8 -1
- package/build/get/constructs-maker.js +46 -10
- package/build/get/generator/emitter/attributes-emitter.d.ts +13 -2
- package/build/get/generator/emitter/attributes-emitter.js +71 -5
- package/build/get/generator/emitter/index.d.ts +1 -0
- package/build/get/generator/emitter/index.js +2 -1
- package/build/get/generator/emitter/provider-functions-emitter.d.ts +11 -0
- package/build/get/generator/emitter/provider-functions-emitter.js +94 -0
- package/build/get/generator/emitter/resource-emitter.d.ts +5 -1
- package/build/get/generator/emitter/resource-emitter.js +83 -7
- package/build/get/generator/emitter/struct-emitter.js +10 -10
- package/build/get/generator/models/attribute-model.d.ts +4 -0
- package/build/get/generator/models/attribute-model.js +50 -4
- package/build/get/generator/models/attribute-type-model.d.ts +24 -0
- package/build/get/generator/models/attribute-type-model.js +91 -5
- package/build/get/generator/models/index.d.ts +1 -0
- package/build/get/generator/models/index.js +2 -1
- package/build/get/generator/models/provider-function-model.d.ts +102 -0
- package/build/get/generator/models/provider-function-model.js +667 -0
- package/build/get/generator/models/resource-model.d.ts +3 -0
- package/build/get/generator/models/resource-model.js +13 -4
- package/build/get/generator/models/struct.d.ts +2 -0
- package/build/get/generator/models/struct.js +6 -2
- package/build/get/generator/models/supported-stored-classes.d.ts +5 -0
- package/build/get/generator/models/supported-stored-classes.js +86 -0
- package/build/get/generator/module-generator.js +3 -3
- package/build/get/generator/provider-generator.d.ts +2 -0
- package/build/get/generator/provider-generator.js +41 -6
- package/build/get/generator/resource-parser.js +3 -1
- package/jest.config.js +16 -9
- package/package.json +28 -27
- package/package.sh +1 -1
- package/src/__tests__/__snapshots__/edge-provider-schema.test.ts.snap +4 -0
- package/src/__tests__/__snapshots__/provider.test.ts.snap +2828 -2828
- package/src/__tests__/edge-provider-schema/builder.ts +201 -0
- package/src/__tests__/edge-provider-schema/cli.ts +51 -0
- package/src/__tests__/edge-provider-schema/index.ts +284 -0
- package/src/__tests__/edge-provider-schema.test.ts +24 -0
- package/src/__tests__/provider.test.ts +183 -0
- package/src/get/__tests__/constructs-maker.test.ts +118 -0
- package/src/get/__tests__/generator/__snapshots__/complex-computed-types.test.ts.snap +5 -5
- package/src/get/__tests__/generator/__snapshots__/ephemeral-resources.test.ts.snap +524 -0
- package/src/get/__tests__/generator/__snapshots__/export-sharding.test.ts.snap +3310 -3310
- package/src/get/__tests__/generator/__snapshots__/module-generator.test.ts.snap +355 -355
- package/src/get/__tests__/generator/__snapshots__/nested-types.test.ts.snap +8 -8
- package/src/get/__tests__/generator/__snapshots__/provider-functions.test.ts.snap +444 -0
- package/src/get/__tests__/generator/__snapshots__/provider.test.ts.snap +8 -8
- package/src/get/__tests__/generator/__snapshots__/resource-types.test.ts.snap +126 -126
- package/src/get/__tests__/generator/__snapshots__/skipped-attributes.test.ts.snap +17 -17
- package/src/get/__tests__/generator/__snapshots__/types.test.ts.snap +201 -52
- package/src/get/__tests__/generator/__snapshots__/write-only.test.ts.snap +262 -0
- package/src/get/__tests__/generator/complex-computed-types.test.ts +28 -0
- package/src/get/__tests__/generator/deep-nested-attributes.test.ts +56 -0
- package/src/get/__tests__/generator/description-escaping.test.ts +84 -0
- package/src/get/__tests__/generator/empty-provider-resources.test.ts +26 -0
- package/src/get/__tests__/generator/ephemeral-resources.test.ts +46 -0
- package/src/get/__tests__/generator/export-sharding.test.ts +169 -0
- package/src/get/__tests__/generator/fixtures/deeply-nested-collections.test.fixture.json +129 -0
- package/src/get/__tests__/generator/fixtures/ephemeral-resources.test.fixture.json +160 -0
- package/src/get/__tests__/generator/fixtures/provider-functions-collision.test.fixture.json +31 -0
- package/src/get/__tests__/generator/fixtures/provider-functions-synthetic.test.fixture.json +192 -0
- package/src/get/__tests__/generator/fixtures/provider-functions.test.fixture.json +170 -0
- package/src/get/__tests__/generator/fixtures/write-only.test.fixture.json +58 -0
- package/src/get/__tests__/generator/import-style.test.ts +129 -0
- package/src/get/__tests__/generator/module-generator.test.ts +528 -0
- package/src/get/__tests__/generator/nested-types.test.ts +54 -0
- package/src/get/__tests__/generator/provider-functions.test.ts +262 -0
- package/src/get/__tests__/generator/provider-source-matching.test.ts +84 -0
- package/src/get/__tests__/generator/provider.test.ts +59 -0
- package/src/get/__tests__/generator/resource-types.test.ts +118 -0
- package/src/get/__tests__/generator/skipped-attributes.test.ts +72 -0
- package/src/get/__tests__/generator/supported-stored-classes.test.ts +147 -0
- package/src/get/__tests__/generator/types.test.ts +686 -0
- package/src/get/__tests__/generator/versions-file.test.ts +72 -0
- package/src/get/__tests__/generator/write-only.test.ts +71 -0
- package/src/get/__tests__/target-versions.test.ts +206 -0
- package/src/get/__tests__/util.ts +75 -0
- package/src/get/constructs-maker.ts +885 -0
- package/src/get/generator/custom-defaults.ts +493 -0
- package/src/get/generator/emitter/attributes-emitter.ts +316 -0
- package/src/get/generator/emitter/index.ts +6 -0
- package/src/get/generator/emitter/provider-functions-emitter.ts +117 -0
- package/src/get/generator/emitter/resource-emitter.ts +330 -0
- package/src/get/generator/emitter/struct-emitter.ts +683 -0
- package/src/get/generator/loop-detection.ts +81 -0
- package/src/get/generator/models/attribute-model.ts +273 -0
- package/src/get/generator/models/attribute-type-model.ts +560 -0
- package/src/get/generator/models/index.ts +8 -0
- package/src/get/generator/models/provider-function-model.ts +872 -0
- package/src/get/generator/models/resource-model.ts +183 -0
- package/src/get/generator/models/scope.ts +54 -0
- package/src/get/generator/models/struct.ts +124 -0
- package/src/get/generator/models/supported-stored-classes.ts +88 -0
- package/src/get/generator/module-generator.ts +234 -0
- package/src/get/generator/provider-generator.ts +428 -0
- package/src/get/generator/resource-parser.ts +764 -0
- package/src/get/generator/sanitized-comments.ts +49 -0
- package/src/get/generator/skipped-attributes.ts +27 -0
- package/src/index.ts +40 -0
- package/src/util.ts +26 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
6
|
+
import {
|
|
7
|
+
assertNoFunctionsGetterCollision,
|
|
8
|
+
buildProviderFunctionsModel,
|
|
9
|
+
} from "../../generator/models/provider-function-model";
|
|
10
|
+
import { CodeMaker } from "codemaker";
|
|
11
|
+
import { FunctionSignature } from "@cdktn/commons";
|
|
12
|
+
import { createTmpHelper } from "../util";
|
|
13
|
+
|
|
14
|
+
const tmp = createTmpHelper();
|
|
15
|
+
|
|
16
|
+
test("generate provider functions for the time provider (real terraform 1.15.6 schema fragment)", async () => {
|
|
17
|
+
const code = new CodeMaker();
|
|
18
|
+
const workdir = tmp("provider-functions.test");
|
|
19
|
+
const spec = JSON.parse(
|
|
20
|
+
fs.readFileSync(
|
|
21
|
+
path.join(__dirname, "fixtures", "provider-functions.test.fixture.json"),
|
|
22
|
+
"utf-8",
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
26
|
+
await code.save(workdir);
|
|
27
|
+
|
|
28
|
+
const providerFunctionsOutput = fs.readFileSync(
|
|
29
|
+
path.join(workdir, "providers/time/provider-functions/index.ts"),
|
|
30
|
+
"utf-8",
|
|
31
|
+
);
|
|
32
|
+
expect(providerFunctionsOutput).toMatchSnapshot("time-provider-functions");
|
|
33
|
+
|
|
34
|
+
const providerOutput = fs.readFileSync(
|
|
35
|
+
path.join(workdir, "providers/time/provider/index.ts"),
|
|
36
|
+
"utf-8",
|
|
37
|
+
);
|
|
38
|
+
expect(providerOutput).toMatchSnapshot("time-provider");
|
|
39
|
+
|
|
40
|
+
const providerIndex = fs.readFileSync(
|
|
41
|
+
path.join(workdir, "providers/time/index.ts"),
|
|
42
|
+
"utf-8",
|
|
43
|
+
);
|
|
44
|
+
expect(providerIndex).toMatchSnapshot("provider-index");
|
|
45
|
+
|
|
46
|
+
const providerLazyIndex = fs.readFileSync(
|
|
47
|
+
path.join(workdir, "providers/time/lazy-index.ts"),
|
|
48
|
+
"utf-8",
|
|
49
|
+
);
|
|
50
|
+
expect(providerLazyIndex).toMatchSnapshot("provider-lazy-index");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("generate provider functions covering variadic parameters, primitive/list returns, and a 'default' parameter name", () => {
|
|
54
|
+
let providerFunctionsOutput: string;
|
|
55
|
+
let providerIndex: string;
|
|
56
|
+
let providerLazyIndex: string;
|
|
57
|
+
|
|
58
|
+
beforeAll(async () => {
|
|
59
|
+
const code = new CodeMaker();
|
|
60
|
+
const workdir = tmp("provider-functions-synthetic.test");
|
|
61
|
+
const spec = JSON.parse(
|
|
62
|
+
fs.readFileSync(
|
|
63
|
+
path.join(
|
|
64
|
+
__dirname,
|
|
65
|
+
"fixtures",
|
|
66
|
+
"provider-functions-synthetic.test.fixture.json",
|
|
67
|
+
),
|
|
68
|
+
"utf-8",
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
72
|
+
await code.save(workdir);
|
|
73
|
+
|
|
74
|
+
providerFunctionsOutput = fs.readFileSync(
|
|
75
|
+
path.join(workdir, "providers/example/provider-functions/index.ts"),
|
|
76
|
+
"utf-8",
|
|
77
|
+
);
|
|
78
|
+
providerIndex = fs.readFileSync(
|
|
79
|
+
path.join(workdir, "providers/example/index.ts"),
|
|
80
|
+
"utf-8",
|
|
81
|
+
);
|
|
82
|
+
providerLazyIndex = fs.readFileSync(
|
|
83
|
+
path.join(workdir, "providers/example/lazy-index.ts"),
|
|
84
|
+
"utf-8",
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("matches the snapshot", () => {
|
|
89
|
+
expect(providerFunctionsOutput).toMatchSnapshot(
|
|
90
|
+
"example-provider-functions",
|
|
91
|
+
);
|
|
92
|
+
expect(providerIndex).toMatchSnapshot("provider-index");
|
|
93
|
+
expect(providerLazyIndex).toMatchSnapshot("provider-lazy-index");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("nested collection docstrings use the compact recursive Array/Set notation", () => {
|
|
97
|
+
// list(set(string)): notation carries the structure; a plain Terraform
|
|
98
|
+
// list needs no prose note.
|
|
99
|
+
expect(providerFunctionsOutput).toContain(
|
|
100
|
+
"@param {Array<Set<string>>} nameGroups",
|
|
101
|
+
);
|
|
102
|
+
expect(providerFunctionsOutput).toContain("@returns {Array<Set<string>>}");
|
|
103
|
+
// set(list(string)): Set<...> notation plus the set-semantics prose note
|
|
104
|
+
// (the jsii type is still an array - the note keeps a reader from
|
|
105
|
+
// literally passing a JS Set).
|
|
106
|
+
expect(providerFunctionsOutput).toContain(
|
|
107
|
+
"@param {Set<Array<string>>} nameLists - Terraform set; ordering is not guaranteed and duplicate values are removed.",
|
|
108
|
+
);
|
|
109
|
+
expect(providerFunctionsOutput).toContain(
|
|
110
|
+
"@returns {Set<Array<string>>} Terraform set; ordering is not guaranteed and duplicate values are removed.",
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// This package has no TypeScript-compiling harness
|
|
115
|
+
// for generated output (no fixture/test anywhere generates a snippet and
|
|
116
|
+
// feeds it through `tsc`/`ts.transpileModule`, and `cdktn` itself isn't a
|
|
117
|
+
// dependency of this package to typecheck against), so these are
|
|
118
|
+
// structural assertions on the emitted declaration text rather than an
|
|
119
|
+
// actual compile - see also the runtime-level proof in
|
|
120
|
+
// packages/cdktn/test/provider-functions.test.ts, which exercises the
|
|
121
|
+
// same union/token mechanics these signatures rely on.
|
|
122
|
+
describe("composition: a function's return type is directly assignable to another function's parameter of the 'same' Terraform type", () => {
|
|
123
|
+
test("flagsForSeed()'s set(bool) return type (cdktn.IResolvable) is one of the exact union members flagSet()'s set(bool) parameter declares", () => {
|
|
124
|
+
expect(providerFunctionsOutput).toContain(
|
|
125
|
+
"public flagsForSeed(seed: string): cdktn.IResolvable {",
|
|
126
|
+
);
|
|
127
|
+
expect(providerFunctionsOutput).toContain(
|
|
128
|
+
"public flagSet(flags: Array<boolean | cdktn.IResolvable> | cdktn.IResolvable): cdktn.IResolvable {",
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("defaultTags()'s map(string) return type is textually identical to tagMap()'s map(string) parameter type", () => {
|
|
133
|
+
expect(providerFunctionsOutput).toContain(
|
|
134
|
+
"public defaultTags(prefix: string): { [key: string]: string } {",
|
|
135
|
+
);
|
|
136
|
+
expect(providerFunctionsOutput).toContain(
|
|
137
|
+
"public tagMap(tags: { [key: string]: string }): string {",
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("a list(bool) parameter (not just set(bool)) also accepts a whole-collection token", () => {
|
|
143
|
+
expect(providerFunctionsOutput).toContain(
|
|
144
|
+
"public flagsOrdered(flags: Array<boolean | cdktn.IResolvable> | cdktn.IResolvable): cdktn.IResolvable {",
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("a list(object) parameter widens to any[] | cdktn.IResolvable (whole-collection token accepted)", () => {
|
|
149
|
+
expect(providerFunctionsOutput).toContain(
|
|
150
|
+
"public describeConfigs(configs: any[] | cdktn.IResolvable): string {",
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("buildProviderFunctionsModel throws when two function names collapse to the same generated method name", () => {
|
|
156
|
+
expect(() =>
|
|
157
|
+
buildProviderFunctionsModel("example", {
|
|
158
|
+
foo_bar: { return_type: "string", parameters: [] },
|
|
159
|
+
foo__bar: { return_type: "string", parameters: [] },
|
|
160
|
+
}),
|
|
161
|
+
).toThrow(/foo_bar/);
|
|
162
|
+
expect(() =>
|
|
163
|
+
buildProviderFunctionsModel("example", {
|
|
164
|
+
foo_bar: { return_type: "string", parameters: [] },
|
|
165
|
+
foo__bar: { return_type: "string", parameters: [] },
|
|
166
|
+
}),
|
|
167
|
+
).toThrow(/foo__bar/);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("buildProviderFunctionsModel throws when two parameter names collapse within one function", () => {
|
|
171
|
+
expect(() =>
|
|
172
|
+
buildProviderFunctionsModel("example", {
|
|
173
|
+
my_function: {
|
|
174
|
+
return_type: "string",
|
|
175
|
+
parameters: [
|
|
176
|
+
{ name: "some_value", type: "string" },
|
|
177
|
+
{ name: "some__value", type: "string" },
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
).toThrow(/some_value/);
|
|
182
|
+
expect(() =>
|
|
183
|
+
buildProviderFunctionsModel("example", {
|
|
184
|
+
my_function: {
|
|
185
|
+
return_type: "string",
|
|
186
|
+
parameters: [
|
|
187
|
+
{ name: "some_value", type: "string" },
|
|
188
|
+
{ name: "some__value", type: "string" },
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
}),
|
|
192
|
+
).toThrow(/some__value/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Both of these assert the model-level throw happens before any file is
|
|
196
|
+
// generated: buildProviderFunctionsModel is called directly, with no
|
|
197
|
+
// CodeMaker/generator involved, so there is no emission step to reach.
|
|
198
|
+
test("buildProviderFunctionsModel throws when a function name sanitizes to 'constructor'", () => {
|
|
199
|
+
// The nested signature literal is cast to `FunctionSignature` (rather than
|
|
200
|
+
// relying on contextual typing from the surrounding index signature)
|
|
201
|
+
// because TypeScript special-cases a property literally named
|
|
202
|
+
// "constructor" - contextually typing it against `Function` (from
|
|
203
|
+
// `Object.prototype`) instead of the index signature's `FunctionSignature`,
|
|
204
|
+
// and spuriously flagging `return_type: "string"` as an error. See
|
|
205
|
+
// https://github.com/microsoft/TypeScript/issues/40776.
|
|
206
|
+
const signature: FunctionSignature = {
|
|
207
|
+
return_type: "string",
|
|
208
|
+
parameters: [],
|
|
209
|
+
};
|
|
210
|
+
expect(() =>
|
|
211
|
+
buildProviderFunctionsModel("example", {
|
|
212
|
+
constructor: signature,
|
|
213
|
+
}),
|
|
214
|
+
).toThrow(/constructor/);
|
|
215
|
+
expect(() =>
|
|
216
|
+
buildProviderFunctionsModel("example", {
|
|
217
|
+
constructor: signature,
|
|
218
|
+
}),
|
|
219
|
+
).toThrow(/example/);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("buildProviderFunctionsModel throws when a function name sanitizes to 'providerLocalName'", () => {
|
|
223
|
+
expect(() =>
|
|
224
|
+
buildProviderFunctionsModel("example", {
|
|
225
|
+
provider_local_name: { return_type: "string", parameters: [] },
|
|
226
|
+
}),
|
|
227
|
+
).toThrow(/provider_local_name/);
|
|
228
|
+
expect(() =>
|
|
229
|
+
buildProviderFunctionsModel("example", {
|
|
230
|
+
provider_local_name: { return_type: "string", parameters: [] },
|
|
231
|
+
}),
|
|
232
|
+
).toThrow(/providerLocalName/);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("assertNoFunctionsGetterCollision throws when the provider's own config schema would generate a 'functions' property", () => {
|
|
236
|
+
expect(() =>
|
|
237
|
+
assertNoFunctionsGetterCollision("example", ["alias", "functions"]),
|
|
238
|
+
).toThrow(/"functions"/);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("assertNoFunctionsGetterCollision does not throw when there is no colliding attribute", () => {
|
|
242
|
+
expect(() =>
|
|
243
|
+
assertNoFunctionsGetterCollision("example", ["alias"]),
|
|
244
|
+
).not.toThrow();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("generation throws when a provider's config attribute collides with the generated 'functions' getter", async () => {
|
|
248
|
+
const code = new CodeMaker();
|
|
249
|
+
const spec = JSON.parse(
|
|
250
|
+
fs.readFileSync(
|
|
251
|
+
path.join(
|
|
252
|
+
__dirname,
|
|
253
|
+
"fixtures",
|
|
254
|
+
"provider-functions-collision.test.fixture.json",
|
|
255
|
+
),
|
|
256
|
+
"utf-8",
|
|
257
|
+
),
|
|
258
|
+
);
|
|
259
|
+
expect(() =>
|
|
260
|
+
new TerraformProviderGenerator(code, spec).generateAll(),
|
|
261
|
+
).toThrow(/"functions"/);
|
|
262
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import {
|
|
4
|
+
ConstructsMakerProviderTarget,
|
|
5
|
+
Language,
|
|
6
|
+
ProviderSchema,
|
|
7
|
+
TerraformProviderConstraint,
|
|
8
|
+
} from "@cdktn/commons";
|
|
9
|
+
import { CodeMaker } from "codemaker";
|
|
10
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
11
|
+
|
|
12
|
+
// Builds a minimal provider schema keyed by a single FQPN. The provider body is
|
|
13
|
+
// intentionally empty (like the empty-provider-resources fixture) since these
|
|
14
|
+
// tests only exercise the schema-key <-> target-source matching logic.
|
|
15
|
+
const schemaWithKey = (fqpn: string): ProviderSchema => ({
|
|
16
|
+
provider_schemas: {
|
|
17
|
+
[fqpn]: { resource_schemas: null } as any,
|
|
18
|
+
},
|
|
19
|
+
provider_versions: {
|
|
20
|
+
[fqpn]: "1.84.0",
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const targetForSource = (source: string) =>
|
|
25
|
+
new ConstructsMakerProviderTarget(
|
|
26
|
+
new TerraformProviderConstraint(`${source}@1.84.0`),
|
|
27
|
+
Language.TYPESCRIPT,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const generateFor = (schemaKey: string, source: string) => {
|
|
31
|
+
const generator = new TerraformProviderGenerator(
|
|
32
|
+
new CodeMaker(),
|
|
33
|
+
schemaWithKey(schemaKey),
|
|
34
|
+
);
|
|
35
|
+
generator.generate(targetForSource(source));
|
|
36
|
+
return generator;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
describe("provider source matching against schema keys", () => {
|
|
40
|
+
it("matches a shorthand source against an OpenTofu registry schema key", () => {
|
|
41
|
+
// Regression test for #216: `tofu providers schema` returns keys under
|
|
42
|
+
// registry.opentofu.org, so a shorthand source must not assume
|
|
43
|
+
// registry.terraform.io.
|
|
44
|
+
const generator = generateFor(
|
|
45
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
46
|
+
"hashicorp/awscc",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(Object.keys(generator.versions)).toContain(
|
|
50
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("matches a shorthand source against a Terraform registry schema key", () => {
|
|
55
|
+
const generator = generateFor(
|
|
56
|
+
"registry.terraform.io/hashicorp/awscc",
|
|
57
|
+
"hashicorp/awscc",
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
expect(Object.keys(generator.versions)).toContain(
|
|
61
|
+
"registry.terraform.io/hashicorp/awscc",
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("keeps an explicit hostname strict and does not cross registries", () => {
|
|
66
|
+
expect(() =>
|
|
67
|
+
generateFor(
|
|
68
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
69
|
+
"registry.terraform.io/hashicorp/awscc",
|
|
70
|
+
),
|
|
71
|
+
).toThrow(/Could not find provider with constraint/);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("matches an explicit hostname when it agrees with the schema key", () => {
|
|
75
|
+
const generator = generateFor(
|
|
76
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
77
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(Object.keys(generator.versions)).toContain(
|
|
81
|
+
"registry.opentofu.org/hashicorp/awscc",
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
6
|
+
import { CodeMaker } from "codemaker";
|
|
7
|
+
import { createTmpHelper } from "../util";
|
|
8
|
+
|
|
9
|
+
const tmp = createTmpHelper();
|
|
10
|
+
|
|
11
|
+
test("generate provider", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("provider.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(__dirname, "fixtures", "aws-provider.test.fixture.json"),
|
|
17
|
+
"utf-8",
|
|
18
|
+
),
|
|
19
|
+
);
|
|
20
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
21
|
+
await code.save(workdir);
|
|
22
|
+
|
|
23
|
+
const output = fs.readFileSync(
|
|
24
|
+
path.join(workdir, "providers/aws/provider/index.ts"),
|
|
25
|
+
"utf-8",
|
|
26
|
+
);
|
|
27
|
+
expect(output).toMatchSnapshot();
|
|
28
|
+
// aws has no provider-defined functions in this fixture - no getter, no
|
|
29
|
+
// cross-directory import should be emitted.
|
|
30
|
+
expect(output).not.toContain("public get functions()");
|
|
31
|
+
expect(output).not.toContain("provider-functions");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("generate provider with only block_types", async () => {
|
|
35
|
+
const code = new CodeMaker();
|
|
36
|
+
const workdir = tmp("provider.test");
|
|
37
|
+
const spec = JSON.parse(
|
|
38
|
+
fs.readFileSync(
|
|
39
|
+
path.join(
|
|
40
|
+
__dirname,
|
|
41
|
+
"fixtures",
|
|
42
|
+
"elasticstack-provider.test.fixture.json",
|
|
43
|
+
),
|
|
44
|
+
"utf-8",
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
48
|
+
await code.save(workdir);
|
|
49
|
+
|
|
50
|
+
const output = fs.readFileSync(
|
|
51
|
+
path.join(workdir, "providers/elasticstack/provider/index.ts"),
|
|
52
|
+
"utf-8",
|
|
53
|
+
);
|
|
54
|
+
expect(output).toMatchSnapshot();
|
|
55
|
+
// elasticstack has no provider-defined functions in this fixture - no
|
|
56
|
+
// getter, no cross-directory import should be emitted.
|
|
57
|
+
expect(output).not.toContain("public get functions()");
|
|
58
|
+
expect(output).not.toContain("provider-functions");
|
|
59
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
6
|
+
import { CodeMaker } from "codemaker";
|
|
7
|
+
import { createTmpHelper } from "../util";
|
|
8
|
+
|
|
9
|
+
const tmp = createTmpHelper();
|
|
10
|
+
|
|
11
|
+
test("generate a cloudfront distribution resource", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("cloudfront.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(
|
|
17
|
+
__dirname,
|
|
18
|
+
"fixtures",
|
|
19
|
+
"aws_cloudfront_distribution.test.fixture.json",
|
|
20
|
+
),
|
|
21
|
+
"utf-8",
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
25
|
+
await code.save(workdir);
|
|
26
|
+
|
|
27
|
+
const output = fs.readFileSync(
|
|
28
|
+
path.join(workdir, "providers/aws/cloudfront-distribution/index.ts"),
|
|
29
|
+
"utf-8",
|
|
30
|
+
);
|
|
31
|
+
expect(output).toMatchSnapshot("cloudfront");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("generate a s3 bucket resource", async () => {
|
|
35
|
+
const code = new CodeMaker();
|
|
36
|
+
const workdir = tmp("s3.test");
|
|
37
|
+
const spec = JSON.parse(
|
|
38
|
+
fs.readFileSync(
|
|
39
|
+
path.join(__dirname, "fixtures", "aws_s3_bucket.test.fixture.json"),
|
|
40
|
+
"utf-8",
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
44
|
+
await code.save(workdir);
|
|
45
|
+
|
|
46
|
+
const output = fs.readFileSync(
|
|
47
|
+
path.join(workdir, "providers/aws/s3-bucket/index.ts"),
|
|
48
|
+
"utf-8",
|
|
49
|
+
);
|
|
50
|
+
expect(output).toMatchSnapshot();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("generate a fms admin account with an empty options interface", async () => {
|
|
54
|
+
const code = new CodeMaker();
|
|
55
|
+
const workdir = tmp("fms.test");
|
|
56
|
+
const spec = JSON.parse(
|
|
57
|
+
fs.readFileSync(
|
|
58
|
+
path.join(
|
|
59
|
+
__dirname,
|
|
60
|
+
"fixtures",
|
|
61
|
+
"aws_fms_admin_account.test.fixture.json",
|
|
62
|
+
),
|
|
63
|
+
"utf-8",
|
|
64
|
+
),
|
|
65
|
+
);
|
|
66
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
67
|
+
await code.save(workdir);
|
|
68
|
+
|
|
69
|
+
const output = fs.readFileSync(
|
|
70
|
+
path.join(workdir, "providers/aws/fms-admin-account/index.ts"),
|
|
71
|
+
"utf-8",
|
|
72
|
+
);
|
|
73
|
+
expect(output).toMatchSnapshot();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("generate a security group", async () => {
|
|
77
|
+
const code = new CodeMaker();
|
|
78
|
+
const workdir = tmp("sg.test");
|
|
79
|
+
const spec = JSON.parse(
|
|
80
|
+
fs.readFileSync(
|
|
81
|
+
path.join(__dirname, "fixtures", "aws_security_group.test.fixture.json"),
|
|
82
|
+
"utf-8",
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
86
|
+
await code.save(workdir);
|
|
87
|
+
|
|
88
|
+
const output = fs.readFileSync(
|
|
89
|
+
path.join(workdir, "providers/aws/security-group/index.ts"),
|
|
90
|
+
"utf-8",
|
|
91
|
+
);
|
|
92
|
+
expect(output).toMatchSnapshot();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("generate a datadog spans metric", async () => {
|
|
96
|
+
const code = new CodeMaker();
|
|
97
|
+
const workdir = tmp("sg.test");
|
|
98
|
+
const spec = JSON.parse(
|
|
99
|
+
fs.readFileSync(
|
|
100
|
+
path.join(
|
|
101
|
+
__dirname,
|
|
102
|
+
"fixtures",
|
|
103
|
+
"datadog_spans_metric.test.fixture.json",
|
|
104
|
+
),
|
|
105
|
+
"utf-8",
|
|
106
|
+
),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
110
|
+
await code.save(workdir);
|
|
111
|
+
|
|
112
|
+
const output = fs.readFileSync(
|
|
113
|
+
path.join(workdir, "providers/datadog/spans-metric/index.ts"),
|
|
114
|
+
"utf-8",
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(output).toMatchSnapshot();
|
|
118
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
6
|
+
import { CodeMaker } from "codemaker";
|
|
7
|
+
import { createTmpHelper } from "../util";
|
|
8
|
+
|
|
9
|
+
const tmp = createTmpHelper();
|
|
10
|
+
|
|
11
|
+
test("skips block type attributes in the disallow list", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("skip-attributes.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(
|
|
17
|
+
__dirname,
|
|
18
|
+
"fixtures",
|
|
19
|
+
"aws_quicksight_template.test.fixture.json",
|
|
20
|
+
),
|
|
21
|
+
"utf-8",
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
25
|
+
await code.save(workdir);
|
|
26
|
+
|
|
27
|
+
const output = fs.readFileSync(
|
|
28
|
+
path.join(workdir, "providers/aws/quicksight-template/index.ts"),
|
|
29
|
+
"utf-8",
|
|
30
|
+
);
|
|
31
|
+
expect(output).toMatchSnapshot(`quicksight-template`);
|
|
32
|
+
|
|
33
|
+
// There should also be no index structs
|
|
34
|
+
expect(
|
|
35
|
+
fs.existsSync(
|
|
36
|
+
path.join(workdir, "providers/aws/quicksight-template/index-structs"),
|
|
37
|
+
),
|
|
38
|
+
).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("skips attribute type attributes in the disallow list", async () => {
|
|
42
|
+
const code = new CodeMaker();
|
|
43
|
+
const workdir = tmp("skip-attributes.test");
|
|
44
|
+
const spec = JSON.parse(
|
|
45
|
+
fs.readFileSync(
|
|
46
|
+
path.join(
|
|
47
|
+
__dirname,
|
|
48
|
+
"fixtures",
|
|
49
|
+
"data_aws_quicksight_analysis.fixture.json",
|
|
50
|
+
),
|
|
51
|
+
"utf-8",
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
55
|
+
await code.save(workdir);
|
|
56
|
+
|
|
57
|
+
const output = fs.readFileSync(
|
|
58
|
+
path.join(workdir, "providers/aws/data-aws-quicksight-analysis/index.ts"),
|
|
59
|
+
"utf-8",
|
|
60
|
+
);
|
|
61
|
+
expect(output).toMatchSnapshot(`data-quicksight-analysis`);
|
|
62
|
+
|
|
63
|
+
// There should also be no index structs
|
|
64
|
+
expect(
|
|
65
|
+
fs.existsSync(
|
|
66
|
+
path.join(
|
|
67
|
+
workdir,
|
|
68
|
+
"providers/aws/data-aws-quicksight-analysis/index-structs",
|
|
69
|
+
),
|
|
70
|
+
),
|
|
71
|
+
).toBe(false);
|
|
72
|
+
});
|