Skip to content

Commit

Permalink
make fmt and remove resource namer
Browse files Browse the repository at this point in the history
  • Loading branch information
nfx committed Feb 18, 2022
1 parent 068e9a6 commit 8de53e2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 55 deletions.
2 changes: 1 addition & 1 deletion exporter/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestInteractivePrompts(t *testing.T) {
cliInput = dummyReader("y\n")
cliOutput = &bytes.Buffer{}
ic := &importContext{
Client: &common.DatabricksClient{},
Client: &common.DatabricksClient{},
Context: context.Background(),
Importables: map[string]importable{
"x": {
Expand Down
22 changes: 12 additions & 10 deletions exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ type mount struct {
ClusterID string
}

var nameFixes = []regexFix{
{regexp.MustCompile(`[0-9a-f]{8}[_-][0-9a-f]{4}[_-][0-9a-f]{4}` +
`[_-][0-9a-f]{4}[_-][0-9a-f]{12}[_-]`), ""},
{regexp.MustCompile(`[_-][0-9]+[\._-][0-9]+[\._-].*\.([a-z0-9]{1,4})`), "_$1"},
{regexp.MustCompile(`@.*$`), ""},
{regexp.MustCompile(`[-\s\.\|]`), "_"},
{regexp.MustCompile(`\W+`), ""},
{regexp.MustCompile(`[_]{2,}`), "_"},
}

func newImportContext(c *common.DatabricksClient) *importContext {
p := provider.DatabricksProvider()
p.TerraformVersion = "exporter"
Expand All @@ -103,16 +113,8 @@ func newImportContext(c *common.DatabricksClient) *importContext {
Files: map[string]*hclwrite.File{},
Scope: []*resource{},
importing: map[string]bool{},
nameFixes: []regexFix{
{regexp.MustCompile(`[0-9a-f]{8}[_-][0-9a-f]{4}[_-][0-9a-f]{4}` +
`[_-][0-9a-f]{4}[_-][0-9a-f]{12}[_-]`), ""},
{regexp.MustCompile(`[_-][0-9]+[\._-][0-9]+[\._-].*\.([a-z0-9]{1,4})`), "_$1"},
{regexp.MustCompile(`@.*$`), ""},
{regexp.MustCompile(`[-\s\.\|]`), "_"},
{regexp.MustCompile(`\W+`), ""},
{regexp.MustCompile(`[_]{2,}`), "_"},
},
hclFixes: []regexFix{ // Be careful with that! it may break working code
nameFixes: nameFixes,
hclFixes: []regexFix{ // Be careful with that! it may break working code
},
allUsers: []scim.User{},
variables: map[string]string{},
Expand Down
27 changes: 10 additions & 17 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ var resourcesMap map[string]importable = map[string]importable{
if scopes, err := ssAPI.List(); err == nil {
for i, scope := range scopes {
if !ic.MatchesName(scope.Name) {
log.Printf("[INFO] Secret scope %s doesn't match %s filter",
log.Printf("[INFO] Secret scope %s doesn't match %s filter",
scope.Name, ic.match)
continue
}
Expand Down Expand Up @@ -718,8 +718,7 @@ var resourcesMap map[string]importable = map[string]importable{
if name == "" {
return d.Id()
}
re := regexp.MustCompile(`[^0-9A-Za-z_]`)
return re.ReplaceAllString(name, "_")
return name
},
List: func(ic *importContext) error {
globalInitScripts, err := workspace.NewGlobalInitScriptsAPI(ic.Context, ic.Client).List()
Expand Down Expand Up @@ -761,11 +760,8 @@ var resourcesMap map[string]importable = map[string]importable{
name := d.Get("path").(string)
if name == "" {
return d.Id()
} else {
name = strings.TrimPrefix(name, "/")
}
re := regexp.MustCompile(`[^0-9A-Za-z_]`)
return re.ReplaceAllString(name, "_")
return strings.TrimPrefix(name, "/")
},
List: func(ic *importContext) error {
repoList, err := repos.NewReposAPI(ic.Context, ic.Client).ListAll()
Expand Down Expand Up @@ -825,9 +821,9 @@ var resourcesMap map[string]importable = map[string]importable{
Import: func(ic *importContext, r *resource) error {
wsConfAPI := workspace.NewWorkspaceConfAPI(ic.Context, ic.Client)
keys := map[string]interface{}{
"enableIpAccessLists": false,
"maxTokenLifetimeDays": 0,
"enableTokensConfig": false,
"enableIpAccessLists": false,
"maxTokenLifetimeDays": 0,
"enableTokensConfig": false,
}
err := wsConfAPI.Read(&keys)
if err != nil {
Expand Down Expand Up @@ -875,11 +871,8 @@ var resourcesMap map[string]importable = map[string]importable{
name := d.Get("path").(string)
if name == "" {
return d.Id()
} else {
name = strings.TrimPrefix(name, "/")
}
re := regexp.MustCompile(`[^0-9A-Za-z_]`)
return strings.ToLower(re.ReplaceAllString(name, "_"))
return name
},
List: func(ic *importContext) error {
notebooksAPI := workspace.NewNotebooksAPI(ic.Context, ic.Client)
Expand Down Expand Up @@ -912,10 +905,10 @@ var resourcesMap map[string]importable = map[string]importable{
}
language := r.Data.Get("language").(string)
ext := map[string]string{
"SCALA": ".scala",
"SCALA": ".scala",
"PYTHON": ".py",
"SQL": ".sql",
"R": ".r",
"SQL": ".sql",
"R": ".r",
}
name := r.ID[1:] + ext[language] // todo: replace non-alphanum+/ with _
content, _ := base64.StdEncoding.DecodeString(contentB64)
Expand Down
46 changes: 19 additions & 27 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func importContextForTest() *importContext {
return &importContext{
Importables: resourcesMap,
Resources: p.ResourcesMap,
Files: map[string]*hclwrite.File{},
Files: map[string]*hclwrite.File{},
testEmits: map[string]bool{},
nameFixes: nameFixes,
}
}

Expand Down Expand Up @@ -542,15 +543,6 @@ func TestRepoListFails(t *testing.T) {
})
}

func TestNotebookName(t *testing.T) {
d := workspace.ResourceNotebook().TestResourceData()
d.SetId("x")
assert.Equal(t, "x", resourcesMap["databricks_notebook"].Name(d))

d.Set("path", "/Foo/Bar/Baz")
assert.Equal(t, "foo_bar_baz", resourcesMap["databricks_notebook"].Name(d))
}

func testGenerate(t *testing.T, fixtures []qa.HTTPFixture, services string, cb func(*importContext)) {
qa.HTTPFixturesApply(t, fixtures, func(ctx context.Context, client *common.DatabricksClient) {
ic := importContextForTest()
Expand Down Expand Up @@ -607,7 +599,7 @@ func TestNotebookGeneration(t *testing.T) {

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
resource "databricks_notebook" "first_second" {
resource "databricks_notebook" "firstsecond" {
source = "${path.module}/notebooks/First/Second.py"
path = "/First/Second"
}`), string(ic.Files["notebooks"].Bytes()))
Expand All @@ -619,15 +611,15 @@ func TestGitCredentialGen(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.0/git-credentials/a",
Response: repos.GitCredentialResponse {
Response: repos.GitCredentialResponse{
UserName: "me",
Provider: "github",
},
},
}, "repos", func(ic *importContext) {
ic.Emit(&resource{
Resource: "databricks_git_credential",
ID: "a",
ID: "a",
})

ic.generateHclForResources(nil)
Expand All @@ -643,26 +635,26 @@ func TestGitCredentialGen(t *testing.T) {
func TestGlobalInitScriptGen(t *testing.T) {
testGenerate(t, []qa.HTTPFixture{
{
Method: "GET",
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/global-init-scripts/a",
Response: workspace.GlobalInitScriptInfo {
Name: "b",
Enabled: true,
Resource: "/api/2.0/global-init-scripts/a",
Response: workspace.GlobalInitScriptInfo{
Name: "New: Importing ^ Things",
Enabled: true,
ContentBase64: "YWJj",
},
},
}, "workspace", func(ic *importContext) {
ic.Emit(&resource{
Resource: "databricks_global_init_script",
ID: "a",
ID: "a",
})

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
resource "databricks_global_init_script" "b" {
source = "${path.module}/files/b.sh"
name = "b"
resource "databricks_global_init_script" "new_importing_things" {
source = "${path.module}/files/new_importing_things.sh"
name = "New: Importing ^ Things"
enabled = true
}`), string(ic.Files["workspace"].Bytes()))
})
Expand All @@ -674,7 +666,7 @@ func TestSecretGen(t *testing.T) {
Method: "GET",
Resource: "/api/2.0/secrets/list?scope=a",

Response: secrets.SecretsList {
Response: secrets.SecretsList{
Secrets: []secrets.SecretMetadata{
{
Key: "b",
Expand All @@ -685,7 +677,7 @@ func TestSecretGen(t *testing.T) {
}, "secrets", func(ic *importContext) {
ic.Emit(&resource{
Resource: "databricks_secret",
ID: "a|||b",
ID: "a|||b",
})

ic.generateHclForResources(nil)
Expand All @@ -703,7 +695,7 @@ func TestDbfsFileGen(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.0/dbfs/get-status?path=a",
Response: storage.FileInfo {
Response: storage.FileInfo{
Path: "a",
},
},
Expand All @@ -718,7 +710,7 @@ func TestDbfsFileGen(t *testing.T) {
}, "storage", func(ic *importContext) {
ic.Emit(&resource{
Resource: "databricks_dbfs_file",
ID: "a",
ID: "a",
})

ic.generateHclForResources(nil)
Expand All @@ -728,4 +720,4 @@ func TestDbfsFileGen(t *testing.T) {
path = "a"
}`), string(ic.Files["storage"].Bytes()))
})
}
}

0 comments on commit 8de53e2

Please sign in to comment.