Return `application_id` for service principal lookups (#1245)

## Changes
Return ApplicationId for service principals lookups

Fixes #1234 

## Tests
Added (regression) tests
This commit is contained in:
Andrew Nester 2024-03-04 17:12:10 +01:00 committed by GitHub
parent 29ab96f327
commit 09d1846e13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 7 deletions

View File

@ -18,6 +18,11 @@ package variable
"warehouses"
}}
{{ $customField :=
dict
"service-principals" "ApplicationId"
}}
import (
"context"
"fmt"
@ -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 -}}

View File

@ -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) {
@ -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)
}

View File

@ -297,7 +297,7 @@ func allResolvers() *resolvers {
return "", err
}
return fmt.Sprint(entity.Id), nil
return fmt.Sprint(entity.ApplicationId), nil
}
r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Warehouses.GetByName(ctx, name)