Files
oneuptime/Scripts/TerraformProvider/GenerateProvider.ts

118 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { generateOpenAPISpec } from "../OpenAPI/GenerateSpec";
import { ToolInstaller } from "./InstallTools";
import FrameworkGenerator from "./FrameworkGenerator";
import SpecificationConverter from "./SpecificationConverter";
import path from "path";
async function main(): Promise<void> {
// eslint-disable-next-line no-console
console.log("🚀 Starting Terraform Provider Generation Process...");
try {
// 1. Generate OpenAPI spec
// eslint-disable-next-line no-console
console.log("\n📄 Step 1: Generating OpenAPI specification...");
const openApiSpecPath: string = path.resolve(
__dirname,
"../../Terraform/openapi.json",
);
await generateOpenAPISpec(openApiSpecPath);
// 2. Convert OpenAPI spec to Provider Code Specification
// eslint-disable-next-line no-console
console.log("\n🔄 Step 2: Converting to Provider Code Specification...");
const providerSpecPath: string = path.resolve(
__dirname,
"../../Terraform/provider-code-spec.json",
);
SpecificationConverter.convertOpenAPIToProviderSpec({
openApiSpecPath: openApiSpecPath,
outputPath: providerSpecPath,
providerName: "oneuptime",
});
// 3. Install Framework Generator tool
// eslint-disable-next-line no-console
console.log(
"\n🔧 Step 3: Installing Terraform Plugin Framework Generator...",
);
const frameworkInstallResult: any =
await ToolInstaller.installTerraformPluginFrameworkGenerator();
if (!frameworkInstallResult.success) {
throw new Error(
`Failed to install framework generator: ${frameworkInstallResult.message}`,
);
}
// eslint-disable-next-line no-console
console.log(`${frameworkInstallResult.message}`);
// 4. Generate Terraform Provider Framework code
// eslint-disable-next-line no-console
console.log(
"\n🏗 Step 4: Generating Terraform Provider Framework code...",
);
const frameworkOutputPath: string = path.resolve(
__dirname,
"../../Terraform/terraform-provider-framework",
);
FrameworkGenerator.generateAll({
specificationPath: providerSpecPath,
outputPath: frameworkOutputPath,
packageName: "oneuptime", // Optional: specify a package name
});
// eslint-disable-next-line no-console
console.log("\n🎉 Provider generation completed successfully!");
// eslint-disable-next-line no-console
console.log("\n📋 Generated Files:");
// eslint-disable-next-line no-console
console.log(` 📄 OpenAPI Spec: ${openApiSpecPath}`);
// eslint-disable-next-line no-console
console.log(` 📄 Provider Code Spec: ${providerSpecPath}`);
// eslint-disable-next-line no-console
console.log(` 📁 Framework Provider Code: ${frameworkOutputPath}`);
// eslint-disable-next-line no-console
console.log("\n📖 Next Steps:");
// eslint-disable-next-line no-console
console.log(" 1. Review the generated Provider Code Specification");
// eslint-disable-next-line no-console
console.log(
" 2. Customize the specification as needed for your use case",
);
// eslint-disable-next-line no-console
console.log(
" 3. Use the Framework Generator to regenerate code after modifications",
);
// eslint-disable-next-line no-console
console.log(
" 4. Implement the actual provider logic in the generated Go files",
);
FrameworkGenerator.printUsageInfo();
} catch (error) {
const err: Error = error as Error;
// eslint-disable-next-line no-console
console.error("\n❌ Error during provider generation:", err);
// eslint-disable-next-line no-console
console.error("\n🔍 Troubleshooting Tips:");
// eslint-disable-next-line no-console
console.error(" - Ensure Go is installed and properly configured");
// eslint-disable-next-line no-console
console.error(" - Check that GOPATH is set correctly");
// eslint-disable-next-line no-console
console.error(" - Verify internet connectivity for downloading tools");
// eslint-disable-next-line no-console
console.error(
" - Make sure you have write permissions in the output directories",
);
process.exit(1);
}
}
main().catch((err) => {
console.error("💥 Unexpected error:", err);
process.exit(1);
});