Skip to content

Commit 481216b

Browse files
dagang-wfuncticons
authored andcommitted
Validate GOOGLE_APPLICATION_CREDENTIALS
Adds an early check for `terraformer import google` to make sure GOOGLE_APPLICATION_CREDENTIALS is set and the file exists, otherwise returns an error and fails. Fix #2064
1 parent 069060c commit 481216b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

cmd/provider_cmd_google.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
package cmd
1515

1616
import (
17+
"errors"
1718
"log"
19+
"os"
1820
"strings"
1921

2022
gcp_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/gcp"
@@ -29,6 +31,11 @@ func newCmdGoogleImporter(options ImportOptions) *cobra.Command {
2931
Short: "Import current state to Terraform configuration from Google Cloud",
3032
Long: "Import current state to Terraform configuration from Google Cloud",
3133
RunE: func(cmd *cobra.Command, args []string) error {
34+
if credentialPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); credentialPath == "" {
35+
return errors.New("gcp: GOOGLE_APPLICATION_CREDENTIALS is not set")
36+
} else if _, err := os.Stat(credentialPath); os.IsNotExist(err) {
37+
return errors.New("gcp: GOOGLE_APPLICATION_CREDENTIALS file does not exist")
38+
}
3239
originalPathPattern := options.PathPattern
3340
for _, project := range options.Projects {
3441
for _, region := range options.Regions {

0 commit comments

Comments
 (0)