Skip to content

Commit

Permalink
fix: given provider name can differ from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Dec 20, 2023
1 parent 8d33396 commit 1076e73
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const isMatching = (
throw new Error(`can't handle ${terraformSchemaName}`);
}

return target.name === provider;
return target.name === provider || target.source === terraformSchemaName;
}
};

Expand Down Expand Up @@ -69,6 +69,7 @@ export class TerraformProviderGenerator {
}

public generate(providerConstraint: ConstructsMakerTarget) {
// Alter fqpn with provided name somehow?
const fqpn = this.getProviderByConstraint(providerConstraint);
if (!fqpn) {
logger.debug(
Expand Down Expand Up @@ -105,20 +106,35 @@ export class TerraformProviderGenerator {
await this.code.save(outdir);
}

public buildResourceModels(fqpn: FQPN): ResourceModel[] {
public buildResourceModels(
fqpn: FQPN,
providedName?: ProviderName
): ResourceModel[] {
const provider = this.schema.provider_schemas?.[fqpn];
if (!provider) {
throw new Error(`Can not find provider '${fqpn}' in schema`);
}

const resources = Object.entries(provider.resource_schemas || {}).map(
([type, resource]) =>
this.resourceParser.parse(fqpn, type, resource, "resource")
this.resourceParser.parse(
fqpn,
type,
resource,
"resource",
providedName
)
);

const dataSources = Object.entries(provider.data_source_schemas || {}).map(
([type, resource]) =>
this.resourceParser.parse(fqpn, `data_${type}`, resource, "data_source")
this.resourceParser.parse(
fqpn,
`data_${type}`,
resource,
"data_source",
providedName
)
);

return ([] as ResourceModel[]).concat(...resources, ...dataSources);
Expand All @@ -137,34 +153,37 @@ export class TerraformProviderGenerator {
providerVersion?: string,
constraint?: ConstructsMakerTarget
) {
const { name } = parseFQPN(fqpn);
const { name } = parseFQPN(fqpn, constraint);
const provider = this.schema.provider_schemas?.[fqpn];
if (!provider) {
throw new Error(`Can not find provider '${fqpn}' in schema`);
}

const files: string[] = [];
this.buildResourceModels(fqpn).forEach((resourceModel) => {
if (constraint) {
resourceModel.providerVersionConstraint = constraint.version;
resourceModel.terraformProviderSource = constraint.source;
this.buildResourceModels(fqpn, constraint?.name as ProviderName).forEach(
(resourceModel) => {
if (constraint) {
resourceModel.providerVersionConstraint = constraint.version;
resourceModel.terraformProviderSource = constraint.source;
}
resourceModel.providerVersion = providerVersion;

if (resourceModel.structsRequireSharding) {
files.push(this.emitResourceWithComplexStruct(resourceModel));
} else {
files.push(this.emitResource(resourceModel));
}
this.emitResourceReadme(resourceModel);
}
resourceModel.providerVersion = providerVersion;

if (resourceModel.structsRequireSharding) {
files.push(this.emitResourceWithComplexStruct(resourceModel));
} else {
files.push(this.emitResource(resourceModel));
}
this.emitResourceReadme(resourceModel);
});
);

if (provider.provider) {
const providerResource = this.resourceParser.parse(
fqpn,
`provider`,
provider.provider,
"provider"
"provider",
constraint?.name as ProviderName
);
if (constraint) {
providerResource.providerVersionConstraint = constraint.version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ class Parser {
fqpn: FQPN,
type: string,
schema: Schema,
terraformSchemaType: string
terraformSchemaType: string,
providedName?: ProviderName
): ResourceModel {
const provider = parseFQPN(fqpn).name;
const provider = providedName ? providedName : parseFQPN(fqpn).name;
let baseName = type;
if (baseName.startsWith(`${provider}_`)) {
baseName = baseName.substr(provider.length + 1);
Expand Down Expand Up @@ -664,14 +665,21 @@ export class ResourceParser {
fqpn: FQPN,
type: string,
schema: Schema,
terraformType: string
terraformType: string,
providedName?: ProviderName
): ResourceModel {
if (this.resources[type]) {
return this.resources[type];
}

const parser = new Parser(this.uniqueClassnames);
const resource = parser.resourceFrom(fqpn, type, schema, terraformType);
const resource = parser.resourceFrom(
fqpn,
type,
schema,
terraformType,
providedName
);
this.resources[type] = resource;
return resource;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/@cdktf/provider-schema/src/provider-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { convertFiles } from "@cdktf/hcl2json";
import {
ConstructsMakerModuleTarget,
ConstructsMakerProviderTarget,
ConstructsMakerTarget,
Input,
ModuleIndex,
ModuleSchema,
Expand All @@ -27,11 +28,13 @@ export type ProviderHostname = string & { __type: "ProviderHostname" };
export type ProviderNamespace = string & { __type: "ProviderNamespace" };
export type ProviderName = string & { __type: "ProviderName" };

export const parseFQPN = (f: FQPN) => {
const [hostname, namespace, name] = f.split("/");
if (!name) {
export const parseFQPN = (f: FQPN, constraint?: ConstructsMakerTarget) => {
const [hostname, namespace, sourceName] = f.split("/");
const providedName = constraint?.name;
if (!sourceName && !providedName) {
throw new Error(`can't handle ${f}`);
}
const name = providedName ? providedName : sourceName;
return { hostname, namespace, name } as {
hostname: ProviderHostname;
namespace: ProviderNamespace;
Expand Down

0 comments on commit 1076e73

Please sign in to comment.