-
Notifications
You must be signed in to change notification settings - Fork 7
/
google_scanner.go
68 lines (54 loc) · 2.42 KB
/
google_scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package terraformercli
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
terraformValueObjects "github.com/dragondrop-cloud/cloud-concierge/main/internal/implementations/terraform_value_objects"
)
var defaultGoogleRegions = []string{"us-east4"}
// GoogleScanner implements the Scanner interface for use with GCP cloud environments.
type GoogleScanner struct {
// credential is the credential needed to scan a GCP cloud environment.
credential terraformValueObjects.Credential
// terraformer is the TerraformerCLI interface used to scan the GCP cloud environment.
terraformer TerraformerCLI
// CloudRegions represents the list of cloud regions that will be considered for inclusion in the import statement.
CloudRegions []terraformValueObjects.CloudRegion `required:"true"`
}
// NewGoogleScanner creates and returns a new instance of GCPScanner.
func NewGoogleScanner(credential terraformValueObjects.Credential, cliConfig Config, cloudRegions []terraformValueObjects.CloudRegion) (Scanner, error) {
return &GoogleScanner{
CloudRegions: cloudRegions,
credential: credential,
terraformer: newTerraformerCLI(cliConfig),
}, nil
}
// Scan uses the TerraformerCLI interface to scan a given division's cloud environment
func (gcpScan *GoogleScanner) Scan(project terraformValueObjects.Division, credential terraformValueObjects.Credential, _ ...string) error {
logrus.Debugf("[Scan] Scanning GCP project %v", project)
_ = os.MkdirAll("credentials", 0o660)
err := os.WriteFile("credentials/google.json", []byte(credential), 0o400)
if err != nil {
return fmt.Errorf("[Scan] error saving credential file: %v", err)
}
err = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "credentials/google.json")
if err != nil {
return fmt.Errorf("[Scan] Error in setting GOOGLE_APPLICATION_CREDENTIALS value: %v", err)
}
projectsFlag := fmt.Sprintf("--projects=%v", project)
err = gcpScan.terraformer.Import(TerraformImportMigrationGeneratorParams{
Provider: "google",
Regions: getValidRegions(gcpScan.CloudRegions, terraformValueObjects.GoogleRegions, defaultGoogleRegions),
Resources: []string{"us-east4", "global"},
AdditionalArgs: []string{projectsFlag},
IsCompact: true,
})
if err != nil {
return fmt.Errorf("[Scan] Error in terraformer.Import(): %v", err)
}
err = gcpScan.terraformer.UpdateState("google")
if err != nil {
return fmt.Errorf("[Scan] Error in terraformer.UpdateState(): %v", err)
}
return nil
}