From 16cd2de149a81cf98da70df8d4f6e0524533dc87 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:40:43 +0100 Subject: [PATCH] WIP --- bundle/config/variable/lookup.go | 388 ++++++------------ bundle/config/variable/lookup_alert_test.go | 48 ++- bundle/config/variable/lookup_cluster.go | 1 - .../variable/lookup_cluster_policy_test.go | 48 ++- bundle/config/variable/lookup_cluster_test.go | 49 ++- bundle/config/variable/lookup_dashboard.go | 23 +- .../config/variable/lookup_dashboard_test.go | 48 ++- .../variable/lookup_instance_pool_test.go | 48 ++- bundle/config/variable/lookup_job_test.go | 48 ++- bundle/config/variable/lookup_metastore.go | 23 +- .../config/variable/lookup_metastore_test.go | 48 ++- .../config/variable/lookup_pipeline_test.go | 48 ++- bundle/config/variable/lookup_query_test.go | 48 ++- .../variable/lookup_service_principal.go | 23 +- .../variable/lookup_service_principal_test.go | 48 ++- bundle/config/variable/lookup_warehouse.go | 23 ++ .../config/variable/lookup_warehouse_test.go | 48 ++- bundle/config/variable/variable.go | 2 +- 18 files changed, 729 insertions(+), 283 deletions(-) diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index da3856ca6..9f59103e9 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -4,13 +4,11 @@ package variable import ( "context" - "fmt" - "strings" "github.com/databricks/databricks-sdk-go" ) -type Resolver interface { +type resolver interface { Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) String() string @@ -40,123 +38,132 @@ type Lookup struct { Warehouse string `json:"warehouse,omitempty"` } -func LookupFromMap(m map[string]any) *Lookup { - l := &Lookup{} - if v, ok := m["alert"]; ok { - l.Alert = v.(string) - } - if v, ok := m["cluster_policy"]; ok { - l.ClusterPolicy = v.(string) - } - if v, ok := m["cluster"]; ok { - l.Cluster = v.(string) - } - if v, ok := m["dashboard"]; ok { - l.Dashboard = v.(string) - } - if v, ok := m["instance_pool"]; ok { - l.InstancePool = v.(string) - } - if v, ok := m["job"]; ok { - l.Job = v.(string) - } - if v, ok := m["metastore"]; ok { - l.Metastore = v.(string) - } - if v, ok := m["pipeline"]; ok { - l.Pipeline = v.(string) - } - if v, ok := m["query"]; ok { - l.Query = v.(string) - } - if v, ok := m["service_principal"]; ok { - l.ServicePrincipal = v.(string) - } - if v, ok := m["warehouse"]; ok { - l.Warehouse = v.(string) - } +func (l *Lookup) constructResolver() (resolver, error) { - return l +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 } -func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - if err := l.validate(); err != nil { - return "", err - } - - r := allResolvers() - if l.Alert != "" { - return r.Alert(ctx, w, l.Alert) - } - if l.ClusterPolicy != "" { - return r.ClusterPolicy(ctx, w, l.ClusterPolicy) - } - if l.Cluster != "" { - return r.Cluster(ctx, w, l.Cluster) - } - if l.Dashboard != "" { - return r.Dashboard(ctx, w, l.Dashboard) - } - if l.InstancePool != "" { - return r.InstancePool(ctx, w, l.InstancePool) - } - if l.Job != "" { - return r.Job(ctx, w, l.Job) - } - if l.Metastore != "" { - return r.Metastore(ctx, w, l.Metastore) - } - if l.Pipeline != "" { - return r.Pipeline(ctx, w, l.Pipeline) - } - if l.Query != "" { - return r.Query(ctx, w, l.Query) - } - if l.ServicePrincipal != "" { - return r.ServicePrincipal(ctx, w, l.ServicePrincipal) - } - if l.Warehouse != "" { - return r.Warehouse(ctx, w, l.Warehouse) - } - - return "", fmt.Errorf("no valid lookup fields provided") +return nil, fmt.Errorf("no valid lookup fields provided") } +// func LookupFromMap(m map[string]any) *Lookup { +// l := &Lookup{} +// if v, ok := m["alert"]; ok { +// l.Alert = v.(string) +// } +// if v, ok := m["cluster_policy"]; ok { +// l.ClusterPolicy = v.(string) +// } +// if v, ok := m["cluster"]; ok { +// l.Cluster = v.(string) +// } +// if v, ok := m["dashboard"]; ok { +// l.Dashboard = v.(string) +// } +// if v, ok := m["instance_pool"]; ok { +// l.InstancePool = v.(string) +// } +// if v, ok := m["job"]; ok { +// l.Job = v.(string) +// } +// if v, ok := m["metastore"]; ok { +// l.Metastore = v.(string) +// } +// if v, ok := m["pipeline"]; ok { +// l.Pipeline = v.(string) +// } +// if v, ok := m["query"]; ok { +// l.Query = v.(string) +// } +// if v, ok := m["service_principal"]; ok { +// l.ServicePrincipal = v.(string) +// } +// if v, ok := m["warehouse"]; ok { +// l.Warehouse = v.(string) +// } + +// return l +// } + +// func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +// if err := l.validate(); err != nil { +// return "", err +// } + +// r := allResolvers() +// if l.Alert != "" { +// return r.Alert(ctx, w, l.Alert) +// } +// if l.ClusterPolicy != "" { +// return r.ClusterPolicy(ctx, w, l.ClusterPolicy) +// } +// if l.Cluster != "" { +// return r.Cluster(ctx, w, l.Cluster) +// } +// if l.Dashboard != "" { +// return r.Dashboard(ctx, w, l.Dashboard) +// } +// if l.InstancePool != "" { +// return r.InstancePool(ctx, w, l.InstancePool) +// } +// if l.Job != "" { +// return r.Job(ctx, w, l.Job) +// } +// if l.Metastore != "" { +// return r.Metastore(ctx, w, l.Metastore) +// } +// if l.Pipeline != "" { +// return r.Pipeline(ctx, w, l.Pipeline) +// } +// if l.Query != "" { +// return r.Query(ctx, w, l.Query) +// } +// if l.ServicePrincipal != "" { +// return r.ServicePrincipal(ctx, w, l.ServicePrincipal) +// } +// if l.Warehouse != "" { +// return r.Warehouse(ctx, w, l.Warehouse) +// } + +// return "", fmt.Errorf("no valid lookup fields provided") +// } + func (l *Lookup) String() string { - if l.Alert != "" { - return fmt.Sprintf("alert: %s", l.Alert) - } - if l.ClusterPolicy != "" { - return fmt.Sprintf("cluster-policy: %s", l.ClusterPolicy) - } - if l.Cluster != "" { - return fmt.Sprintf("cluster: %s", l.Cluster) - } - if l.Dashboard != "" { - return fmt.Sprintf("dashboard: %s", l.Dashboard) - } - if l.InstancePool != "" { - return fmt.Sprintf("instance-pool: %s", l.InstancePool) - } - if l.Job != "" { - return fmt.Sprintf("job: %s", l.Job) - } - if l.Metastore != "" { - return fmt.Sprintf("metastore: %s", l.Metastore) - } - if l.Pipeline != "" { - return fmt.Sprintf("pipeline: %s", l.Pipeline) - } - if l.Query != "" { - return fmt.Sprintf("query: %s", l.Query) - } - if l.ServicePrincipal != "" { - return fmt.Sprintf("service-principal: %s", l.ServicePrincipal) - } - if l.Warehouse != "" { - return fmt.Sprintf("warehouse: %s", l.Warehouse) - } - +r, _ := l.constructResolver() +if r != nil { + return r.String() +} return "" } @@ -207,156 +214,3 @@ func (l *Lookup) validate() error { return nil } - -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 allResolvers() *resolvers { - r := &resolvers{} - r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Alert"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Alerts.GetByDisplayName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.Id), nil - } - r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["ClusterPolicy"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.ClusterPolicies.GetByName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.PolicyId), nil - } - r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Cluster"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Clusters.GetByClusterName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.ClusterId), nil - } - r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Dashboard"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Dashboards.GetByName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.Id), nil - } - r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["InstancePool"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.InstancePools.GetByInstancePoolName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.InstancePoolId), nil - } - r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Job"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Jobs.GetBySettingsName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.JobId), nil - } - r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Metastore"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Metastores.GetByName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.MetastoreId), nil - } - r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Pipeline"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Pipelines.GetByName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.PipelineId), nil - } - r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Query"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Queries.GetByDisplayName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.Id), nil - } - r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["ServicePrincipal"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.ApplicationId), nil - } - r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) { - fn, ok := lookupOverrides["Warehouse"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.Warehouses.GetByName(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.Id), nil - } - - return r -} diff --git a/bundle/config/variable/lookup_alert_test.go b/bundle/config/variable/lookup_alert_test.go index 212c7e993..14a387824 100644 --- a/bundle/config/variable/lookup_alert_test.go +++ b/bundle/config/variable/lookup_alert_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for alert +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/sql" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupAlert_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockAlertsAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "alert"). + Return(&sql.ListAlertsResponseAlert{ + Id: "1234", + }, nil) + + ctx := context.Background() + l := &lookupAlert{name: "alert"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "1234", result) +} + +func TestLookupAlert_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockAlertsAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "alert"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "alert: name", l.String()) +} diff --git a/bundle/config/variable/lookup_cluster.go b/bundle/config/variable/lookup_cluster.go index f7778523f..4081491ee 100644 --- a/bundle/config/variable/lookup_cluster.go +++ b/bundle/config/variable/lookup_cluster.go @@ -40,7 +40,6 @@ func (l *lookupCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClie return "", fmt.Errorf("there are %d instances of clusters named '%s'", len(alternatives), name) } return alternatives[0].ClusterId, nil - } func (l *lookupCluster) String() string { diff --git a/bundle/config/variable/lookup_cluster_policy_test.go b/bundle/config/variable/lookup_cluster_policy_test.go index d922995a5..379fcff01 100644 --- a/bundle/config/variable/lookup_cluster_policy_test.go +++ b/bundle/config/variable/lookup_cluster_policy_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for cluster_policy +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupClusterPolicy_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockClusterPoliciesAPI() + api.EXPECT(). + GetByName(mock.Anything, "policy"). + Return(&compute.Policy{ + PolicyId: "1234", + }, nil) + + ctx := context.Background() + l := &lookupClusterPolicy{name: "policy"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "1234", result) +} + +func TestLookupClusterPolicy_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockClusterPoliciesAPI() + api.EXPECT(). + GetByName(mock.Anything, "policy"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "cluster-policy: name", l.String()) +} diff --git a/bundle/config/variable/lookup_cluster_test.go b/bundle/config/variable/lookup_cluster_test.go index 16093b0d0..5629eacc3 100644 --- a/bundle/config/variable/lookup_cluster_test.go +++ b/bundle/config/variable/lookup_cluster_test.go @@ -1,3 +1,50 @@ package variable -// TODO: Add tests for cluster +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupCluster_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockClustersAPI() + api.EXPECT(). + ListAll(mock.Anything, mock.Anything). + Return([]compute.ClusterDetails{ + {ClusterId: "1234", ClusterName: "cluster1"}, + {ClusterId: "2345", ClusterName: "cluster2"}, + }, nil) + + ctx := context.Background() + l := &lookupCluster{name: "cluster2"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "2345", result) +} + +func TestLookupCluster_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockClustersAPI() + api.EXPECT(). + ListAll(mock.Anything, mock.Anything). + Return([]compute.ClusterDetails{}, nil) + + ctx := context.Background() + 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"} + assert.Equal(t, "cluster: name", l.String()) +} diff --git a/bundle/config/variable/lookup_dashboard.go b/bundle/config/variable/lookup_dashboard.go index 7a4d97fb9..c43e4cc03 100644 --- a/bundle/config/variable/lookup_dashboard.go +++ b/bundle/config/variable/lookup_dashboard.go @@ -1,3 +1,24 @@ package variable -// TODO: Add implementation for dashboard +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupDashboard struct { + name string +} + +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 + } + return fmt.Sprint(entity.Id), nil +} + +func (l *lookupDashboard) String() string { + return fmt.Sprintf("dashboard: %s", l.name) +} diff --git a/bundle/config/variable/lookup_dashboard_test.go b/bundle/config/variable/lookup_dashboard_test.go index 98662c288..1d9cc2b8a 100644 --- a/bundle/config/variable/lookup_dashboard_test.go +++ b/bundle/config/variable/lookup_dashboard_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for dashboard +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/sql" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupDashboard_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockDashboardsAPI() + api.EXPECT(). + GetByName(mock.Anything, "dashboard"). + Return(&sql.Dashboard{ + Id: "1234", + }, nil) + + ctx := context.Background() + l := &lookupDashboard{name: "dashboard"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "1234", result) +} + +func TestLookupDashboard_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockDashboardsAPI() + api.EXPECT(). + GetByName(mock.Anything, "dashboard"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "dashboard: name", l.String()) +} diff --git a/bundle/config/variable/lookup_instance_pool_test.go b/bundle/config/variable/lookup_instance_pool_test.go index 20b376098..cafb39323 100644 --- a/bundle/config/variable/lookup_instance_pool_test.go +++ b/bundle/config/variable/lookup_instance_pool_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for instance_pool +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupInstancePool_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockInstancePoolsAPI() + api.EXPECT(). + GetByInstancePoolName(mock.Anything, "instance_pool"). + Return(&compute.InstancePoolAndStats{ + InstancePoolId: "5678", + }, nil) + + ctx := context.Background() + l := &lookupInstancePool{name: "instance_pool"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "5678", result) +} + +func TestLookupInstancePool_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockInstancePoolsAPI() + api.EXPECT(). + GetByInstancePoolName(mock.Anything, "instance_pool"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "instance-pool: name", l.String()) +} diff --git a/bundle/config/variable/lookup_job_test.go b/bundle/config/variable/lookup_job_test.go index b2db23a8e..442b4f214 100644 --- a/bundle/config/variable/lookup_job_test.go +++ b/bundle/config/variable/lookup_job_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for job +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/jobs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupJob_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockJobsAPI() + api.EXPECT(). + GetBySettingsName(mock.Anything, "job"). + Return(&jobs.BaseJob{ + JobId: 5678, + }, nil) + + ctx := context.Background() + l := &lookupJob{name: "job"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "5678", result) +} + +func TestLookupJob_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockJobsAPI() + api.EXPECT(). + GetBySettingsName(mock.Anything, "job"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "job: name", l.String()) +} diff --git a/bundle/config/variable/lookup_metastore.go b/bundle/config/variable/lookup_metastore.go index 8b038bbc0..3be6368d6 100644 --- a/bundle/config/variable/lookup_metastore.go +++ b/bundle/config/variable/lookup_metastore.go @@ -1,3 +1,24 @@ package variable -// TODO: Add implementation for metastore +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupMetastore struct { + name string +} + +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 + } + return fmt.Sprint(entity.MetastoreId), nil +} + +func (l *lookupMetastore) String() string { + return fmt.Sprintf("metastore: %s", l.name) +} diff --git a/bundle/config/variable/lookup_metastore_test.go b/bundle/config/variable/lookup_metastore_test.go index e73a4d610..b25a647be 100644 --- a/bundle/config/variable/lookup_metastore_test.go +++ b/bundle/config/variable/lookup_metastore_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for metastore +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupMetastore_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockMetastoresAPI() + api.EXPECT(). + GetByName(mock.Anything, "metastore"). + Return(&catalog.MetastoreInfo{ + MetastoreId: "abcd", + }, nil) + + ctx := context.Background() + l := &lookupMetastore{name: "metastore"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "abcd", result) +} + +func TestLookupMetastore_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockMetastoresAPI() + api.EXPECT(). + GetByName(mock.Anything, "metastore"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "metastore: name", l.String()) +} diff --git a/bundle/config/variable/lookup_pipeline_test.go b/bundle/config/variable/lookup_pipeline_test.go index 21ce6224d..0c35b1862 100644 --- a/bundle/config/variable/lookup_pipeline_test.go +++ b/bundle/config/variable/lookup_pipeline_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for pipeline +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/pipelines" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupPipeline_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockPipelinesAPI() + api.EXPECT(). + GetByName(mock.Anything, "pipeline"). + Return(&pipelines.PipelineStateInfo{ + PipelineId: "abcd", + }, nil) + + ctx := context.Background() + l := &lookupPipeline{name: "pipeline"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "abcd", result) +} + +func TestLookupPipeline_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockPipelinesAPI() + api.EXPECT(). + GetByName(mock.Anything, "pipeline"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "pipeline: name", l.String()) +} diff --git a/bundle/config/variable/lookup_query_test.go b/bundle/config/variable/lookup_query_test.go index 58a153314..7eacd9752 100644 --- a/bundle/config/variable/lookup_query_test.go +++ b/bundle/config/variable/lookup_query_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for query +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/sql" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupQuery_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockQueriesAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "query"). + Return(&sql.ListQueryObjectsResponseQuery{ + Id: "1234", + }, nil) + + ctx := context.Background() + l := &lookupQuery{name: "query"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "1234", result) +} + +func TestLookupQuery_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockQueriesAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "query"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "query: name", l.String()) +} diff --git a/bundle/config/variable/lookup_service_principal.go b/bundle/config/variable/lookup_service_principal.go index 11705ef07..c9d4d3768 100644 --- a/bundle/config/variable/lookup_service_principal.go +++ b/bundle/config/variable/lookup_service_principal.go @@ -1,3 +1,24 @@ package variable -// TODO: Add implementation for service_principal +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupServicePrincipal struct { + name string +} + +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 + } + return fmt.Sprint(entity.ApplicationId), nil +} + +func (l *lookupServicePrincipal) String() string { + return fmt.Sprintf("service-principal: %s", l.name) +} diff --git a/bundle/config/variable/lookup_service_principal_test.go b/bundle/config/variable/lookup_service_principal_test.go index 9dd42b188..6e8b34a44 100644 --- a/bundle/config/variable/lookup_service_principal_test.go +++ b/bundle/config/variable/lookup_service_principal_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for service_principal +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupServicePrincipal_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockServicePrincipalsAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "service-principal"). + Return(&iam.ServicePrincipal{ + ApplicationId: "5678", + }, nil) + + ctx := context.Background() + l := &lookupServicePrincipal{name: "service-principal"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "5678", result) +} + +func TestLookupServicePrincipal_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockServicePrincipalsAPI() + api.EXPECT(). + GetByDisplayName(mock.Anything, "service-principal"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "service-principal: name", l.String()) +} diff --git a/bundle/config/variable/lookup_warehouse.go b/bundle/config/variable/lookup_warehouse.go index abd4345f8..90199ec61 100644 --- a/bundle/config/variable/lookup_warehouse.go +++ b/bundle/config/variable/lookup_warehouse.go @@ -1 +1,24 @@ package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupWarehouse struct { + name string +} + +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 + } + return fmt.Sprint(entity.Id), nil +} + +func (l *lookupWarehouse) String() string { + return fmt.Sprintf("warehouse: %s", l.name) +} diff --git a/bundle/config/variable/lookup_warehouse_test.go b/bundle/config/variable/lookup_warehouse_test.go index 6aad3e9ad..20a535cc9 100644 --- a/bundle/config/variable/lookup_warehouse_test.go +++ b/bundle/config/variable/lookup_warehouse_test.go @@ -1,3 +1,49 @@ package variable -// TODO: Add tests for warehouse +import ( + "context" + "testing" + + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/sql" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestLookupWarehouse_ResolveSuccess(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockWarehousesAPI() + api.EXPECT(). + GetByName(mock.Anything, "warehouse"). + Return(&sql.EndpointInfo{ + Id: "abcd", + }, nil) + + ctx := context.Background() + l := &lookupWarehouse{name: "warehouse"} + result, err := l.Resolve(ctx, m.WorkspaceClient) + require.NoError(t, err) + assert.Equal(t, "abcd", result) +} + +func TestLookupWarehouse_ResolveNotFound(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + + api := m.GetMockWarehousesAPI() + api.EXPECT(). + GetByName(mock.Anything, "warehouse"). + Return(nil, &apierr.APIError{StatusCode: 404}) + + ctx := context.Background() + 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"} + assert.Equal(t, "warehouse: name", l.String()) +} diff --git a/bundle/config/variable/variable.go b/bundle/config/variable/variable.go index 2362ad10d..89abc4cf7 100644 --- a/bundle/config/variable/variable.go +++ b/bundle/config/variable/variable.go @@ -45,7 +45,7 @@ type Variable struct { // The value of this field will be used to lookup the resource by name // And assign the value of the variable to ID of the resource found. - Lookup *Lookup `json:"lookup,omitempty"` + // Lookup *Lookup `json:"lookup,omitempty"` } // True if the variable has been assigned a default value. Variables without a