diff --git a/organizations/tools/gh_foundations/cmd/import/tea_commands.go b/organizations/tools/gh_foundations/cmd/import/tea_commands.go index 42d4a0e..c8af2c4 100644 --- a/organizations/tools/gh_foundations/cmd/import/tea_commands.go +++ b/organizations/tools/gh_foundations/cmd/import/tea_commands.go @@ -80,7 +80,7 @@ func resolveResourceId(address string, archive types.TerragruntPlanArchive) tea. func runTerragruntImport(archive types.TerragruntPlanArchive, address string, id string) tea.Cmd { return func() tea.Msg { - err, errBytes := functions.RunImportCommand(archive, address, id) + errBytes, err := functions.RunImportCommand(archive, address, id) if err != nil { return errMsg{fmt.Errorf("error running import command: %s", errBytes.String())} } diff --git a/organizations/tools/gh_foundations/internal/pkg/functions/terragrunt.go b/organizations/tools/gh_foundations/internal/pkg/functions/terragrunt.go index e9650e8..72694fa 100644 --- a/organizations/tools/gh_foundations/internal/pkg/functions/terragrunt.go +++ b/organizations/tools/gh_foundations/internal/pkg/functions/terragrunt.go @@ -26,13 +26,13 @@ func ArchivePlan(modulePath string, planName string) (*types.TerragruntPlanArchi return planArchive, nil } -func RunImportCommand(archive types.TerragruntPlanArchive, address string, id string) (error, bytes.Buffer) { +func RunImportCommand(archive types.TerragruntPlanArchive, address string, id string) (bytes.Buffer, error) { errorBytes := bytes.Buffer{} importCmd := exec.Command("terragrunt", "import", address, id) importCmd.Stderr = &errorBytes importCmd.Stdout = nil importCmd.Dir = archive.ModuleDir - return importCmd.Run(), errorBytes + return errorBytes, importCmd.Run() } func CreateImportIdResolver(resourceAddress string, stateExplorer terraform_state.IStateExplorer) types.ImportIdResolver { @@ -51,6 +51,14 @@ func CreateImportIdResolver(resourceAddress string, stateExplorer terraform_stat return &types.RepositoryBranchDefaultImportIdResolver{StateExplorer: stateExplorer} case "github_repository_collaborators": return &types.RepositoryCollaboratorsImportIdResolver{StateExplorer: stateExplorer} + case "github_actions_secret", "github_codespaces_secret", "dependabot_secret": + return &types.RepositorySecretsImportIdResolver{StateExplorer: stateExplorer} + case "github_repository_dependabot_security_updates": + return &types.RepositoryDependabotSecurityUpdatesImportIdResolver{StateExplorer: stateExplorer} + case "github_repository_environment": + return &types.RepositoryEnvironmentImportIdResolver{StateExplorer: stateExplorer} + case "github_repository_ruleset": + return &types.RepositoryRulesetImportIdResolver{StateExplorer: stateExplorer} default: return nil } diff --git a/organizations/tools/gh_foundations/internal/pkg/types/terragrunt/repository.go b/organizations/tools/gh_foundations/internal/pkg/types/terragrunt/repository.go index c6be340..9e2df01 100644 --- a/organizations/tools/gh_foundations/internal/pkg/types/terragrunt/repository.go +++ b/organizations/tools/gh_foundations/internal/pkg/types/terragrunt/repository.go @@ -3,6 +3,8 @@ package terragrunt import ( "fmt" "gh_foundations/internal/pkg/types/terraform_state" + + "github.com/tidwall/gjson" ) type RepositoryImportIdResolver struct { @@ -49,3 +51,90 @@ func (r *RepositoryCollaboratorsImportIdResolver) ResolveImportId(resourceAddres return repository.String(), nil } + +type RepositorySecretsImportIdResolver struct { + StateExplorer terraform_state.IStateExplorer +} + +func (t *RepositorySecretsImportIdResolver) ResolveImportId(resourceAddress string) (string, error) { + repository, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "repository") + if err != nil { + return "", err + } else if !repository.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if repository.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: repository attribute is not a string") + } + + secretName, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "secret_name") + if err != nil { + return "", err + } else if !secretName.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if secretName.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: secret_name attribute is not a string") + } + + return fmt.Sprintf("%s/%s", repository.String(), secretName.String()), nil +} + +type RepositoryDependabotSecurityUpdatesImportIdResolver struct { + StateExplorer terraform_state.IStateExplorer +} + +func (t *RepositoryDependabotSecurityUpdatesImportIdResolver) ResolveImportId(resourceAddress string) (string, error) { + repository, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "repository") + if err != nil { + return "", err + } else if !repository.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if repository.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: repository attribute is not a string") + } + + return repository.String(), nil +} + +type RepositoryEnvironmentImportIdResolver struct { + StateExplorer terraform_state.IStateExplorer +} + +func (t *RepositoryEnvironmentImportIdResolver) ResolveImportId(resourceAddress string) (string, error) { + repository, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "repository") + if err != nil { + return "", err + } else if !repository.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if repository.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: repository attribute is not a string") + } + + environment, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "environment") + if err != nil { + return "", err + } else if !environment.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if environment.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: environment attribute is not a string") + } + + return fmt.Sprintf("%s/%s", repository.String(), environment.String()), nil +} + +type RepositoryRulesetImportIdResolver struct { + StateExplorer terraform_state.IStateExplorer +} + +func (t *RepositoryRulesetImportIdResolver) ResolveImportId(resourceAddress string) (string, error) { + repository, err := t.StateExplorer.GetResourceChangeAfterAttribute(resourceAddress, "repository") + if err != nil { + return "", err + } else if !repository.Exists() { + return "", fmt.Errorf("unable to resolve import id: unexpected error occurred") + } else if repository.Type != gjson.String { + return "", fmt.Errorf("unable to resolve import id: repository attribute is not a string") + } + + // The full import id includes the ruleset id. We won't be able to get this info from a terraform plan. And will need to be typed out unless we add a github client to this tool. + return fmt.Sprintf("%s:", repository.String()), nil +}