Fixed instance pool resolving by name (#1102)

## Changes
Fixed instance pool resolving by name

## Tests
Added regression test
This commit is contained in:
Andrew Nester 2024-01-05 11:50:53 +01:00 committed by GitHub
parent 5fb40f9d07
commit 4b01fff03d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 37 deletions

View File

@ -38,7 +38,7 @@ func LookupFromMap(m map[string]any) *Lookup {
l := &Lookup{} l := &Lookup{}
{{range .Services -}} {{range .Services -}}
{{- if in $allowlist .KebabName -}} {{- if in $allowlist .KebabName -}}
if v, ok := m["{{.Singular.KebabName}}"]; ok { if v, ok := m["{{.Singular.SnakeName}}"]; ok {
l.{{.Singular.PascalName}} = v.(string) l.{{.Singular.PascalName}} = v.(string)
} }
{{end -}} {{end -}}
@ -51,11 +51,11 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st
return "", err return "", err
} }
resolvers := resolvers() r := allResolvers()
{{range .Services -}} {{range .Services -}}
{{- if in $allowlist .KebabName -}} {{- if in $allowlist .KebabName -}}
if l.{{.Singular.PascalName}} != "" { if l.{{.Singular.PascalName}} != "" {
return resolvers["{{.Singular.KebabName}}"](ctx, w, l.{{.Singular.PascalName}}) return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}})
} }
{{end -}} {{end -}}
{{- end}} {{- end}}
@ -98,12 +98,19 @@ func (l *Lookup) validate() error {
type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error)
type resolvers struct {
func resolvers() map[string](resolverFunc) {
resolvers := make(map[string](resolverFunc), 0)
{{range .Services -}} {{range .Services -}}
{{- if in $allowlist .KebabName -}} {{- if in $allowlist .KebabName -}}
resolvers["{{.Singular.KebabName}}"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { {{.Singular.PascalName}} resolverFunc
{{end -}}
{{- end}}
}
func allResolvers() *resolvers {
r := &resolvers{}
{{range .Services -}}
{{- if in $allowlist .KebabName -}}
r.{{.Singular.PascalName}} = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.{{.PascalName}}.GetBy{{range .List.NamedIdMap.NamePath}}{{.PascalName}}{{end}}(ctx, name) entity, err := w.{{.PascalName}}.GetBy{{range .List.NamedIdMap.NamePath}}{{.PascalName}}{{end}}(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -114,7 +121,7 @@ func resolvers() map[string](resolverFunc) {
{{end -}} {{end -}}
{{- end}} {{- end}}
return resolvers return r
} }

View File

@ -39,7 +39,7 @@ func LookupFromMap(m map[string]any) *Lookup {
if v, ok := m["alert"]; ok { if v, ok := m["alert"]; ok {
l.Alert = v.(string) l.Alert = v.(string)
} }
if v, ok := m["cluster-policy"]; ok { if v, ok := m["cluster_policy"]; ok {
l.ClusterPolicy = v.(string) l.ClusterPolicy = v.(string)
} }
if v, ok := m["cluster"]; ok { if v, ok := m["cluster"]; ok {
@ -48,7 +48,7 @@ func LookupFromMap(m map[string]any) *Lookup {
if v, ok := m["dashboard"]; ok { if v, ok := m["dashboard"]; ok {
l.Dashboard = v.(string) l.Dashboard = v.(string)
} }
if v, ok := m["instance-pool"]; ok { if v, ok := m["instance_pool"]; ok {
l.InstancePool = v.(string) l.InstancePool = v.(string)
} }
if v, ok := m["job"]; ok { if v, ok := m["job"]; ok {
@ -63,7 +63,7 @@ func LookupFromMap(m map[string]any) *Lookup {
if v, ok := m["query"]; ok { if v, ok := m["query"]; ok {
l.Query = v.(string) l.Query = v.(string)
} }
if v, ok := m["service-principal"]; ok { if v, ok := m["service_principal"]; ok {
l.ServicePrincipal = v.(string) l.ServicePrincipal = v.(string)
} }
if v, ok := m["warehouse"]; ok { if v, ok := m["warehouse"]; ok {
@ -78,39 +78,39 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st
return "", err return "", err
} }
resolvers := resolvers() r := allResolvers()
if l.Alert != "" { if l.Alert != "" {
return resolvers["alert"](ctx, w, l.Alert) return r.Alert(ctx, w, l.Alert)
} }
if l.ClusterPolicy != "" { if l.ClusterPolicy != "" {
return resolvers["cluster-policy"](ctx, w, l.ClusterPolicy) return r.ClusterPolicy(ctx, w, l.ClusterPolicy)
} }
if l.Cluster != "" { if l.Cluster != "" {
return resolvers["cluster"](ctx, w, l.Cluster) return r.Cluster(ctx, w, l.Cluster)
} }
if l.Dashboard != "" { if l.Dashboard != "" {
return resolvers["dashboard"](ctx, w, l.Dashboard) return r.Dashboard(ctx, w, l.Dashboard)
} }
if l.InstancePool != "" { if l.InstancePool != "" {
return resolvers["instance-pool"](ctx, w, l.InstancePool) return r.InstancePool(ctx, w, l.InstancePool)
} }
if l.Job != "" { if l.Job != "" {
return resolvers["job"](ctx, w, l.Job) return r.Job(ctx, w, l.Job)
} }
if l.Metastore != "" { if l.Metastore != "" {
return resolvers["metastore"](ctx, w, l.Metastore) return r.Metastore(ctx, w, l.Metastore)
} }
if l.Pipeline != "" { if l.Pipeline != "" {
return resolvers["pipeline"](ctx, w, l.Pipeline) return r.Pipeline(ctx, w, l.Pipeline)
} }
if l.Query != "" { if l.Query != "" {
return resolvers["query"](ctx, w, l.Query) return r.Query(ctx, w, l.Query)
} }
if l.ServicePrincipal != "" { if l.ServicePrincipal != "" {
return resolvers["service-principal"](ctx, w, l.ServicePrincipal) return r.ServicePrincipal(ctx, w, l.ServicePrincipal)
} }
if l.Warehouse != "" { if l.Warehouse != "" {
return resolvers["warehouse"](ctx, w, l.Warehouse) return r.Warehouse(ctx, w, l.Warehouse)
} }
return "", fmt.Errorf("no valid lookup fields provided") return "", fmt.Errorf("no valid lookup fields provided")
@ -203,10 +203,23 @@ func (l *Lookup) validate() error {
} }
type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error)
type resolvers struct {
Alert resolverFunc
ClusterPolicy resolverFunc
Cluster resolverFunc
Dashboard resolverFunc
InstancePool resolverFunc
Job resolverFunc
Metastore resolverFunc
Pipeline resolverFunc
Query resolverFunc
ServicePrincipal resolverFunc
Warehouse resolverFunc
}
func resolvers() map[string](resolverFunc) { func allResolvers() *resolvers {
resolvers := make(map[string](resolverFunc), 0) r := &resolvers{}
resolvers["alert"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Alerts.GetByName(ctx, name) entity, err := w.Alerts.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -214,7 +227,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.Id), nil return fmt.Sprint(entity.Id), nil
} }
resolvers["cluster-policy"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.ClusterPolicies.GetByName(ctx, name) entity, err := w.ClusterPolicies.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -222,7 +235,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.PolicyId), nil return fmt.Sprint(entity.PolicyId), nil
} }
resolvers["cluster"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Clusters.GetByClusterName(ctx, name) entity, err := w.Clusters.GetByClusterName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -230,7 +243,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.ClusterId), nil return fmt.Sprint(entity.ClusterId), nil
} }
resolvers["dashboard"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Dashboards.GetByName(ctx, name) entity, err := w.Dashboards.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -238,7 +251,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.Id), nil return fmt.Sprint(entity.Id), nil
} }
resolvers["instance-pool"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.InstancePools.GetByInstancePoolName(ctx, name) entity, err := w.InstancePools.GetByInstancePoolName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -246,7 +259,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.InstancePoolId), nil return fmt.Sprint(entity.InstancePoolId), nil
} }
resolvers["job"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Jobs.GetBySettingsName(ctx, name) entity, err := w.Jobs.GetBySettingsName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -254,7 +267,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.JobId), nil return fmt.Sprint(entity.JobId), nil
} }
resolvers["metastore"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Metastores.GetByName(ctx, name) entity, err := w.Metastores.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -262,7 +275,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.MetastoreId), nil return fmt.Sprint(entity.MetastoreId), nil
} }
resolvers["pipeline"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Pipelines.GetByName(ctx, name) entity, err := w.Pipelines.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -270,7 +283,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.PipelineId), nil return fmt.Sprint(entity.PipelineId), nil
} }
resolvers["query"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Queries.GetByName(ctx, name) entity, err := w.Queries.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -278,7 +291,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.Id), nil return fmt.Sprint(entity.Id), nil
} }
resolvers["service-principal"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name) entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -286,7 +299,7 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.Id), nil return fmt.Sprint(entity.Id), nil
} }
resolvers["warehouse"] = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
entity, err := w.Warehouses.GetByName(ctx, name) entity, err := w.Warehouses.GetByName(ctx, name)
if err != nil { if err != nil {
return "", err return "", err
@ -295,5 +308,5 @@ func resolvers() map[string](resolverFunc) {
return fmt.Sprint(entity.Id), nil return fmt.Sprint(entity.Id), nil
} }
return resolvers return r
} }

View File

@ -11,6 +11,11 @@ variables:
lookup: lookup:
cluster: some-cluster cluster: some-cluster
e:
description: variable with lookup
lookup:
instance_pool: some-pool
bundle: bundle:
name: test bundle name: test bundle
@ -41,4 +46,7 @@ targets:
d: d:
lookup: lookup:
cluster: some-test-cluster cluster: some-test-cluster
e:
lookup:
instance_pool: some-test-instance-pool
b: prod-b b: prod-b

View File

@ -115,4 +115,5 @@ func TestVariablesWithTargetLookupOverrides(t *testing.T) {
))) )))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "cluster: some-test-cluster", b.Config.Variables["d"].Lookup.String()) assert.Equal(t, "cluster: some-test-cluster", b.Config.Variables["d"].Lookup.String())
assert.Equal(t, "instance-pool: some-test-instance-pool", b.Config.Variables["e"].Lookup.String())
} }