Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove redundant export code #38

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,9 @@ The resources.tf file can be renamed by using the flag --resourceFileName.
The command will fail if a resource file already exists`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Invalid command\n\nUse 'btptfexporter export --help' for syntax instructions.\n\nERROR")
//fmt.Println("please provide the resource to be imported with this commnad. Supported resources are subaccount, entilements, environment-instances, subscriptions, trust-configurations")
},
}

func init() {
rootCmd.AddCommand(exportCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// exportCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// exportCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 5 additions & 5 deletions cmd/exportAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ btp_subaccount_trust_configurations `,
setupConfigDir(configDir)

execPreExportSteps("saconf")
exportSubaccount(subaccount, "saconf")
exportSubaccount(subaccount, "saconf", nil)
execPostExportSteps("saconf", configDir, resourceFileName, "SUBACCOUNT")

execPreExportSteps("saentitlementconf")
exportSubaccountEntitlements(subaccount, "saentitlementconf")
exportSubaccountEntitlements(subaccount, "saentitlementconf", nil)
execPostExportSteps("saentitlementconf", configDir, resourceFileName, "SUBACCOUNT ENTITLEMENTS")

execPreExportSteps("saenvinstanceconf")
exportEnvironmentInstances(subaccount, "saenvinstanceconf")
exportEnvironmentInstances(subaccount, "saenvinstanceconf", nil)
execPostExportSteps("saenvinstanceconf", configDir, resourceFileName, "SUBACCOUNT ENVIRONMENT INSTANCES")

execPreExportSteps("sasubscriptionconf")
exportSubaccountSubscriptions(subaccount, "sasubscriptionconf")
exportSubaccountSubscriptions(subaccount, "sasubscriptionconf", nil)
execPostExportSteps("sasubscriptionconf", configDir, resourceFileName, "SUBACCOUNT SUBSCRIPTIONS")

execPreExportSteps("satrustconf")
exportTrustConfigurations(subaccount, "satrustconf")
exportTrustConfigurations(subaccount, "satrustconf", nil)
execPostExportSteps("satrustconf", configDir, resourceFileName, "SUBACCOUNT TRUST CONFIGURATIONS")

finalizeTfConfig(configDir)
Expand Down
2 changes: 1 addition & 1 deletion cmd/exportEnvironmentInstances.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var exportEnvironmentInstancesCmd = &cobra.Command{
resourceFileName, _ := cmd.Flags().GetString("resourceFileName")
configDir, _ := cmd.Flags().GetString("config-output-dir")
setupConfigDir(configDir)
exportEnvironmentInstances(subaccount, configDir)
exportEnvironmentInstances(subaccount, configDir, nil)
generateConfig(resourceFileName, configDir)
},
}
Expand Down
44 changes: 35 additions & 9 deletions cmd/exportEnvironmentInstancesHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"
)

func exportEnvironmentInstances(subaccountID string, configFolder string) {
func exportEnvironmentInstances(subaccountID string, configFolder string, filterValues []string) {

dataBlock, err := readDataSource(subaccountID)
if err != nil {
Expand Down Expand Up @@ -44,7 +45,7 @@ func exportEnvironmentInstances(subaccountID string, configFolder string) {
return
}

importBlock, err := getImportBlock(data, subaccountID)
importBlock, err := getImportBlock(data, subaccountID, filterValues)
if err != nil {
log.Fatalf("error: %v", err)
return
Expand Down Expand Up @@ -81,7 +82,7 @@ func readDataSource(subaccountId string) (string, error) {

}

func getImportBlock(data map[string]interface{}, subaccountId string) (string, error) {
func getImportBlock(data map[string]interface{}, subaccountId string, filterValues []string) (string, error) {
choice := "btp_subaccount_environment_instance"
resource_doc, err := tfutils.GetDocsForResource("SAP", "btp", "btp", "resources", choice, BtpProviderVersion, "github.com")
if err != nil {
Expand All @@ -92,13 +93,38 @@ func getImportBlock(data map[string]interface{}, subaccountId string) (string, e
var importBlock string
environmentInstances := data["values"].([]interface{})

for _, value := range environmentInstances {
environmentInstance := value.(map[string]interface{})
template := strings.Replace(resource_doc.Import, "<resource_name>", fmt.Sprintf("%v", environmentInstance["environment_type"]), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
template = strings.Replace(template, "<environment_instance_id>", fmt.Sprintf("%v", environmentInstance["id"]), -1)
importBlock += template + "\n"
if len(filterValues) != 0 {
var subaccountAllEnvInstances []string

for _, value := range environmentInstances {

environmentInstance := value.(map[string]interface{})
subaccountAllEnvInstances = append(subaccountAllEnvInstances, fmt.Sprintf("%v", environmentInstance["environment_type"]))
if slices.Contains(filterValues, fmt.Sprintf("%v", environmentInstance["environment_type"])) {
importBlock += templateEnvironmentInstanceImport(environmentInstance, subaccountId, resource_doc)
}
}

missingEnvInstance, subset := isSubset(subaccountAllEnvInstances, filterValues)

if !subset {
return "", fmt.Errorf("environment instance %s not found in the subaccount. Please adjust it in the provided file", missingEnvInstance)

}
} else {

for _, value := range environmentInstances {
environmentInstance := value.(map[string]interface{})
importBlock += templateEnvironmentInstanceImport(environmentInstance, subaccountId, resource_doc)
}
}

return importBlock, nil
}

func templateEnvironmentInstanceImport(environmentInstance map[string]interface{}, subaccountId string, resource_doc tfutils.EntityDocs) string {
template := strings.Replace(resource_doc.Import, "<resource_name>", fmt.Sprintf("%v", environmentInstance["environment_type"]), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
template = strings.Replace(template, "<environment_instance_id>", fmt.Sprintf("%v", environmentInstance["id"]), -1)
return template + "\n"
}
2 changes: 1 addition & 1 deletion cmd/exportEnvironmentInstancesHelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetImportBlock(t *testing.T) {
t.Errorf("error in unmarshalling")
}

importBlock, err := getImportBlock(data, "5163621f-6a1e-4fbf-af3a-0f530a0dc4d5")
importBlock, err := getImportBlock(data, "5163621f-6a1e-4fbf-af3a-0f530a0dc4d5", nil)
if err != nil {
t.Errorf("error creating importBlock")
}
Expand Down
Loading
Loading