Skip to content

Commit

Permalink
Return application_id for service principal lookups (#1245)
Browse files Browse the repository at this point in the history
## Changes
Return ApplicationId for service principals lookups

Fixes #1234 

## Tests
Added (regression) tests
  • Loading branch information
andrewnester authored Mar 4, 2024
1 parent 29ab96f commit 09d1846
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .codegen/lookup.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package variable
"warehouses"
}}

{{ $customField :=
dict
"service-principals" "ApplicationId"
}}

import (
"context"
"fmt"
Expand Down Expand Up @@ -116,15 +121,10 @@ func allResolvers() *resolvers {
return "", err
}

return fmt.Sprint(entity{{ template "field-path" .List.NamedIdMap.IdPath }}), nil
return fmt.Sprint(entity.{{ getOrDefault $customField .KebabName ((index .List.NamedIdMap.IdPath 0).PascalName) }}), nil
}
{{end -}}
{{- end}}

return r
}


{{- define "field-path" -}}
{{- range .}}.{{.PascalName}}{{end}}
{{- end -}}
28 changes: 28 additions & 0 deletions bundle/config/mutator/resolve_resource_references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/iam"
)

func TestResolveClusterReference(t *testing.T) {
Expand Down Expand Up @@ -105,3 +106,30 @@ func TestNoLookupIfVariableIsSet(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "random value", *b.Config.Variables["my-cluster-id"].Value)
}

func TestResolveServicePrincipal(t *testing.T) {
spName := "Some SP name"
b := &bundle.Bundle{
Config: config.Root{
Variables: map[string]*variable.Variable{
"my-sp": {
Lookup: &variable.Lookup{
ServicePrincipal: spName,
},
},
},
},
}

m := mocks.NewMockWorkspaceClient(t)
b.SetWorkpaceClient(m.WorkspaceClient)
spApi := m.GetMockServicePrincipalsAPI()
spApi.EXPECT().GetByDisplayName(mock.Anything, spName).Return(&iam.ServicePrincipal{
Id: "1234",
ApplicationId: "app-1234",
}, nil)

err := bundle.Apply(context.Background(), b, ResolveResourceReferences())
require.NoError(t, err)
require.Equal(t, "app-1234", *b.Config.Variables["my-sp"].Value)
}
2 changes: 1 addition & 1 deletion bundle/config/variable/lookup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09d1846

Please sign in to comment.