Skip to content

Commit af908e2

Browse files
alexottnfx
authored andcommitted
Allow to specify prefix that will be used in all exported names
1 parent d4a20e2 commit af908e2

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

docs/guides/experimental-exporter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ All arguments are optional and they tune what code is being generated.
3636
* `-match` - Match resource names during listing operation. This filter applies to all resources that are getting listed, so if you want to import all dependencies of just one cluster, specify `-match=autoscaling -listing=compute`. By default is empty, which matches everything.
3737
* `-mounts` - List DBFS mount points, which is a extremely slow operation and would not trigger unless explicitly specified.
3838
* `-generateProviderDeclaration` - flag that toggles generation of `databricks.tf` file with declaration of the Databricks Terraform provider that is necessary for Terraform versions since Terraform 0.13 (disabled by default).
39+
* `-prefix` - optional prefix that will be added to the name of all exported resources - that's useful for exporting resources multiple workspaces for merging into single one.
3940

4041
## Services
4142

exporter/command.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func Run(args ...string) error {
7777
flags.StringVar(&ic.match, "match", "", "Match resource names during listing operation. "+
7878
"This filter applies to all resources that are getting listed, so if you want to import "+
7979
"all dependencies of just one cluster, specify -listing=compute")
80+
prefix := ""
81+
flags.StringVar(&prefix, "prefix", "", "Prefix that will be added to the name of all exported resources")
8082
newArgs := args
8183
if len(args) > 1 && args[1] == "exporter" {
8284
newArgs = args[2:]
@@ -85,6 +87,9 @@ func Run(args ...string) error {
8587
if err != nil {
8688
return err
8789
}
90+
if len(prefix) > 0 {
91+
ic.prefix = prefix + "_"
92+
}
8893
if ic.debug {
8994
logLevel = append(logLevel, "[DEBUG]")
9095
}

exporter/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type importContext struct {
7272
lastActiveDays int64
7373
generateDeclaration bool
7474
meAdmin bool
75+
prefix string
7576
}
7677

7778
type mount struct {
@@ -359,6 +360,7 @@ func (ic *importContext) ResourceName(r *resource) string {
359360
if name == "" {
360361
name = r.ID
361362
}
363+
name = ic.prefix + name
362364
name = strings.ToLower(name)
363365
name = ic.regexFix(name, ic.nameFixes)
364366
// this is either numeric id or all-non-ascii

exporter/exporter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ func TestImportingWithError(t *testing.T) {
747747
err := Run("-directory", "/bin/sh", "-services", "groups,users")
748748
assert.EqualError(t, err, "The path /bin/sh is not a directory")
749749

750-
err = Run("-directory", "/bin/abcd", "-services", "groups,users")
750+
err = Run("-directory", "/bin/abcd", "-services", "groups,users", "-prefix", "abc")
751751
assert.EqualError(t, err, "Can't create directory /bin/abcd")
752752
}
753753

exporter/importables.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ var resourcesMap map[string]importable = map[string]importable{
4747
if err != nil && !os.IsExist(err) {
4848
return err
4949
}
50-
local, err := os.Create(fmt.Sprintf("%s/files/%s", ic.Directory, path.Base(r.ID)))
50+
fileName := ic.prefix + path.Base(r.ID)
51+
local, err := os.Create(fmt.Sprintf("%s/files/%s", ic.Directory, fileName))
5152
if err != nil {
5253
return err
5354
}
@@ -58,7 +59,7 @@ var resourcesMap map[string]importable = map[string]importable{
5859
}
5960
// libraries installed with init scripts won't be exported.
6061
b := body.AppendNewBlock("resource", []string{r.Resource, r.Name}).Body()
61-
relativeFile := fmt.Sprintf("${path.module}/files/%s", path.Base(r.ID))
62+
relativeFile := fmt.Sprintf("${path.module}/files/%s", fileName)
6263
b.SetAttributeValue("path", cty.StringVal(strings.Replace(r.ID, "dbfs:", "", 1)))
6364
b.SetAttributeRaw("source", hclwrite.Tokens{
6465
&hclwrite.Token{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}},
@@ -816,7 +817,8 @@ var resourcesMap map[string]importable = map[string]importable{
816817
if err != nil && !os.IsExist(err) {
817818
return err
818819
}
819-
local, err := os.Create(fmt.Sprintf("%s/files/gis-%s", ic.Directory, path.Base(r.Name)))
820+
fileName := path.Base(r.Name)
821+
local, err := os.Create(fmt.Sprintf("%s/files/gis-%s", ic.Directory, fileName))
820822
if err != nil {
821823
return err
822824
}
@@ -829,7 +831,7 @@ var resourcesMap map[string]importable = map[string]importable{
829831
if err != nil {
830832
return err
831833
}
832-
relativeFile := fmt.Sprintf("${path.module}/files/gis-%s", path.Base(r.Name))
834+
relativeFile := fmt.Sprintf("${path.module}/files/gis-%s", fileName)
833835
b := body.AppendNewBlock("resource", []string{r.Resource, r.Name}).Body()
834836
b.SetAttributeValue("name", cty.StringVal(gis.Name))
835837
b.SetAttributeValue("enabled", cty.BoolVal(gis.Enabled))

0 commit comments

Comments
 (0)