Skip to content

Commit

Permalink
Merge pull request #403 from ColOfAbRiX/bugfix/402
Browse files Browse the repository at this point in the history
Fix of issue #402: load given state file when specified.
  • Loading branch information
brikis98 committed Jan 19, 2018
2 parents 4123182 + 1c383e9 commit 9ee518b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion remote/remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (remoteState *RemoteState) Initialize(terragruntOptions *options.Terragrunt
// 2. Remote state has been configured, but with a different configuration
// 3. The remote state initializer for this backend type, if there is one, says initialization is necessary
func (remoteState *RemoteState) NeedsInit(terragruntOptions *options.TerragruntOptions) (bool, error) {
state, err := ParseTerraformStateFileFromLocation(terragruntOptions.WorkingDir)
state, err := ParseTerraformStateFileFromLocation(remoteState.Backend, remoteState.Config, terragruntOptions.WorkingDir)
if err != nil {
return false, err
}
Expand Down
13 changes: 9 additions & 4 deletions remote/terraform_state_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ func (state *TerraformState) IsRemote() bool {
return state.Backend != nil && state.Backend.Type != "local"
}

// Parse the Terraform .tfstate file from the location specified by workingDir. If no location is specified,
// search the current directory. If the file doesn't exist at any of the default locations, return nil.
func ParseTerraformStateFileFromLocation(workingDir string) (*TerraformState, error) {
if util.FileExists(util.JoinPath(workingDir, DEFAULT_PATH_TO_LOCAL_STATE_FILE)) {
// Parses the Terraform .tfstate file. If a local backend is used then search the given path, or
// return nil if the file is missing. If the backend is not local then parse the Terraform .tfstate
// file from the location specified by workingDir. If no location is specified, search the current
// directory. If the file doesn't exist at any of the default locations, return nil.
func ParseTerraformStateFileFromLocation(backend string, config map[string]interface{}, workingDir string) (*TerraformState, error) {
stateFile, ok := config["path"].(string)
if backend == "local" && ok && util.FileExists(stateFile) {
return ParseTerraformStateFile(stateFile)
} else if util.FileExists(util.JoinPath(workingDir, DEFAULT_PATH_TO_LOCAL_STATE_FILE)) {
return ParseTerraformStateFile(util.JoinPath(workingDir, DEFAULT_PATH_TO_LOCAL_STATE_FILE))
} else if util.FileExists(util.JoinPath(workingDir, DEFAULT_PATH_TO_REMOTE_STATE_FILE)) {
return ParseTerraformStateFile(util.JoinPath(workingDir, DEFAULT_PATH_TO_REMOTE_STATE_FILE))
Expand Down

0 comments on commit 9ee518b

Please sign in to comment.