Skip to content

Commit

Permalink
Importer: Add resource_types flag (#779)
Browse files Browse the repository at this point in the history
* Support interactive correctly

* Add --resources flag to tfimport

* Switch to specific resource types

* Remove leftover from branch

* Fix custom type repr
  • Loading branch information
MartinPetkov committed Feb 5, 2021
1 parent 1b25823 commit bef1bc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
31 changes: 26 additions & 5 deletions cmd/tfimport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
// Requires Terraform to be installed and for authentication to be configured for each provider in the Terraform configs provider blocks.
//
// Usage:
// $ go run . [--input_dir=/path/to/config]
// $ go run . [--input_dir=/path/to/config] [--resource_types 'google_storage_bucket.bucket' --resources 'google_resource_manager_lien.lien']
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
Expand All @@ -29,6 +30,23 @@ import (
"github.com/GoogleCloudPlatform/healthcare-data-protection-suite/internal/tfimport"
)

// From https://stackoverflow.com/a/28323276
type mapFlag map[string]bool

func (i *mapFlag) String() string {
if b, err := json.Marshal(*i); err == nil {
return string(b)
}
return ""
}

func (i *mapFlag) Set(value string) error {
(*i)[value] = true
return nil
}

var resourcesFlag = make(mapFlag)

var (
inputDir = flag.String("input_dir", ".", "Path to the directory containing Terraform configs.")
terraformPath = flag.String("terraform_path", "terraform", "Name or path to the terraform binary to use.")
Expand All @@ -38,6 +56,8 @@ var (
)

func main() {
flag.Var(&resourcesFlag, "resource_types", "Specific resource types to import, specified as terraform resource names (e.g. 'google_storage_bucket', 'google_resource_manager_lien'). Leave empty to import all.")

if err := run(); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -66,10 +86,11 @@ func run() error {
}

args := &tfimport.RunArgs{
InputDir: *inputDir,
TerraformPath: *terraformPath,
DryRun: *dryRun,
Interactive: *interactive,
InputDir: *inputDir,
TerraformPath: *terraformPath,
DryRun: *dryRun,
Interactive: *interactive,
SpecificResourceTypes: resourcesFlag,
}

if err := tfimport.Run(rn, importRn, args); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions internal/tfimport/tfimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ type RunArgs struct {
TerraformPath string
DryRun bool
Interactive bool

// This is a "set" of resource types to import.
// If not nil and not empty, will import only resources which match it.
SpecificResourceTypes map[string]bool
}

// Run executes the main tfimport logic.
Expand Down Expand Up @@ -614,6 +618,14 @@ func planAndImport(rn, importRn runner.Runner, runArgs *RunArgs) (retry bool, er

log.Printf("Found importable resource: %q\n", ir.Change.Address)

// Check against specific resources list, if present.
if len(runArgs.SpecificResourceTypes) > 0 {
if _, ok := runArgs.SpecificResourceTypes[ir.Change.Kind]; !ok {
log.Printf("Skipping %v, not in list of specific resource types to import", ir.Change.Address)
continue
}
}

// Attempt the import.
output, err := Import(importRn, ir, runArgs.InputDir, runArgs.TerraformPath, runArgs.Interactive)

Expand Down

0 comments on commit bef1bc2

Please sign in to comment.