Skip to content

Commit

Permalink
Exporter: fix generation of run_as blocks in databricks_job (#3724)
Browse files Browse the repository at this point in the history
* Exporter: fix generation of `run_as` blocks in `databricks_job`

Because the `run_as` was marked as `computed` it was ignored when generating the code.

* Ignore `run_as` for the current user
  • Loading branch information
alexott authored Jul 2, 2024
1 parent fc889cc commit c6f949c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ func (ic *importContext) Run() error {
if err != nil {
return err
}
ic.meUserName = me.UserName
for _, g := range me.Groups {
if g.Display == "admins" {
ic.meAdmin = true
ic.meUserName = me.UserName
break
}
}
Expand Down
20 changes: 19 additions & 1 deletion exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2553,7 +2553,19 @@ resource "databricks_pipeline" "def" {
func TestImportingRunJobTask(t *testing.T) {
qa.HTTPFixturesApply(t,
[]qa.HTTPFixture{
meAdminFixture,
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/preview/scim/v2/Me",
Response: scim.User{
Groups: []scim.ComplexValue{
{
Display: "admins",
},
},
UserName: "[email protected]",
},
},
noCurrentMetastoreAttached,
emptyRepos,
emptyIpAccessLIst,
Expand Down Expand Up @@ -2596,5 +2608,11 @@ func TestImportingRunJobTask(t *testing.T) {
assert.True(t, strings.Contains(contentStr, `job_id = databricks_job.jartask_932035899730845.id`))
assert.True(t, strings.Contains(contentStr, `resource "databricks_job" "runjobtask_1047501313827425"`))
assert.True(t, strings.Contains(contentStr, `resource "databricks_job" "jartask_932035899730845"`))
assert.True(t, strings.Contains(contentStr, `run_as {
service_principal_name = "c1b2a35b-87c4-481a-a0fb-0508be621957"
}`))
assert.False(t, strings.Contains(contentStr, `run_as {
user_name = "[email protected]"
}`))
})
}
11 changes: 11 additions & 0 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,17 @@ var resourcesMap map[string]importable = map[string]importable{
if js.NotificationSettings != nil {
return reflect.DeepEqual(*js.NotificationSettings, sdk_jobs.JobNotificationSettings{})
}
case "run_as":
if js.RunAs != nil && (js.RunAs.UserName != "" || js.RunAs.ServicePrincipalName != "") {
var user string
if js.RunAs.UserName != "" {
user = js.RunAs.UserName
} else {
user = js.RunAs.ServicePrincipalName
}
return user == ic.meUserName
}
return true
}
if strings.HasPrefix(pathString, "task.") {
parts := strings.Split(pathString, ".")
Expand Down
2 changes: 2 additions & 0 deletions exporter/test-data/run-job-child.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"created_time":1678702840675,
"job_id":932035899730845,
"run_as_user_name": "c1b2a35b-87c4-481a-a0fb-0508be621957",
"run_as_owner": false,
"settings": {
"format":"MULTI_TASK",
"max_concurrent_runs":1,
Expand Down
2 changes: 2 additions & 0 deletions exporter/test-data/run-job-main.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"created_time":1700654567867,
"job_id":1047501313827425,
"run_as_user_name": "[email protected]",
"run_as_owner": false,
"settings": {
"format":"MULTI_TASK",
"max_concurrent_runs":1,
Expand Down

0 comments on commit c6f949c

Please sign in to comment.