No need for pointers to the lookup structs

This commit is contained in:
Pieter Noordhuis 2024-11-20 22:46:07 +01:00
parent 16cd2de149
commit 7a99df58ec
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
23 changed files with 95 additions and 93 deletions

View File

@ -4,6 +4,8 @@ package variable
import (
"context"
"fmt"
"strings"
"github.com/databricks/databricks-sdk-go"
)
@ -40,41 +42,41 @@ type Lookup struct {
func (l *Lookup) constructResolver() (resolver, error) {
if l.Alert != "" {
return lookupAlert{name: l.Alert}, nil
}
if l.ClusterPolicy != "" {
return lookupClusterPolicy{name: l.ClusterPolicy}, nil
}
if l.Cluster != "" {
return lookupCluster{name: l.Cluster}, nil
}
if l.Dashboard != "" {
return lookupDashboard{name: l.Dashboard}, nil
}
if l.InstancePool != "" {
return lookupInstancePool{name: l.InstancePool}, nil
}
if l.Job != "" {
return lookupJob{name: l.Job}, nil
}
if l.Metastore != "" {
return lookupMetastore{name: l.Metastore}, nil
}
if l.Pipeline != "" {
return lookupPipeline{name: l.Pipeline}, nil
}
if l.Query != "" {
return lookupQuery{name: l.Query}, nil
}
if l.ServicePrincipal != "" {
return lookupServicePrincipal{name: l.ServicePrincipal}, nil
}
if l.Warehouse != "" {
return lookupWarehouse{name: l.Warehouse}, nil
}
if l.Alert != "" {
return lookupAlert{name: l.Alert}, nil
}
if l.ClusterPolicy != "" {
return lookupClusterPolicy{name: l.ClusterPolicy}, nil
}
if l.Cluster != "" {
return lookupCluster{name: l.Cluster}, nil
}
if l.Dashboard != "" {
return lookupDashboard{name: l.Dashboard}, nil
}
if l.InstancePool != "" {
return lookupInstancePool{name: l.InstancePool}, nil
}
if l.Job != "" {
return lookupJob{name: l.Job}, nil
}
if l.Metastore != "" {
return lookupMetastore{name: l.Metastore}, nil
}
if l.Pipeline != "" {
return lookupPipeline{name: l.Pipeline}, nil
}
if l.Query != "" {
return lookupQuery{name: l.Query}, nil
}
if l.ServicePrincipal != "" {
return lookupServicePrincipal{name: l.ServicePrincipal}, nil
}
if l.Warehouse != "" {
return lookupWarehouse{name: l.Warehouse}, nil
}
return nil, fmt.Errorf("no valid lookup fields provided")
return nil, fmt.Errorf("no valid lookup fields provided")
}
// func LookupFromMap(m map[string]any) *Lookup {
@ -160,10 +162,10 @@ return nil, fmt.Errorf("no valid lookup fields provided")
// }
func (l *Lookup) String() string {
r, _ := l.constructResolver()
if r != nil {
return r.String()
}
r, _ := l.constructResolver()
if r != nil {
return r.String()
}
return ""
}

View File

@ -11,7 +11,7 @@ type lookupAlert struct {
name string
}
func (l *lookupAlert) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupAlert) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Alerts.GetByDisplayName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupAlert) Resolve(ctx context.Context, w *databricks.WorkspaceClient
return fmt.Sprint(entity.Id), nil
}
func (l *lookupAlert) String() string {
func (l lookupAlert) String() string {
return fmt.Sprintf("alert: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupAlert_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupAlert{name: "alert"}
l := lookupAlert{name: "alert"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "1234", result)
@ -38,12 +38,12 @@ func TestLookupAlert_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupAlert{name: "alert"}
l := lookupAlert{name: "alert"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupAlert_String(t *testing.T) {
l := &lookupAlert{name: "name"}
l := lookupAlert{name: "name"}
assert.Equal(t, "alert: name", l.String())
}

View File

@ -14,7 +14,7 @@ type lookupCluster struct {
// We added a custom resolver for the cluster to add filtering for the cluster source when we list all clusters.
// Without the filtering listing could take a very long time (5-10 mins) which leads to lookup timeouts.
func (l *lookupCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
result, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{
FilterBy: &compute.ListClustersFilterBy{
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
@ -42,6 +42,6 @@ func (l *lookupCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClie
return alternatives[0].ClusterId, nil
}
func (l *lookupCluster) String() string {
func (l lookupCluster) String() string {
return fmt.Sprintf("cluster: %s", l.name)
}

View File

@ -11,7 +11,7 @@ type lookupClusterPolicy struct {
name string
}
func (l *lookupClusterPolicy) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupClusterPolicy) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.ClusterPolicies.GetByName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupClusterPolicy) Resolve(ctx context.Context, w *databricks.Workspa
return fmt.Sprint(entity.PolicyId), nil
}
func (l *lookupClusterPolicy) String() string {
func (l lookupClusterPolicy) String() string {
return fmt.Sprintf("cluster-policy: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupClusterPolicy_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupClusterPolicy{name: "policy"}
l := lookupClusterPolicy{name: "policy"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "1234", result)
@ -38,12 +38,12 @@ func TestLookupClusterPolicy_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupClusterPolicy{name: "policy"}
l := lookupClusterPolicy{name: "policy"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupClusterPolicy_String(t *testing.T) {
l := &lookupClusterPolicy{name: "name"}
l := lookupClusterPolicy{name: "name"}
assert.Equal(t, "cluster-policy: name", l.String())
}

View File

@ -23,7 +23,7 @@ func TestLookupCluster_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupCluster{name: "cluster2"}
l := lookupCluster{name: "cluster2"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "2345", result)
@ -38,13 +38,13 @@ func TestLookupCluster_ResolveNotFound(t *testing.T) {
Return([]compute.ClusterDetails{}, nil)
ctx := context.Background()
l := &lookupCluster{name: "cluster"}
l := lookupCluster{name: "cluster"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.Error(t, err)
assert.Contains(t, err.Error(), "cluster named 'cluster' does not exist")
}
func TestLookupCluster_String(t *testing.T) {
l := &lookupCluster{name: "name"}
l := lookupCluster{name: "name"}
assert.Equal(t, "cluster: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupDashboard struct {
name string
}
func (l *lookupDashboard) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupDashboard) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Dashboards.GetByName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupDashboard) Resolve(ctx context.Context, w *databricks.WorkspaceCl
return fmt.Sprint(entity.Id), nil
}
func (l *lookupDashboard) String() string {
func (l lookupDashboard) String() string {
return fmt.Sprintf("dashboard: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupDashboard_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupDashboard{name: "dashboard"}
l := lookupDashboard{name: "dashboard"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "1234", result)
@ -38,12 +38,12 @@ func TestLookupDashboard_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupDashboard{name: "dashboard"}
l := lookupDashboard{name: "dashboard"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupDashboard_String(t *testing.T) {
l := &lookupDashboard{name: "name"}
l := lookupDashboard{name: "name"}
assert.Equal(t, "dashboard: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupInstancePool struct {
name string
}
func (l *lookupInstancePool) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupInstancePool) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.InstancePools.GetByInstancePoolName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupInstancePool) Resolve(ctx context.Context, w *databricks.Workspac
return fmt.Sprint(entity.InstancePoolId), nil
}
func (l *lookupInstancePool) String() string {
func (l lookupInstancePool) String() string {
return fmt.Sprintf("instance-pool: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupInstancePool_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupInstancePool{name: "instance_pool"}
l := lookupInstancePool{name: "instance_pool"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "5678", result)
@ -38,12 +38,12 @@ func TestLookupInstancePool_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupInstancePool{name: "instance_pool"}
l := lookupInstancePool{name: "instance_pool"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupInstancePool_String(t *testing.T) {
l := &lookupInstancePool{name: "name"}
l := lookupInstancePool{name: "name"}
assert.Equal(t, "instance-pool: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupJob struct {
name string
}
func (l *lookupJob) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupJob) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Jobs.GetBySettingsName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupJob) Resolve(ctx context.Context, w *databricks.WorkspaceClient)
return fmt.Sprint(entity.JobId), nil
}
func (l *lookupJob) String() string {
func (l lookupJob) String() string {
return fmt.Sprintf("job: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupJob_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupJob{name: "job"}
l := lookupJob{name: "job"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "5678", result)
@ -38,12 +38,12 @@ func TestLookupJob_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupJob{name: "job"}
l := lookupJob{name: "job"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupJob_String(t *testing.T) {
l := &lookupJob{name: "name"}
l := lookupJob{name: "name"}
assert.Equal(t, "job: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupMetastore struct {
name string
}
func (l *lookupMetastore) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupMetastore) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Metastores.GetByName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupMetastore) Resolve(ctx context.Context, w *databricks.WorkspaceCl
return fmt.Sprint(entity.MetastoreId), nil
}
func (l *lookupMetastore) String() string {
func (l lookupMetastore) String() string {
return fmt.Sprintf("metastore: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupMetastore_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupMetastore{name: "metastore"}
l := lookupMetastore{name: "metastore"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "abcd", result)
@ -38,12 +38,12 @@ func TestLookupMetastore_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupMetastore{name: "metastore"}
l := lookupMetastore{name: "metastore"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupMetastore_String(t *testing.T) {
l := &lookupMetastore{name: "name"}
l := lookupMetastore{name: "name"}
assert.Equal(t, "metastore: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupPipeline struct {
name string
}
func (l *lookupPipeline) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupPipeline) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Pipelines.GetByName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupPipeline) Resolve(ctx context.Context, w *databricks.WorkspaceCli
return fmt.Sprint(entity.PipelineId), nil
}
func (l *lookupPipeline) String() string {
func (l lookupPipeline) String() string {
return fmt.Sprintf("pipeline: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupPipeline_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupPipeline{name: "pipeline"}
l := lookupPipeline{name: "pipeline"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "abcd", result)
@ -38,12 +38,12 @@ func TestLookupPipeline_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupPipeline{name: "pipeline"}
l := lookupPipeline{name: "pipeline"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupPipeline_String(t *testing.T) {
l := &lookupPipeline{name: "name"}
l := lookupPipeline{name: "name"}
assert.Equal(t, "pipeline: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupQuery struct {
name string
}
func (l *lookupQuery) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupQuery) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Queries.GetByDisplayName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupQuery) Resolve(ctx context.Context, w *databricks.WorkspaceClient
return fmt.Sprint(entity.Id), nil
}
func (l *lookupQuery) String() string {
func (l lookupQuery) String() string {
return fmt.Sprintf("query: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupQuery_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupQuery{name: "query"}
l := lookupQuery{name: "query"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "1234", result)
@ -38,12 +38,12 @@ func TestLookupQuery_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupQuery{name: "query"}
l := lookupQuery{name: "query"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupQuery_String(t *testing.T) {
l := &lookupQuery{name: "name"}
l := lookupQuery{name: "name"}
assert.Equal(t, "query: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupServicePrincipal struct {
name string
}
func (l *lookupServicePrincipal) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupServicePrincipal) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.ServicePrincipals.GetByDisplayName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupServicePrincipal) Resolve(ctx context.Context, w *databricks.Work
return fmt.Sprint(entity.ApplicationId), nil
}
func (l *lookupServicePrincipal) String() string {
func (l lookupServicePrincipal) String() string {
return fmt.Sprintf("service-principal: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupServicePrincipal_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupServicePrincipal{name: "service-principal"}
l := lookupServicePrincipal{name: "service-principal"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "5678", result)
@ -38,12 +38,12 @@ func TestLookupServicePrincipal_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupServicePrincipal{name: "service-principal"}
l := lookupServicePrincipal{name: "service-principal"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupServicePrincipal_String(t *testing.T) {
l := &lookupServicePrincipal{name: "name"}
l := lookupServicePrincipal{name: "name"}
assert.Equal(t, "service-principal: name", l.String())
}

View File

@ -11,7 +11,7 @@ type lookupWarehouse struct {
name string
}
func (l *lookupWarehouse) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
func (l lookupWarehouse) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
entity, err := w.Warehouses.GetByName(ctx, l.name)
if err != nil {
return "", err
@ -19,6 +19,6 @@ func (l *lookupWarehouse) Resolve(ctx context.Context, w *databricks.WorkspaceCl
return fmt.Sprint(entity.Id), nil
}
func (l *lookupWarehouse) String() string {
func (l lookupWarehouse) String() string {
return fmt.Sprintf("warehouse: %s", l.name)
}

View File

@ -23,7 +23,7 @@ func TestLookupWarehouse_ResolveSuccess(t *testing.T) {
}, nil)
ctx := context.Background()
l := &lookupWarehouse{name: "warehouse"}
l := lookupWarehouse{name: "warehouse"}
result, err := l.Resolve(ctx, m.WorkspaceClient)
require.NoError(t, err)
assert.Equal(t, "abcd", result)
@ -38,12 +38,12 @@ func TestLookupWarehouse_ResolveNotFound(t *testing.T) {
Return(nil, &apierr.APIError{StatusCode: 404})
ctx := context.Background()
l := &lookupWarehouse{name: "warehouse"}
l := lookupWarehouse{name: "warehouse"}
_, err := l.Resolve(ctx, m.WorkspaceClient)
require.ErrorIs(t, err, apierr.ErrNotFound)
}
func TestLookupWarehouse_String(t *testing.T) {
l := &lookupWarehouse{name: "name"}
l := lookupWarehouse{name: "name"}
assert.Equal(t, "warehouse: name", l.String())
}