Skip to content

Commit

Permalink
chore: added more id resolvers (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerMizuyabu authored Apr 16, 2024
1 parent ea341ff commit 30fee0d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package terragrunt
import (
"fmt"
"gh_foundations/internal/pkg/types/terraform_state"

"github.com/tidwall/gjson"
)

type RepositoryImportIdResolver struct {
Expand Down Expand Up @@ -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
}

0 comments on commit 30fee0d

Please sign in to comment.