diff --git a/cmd/exportAll.go b/cmd/exportAll.go index bbb5f67..016d7ca 100644 --- a/cmd/exportAll.go +++ b/cmd/exportAll.go @@ -17,13 +17,30 @@ btp_subaccount_trust_configurations `, subaccount, _ := cmd.Flags().GetString("subaccount") resourceFileName, _ := cmd.Flags().GetString("resourceFileName") configDir, _ := cmd.Flags().GetString("config-output-dir") + setupConfigDir(configDir) - exportSubaccount(subaccount, configDir) - exportSubaccountEntitlements(subaccount, configDir) - exportEnvironmentInstances(subaccount, configDir) - exportSubaccountSubscriptions(subaccount, configDir) - exportTrustConfigurations(subaccount, configDir) - generateConfig(resourceFileName, configDir) + + execPreExportSteps("saconf") + exportSubaccount(subaccount, "saconf") + execPostExportSteps("saconf", configDir, resourceFileName, "SUBACCOUNT") + + execPreExportSteps("saentitlementconf") + exportSubaccountEntitlements(subaccount, "saentitlementconf") + execPostExportSteps("saentitlementconf", configDir, resourceFileName, "SUBACCOUNT ENTITLEMENTS") + + execPreExportSteps("saenvinstanceconf") + exportEnvironmentInstances(subaccount, "saenvinstanceconf") + execPostExportSteps("saenvinstanceconf", configDir, resourceFileName, "SUBACCOUNT ENVIRONMENT INSTANCES") + + execPreExportSteps("sasubscriptionconf") + exportSubaccountSubscriptions(subaccount, "sasubscriptionconf") + execPostExportSteps("sasubscriptionconf", configDir, resourceFileName, "SUBACCOUNT SUBSCRIPTIONS") + + execPreExportSteps("satrustconf") + exportTrustConfigurations(subaccount, "satrustconf") + execPostExportSteps("satrustconf", configDir, resourceFileName, "SUBACCOUNT TRUST CONFIGURATIONS") + + finalizeTfConfig(configDir) }, } diff --git a/cmd/generateConfig.go b/cmd/generateConfig.go index 75cedbf..4099310 100644 --- a/cmd/generateConfig.go +++ b/cmd/generateConfig.go @@ -53,6 +53,13 @@ func generateConfig(resourceFileName string, configFolder string) { fmt.Println("Terraform config successfully created") cleanup() + + //Switch back to the original directory + err = os.Chdir(currentDir) + if err != nil { + log.Fatalf("error changing directory to %s: %v \n", currentDir, err) + return + } } func cleanup() { @@ -60,6 +67,9 @@ func cleanup() { if err != nil { log.Fatalf("error deleting temp files: %v", err) } + + // Cleanup temporary folder variable + TmpFolder = "" } func configureProvider() { @@ -201,3 +211,151 @@ func exists(path string) (bool, error) { } return false, err } + +func mergeTfConfig(configFolder string, fileName string, resourceConfigFolder string, resourceName string) { + + currentDir, err := os.Getwd() + + if err != nil { + log.Fatalf("error getting current directory: %v", err) + return + } + + sourceConfigPath := filepath.Join(currentDir, resourceConfigFolder, fileName) + + // Check if the source file exists + exist, err := exists(sourceConfigPath) + if err != nil { + log.Fatalf("error checking if source directory exists: %v", err) + } + + if !exist { + // Nothing to do as the source file does not exist + return + } + + sourceFile, err := os.Open(sourceConfigPath) + if err != nil { + log.Fatalf("error opening resource config file: %v", err) + } + defer sourceFile.Close() + + targetConfigPath := filepath.Join(currentDir, configFolder, fileName) + + exist, err = exists(targetConfigPath) + if err != nil { + log.Fatalf("error checking if target directory exists: %v", err) + } + + if !exist { + // In the first run we must create the file if it does not exist + _, err := os.Create(targetConfigPath) + if err != nil { + log.Fatalf("error creating target configuration file: %v", err) + } + } + + targetFile, err := os.OpenFile(targetConfigPath, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + log.Fatalf("error opening target configuration file: %v", err) + } + defer targetFile.Close() + + headerTemplate := ` +### +# Resource: ` + resourceName + ` +### +` + if _, err := targetFile.Write([]byte(headerTemplate)); err != nil { + log.Fatalf("error adding header line to target file: %v", err) + } + + if _, err := io.Copy(targetFile, sourceFile); err != nil { + log.Fatalf("error copying resource file to target file: %v", err) + } + + copyImportFiles(resourceConfigFolder, configFolder) +} + +func copyImportFiles(srcDir, destDir string) { + // Find all files ending with "_import.tf" in the source directory + files, err := filepath.Glob(filepath.Join(srcDir, "*_import.tf")) + if err != nil { + log.Fatalf("error finding files: %v", err) + } + + // Copy each file to the destination directory + for _, srcFile := range files { + destFile := filepath.Join(destDir, filepath.Base(srcFile)) + + err := copyFile(srcFile, destFile) + if err != nil { + log.Printf("error copying file %s to %s: %v", srcFile, destFile, err) + } + } +} + +func copyFile(src, dest string) error { + sourceFile, err := os.Open(src) + if err != nil { + return err + } + defer sourceFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, sourceFile) + return err +} + +func deleteSourceFolder(srcDir string) { + err := os.RemoveAll(srcDir) + if err != nil { + log.Fatalf("error deleting source folder %s: %v", srcDir, err) + } +} + +func finalizeTfConfig(configFolder string) { + + currentDir, err := os.Getwd() + if err != nil { + log.Fatalf("error getting current directory: %v", err) + return + } + terraformConfigPath := filepath.Join(currentDir, configFolder) + err = os.Chdir(terraformConfigPath) + if err != nil { + log.Fatalf("error changing directory to %s: %v \n", terraformConfigPath, err) + return + } + + if err := runTerraformCommand("init"); err != nil { + log.Fatalf("error initializing Terraform: %v", err) + return + } + + if err := runTerraformCommand("fmt", "-recursive", "-list=false"); err != nil { + log.Fatalf("error running Terraform fmt: %v", err) + return + } + //Switch back to the original directory + err = os.Chdir(currentDir) + if err != nil { + log.Fatalf("error changing directory to %s: %v \n", currentDir, err) + return + } +} + +func execPreExportSteps(tempConfigDir string) { + setupConfigDir(tempConfigDir) +} + +func execPostExportSteps(tempConfigDir string, targetConfigDir string, targetResourceFileName string, resourceNameLong string) { + generateConfig(targetResourceFileName, tempConfigDir) + mergeTfConfig(targetConfigDir, targetResourceFileName, tempConfigDir, resourceNameLong) + deleteSourceFolder(tempConfigDir) +}