Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Dec 20, 2023
1 parent 1076e73 commit e66dba9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
7 changes: 5 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
} from "./iteration";
import { getProviderRequirements } from "./provider";
import { logger } from "./utils";
import { FQPN } from "@cdktf/provider-schema";
import { FQPN, parseFQPN } from "@cdktf/provider-schema";
import { attributeNameToCdktfName } from "./generation";
import {
replaceCsharpImports,
Expand Down Expand Up @@ -105,7 +105,10 @@ export async function convertToTypescript(
new CodeMaker(),
providerSchema
);
providerGenerator.buildResourceModels(fqpn as FQPN); // can't use that type on the keys yet, since we are not on TS >=4.4 yet :sadcat:
providerGenerator.buildResourceModels(
fqpn as FQPN,
parseFQPN(fqpn as FQPN).name
); // can't use that type on the keys yet, since we are not on TS >=4.4 yet :sadcat:
return { ...carry, [fqpn]: providerGenerator };
}, {}),
constructs: new Set<string>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ 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 @@ -108,7 +107,7 @@ export class TerraformProviderGenerator {

public buildResourceModels(
fqpn: FQPN,
providedName?: ProviderName
providerName: ProviderName
): ResourceModel[] {
const provider = this.schema.provider_schemas?.[fqpn];
if (!provider) {
Expand All @@ -122,7 +121,7 @@ export class TerraformProviderGenerator {
type,
resource,
"resource",
providedName
providerName
)
);

Expand All @@ -133,7 +132,7 @@ export class TerraformProviderGenerator {
`data_${type}`,
resource,
"data_source",
providedName
providerName
)
);

Expand All @@ -153,37 +152,37 @@ export class TerraformProviderGenerator {
providerVersion?: string,
constraint?: ConstructsMakerTarget
) {
const { name } = parseFQPN(fqpn, constraint);
const { name } = constraint?.name
? { name: constraint.name as ProviderName }
: parseFQPN(fqpn);
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, 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);
this.buildResourceModels(fqpn, name).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);
});

if (provider.provider) {
const providerResource = this.resourceParser.parse(
fqpn,
`provider`,
provider.provider,
"provider",
constraint?.name as ProviderName
name
);
if (constraint) {
providerResource.providerVersionConstraint = constraint.version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isNestedTypeAttribute,
Schema,
} from "@cdktf/commons";
import { ProviderName, FQPN, parseFQPN } from "@cdktf/provider-schema";
import { ProviderName, FQPN } from "@cdktf/provider-schema";
import {
ResourceModel,
AttributeTypeModel,
Expand Down Expand Up @@ -124,17 +124,16 @@ class Parser {
type: string,
schema: Schema,
terraformSchemaType: string,
providedName?: ProviderName
providerName: ProviderName
): ResourceModel {
const provider = providedName ? providedName : parseFQPN(fqpn).name;
let baseName = type;
if (baseName.startsWith(`${provider}_`)) {
baseName = baseName.substr(provider.length + 1);
if (baseName.startsWith(`${providerName}_`)) {
baseName = baseName.substr(providerName.length + 1);
}

const isProvider = terraformSchemaType === "provider";
if (isProvider) {
baseName = `${provider}_${baseName}`;
baseName = `${providerName}_${baseName}`;
if (!("attributes" in schema.block)) {
schema.block = {
attributes: {},
Expand All @@ -155,16 +154,16 @@ class Parser {
const className = this.uniqueClassName(toPascalCase(baseName));
// avoid naming collision - see https://github.com/hashicorp/terraform-cdk/issues/299
const configStructName = this.uniqueClassName(`${className}Config`);
const fileName = getFileName(provider, baseName);
const fileName = getFileName(providerName, baseName);

const filePath = `providers/${toSnakeCase(provider)}/${fileName}`;
const filePath = `providers/${toSnakeCase(providerName)}/${fileName}`;
let attributes = this.renderAttributesForBlock(
new Scope({
name: baseName,
isProvider,
parent: isProvider
? undefined
: new Scope({ name: provider, isProvider: true }),
: new Scope({ name: providerName, isProvider: true }),
}),
schema.block
);
Expand Down Expand Up @@ -666,7 +665,7 @@ export class ResourceParser {
type: string,
schema: Schema,
terraformType: string,
providedName?: ProviderName
providerName: ProviderName
): ResourceModel {
if (this.resources[type]) {
return this.resources[type];
Expand All @@ -678,7 +677,7 @@ export class ResourceParser {
type,
schema,
terraformType,
providedName
providerName
);
this.resources[type] = resource;
return resource;
Expand Down
9 changes: 3 additions & 6 deletions packages/@cdktf/provider-schema/src/provider-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { convertFiles } from "@cdktf/hcl2json";
import {
ConstructsMakerModuleTarget,
ConstructsMakerProviderTarget,
ConstructsMakerTarget,
Input,
ModuleIndex,
ModuleSchema,
Expand All @@ -28,13 +27,11 @@ export type ProviderHostname = string & { __type: "ProviderHostname" };
export type ProviderNamespace = string & { __type: "ProviderNamespace" };
export type ProviderName = string & { __type: "ProviderName" };

export const parseFQPN = (f: FQPN, constraint?: ConstructsMakerTarget) => {
const [hostname, namespace, sourceName] = f.split("/");
const providedName = constraint?.name;
if (!sourceName && !providedName) {
export const parseFQPN = (f: FQPN) => {
const [hostname, namespace, name] = f.split("/");
if (!name) {
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 e66dba9

Please sign in to comment.