From a922cc9195dc36f4dcaad40b472181b94bc43bd8 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 29 Feb 2024 15:36:20 +0100 Subject: [PATCH 1/2] Added support for custom lookup field and return ApplicationId for service principals --- .codegen/lookup.go.tmpl | 29 ++- .../resolve_resource_references_test.go | 83 +++++++++ bundle/config/variable/lookup.go | 170 ++++++++++++++---- 3 files changed, 239 insertions(+), 43 deletions(-) diff --git a/.codegen/lookup.go.tmpl b/.codegen/lookup.go.tmpl index a982f151a9..e6c6b45d26 100644 --- a/.codegen/lookup.go.tmpl +++ b/.codegen/lookup.go.tmpl @@ -18,6 +18,11 @@ package variable "warehouses" }} +{{ $customField := + dict + "service-principals" "ApplicationId" +}} + import ( "context" "fmt" @@ -26,6 +31,8 @@ import ( ) type Lookup struct { + Field string `json:"field,omitempty"` + {{range .Services -}} {{- if in $allowlist .KebabName -}} {{.Singular.PascalName}} string `json:"{{.Singular.SnakeName}},omitempty"` @@ -55,7 +62,11 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st {{range .Services -}} {{- if in $allowlist .KebabName -}} if l.{{.Singular.PascalName}} != "" { - return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}}) + fieldString := "{{ getOrDefault $customField .KebabName ((index .List.NamedIdMap.IdPath 0).PascalName) }}" + if l.Field != "" { + fieldString = l.Field + } + return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}}, fieldString) } {{end -}} {{- end}} @@ -97,7 +108,7 @@ 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, field string) (string, error) type resolvers struct { {{range .Services -}} {{- if in $allowlist .KebabName -}} @@ -110,21 +121,21 @@ 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) { + r.{{.Singular.PascalName}} = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.{{.PascalName}}.GetBy{{range .List.NamedIdMap.NamePath}}{{.PascalName}}{{end}}(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity{{ template "field-path" .List.NamedIdMap.IdPath }}), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } {{end -}} {{- end}} return r } - - -{{- define "field-path" -}} - {{- range .}}.{{.PascalName}}{{end}} -{{- end -}} diff --git a/bundle/config/mutator/resolve_resource_references_test.go b/bundle/config/mutator/resolve_resource_references_test.go index 4d51285c66..8f41634302 100644 --- a/bundle/config/mutator/resolve_resource_references_test.go +++ b/bundle/config/mutator/resolve_resource_references_test.go @@ -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,85 @@ func TestNoLookupIfVariableIsSet(t *testing.T) { require.NoError(t, err) require.Equal(t, "random value", *b.Config.Variables["my-cluster-id"].Value) } + +func TestResolveClusterWithCustomField(t *testing.T) { + clusterRef := "Some Custom Cluster" + b := &bundle.Bundle{ + Config: config.Root{ + Variables: map[string]*variable.Variable{ + "my-cluster-driver-node-type": { + Lookup: &variable.Lookup{ + Cluster: clusterRef, + Field: "DriverNodeTypeId", + }, + }, + }, + }, + } + + m := mocks.NewMockWorkspaceClient(t) + b.SetWorkpaceClient(m.WorkspaceClient) + clusterApi := m.GetMockClustersAPI() + clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef).Return(&compute.ClusterDetails{ + ClusterId: "1234-5678-abcd", + DriverNodeTypeId: "driver-node-type", + }, nil) + + err := bundle.Apply(context.Background(), b, ResolveResourceReferences()) + require.NoError(t, err) + require.Equal(t, "driver-node-type", *b.Config.Variables["my-cluster-driver-node-type"].Value) +} + +func TestResolveClusterWithCustomNonExistingField(t *testing.T) { + clusterRef := "Some Custom Cluster" + b := &bundle.Bundle{ + Config: config.Root{ + Variables: map[string]*variable.Variable{ + "my-cluster-driver-node-type": { + Lookup: &variable.Lookup{ + Cluster: clusterRef, + Field: "DriverNodeTypeId2", + }, + }, + }, + }, + } + + m := mocks.NewMockWorkspaceClient(t) + b.SetWorkpaceClient(m.WorkspaceClient) + clusterApi := m.GetMockClustersAPI() + clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef).Return(&compute.ClusterDetails{ + ClusterId: "1234-5678-abcd", + DriverNodeTypeId: "driver-node-type", + }, nil) + + err := bundle.Apply(context.Background(), b, ResolveResourceReferences()) + require.ErrorContains(t, err, "failed to resolve cluster: Some Custom Cluster, err: field DriverNodeTypeId2 does not exist") +} + +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) +} diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index 3b29783ebf..ba2d8e5240 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -5,12 +5,15 @@ package variable import ( "context" "fmt" + "reflect" "strings" "github.com/databricks/databricks-sdk-go" ) type Lookup struct { + Field string `json:"field,omitempty"` + Alert string `json:"alert,omitempty"` ClusterPolicy string `json:"cluster_policy,omitempty"` @@ -80,37 +83,81 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st r := allResolvers() if l.Alert != "" { - return r.Alert(ctx, w, l.Alert) + fieldString := "Id" + if l.Field != "" { + fieldString = l.Field + } + return r.Alert(ctx, w, l.Alert, fieldString) } if l.ClusterPolicy != "" { - return r.ClusterPolicy(ctx, w, l.ClusterPolicy) + fieldString := "PolicyId" + if l.Field != "" { + fieldString = l.Field + } + return r.ClusterPolicy(ctx, w, l.ClusterPolicy, fieldString) } if l.Cluster != "" { - return r.Cluster(ctx, w, l.Cluster) + fieldString := "ClusterId" + if l.Field != "" { + fieldString = l.Field + } + return r.Cluster(ctx, w, l.Cluster, fieldString) } if l.Dashboard != "" { - return r.Dashboard(ctx, w, l.Dashboard) + fieldString := "Id" + if l.Field != "" { + fieldString = l.Field + } + return r.Dashboard(ctx, w, l.Dashboard, fieldString) } if l.InstancePool != "" { - return r.InstancePool(ctx, w, l.InstancePool) + fieldString := "InstancePoolId" + if l.Field != "" { + fieldString = l.Field + } + return r.InstancePool(ctx, w, l.InstancePool, fieldString) } if l.Job != "" { - return r.Job(ctx, w, l.Job) + fieldString := "JobId" + if l.Field != "" { + fieldString = l.Field + } + return r.Job(ctx, w, l.Job, fieldString) } if l.Metastore != "" { - return r.Metastore(ctx, w, l.Metastore) + fieldString := "MetastoreId" + if l.Field != "" { + fieldString = l.Field + } + return r.Metastore(ctx, w, l.Metastore, fieldString) } if l.Pipeline != "" { - return r.Pipeline(ctx, w, l.Pipeline) + fieldString := "PipelineId" + if l.Field != "" { + fieldString = l.Field + } + return r.Pipeline(ctx, w, l.Pipeline, fieldString) } if l.Query != "" { - return r.Query(ctx, w, l.Query) + fieldString := "Id" + if l.Field != "" { + fieldString = l.Field + } + return r.Query(ctx, w, l.Query, fieldString) } if l.ServicePrincipal != "" { - return r.ServicePrincipal(ctx, w, l.ServicePrincipal) + fieldString := "ApplicationId" + if l.Field != "" { + fieldString = l.Field + } + return r.ServicePrincipal(ctx, w, l.ServicePrincipal, fieldString) } if l.Warehouse != "" { - return r.Warehouse(ctx, w, l.Warehouse) + fieldString := "Id" + if l.Field != "" { + fieldString = l.Field + } + return r.Warehouse(ctx, w, l.Warehouse, fieldString) } return "", fmt.Errorf("no valid lookup fields provided") @@ -202,7 +249,7 @@ func (l *Lookup) validate() error { return nil } -type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) +type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) type resolvers struct { Alert resolverFunc ClusterPolicy resolverFunc @@ -219,93 +266,148 @@ type resolvers struct { func allResolvers() *resolvers { r := &resolvers{} - r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Alerts.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.Id), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.ClusterPolicies.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.PolicyId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Clusters.GetByClusterName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.ClusterId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Dashboards.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.Id), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.InstancePools.GetByInstancePoolName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.InstancePoolId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Jobs.GetBySettingsName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.JobId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Metastores.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.MetastoreId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Pipelines.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.PipelineId), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Queries.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.Id), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.Id), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } - r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { + r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { entity, err := w.Warehouses.GetByName(ctx, name) if err != nil { return "", err } - return fmt.Sprint(entity.Id), nil + s := reflect.ValueOf(entity) + v := s.Elem().FieldByName(field) + if !v.IsValid() { + return "", fmt.Errorf("field %s does not exist", field) + } + return fmt.Sprint(v.Interface()), nil } return r From e4a51969c91bc4a61dd9911af51b0f2fd9659373 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Mon, 4 Mar 2024 16:59:32 +0100 Subject: [PATCH 2/2] only applicationId --- .codegen/lookup.go.tmpl | 19 +- .../resolve_resource_references_test.go | 55 ------ bundle/config/variable/lookup.go | 170 ++++-------------- 3 files changed, 38 insertions(+), 206 deletions(-) diff --git a/.codegen/lookup.go.tmpl b/.codegen/lookup.go.tmpl index e6c6b45d26..7e643a90c3 100644 --- a/.codegen/lookup.go.tmpl +++ b/.codegen/lookup.go.tmpl @@ -31,8 +31,6 @@ import ( ) type Lookup struct { - Field string `json:"field,omitempty"` - {{range .Services -}} {{- if in $allowlist .KebabName -}} {{.Singular.PascalName}} string `json:"{{.Singular.SnakeName}},omitempty"` @@ -62,11 +60,7 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st {{range .Services -}} {{- if in $allowlist .KebabName -}} if l.{{.Singular.PascalName}} != "" { - fieldString := "{{ getOrDefault $customField .KebabName ((index .List.NamedIdMap.IdPath 0).PascalName) }}" - if l.Field != "" { - fieldString = l.Field - } - return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}}, fieldString) + return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}}) } {{end -}} {{- end}} @@ -108,7 +102,7 @@ func (l *Lookup) validate() error { } -type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) +type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) type resolvers struct { {{range .Services -}} {{- if in $allowlist .KebabName -}} @@ -121,18 +115,13 @@ func allResolvers() *resolvers { r := &resolvers{} {{range .Services -}} {{- if in $allowlist .KebabName -}} - r.{{.Singular.PascalName}} = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + 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) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.{{ getOrDefault $customField .KebabName ((index .List.NamedIdMap.IdPath 0).PascalName) }}), nil } {{end -}} {{- end}} diff --git a/bundle/config/mutator/resolve_resource_references_test.go b/bundle/config/mutator/resolve_resource_references_test.go index 8f41634302..5f5dab3162 100644 --- a/bundle/config/mutator/resolve_resource_references_test.go +++ b/bundle/config/mutator/resolve_resource_references_test.go @@ -107,61 +107,6 @@ func TestNoLookupIfVariableIsSet(t *testing.T) { require.Equal(t, "random value", *b.Config.Variables["my-cluster-id"].Value) } -func TestResolveClusterWithCustomField(t *testing.T) { - clusterRef := "Some Custom Cluster" - b := &bundle.Bundle{ - Config: config.Root{ - Variables: map[string]*variable.Variable{ - "my-cluster-driver-node-type": { - Lookup: &variable.Lookup{ - Cluster: clusterRef, - Field: "DriverNodeTypeId", - }, - }, - }, - }, - } - - m := mocks.NewMockWorkspaceClient(t) - b.SetWorkpaceClient(m.WorkspaceClient) - clusterApi := m.GetMockClustersAPI() - clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef).Return(&compute.ClusterDetails{ - ClusterId: "1234-5678-abcd", - DriverNodeTypeId: "driver-node-type", - }, nil) - - err := bundle.Apply(context.Background(), b, ResolveResourceReferences()) - require.NoError(t, err) - require.Equal(t, "driver-node-type", *b.Config.Variables["my-cluster-driver-node-type"].Value) -} - -func TestResolveClusterWithCustomNonExistingField(t *testing.T) { - clusterRef := "Some Custom Cluster" - b := &bundle.Bundle{ - Config: config.Root{ - Variables: map[string]*variable.Variable{ - "my-cluster-driver-node-type": { - Lookup: &variable.Lookup{ - Cluster: clusterRef, - Field: "DriverNodeTypeId2", - }, - }, - }, - }, - } - - m := mocks.NewMockWorkspaceClient(t) - b.SetWorkpaceClient(m.WorkspaceClient) - clusterApi := m.GetMockClustersAPI() - clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef).Return(&compute.ClusterDetails{ - ClusterId: "1234-5678-abcd", - DriverNodeTypeId: "driver-node-type", - }, nil) - - err := bundle.Apply(context.Background(), b, ResolveResourceReferences()) - require.ErrorContains(t, err, "failed to resolve cluster: Some Custom Cluster, err: field DriverNodeTypeId2 does not exist") -} - func TestResolveServicePrincipal(t *testing.T) { spName := "Some SP name" b := &bundle.Bundle{ diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index ba2d8e5240..56d2ca810a 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -5,15 +5,12 @@ package variable import ( "context" "fmt" - "reflect" "strings" "github.com/databricks/databricks-sdk-go" ) type Lookup struct { - Field string `json:"field,omitempty"` - Alert string `json:"alert,omitempty"` ClusterPolicy string `json:"cluster_policy,omitempty"` @@ -83,81 +80,37 @@ func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (st r := allResolvers() if l.Alert != "" { - fieldString := "Id" - if l.Field != "" { - fieldString = l.Field - } - return r.Alert(ctx, w, l.Alert, fieldString) + return r.Alert(ctx, w, l.Alert) } if l.ClusterPolicy != "" { - fieldString := "PolicyId" - if l.Field != "" { - fieldString = l.Field - } - return r.ClusterPolicy(ctx, w, l.ClusterPolicy, fieldString) + return r.ClusterPolicy(ctx, w, l.ClusterPolicy) } if l.Cluster != "" { - fieldString := "ClusterId" - if l.Field != "" { - fieldString = l.Field - } - return r.Cluster(ctx, w, l.Cluster, fieldString) + return r.Cluster(ctx, w, l.Cluster) } if l.Dashboard != "" { - fieldString := "Id" - if l.Field != "" { - fieldString = l.Field - } - return r.Dashboard(ctx, w, l.Dashboard, fieldString) + return r.Dashboard(ctx, w, l.Dashboard) } if l.InstancePool != "" { - fieldString := "InstancePoolId" - if l.Field != "" { - fieldString = l.Field - } - return r.InstancePool(ctx, w, l.InstancePool, fieldString) + return r.InstancePool(ctx, w, l.InstancePool) } if l.Job != "" { - fieldString := "JobId" - if l.Field != "" { - fieldString = l.Field - } - return r.Job(ctx, w, l.Job, fieldString) + return r.Job(ctx, w, l.Job) } if l.Metastore != "" { - fieldString := "MetastoreId" - if l.Field != "" { - fieldString = l.Field - } - return r.Metastore(ctx, w, l.Metastore, fieldString) + return r.Metastore(ctx, w, l.Metastore) } if l.Pipeline != "" { - fieldString := "PipelineId" - if l.Field != "" { - fieldString = l.Field - } - return r.Pipeline(ctx, w, l.Pipeline, fieldString) + return r.Pipeline(ctx, w, l.Pipeline) } if l.Query != "" { - fieldString := "Id" - if l.Field != "" { - fieldString = l.Field - } - return r.Query(ctx, w, l.Query, fieldString) + return r.Query(ctx, w, l.Query) } if l.ServicePrincipal != "" { - fieldString := "ApplicationId" - if l.Field != "" { - fieldString = l.Field - } - return r.ServicePrincipal(ctx, w, l.ServicePrincipal, fieldString) + return r.ServicePrincipal(ctx, w, l.ServicePrincipal) } if l.Warehouse != "" { - fieldString := "Id" - if l.Field != "" { - fieldString = l.Field - } - return r.Warehouse(ctx, w, l.Warehouse, fieldString) + return r.Warehouse(ctx, w, l.Warehouse) } return "", fmt.Errorf("no valid lookup fields provided") @@ -249,7 +202,7 @@ func (l *Lookup) validate() error { return nil } -type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) +type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) type resolvers struct { Alert resolverFunc ClusterPolicy resolverFunc @@ -266,148 +219,93 @@ type resolvers struct { func allResolvers() *resolvers { r := &resolvers{} - r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Alerts.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.Id), nil } - r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.ClusterPolicies.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.PolicyId), nil } - r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Clusters.GetByClusterName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.ClusterId), nil } - r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Dashboards.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.Id), nil } - r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.InstancePools.GetByInstancePoolName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.InstancePoolId), nil } - r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Jobs.GetBySettingsName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.JobId), nil } - r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Metastores.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.MetastoreId), nil } - r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Pipelines.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.PipelineId), nil } - r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Queries.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.Id), nil } - r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.ApplicationId), nil } - r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string, field string) (string, error) { + r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { entity, err := w.Warehouses.GetByName(ctx, name) if err != nil { return "", err } - s := reflect.ValueOf(entity) - v := s.Elem().FieldByName(field) - if !v.IsValid() { - return "", fmt.Errorf("field %s does not exist", field) - } - return fmt.Sprint(v.Interface()), nil + return fmt.Sprint(entity.Id), nil } return r