From 1f8f85982dc889ce7a36e996735ccec99e97bb26 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:40:33 +0100 Subject: [PATCH 1/7] WIP --- bundle/config/variable/lookup.go | 6 +++++ bundle/config/variable/lookup_alert.go | 24 +++++++++++++++++++ bundle/config/variable/lookup_alert_test.go | 3 +++ ...{lookup_overrides.go => lookup_cluster.go} | 13 +++++++--- .../config/variable/lookup_cluster_policy.go | 24 +++++++++++++++++++ .../variable/lookup_cluster_policy_test.go | 3 +++ bundle/config/variable/lookup_cluster_test.go | 3 +++ bundle/config/variable/lookup_dashboard.go | 3 +++ .../config/variable/lookup_dashboard_test.go | 3 +++ .../config/variable/lookup_instance_pool.go | 24 +++++++++++++++++++ .../variable/lookup_instance_pool_test.go | 3 +++ bundle/config/variable/lookup_job.go | 24 +++++++++++++++++++ bundle/config/variable/lookup_job_test.go | 3 +++ bundle/config/variable/lookup_metastore.go | 3 +++ .../config/variable/lookup_metastore_test.go | 3 +++ bundle/config/variable/lookup_pipeline.go | 24 +++++++++++++++++++ .../config/variable/lookup_pipeline_test.go | 3 +++ bundle/config/variable/lookup_query.go | 24 +++++++++++++++++++ bundle/config/variable/lookup_query_test.go | 3 +++ .../variable/lookup_service_principal.go | 3 +++ .../variable/lookup_service_principal_test.go | 3 +++ bundle/config/variable/lookup_warehouse.go | 1 + .../config/variable/lookup_warehouse_test.go | 3 +++ 23 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 bundle/config/variable/lookup_alert.go create mode 100644 bundle/config/variable/lookup_alert_test.go rename bundle/config/variable/{lookup_overrides.go => lookup_cluster.go} (81%) create mode 100644 bundle/config/variable/lookup_cluster_policy.go create mode 100644 bundle/config/variable/lookup_cluster_policy_test.go create mode 100644 bundle/config/variable/lookup_cluster_test.go create mode 100644 bundle/config/variable/lookup_dashboard.go create mode 100644 bundle/config/variable/lookup_dashboard_test.go create mode 100644 bundle/config/variable/lookup_instance_pool.go create mode 100644 bundle/config/variable/lookup_instance_pool_test.go create mode 100644 bundle/config/variable/lookup_job.go create mode 100644 bundle/config/variable/lookup_job_test.go create mode 100644 bundle/config/variable/lookup_metastore.go create mode 100644 bundle/config/variable/lookup_metastore_test.go create mode 100644 bundle/config/variable/lookup_pipeline.go create mode 100644 bundle/config/variable/lookup_pipeline_test.go create mode 100644 bundle/config/variable/lookup_query.go create mode 100644 bundle/config/variable/lookup_query_test.go create mode 100644 bundle/config/variable/lookup_service_principal.go create mode 100644 bundle/config/variable/lookup_service_principal_test.go create mode 100644 bundle/config/variable/lookup_warehouse.go create mode 100644 bundle/config/variable/lookup_warehouse_test.go diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index e40b0ef7a9..da3856ca6a 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -10,6 +10,12 @@ import ( "github.com/databricks/databricks-sdk-go" ) +type Resolver interface { + Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) + + String() string +} + type Lookup struct { Alert string `json:"alert,omitempty"` diff --git a/bundle/config/variable/lookup_alert.go b/bundle/config/variable/lookup_alert.go new file mode 100644 index 0000000000..768e3b7369 --- /dev/null +++ b/bundle/config/variable/lookup_alert.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupAlert struct { + name string +} + +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 + } + return fmt.Sprint(entity.Id), nil +} + +func (l *lookupAlert) String() string { + return fmt.Sprintf("alert: %s", l.name) +} diff --git a/bundle/config/variable/lookup_alert_test.go b/bundle/config/variable/lookup_alert_test.go new file mode 100644 index 0000000000..212c7e993b --- /dev/null +++ b/bundle/config/variable/lookup_alert_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for alert diff --git a/bundle/config/variable/lookup_overrides.go b/bundle/config/variable/lookup_cluster.go similarity index 81% rename from bundle/config/variable/lookup_overrides.go rename to bundle/config/variable/lookup_cluster.go index 1be373dc68..f7778523f4 100644 --- a/bundle/config/variable/lookup_overrides.go +++ b/bundle/config/variable/lookup_cluster.go @@ -8,13 +8,13 @@ import ( "github.com/databricks/databricks-sdk-go/service/compute" ) -var lookupOverrides = map[string]resolverFunc{ - "Cluster": resolveCluster, +type lookupCluster struct { + name string } // 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 resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name string) (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}, @@ -30,6 +30,8 @@ func resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name str key := v.ClusterName tmp[key] = append(tmp[key], v) } + + name := l.name alternatives, ok := tmp[name] if !ok || len(alternatives) == 0 { return "", fmt.Errorf("cluster named '%s' does not exist", name) @@ -38,4 +40,9 @@ func resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name str return "", fmt.Errorf("there are %d instances of clusters named '%s'", len(alternatives), name) } return alternatives[0].ClusterId, nil + +} + +func (l *lookupCluster) String() string { + return fmt.Sprintf("cluster: %s", l.name) } diff --git a/bundle/config/variable/lookup_cluster_policy.go b/bundle/config/variable/lookup_cluster_policy.go new file mode 100644 index 0000000000..54eefd6966 --- /dev/null +++ b/bundle/config/variable/lookup_cluster_policy.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupClusterPolicy struct { + name string +} + +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 + } + return fmt.Sprint(entity.PolicyId), nil +} + +func (l *lookupClusterPolicy) String() string { + return fmt.Sprintf("cluster-policy: %s", l.name) +} diff --git a/bundle/config/variable/lookup_cluster_policy_test.go b/bundle/config/variable/lookup_cluster_policy_test.go new file mode 100644 index 0000000000..d922995a5b --- /dev/null +++ b/bundle/config/variable/lookup_cluster_policy_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for cluster_policy diff --git a/bundle/config/variable/lookup_cluster_test.go b/bundle/config/variable/lookup_cluster_test.go new file mode 100644 index 0000000000..16093b0d05 --- /dev/null +++ b/bundle/config/variable/lookup_cluster_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for cluster diff --git a/bundle/config/variable/lookup_dashboard.go b/bundle/config/variable/lookup_dashboard.go new file mode 100644 index 0000000000..7a4d97fb9a --- /dev/null +++ b/bundle/config/variable/lookup_dashboard.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add implementation for dashboard diff --git a/bundle/config/variable/lookup_dashboard_test.go b/bundle/config/variable/lookup_dashboard_test.go new file mode 100644 index 0000000000..98662c288f --- /dev/null +++ b/bundle/config/variable/lookup_dashboard_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for dashboard diff --git a/bundle/config/variable/lookup_instance_pool.go b/bundle/config/variable/lookup_instance_pool.go new file mode 100644 index 0000000000..f567d34518 --- /dev/null +++ b/bundle/config/variable/lookup_instance_pool.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupInstancePool struct { + name string +} + +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 + } + return fmt.Sprint(entity.InstancePoolId), nil +} + +func (l *lookupInstancePool) String() string { + return fmt.Sprintf("instance-pool: %s", l.name) +} diff --git a/bundle/config/variable/lookup_instance_pool_test.go b/bundle/config/variable/lookup_instance_pool_test.go new file mode 100644 index 0000000000..20b3760983 --- /dev/null +++ b/bundle/config/variable/lookup_instance_pool_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for instance_pool diff --git a/bundle/config/variable/lookup_job.go b/bundle/config/variable/lookup_job.go new file mode 100644 index 0000000000..dbf1e9da99 --- /dev/null +++ b/bundle/config/variable/lookup_job.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupJob struct { + name string +} + +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 + } + return fmt.Sprint(entity.JobId), nil +} + +func (l *lookupJob) String() string { + return fmt.Sprintf("job: %s", l.name) +} diff --git a/bundle/config/variable/lookup_job_test.go b/bundle/config/variable/lookup_job_test.go new file mode 100644 index 0000000000..b2db23a8e8 --- /dev/null +++ b/bundle/config/variable/lookup_job_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for job diff --git a/bundle/config/variable/lookup_metastore.go b/bundle/config/variable/lookup_metastore.go new file mode 100644 index 0000000000..8b038bbc06 --- /dev/null +++ b/bundle/config/variable/lookup_metastore.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add implementation for metastore diff --git a/bundle/config/variable/lookup_metastore_test.go b/bundle/config/variable/lookup_metastore_test.go new file mode 100644 index 0000000000..e73a4d6101 --- /dev/null +++ b/bundle/config/variable/lookup_metastore_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for metastore diff --git a/bundle/config/variable/lookup_pipeline.go b/bundle/config/variable/lookup_pipeline.go new file mode 100644 index 0000000000..1141e24865 --- /dev/null +++ b/bundle/config/variable/lookup_pipeline.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupPipeline struct { + name string +} + +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 + } + return fmt.Sprint(entity.PipelineId), nil +} + +func (l *lookupPipeline) String() string { + return fmt.Sprintf("pipeline: %s", l.name) +} diff --git a/bundle/config/variable/lookup_pipeline_test.go b/bundle/config/variable/lookup_pipeline_test.go new file mode 100644 index 0000000000..21ce6224dd --- /dev/null +++ b/bundle/config/variable/lookup_pipeline_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for pipeline diff --git a/bundle/config/variable/lookup_query.go b/bundle/config/variable/lookup_query.go new file mode 100644 index 0000000000..cc1d8133fe --- /dev/null +++ b/bundle/config/variable/lookup_query.go @@ -0,0 +1,24 @@ +package variable + +import ( + "context" + "fmt" + + "github.com/databricks/databricks-sdk-go" +) + +type lookupQuery struct { + name string +} + +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 + } + return fmt.Sprint(entity.Id), nil +} + +func (l *lookupQuery) String() string { + return fmt.Sprintf("query: %s", l.name) +} diff --git a/bundle/config/variable/lookup_query_test.go b/bundle/config/variable/lookup_query_test.go new file mode 100644 index 0000000000..58a1533141 --- /dev/null +++ b/bundle/config/variable/lookup_query_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for query diff --git a/bundle/config/variable/lookup_service_principal.go b/bundle/config/variable/lookup_service_principal.go new file mode 100644 index 0000000000..11705ef076 --- /dev/null +++ b/bundle/config/variable/lookup_service_principal.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add implementation for service_principal diff --git a/bundle/config/variable/lookup_service_principal_test.go b/bundle/config/variable/lookup_service_principal_test.go new file mode 100644 index 0000000000..9dd42b188b --- /dev/null +++ b/bundle/config/variable/lookup_service_principal_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for service_principal diff --git a/bundle/config/variable/lookup_warehouse.go b/bundle/config/variable/lookup_warehouse.go new file mode 100644 index 0000000000..abd4345f8d --- /dev/null +++ b/bundle/config/variable/lookup_warehouse.go @@ -0,0 +1 @@ +package variable diff --git a/bundle/config/variable/lookup_warehouse_test.go b/bundle/config/variable/lookup_warehouse_test.go new file mode 100644 index 0000000000..6aad3e9ad2 --- /dev/null +++ b/bundle/config/variable/lookup_warehouse_test.go @@ -0,0 +1,3 @@ +package variable + +// TODO: Add tests for warehouse From 16cd2de149a81cf98da70df8d4f6e0524533dc87 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:40:43 +0100 Subject: [PATCH 2/7] 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 da3856ca6a..9f59103e96 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 (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) - } +// 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 { +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 212c7e993b..14a387824a 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 f7778523f4..4081491ee1 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 d922995a5b..379fcff015 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 16093b0d05..5629eacc32 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 7a4d97fb9a..c43e4cc032 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 98662c288f..1d9cc2b8a0 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 20b3760983..cafb39323e 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 b2db23a8e8..442b4f2140 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 8b038bbc06..3be6368d6f 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 e73a4d6101..b25a647be9 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 21ce6224dd..0c35b18625 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 58a1533141..7eacd9752c 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 11705ef076..c9d4d3768e 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 9dd42b188b..6e8b34a44f 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 abd4345f8d..90199ec61d 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 6aad3e9ad2..20a535cc9f 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 2362ad10d9..89abc4cf7f 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 From 7a99df58ec399d8b7e601e3aeb081ee4567c064a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:46:07 +0100 Subject: [PATCH 3/7] No need for pointers to the lookup structs --- bundle/config/variable/lookup.go | 78 ++++++++++--------- bundle/config/variable/lookup_alert.go | 4 +- bundle/config/variable/lookup_alert_test.go | 6 +- bundle/config/variable/lookup_cluster.go | 4 +- .../config/variable/lookup_cluster_policy.go | 4 +- .../variable/lookup_cluster_policy_test.go | 6 +- bundle/config/variable/lookup_cluster_test.go | 6 +- bundle/config/variable/lookup_dashboard.go | 4 +- .../config/variable/lookup_dashboard_test.go | 6 +- .../config/variable/lookup_instance_pool.go | 4 +- .../variable/lookup_instance_pool_test.go | 6 +- bundle/config/variable/lookup_job.go | 4 +- bundle/config/variable/lookup_job_test.go | 6 +- bundle/config/variable/lookup_metastore.go | 4 +- .../config/variable/lookup_metastore_test.go | 6 +- bundle/config/variable/lookup_pipeline.go | 4 +- .../config/variable/lookup_pipeline_test.go | 6 +- bundle/config/variable/lookup_query.go | 4 +- bundle/config/variable/lookup_query_test.go | 6 +- .../variable/lookup_service_principal.go | 4 +- .../variable/lookup_service_principal_test.go | 6 +- bundle/config/variable/lookup_warehouse.go | 4 +- .../config/variable/lookup_warehouse_test.go | 6 +- 23 files changed, 95 insertions(+), 93 deletions(-) diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index 9f59103e96..39c1a6e83f 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -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 "" } diff --git a/bundle/config/variable/lookup_alert.go b/bundle/config/variable/lookup_alert.go index 768e3b7369..762e39a9e9 100644 --- a/bundle/config/variable/lookup_alert.go +++ b/bundle/config/variable/lookup_alert.go @@ -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) } diff --git a/bundle/config/variable/lookup_alert_test.go b/bundle/config/variable/lookup_alert_test.go index 14a387824a..593bc4a8e1 100644 --- a/bundle/config/variable/lookup_alert_test.go +++ b/bundle/config/variable/lookup_alert_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_cluster.go b/bundle/config/variable/lookup_cluster.go index 4081491ee1..df336d2db0 100644 --- a/bundle/config/variable/lookup_cluster.go +++ b/bundle/config/variable/lookup_cluster.go @@ -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) } diff --git a/bundle/config/variable/lookup_cluster_policy.go b/bundle/config/variable/lookup_cluster_policy.go index 54eefd6966..324a5cc4f9 100644 --- a/bundle/config/variable/lookup_cluster_policy.go +++ b/bundle/config/variable/lookup_cluster_policy.go @@ -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) } diff --git a/bundle/config/variable/lookup_cluster_policy_test.go b/bundle/config/variable/lookup_cluster_policy_test.go index 379fcff015..b9f7e8580e 100644 --- a/bundle/config/variable/lookup_cluster_policy_test.go +++ b/bundle/config/variable/lookup_cluster_policy_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_cluster_test.go b/bundle/config/variable/lookup_cluster_test.go index 5629eacc32..4440dcda16 100644 --- a/bundle/config/variable/lookup_cluster_test.go +++ b/bundle/config/variable/lookup_cluster_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_dashboard.go b/bundle/config/variable/lookup_dashboard.go index c43e4cc032..604cc900b4 100644 --- a/bundle/config/variable/lookup_dashboard.go +++ b/bundle/config/variable/lookup_dashboard.go @@ -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) } diff --git a/bundle/config/variable/lookup_dashboard_test.go b/bundle/config/variable/lookup_dashboard_test.go index 1d9cc2b8a0..45babdf95e 100644 --- a/bundle/config/variable/lookup_dashboard_test.go +++ b/bundle/config/variable/lookup_dashboard_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_instance_pool.go b/bundle/config/variable/lookup_instance_pool.go index f567d34518..ff9499d887 100644 --- a/bundle/config/variable/lookup_instance_pool.go +++ b/bundle/config/variable/lookup_instance_pool.go @@ -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) } diff --git a/bundle/config/variable/lookup_instance_pool_test.go b/bundle/config/variable/lookup_instance_pool_test.go index cafb39323e..84f3a3c54a 100644 --- a/bundle/config/variable/lookup_instance_pool_test.go +++ b/bundle/config/variable/lookup_instance_pool_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_job.go b/bundle/config/variable/lookup_job.go index dbf1e9da99..5893e3f0ec 100644 --- a/bundle/config/variable/lookup_job.go +++ b/bundle/config/variable/lookup_job.go @@ -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) } diff --git a/bundle/config/variable/lookup_job_test.go b/bundle/config/variable/lookup_job_test.go index 442b4f2140..7d371ca6ce 100644 --- a/bundle/config/variable/lookup_job_test.go +++ b/bundle/config/variable/lookup_job_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_metastore.go b/bundle/config/variable/lookup_metastore.go index 3be6368d6f..96d7387932 100644 --- a/bundle/config/variable/lookup_metastore.go +++ b/bundle/config/variable/lookup_metastore.go @@ -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) } diff --git a/bundle/config/variable/lookup_metastore_test.go b/bundle/config/variable/lookup_metastore_test.go index b25a647be9..69c2690213 100644 --- a/bundle/config/variable/lookup_metastore_test.go +++ b/bundle/config/variable/lookup_metastore_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_pipeline.go b/bundle/config/variable/lookup_pipeline.go index 1141e24865..0705a6d9c8 100644 --- a/bundle/config/variable/lookup_pipeline.go +++ b/bundle/config/variable/lookup_pipeline.go @@ -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) } diff --git a/bundle/config/variable/lookup_pipeline_test.go b/bundle/config/variable/lookup_pipeline_test.go index 0c35b18625..0b6b9b3c3e 100644 --- a/bundle/config/variable/lookup_pipeline_test.go +++ b/bundle/config/variable/lookup_pipeline_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_query.go b/bundle/config/variable/lookup_query.go index cc1d8133fe..1bbb558612 100644 --- a/bundle/config/variable/lookup_query.go +++ b/bundle/config/variable/lookup_query.go @@ -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) } diff --git a/bundle/config/variable/lookup_query_test.go b/bundle/config/variable/lookup_query_test.go index 7eacd9752c..370fa32e11 100644 --- a/bundle/config/variable/lookup_query_test.go +++ b/bundle/config/variable/lookup_query_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_service_principal.go b/bundle/config/variable/lookup_service_principal.go index c9d4d3768e..0ee2281eaf 100644 --- a/bundle/config/variable/lookup_service_principal.go +++ b/bundle/config/variable/lookup_service_principal.go @@ -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) } diff --git a/bundle/config/variable/lookup_service_principal_test.go b/bundle/config/variable/lookup_service_principal_test.go index 6e8b34a44f..992d31f61b 100644 --- a/bundle/config/variable/lookup_service_principal_test.go +++ b/bundle/config/variable/lookup_service_principal_test.go @@ -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()) } diff --git a/bundle/config/variable/lookup_warehouse.go b/bundle/config/variable/lookup_warehouse.go index 90199ec61d..c194e5cd2a 100644 --- a/bundle/config/variable/lookup_warehouse.go +++ b/bundle/config/variable/lookup_warehouse.go @@ -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) } diff --git a/bundle/config/variable/lookup_warehouse_test.go b/bundle/config/variable/lookup_warehouse_test.go index 20a535cc9f..086d8383a0 100644 --- a/bundle/config/variable/lookup_warehouse_test.go +++ b/bundle/config/variable/lookup_warehouse_test.go @@ -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()) } From 488a1fbb67b2b2129184e3d5cedbffbb059042c5 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:53:24 +0100 Subject: [PATCH 4/7] Finalize --- bundle/config/variable/lookup.go | 172 +++++------------------------ bundle/config/variable/variable.go | 2 +- 2 files changed, 31 insertions(+), 143 deletions(-) diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index 39c1a6e83f..111bc6ac56 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -41,178 +41,66 @@ type Lookup struct { } func (l *Lookup) constructResolver() (resolver, error) { + var resolvers []resolver if l.Alert != "" { - return lookupAlert{name: l.Alert}, nil + resolvers = append(resolvers, lookupAlert{name: l.Alert}) } if l.ClusterPolicy != "" { - return lookupClusterPolicy{name: l.ClusterPolicy}, nil + resolvers = append(resolvers, lookupClusterPolicy{name: l.ClusterPolicy}) } if l.Cluster != "" { - return lookupCluster{name: l.Cluster}, nil + resolvers = append(resolvers, lookupCluster{name: l.Cluster}) } if l.Dashboard != "" { - return lookupDashboard{name: l.Dashboard}, nil + resolvers = append(resolvers, lookupDashboard{name: l.Dashboard}) } if l.InstancePool != "" { - return lookupInstancePool{name: l.InstancePool}, nil + resolvers = append(resolvers, lookupInstancePool{name: l.InstancePool}) } if l.Job != "" { - return lookupJob{name: l.Job}, nil + resolvers = append(resolvers, lookupJob{name: l.Job}) } if l.Metastore != "" { - return lookupMetastore{name: l.Metastore}, nil + resolvers = append(resolvers, lookupMetastore{name: l.Metastore}) } if l.Pipeline != "" { - return lookupPipeline{name: l.Pipeline}, nil + resolvers = append(resolvers, lookupPipeline{name: l.Pipeline}) } if l.Query != "" { - return lookupQuery{name: l.Query}, nil + resolvers = append(resolvers, lookupQuery{name: l.Query}) } if l.ServicePrincipal != "" { - return lookupServicePrincipal{name: l.ServicePrincipal}, nil + resolvers = append(resolvers, lookupServicePrincipal{name: l.ServicePrincipal}) } if l.Warehouse != "" { - return lookupWarehouse{name: l.Warehouse}, nil + resolvers = append(resolvers, lookupWarehouse{name: l.Warehouse}) } - 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 { - r, _ := l.constructResolver() - if r != nil { - return r.String() + switch len(resolvers) { + case 0: + return nil, fmt.Errorf("no valid lookup fields provided") + case 1: + return resolvers[0], nil + default: + return nil, fmt.Errorf("exactly one lookup field must be provided") } - return "" } -func (l *Lookup) validate() error { - // Validate that only one field is set - count := 0 - if l.Alert != "" { - count++ - } - if l.ClusterPolicy != "" { - count++ - } - if l.Cluster != "" { - count++ - } - if l.Dashboard != "" { - count++ - } - if l.InstancePool != "" { - count++ - } - if l.Job != "" { - count++ - } - if l.Metastore != "" { - count++ - } - if l.Pipeline != "" { - count++ - } - if l.Query != "" { - count++ - } - if l.ServicePrincipal != "" { - count++ - } - if l.Warehouse != "" { - count++ +func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { + r, err := l.constructResolver() + if err != nil { + return "", err } - if count != 1 { - return fmt.Errorf("exactly one lookup field must be provided") - } + return r.Resolve(ctx, w) +} - if strings.Contains(l.String(), "${var") { - return fmt.Errorf("lookup fields cannot contain variable references") +func (l *Lookup) String() string { + r, _ := l.constructResolver() + if r == nil { + return "" } - return nil + return r.String() } diff --git a/bundle/config/variable/variable.go b/bundle/config/variable/variable.go index 89abc4cf7f..2362ad10d9 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 From ed62d0821c60777a8d6982fb86f33912a975b181 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 22:54:52 +0100 Subject: [PATCH 5/7] Remove from codegen --- .codegen.json | 3 +- .codegen/lookup.go.tmpl | 134 ------------------------------- .gitattributes | 1 - bundle/config/variable/lookup.go | 3 - 4 files changed, 1 insertion(+), 140 deletions(-) delete mode 100644 .codegen/lookup.go.tmpl diff --git a/.codegen.json b/.codegen.json index 4524ab55dd..73ab8c2a4f 100644 --- a/.codegen.json +++ b/.codegen.json @@ -5,8 +5,7 @@ }, "batch": { ".codegen/cmds-workspace.go.tmpl": "cmd/workspace/cmd.go", - ".codegen/cmds-account.go.tmpl": "cmd/account/cmd.go", - ".codegen/lookup.go.tmpl": "bundle/config/variable/lookup.go" + ".codegen/cmds-account.go.tmpl": "cmd/account/cmd.go" }, "toolchain": { "required": ["go"], diff --git a/.codegen/lookup.go.tmpl b/.codegen/lookup.go.tmpl deleted file mode 100644 index 124b629d02..0000000000 --- a/.codegen/lookup.go.tmpl +++ /dev/null @@ -1,134 +0,0 @@ -// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT. - -package variable - -{{ $allowlist := - list - "alerts" - "clusters" - "cluster-policies" - "clusters" - "dashboards" - "instance-pools" - "jobs" - "metastores" - "pipelines" - "service-principals" - "queries" - "warehouses" -}} - -{{ $customField := - dict - "service-principals" "ApplicationId" -}} - -import ( - "context" - "fmt" - - "github.com/databricks/databricks-sdk-go" -) - -type Lookup struct { - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - {{.Singular.PascalName}} string `json:"{{.Singular.SnakeName}},omitempty"` - - {{end}} - {{- end}} -} - -func LookupFromMap(m map[string]any) *Lookup { - l := &Lookup{} - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - if v, ok := m["{{.Singular.SnakeName}}"]; ok { - l.{{.Singular.PascalName}} = v.(string) - } - {{end -}} - {{- end}} - return l -} - -func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - if err := l.validate(); err != nil { - return "", err - } - - r := allResolvers() - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - if l.{{.Singular.PascalName}} != "" { - return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}}) - } - {{end -}} - {{- end}} - - return "", fmt.Errorf("no valid lookup fields provided") -} - -func (l *Lookup) String() string { - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - if l.{{.Singular.PascalName}} != "" { - return fmt.Sprintf("{{.Singular.KebabName}}: %s", l.{{.Singular.PascalName}}) - } - {{end -}} - {{- end}} - return "" -} - -func (l *Lookup) validate() error { - // Validate that only one field is set - count := 0 - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - if l.{{.Singular.PascalName}} != "" { - count++ - } - {{end -}} - {{- end}} - - if count != 1 { - return fmt.Errorf("exactly one lookup field must be provided") - } - - if strings.Contains(l.String(), "${var") { - return fmt.Errorf("lookup fields cannot contain variable references") - } - - return nil -} - - -type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) -type resolvers struct { - {{range .Services -}} - {{- if in $allowlist .KebabName -}} - {{.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) { - fn, ok := lookupOverrides["{{.Singular.PascalName}}"] - if ok { - return fn(ctx, w, name) - } - entity, err := w.{{.PascalName}}.GetBy{{range .NamedIdMap.NamePath}}{{.PascalName}}{{end}}(ctx, name) - if err != nil { - return "", err - } - - return fmt.Sprint(entity.{{ getOrDefault $customField .KebabName ((index .NamedIdMap.IdPath 0).PascalName) }}), nil - } - {{end -}} - {{- end}} - - return r -} diff --git a/.gitattributes b/.gitattributes index ecb5669ef8..2755c02d72 100755 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,3 @@ -bundle/config/variable/lookup.go linguist-generated=true cmd/account/access-control/access-control.go linguist-generated=true cmd/account/billable-usage/billable-usage.go linguist-generated=true cmd/account/budgets/budgets.go linguist-generated=true diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index 111bc6ac56..f08c1d8dd7 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -1,11 +1,8 @@ -// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT. - package variable import ( "context" "fmt" - "strings" "github.com/databricks/databricks-sdk-go" ) From 354d76583371d32e86e160dfa2e32749c2099564 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 23:12:16 +0100 Subject: [PATCH 6/7] Rename types and add tests --- bundle/config/variable/lookup.go | 22 +++---- bundle/config/variable/lookup_test.go | 60 +++++++++++++++++++ .../{lookup_alert.go => resolve_alert.go} | 6 +- ...up_alert_test.go => resolve_alert_test.go} | 12 ++-- .../{lookup_cluster.go => resolve_cluster.go} | 6 +- ...er_policy.go => resolve_cluster_policy.go} | 6 +- ...test.go => resolve_cluster_policy_test.go} | 12 ++-- ...luster_test.go => resolve_cluster_test.go} | 12 ++-- ...okup_dashboard.go => resolve_dashboard.go} | 6 +- ...oard_test.go => resolve_dashboard_test.go} | 12 ++-- ...tance_pool.go => resolve_instance_pool.go} | 6 +- ..._test.go => resolve_instance_pool_test.go} | 12 ++-- .../{lookup_job.go => resolve_job.go} | 6 +- ...lookup_job_test.go => resolve_job_test.go} | 12 ++-- ...okup_metastore.go => resolve_metastore.go} | 6 +- ...tore_test.go => resolve_metastore_test.go} | 12 ++-- ...lookup_pipeline.go => resolve_pipeline.go} | 6 +- ...eline_test.go => resolve_pipeline_test.go} | 12 ++-- .../{lookup_query.go => resolve_query.go} | 6 +- ...up_query_test.go => resolve_query_test.go} | 12 ++-- ...ncipal.go => resolve_service_principal.go} | 6 +- ...t.go => resolve_service_principal_test.go} | 12 ++-- ...okup_warehouse.go => resolve_warehouse.go} | 6 +- ...ouse_test.go => resolve_warehouse_test.go} | 12 ++-- 24 files changed, 170 insertions(+), 110 deletions(-) create mode 100644 bundle/config/variable/lookup_test.go rename bundle/config/variable/{lookup_alert.go => resolve_alert.go} (62%) rename bundle/config/variable/{lookup_alert_test.go => resolve_alert_test.go} (79%) rename bundle/config/variable/{lookup_cluster.go => resolve_cluster.go} (86%) rename bundle/config/variable/{lookup_cluster_policy.go => resolve_cluster_policy.go} (60%) rename bundle/config/variable/{lookup_cluster_policy_test.go => resolve_cluster_policy_test.go} (77%) rename bundle/config/variable/{lookup_cluster_test.go => resolve_cluster_test.go} (80%) rename bundle/config/variable/{lookup_dashboard.go => resolve_dashboard.go} (61%) rename bundle/config/variable/{lookup_dashboard_test.go => resolve_dashboard_test.go} (77%) rename bundle/config/variable/{lookup_instance_pool.go => resolve_instance_pool.go} (62%) rename bundle/config/variable/{lookup_instance_pool_test.go => resolve_instance_pool_test.go} (77%) rename bundle/config/variable/{lookup_job.go => resolve_job.go} (63%) rename bundle/config/variable/{lookup_job_test.go => resolve_job_test.go} (80%) rename bundle/config/variable/{lookup_metastore.go => resolve_metastore.go} (61%) rename bundle/config/variable/{lookup_metastore_test.go => resolve_metastore_test.go} (78%) rename bundle/config/variable/{lookup_pipeline.go => resolve_pipeline.go} (62%) rename bundle/config/variable/{lookup_pipeline_test.go => resolve_pipeline_test.go} (78%) rename bundle/config/variable/{lookup_query.go => resolve_query.go} (62%) rename bundle/config/variable/{lookup_query_test.go => resolve_query_test.go} (80%) rename bundle/config/variable/{lookup_service_principal.go => resolve_service_principal.go} (61%) rename bundle/config/variable/{lookup_service_principal_test.go => resolve_service_principal_test.go} (75%) rename bundle/config/variable/{lookup_warehouse.go => resolve_warehouse.go} (61%) rename bundle/config/variable/{lookup_warehouse_test.go => resolve_warehouse_test.go} (77%) diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index f08c1d8dd7..4fd08725a5 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -41,37 +41,37 @@ func (l *Lookup) constructResolver() (resolver, error) { var resolvers []resolver if l.Alert != "" { - resolvers = append(resolvers, lookupAlert{name: l.Alert}) + resolvers = append(resolvers, resolveAlert{name: l.Alert}) } if l.ClusterPolicy != "" { - resolvers = append(resolvers, lookupClusterPolicy{name: l.ClusterPolicy}) + resolvers = append(resolvers, resolveClusterPolicy{name: l.ClusterPolicy}) } if l.Cluster != "" { - resolvers = append(resolvers, lookupCluster{name: l.Cluster}) + resolvers = append(resolvers, resolveCluster{name: l.Cluster}) } if l.Dashboard != "" { - resolvers = append(resolvers, lookupDashboard{name: l.Dashboard}) + resolvers = append(resolvers, resolveDashboard{name: l.Dashboard}) } if l.InstancePool != "" { - resolvers = append(resolvers, lookupInstancePool{name: l.InstancePool}) + resolvers = append(resolvers, resolveInstancePool{name: l.InstancePool}) } if l.Job != "" { - resolvers = append(resolvers, lookupJob{name: l.Job}) + resolvers = append(resolvers, resolveJob{name: l.Job}) } if l.Metastore != "" { - resolvers = append(resolvers, lookupMetastore{name: l.Metastore}) + resolvers = append(resolvers, resolveMetastore{name: l.Metastore}) } if l.Pipeline != "" { - resolvers = append(resolvers, lookupPipeline{name: l.Pipeline}) + resolvers = append(resolvers, resolvePipeline{name: l.Pipeline}) } if l.Query != "" { - resolvers = append(resolvers, lookupQuery{name: l.Query}) + resolvers = append(resolvers, resolveQuery{name: l.Query}) } if l.ServicePrincipal != "" { - resolvers = append(resolvers, lookupServicePrincipal{name: l.ServicePrincipal}) + resolvers = append(resolvers, resolveServicePrincipal{name: l.ServicePrincipal}) } if l.Warehouse != "" { - resolvers = append(resolvers, lookupWarehouse{name: l.Warehouse}) + resolvers = append(resolvers, resolveWarehouse{name: l.Warehouse}) } switch len(resolvers) { diff --git a/bundle/config/variable/lookup_test.go b/bundle/config/variable/lookup_test.go new file mode 100644 index 0000000000..a847487512 --- /dev/null +++ b/bundle/config/variable/lookup_test.go @@ -0,0 +1,60 @@ +package variable + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestLookup_Coverage(t *testing.T) { + var lookup Lookup + val := reflect.ValueOf(lookup) + typ := val.Type() + + for i := 0; i < val.NumField(); i++ { + field := val.Field(i) + if field.Kind() != reflect.String { + t.Fatalf("Field %s is not a string", typ.Field(i).Name) + } + + fieldType := typ.Field(i) + t.Run(fieldType.Name, func(t *testing.T) { + // Use a fresh instance of the struct in each test + var lookup Lookup + + // Set the field to a non-empty string + reflect.ValueOf(&lookup).Elem().Field(i).SetString("value") + + // Test the [String] function + assert.NotEmpty(t, lookup.String()) + }) + } +} + +func TestLookup_Empty(t *testing.T) { + var lookup Lookup + + // Resolve returns an error when no fields are provided + _, err := lookup.Resolve(context.Background(), nil) + assert.ErrorContains(t, err, "no valid lookup fields provided") + + // No string representation for an invalid lookup + assert.Empty(t, lookup.String()) + +} + +func TestLookup_Multiple(t *testing.T) { + lookup := Lookup{ + Alert: "alert", + Query: "query", + } + + // Resolve returns an error when multiple fields are provided + _, err := lookup.Resolve(context.Background(), nil) + assert.ErrorContains(t, err, "exactly one lookup field must be provided") + + // No string representation for an invalid lookup + assert.Empty(t, lookup.String()) +} diff --git a/bundle/config/variable/lookup_alert.go b/bundle/config/variable/resolve_alert.go similarity index 62% rename from bundle/config/variable/lookup_alert.go rename to bundle/config/variable/resolve_alert.go index 762e39a9e9..be83e81fa7 100644 --- a/bundle/config/variable/lookup_alert.go +++ b/bundle/config/variable/resolve_alert.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupAlert struct { +type resolveAlert struct { name string } -func (l lookupAlert) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveAlert) 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 resolveAlert) String() string { return fmt.Sprintf("alert: %s", l.name) } diff --git a/bundle/config/variable/lookup_alert_test.go b/bundle/config/variable/resolve_alert_test.go similarity index 79% rename from bundle/config/variable/lookup_alert_test.go rename to bundle/config/variable/resolve_alert_test.go index 593bc4a8e1..32f8d641bd 100644 --- a/bundle/config/variable/lookup_alert_test.go +++ b/bundle/config/variable/resolve_alert_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupAlert_ResolveSuccess(t *testing.T) { +func TestResolveAlert_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockAlertsAPI() @@ -23,13 +23,13 @@ func TestLookupAlert_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupAlert{name: "alert"} + l := resolveAlert{name: "alert"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "1234", result) } -func TestLookupAlert_ResolveNotFound(t *testing.T) { +func TestResolveAlert_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockAlertsAPI() @@ -38,12 +38,12 @@ func TestLookupAlert_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupAlert{name: "alert"} + l := resolveAlert{name: "alert"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupAlert_String(t *testing.T) { - l := lookupAlert{name: "name"} +func TestResolveAlert_String(t *testing.T) { + l := resolveAlert{name: "name"} assert.Equal(t, "alert: name", l.String()) } diff --git a/bundle/config/variable/lookup_cluster.go b/bundle/config/variable/resolve_cluster.go similarity index 86% rename from bundle/config/variable/lookup_cluster.go rename to bundle/config/variable/resolve_cluster.go index df336d2db0..2d68b7fb7b 100644 --- a/bundle/config/variable/lookup_cluster.go +++ b/bundle/config/variable/resolve_cluster.go @@ -8,13 +8,13 @@ import ( "github.com/databricks/databricks-sdk-go/service/compute" ) -type lookupCluster struct { +type resolveCluster struct { name string } // 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 resolveCluster) 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.WorkspaceClien return alternatives[0].ClusterId, nil } -func (l lookupCluster) String() string { +func (l resolveCluster) String() string { return fmt.Sprintf("cluster: %s", l.name) } diff --git a/bundle/config/variable/lookup_cluster_policy.go b/bundle/config/variable/resolve_cluster_policy.go similarity index 60% rename from bundle/config/variable/lookup_cluster_policy.go rename to bundle/config/variable/resolve_cluster_policy.go index 324a5cc4f9..b19380a63a 100644 --- a/bundle/config/variable/lookup_cluster_policy.go +++ b/bundle/config/variable/resolve_cluster_policy.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupClusterPolicy struct { +type resolveClusterPolicy struct { name string } -func (l lookupClusterPolicy) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveClusterPolicy) 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.Workspac return fmt.Sprint(entity.PolicyId), nil } -func (l lookupClusterPolicy) String() string { +func (l resolveClusterPolicy) String() string { return fmt.Sprintf("cluster-policy: %s", l.name) } diff --git a/bundle/config/variable/lookup_cluster_policy_test.go b/bundle/config/variable/resolve_cluster_policy_test.go similarity index 77% rename from bundle/config/variable/lookup_cluster_policy_test.go rename to bundle/config/variable/resolve_cluster_policy_test.go index b9f7e8580e..fb17fad180 100644 --- a/bundle/config/variable/lookup_cluster_policy_test.go +++ b/bundle/config/variable/resolve_cluster_policy_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupClusterPolicy_ResolveSuccess(t *testing.T) { +func TestResolveClusterPolicy_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockClusterPoliciesAPI() @@ -23,13 +23,13 @@ func TestLookupClusterPolicy_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupClusterPolicy{name: "policy"} + l := resolveClusterPolicy{name: "policy"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "1234", result) } -func TestLookupClusterPolicy_ResolveNotFound(t *testing.T) { +func TestResolveClusterPolicy_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockClusterPoliciesAPI() @@ -38,12 +38,12 @@ func TestLookupClusterPolicy_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupClusterPolicy{name: "policy"} + l := resolveClusterPolicy{name: "policy"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupClusterPolicy_String(t *testing.T) { - l := lookupClusterPolicy{name: "name"} +func TestResolveClusterPolicy_String(t *testing.T) { + l := resolveClusterPolicy{name: "name"} assert.Equal(t, "cluster-policy: name", l.String()) } diff --git a/bundle/config/variable/lookup_cluster_test.go b/bundle/config/variable/resolve_cluster_test.go similarity index 80% rename from bundle/config/variable/lookup_cluster_test.go rename to bundle/config/variable/resolve_cluster_test.go index 4440dcda16..2f3aa27cfd 100644 --- a/bundle/config/variable/lookup_cluster_test.go +++ b/bundle/config/variable/resolve_cluster_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupCluster_ResolveSuccess(t *testing.T) { +func TestResolveCluster_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockClustersAPI() @@ -23,13 +23,13 @@ func TestLookupCluster_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupCluster{name: "cluster2"} + l := resolveCluster{name: "cluster2"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "2345", result) } -func TestLookupCluster_ResolveNotFound(t *testing.T) { +func TestResolveCluster_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockClustersAPI() @@ -38,13 +38,13 @@ func TestLookupCluster_ResolveNotFound(t *testing.T) { Return([]compute.ClusterDetails{}, nil) ctx := context.Background() - l := lookupCluster{name: "cluster"} + l := resolveCluster{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"} +func TestResolveCluster_String(t *testing.T) { + l := resolveCluster{name: "name"} assert.Equal(t, "cluster: name", l.String()) } diff --git a/bundle/config/variable/lookup_dashboard.go b/bundle/config/variable/resolve_dashboard.go similarity index 61% rename from bundle/config/variable/lookup_dashboard.go rename to bundle/config/variable/resolve_dashboard.go index 604cc900b4..44fd45197a 100644 --- a/bundle/config/variable/lookup_dashboard.go +++ b/bundle/config/variable/resolve_dashboard.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupDashboard struct { +type resolveDashboard struct { name string } -func (l lookupDashboard) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveDashboard) 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.WorkspaceCli return fmt.Sprint(entity.Id), nil } -func (l lookupDashboard) String() string { +func (l resolveDashboard) String() string { return fmt.Sprintf("dashboard: %s", l.name) } diff --git a/bundle/config/variable/lookup_dashboard_test.go b/bundle/config/variable/resolve_dashboard_test.go similarity index 77% rename from bundle/config/variable/lookup_dashboard_test.go rename to bundle/config/variable/resolve_dashboard_test.go index 45babdf95e..3afed47941 100644 --- a/bundle/config/variable/lookup_dashboard_test.go +++ b/bundle/config/variable/resolve_dashboard_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupDashboard_ResolveSuccess(t *testing.T) { +func TestResolveDashboard_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockDashboardsAPI() @@ -23,13 +23,13 @@ func TestLookupDashboard_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupDashboard{name: "dashboard"} + l := resolveDashboard{name: "dashboard"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "1234", result) } -func TestLookupDashboard_ResolveNotFound(t *testing.T) { +func TestResolveDashboard_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockDashboardsAPI() @@ -38,12 +38,12 @@ func TestLookupDashboard_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupDashboard{name: "dashboard"} + l := resolveDashboard{name: "dashboard"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupDashboard_String(t *testing.T) { - l := lookupDashboard{name: "name"} +func TestResolveDashboard_String(t *testing.T) { + l := resolveDashboard{name: "name"} assert.Equal(t, "dashboard: name", l.String()) } diff --git a/bundle/config/variable/lookup_instance_pool.go b/bundle/config/variable/resolve_instance_pool.go similarity index 62% rename from bundle/config/variable/lookup_instance_pool.go rename to bundle/config/variable/resolve_instance_pool.go index ff9499d887..cbf0775c9e 100644 --- a/bundle/config/variable/lookup_instance_pool.go +++ b/bundle/config/variable/resolve_instance_pool.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupInstancePool struct { +type resolveInstancePool struct { name string } -func (l lookupInstancePool) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveInstancePool) 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.Workspace return fmt.Sprint(entity.InstancePoolId), nil } -func (l lookupInstancePool) String() string { +func (l resolveInstancePool) String() string { return fmt.Sprintf("instance-pool: %s", l.name) } diff --git a/bundle/config/variable/lookup_instance_pool_test.go b/bundle/config/variable/resolve_instance_pool_test.go similarity index 77% rename from bundle/config/variable/lookup_instance_pool_test.go rename to bundle/config/variable/resolve_instance_pool_test.go index 84f3a3c54a..cfd1ba0154 100644 --- a/bundle/config/variable/lookup_instance_pool_test.go +++ b/bundle/config/variable/resolve_instance_pool_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupInstancePool_ResolveSuccess(t *testing.T) { +func TestResolveInstancePool_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockInstancePoolsAPI() @@ -23,13 +23,13 @@ func TestLookupInstancePool_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupInstancePool{name: "instance_pool"} + l := resolveInstancePool{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) { +func TestResolveInstancePool_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockInstancePoolsAPI() @@ -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 := resolveInstancePool{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"} +func TestResolveInstancePool_String(t *testing.T) { + l := resolveInstancePool{name: "name"} assert.Equal(t, "instance-pool: name", l.String()) } diff --git a/bundle/config/variable/lookup_job.go b/bundle/config/variable/resolve_job.go similarity index 63% rename from bundle/config/variable/lookup_job.go rename to bundle/config/variable/resolve_job.go index 5893e3f0ec..3def64888a 100644 --- a/bundle/config/variable/lookup_job.go +++ b/bundle/config/variable/resolve_job.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupJob struct { +type resolveJob struct { name string } -func (l lookupJob) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveJob) 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 resolveJob) String() string { return fmt.Sprintf("job: %s", l.name) } diff --git a/bundle/config/variable/lookup_job_test.go b/bundle/config/variable/resolve_job_test.go similarity index 80% rename from bundle/config/variable/lookup_job_test.go rename to bundle/config/variable/resolve_job_test.go index 7d371ca6ce..523d07957b 100644 --- a/bundle/config/variable/lookup_job_test.go +++ b/bundle/config/variable/resolve_job_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupJob_ResolveSuccess(t *testing.T) { +func TestResolveJob_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockJobsAPI() @@ -23,13 +23,13 @@ func TestLookupJob_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupJob{name: "job"} + l := resolveJob{name: "job"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "5678", result) } -func TestLookupJob_ResolveNotFound(t *testing.T) { +func TestResolveJob_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockJobsAPI() @@ -38,12 +38,12 @@ func TestLookupJob_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupJob{name: "job"} + l := resolveJob{name: "job"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupJob_String(t *testing.T) { - l := lookupJob{name: "name"} +func TestResolveJob_String(t *testing.T) { + l := resolveJob{name: "name"} assert.Equal(t, "job: name", l.String()) } diff --git a/bundle/config/variable/lookup_metastore.go b/bundle/config/variable/resolve_metastore.go similarity index 61% rename from bundle/config/variable/lookup_metastore.go rename to bundle/config/variable/resolve_metastore.go index 96d7387932..958e437870 100644 --- a/bundle/config/variable/lookup_metastore.go +++ b/bundle/config/variable/resolve_metastore.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupMetastore struct { +type resolveMetastore struct { name string } -func (l lookupMetastore) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveMetastore) 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.WorkspaceCli return fmt.Sprint(entity.MetastoreId), nil } -func (l lookupMetastore) String() string { +func (l resolveMetastore) String() string { return fmt.Sprintf("metastore: %s", l.name) } diff --git a/bundle/config/variable/lookup_metastore_test.go b/bundle/config/variable/resolve_metastore_test.go similarity index 78% rename from bundle/config/variable/lookup_metastore_test.go rename to bundle/config/variable/resolve_metastore_test.go index 69c2690213..55c4d92d09 100644 --- a/bundle/config/variable/lookup_metastore_test.go +++ b/bundle/config/variable/resolve_metastore_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupMetastore_ResolveSuccess(t *testing.T) { +func TestResolveMetastore_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockMetastoresAPI() @@ -23,13 +23,13 @@ func TestLookupMetastore_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupMetastore{name: "metastore"} + l := resolveMetastore{name: "metastore"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "abcd", result) } -func TestLookupMetastore_ResolveNotFound(t *testing.T) { +func TestResolveMetastore_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockMetastoresAPI() @@ -38,12 +38,12 @@ func TestLookupMetastore_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupMetastore{name: "metastore"} + l := resolveMetastore{name: "metastore"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupMetastore_String(t *testing.T) { - l := lookupMetastore{name: "name"} +func TestResolveMetastore_String(t *testing.T) { + l := resolveMetastore{name: "name"} assert.Equal(t, "metastore: name", l.String()) } diff --git a/bundle/config/variable/lookup_pipeline.go b/bundle/config/variable/resolve_pipeline.go similarity index 62% rename from bundle/config/variable/lookup_pipeline.go rename to bundle/config/variable/resolve_pipeline.go index 0705a6d9c8..cabc620daa 100644 --- a/bundle/config/variable/lookup_pipeline.go +++ b/bundle/config/variable/resolve_pipeline.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupPipeline struct { +type resolvePipeline struct { name string } -func (l lookupPipeline) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolvePipeline) 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.WorkspaceClie return fmt.Sprint(entity.PipelineId), nil } -func (l lookupPipeline) String() string { +func (l resolvePipeline) String() string { return fmt.Sprintf("pipeline: %s", l.name) } diff --git a/bundle/config/variable/lookup_pipeline_test.go b/bundle/config/variable/resolve_pipeline_test.go similarity index 78% rename from bundle/config/variable/lookup_pipeline_test.go rename to bundle/config/variable/resolve_pipeline_test.go index 0b6b9b3c3e..620d762434 100644 --- a/bundle/config/variable/lookup_pipeline_test.go +++ b/bundle/config/variable/resolve_pipeline_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupPipeline_ResolveSuccess(t *testing.T) { +func TestResolvePipeline_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockPipelinesAPI() @@ -23,13 +23,13 @@ func TestLookupPipeline_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupPipeline{name: "pipeline"} + l := resolvePipeline{name: "pipeline"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "abcd", result) } -func TestLookupPipeline_ResolveNotFound(t *testing.T) { +func TestResolvePipeline_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockPipelinesAPI() @@ -38,12 +38,12 @@ func TestLookupPipeline_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupPipeline{name: "pipeline"} + l := resolvePipeline{name: "pipeline"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupPipeline_String(t *testing.T) { - l := lookupPipeline{name: "name"} +func TestResolvePipeline_String(t *testing.T) { + l := resolvePipeline{name: "name"} assert.Equal(t, "pipeline: name", l.String()) } diff --git a/bundle/config/variable/lookup_query.go b/bundle/config/variable/resolve_query.go similarity index 62% rename from bundle/config/variable/lookup_query.go rename to bundle/config/variable/resolve_query.go index 1bbb558612..602ff8deb5 100644 --- a/bundle/config/variable/lookup_query.go +++ b/bundle/config/variable/resolve_query.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupQuery struct { +type resolveQuery struct { name string } -func (l lookupQuery) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveQuery) 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 resolveQuery) String() string { return fmt.Sprintf("query: %s", l.name) } diff --git a/bundle/config/variable/lookup_query_test.go b/bundle/config/variable/resolve_query_test.go similarity index 80% rename from bundle/config/variable/lookup_query_test.go rename to bundle/config/variable/resolve_query_test.go index 370fa32e11..21516e4525 100644 --- a/bundle/config/variable/lookup_query_test.go +++ b/bundle/config/variable/resolve_query_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupQuery_ResolveSuccess(t *testing.T) { +func TestResolveQuery_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockQueriesAPI() @@ -23,13 +23,13 @@ func TestLookupQuery_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupQuery{name: "query"} + l := resolveQuery{name: "query"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "1234", result) } -func TestLookupQuery_ResolveNotFound(t *testing.T) { +func TestResolveQuery_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockQueriesAPI() @@ -38,12 +38,12 @@ func TestLookupQuery_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupQuery{name: "query"} + l := resolveQuery{name: "query"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupQuery_String(t *testing.T) { - l := lookupQuery{name: "name"} +func TestResolveQuery_String(t *testing.T) { + l := resolveQuery{name: "name"} assert.Equal(t, "query: name", l.String()) } diff --git a/bundle/config/variable/lookup_service_principal.go b/bundle/config/variable/resolve_service_principal.go similarity index 61% rename from bundle/config/variable/lookup_service_principal.go rename to bundle/config/variable/resolve_service_principal.go index 0ee2281eaf..3bea4314bc 100644 --- a/bundle/config/variable/lookup_service_principal.go +++ b/bundle/config/variable/resolve_service_principal.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupServicePrincipal struct { +type resolveServicePrincipal struct { name string } -func (l lookupServicePrincipal) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveServicePrincipal) 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.Works return fmt.Sprint(entity.ApplicationId), nil } -func (l lookupServicePrincipal) String() string { +func (l resolveServicePrincipal) 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/resolve_service_principal_test.go similarity index 75% rename from bundle/config/variable/lookup_service_principal_test.go rename to bundle/config/variable/resolve_service_principal_test.go index 992d31f61b..c80f9e4a6f 100644 --- a/bundle/config/variable/lookup_service_principal_test.go +++ b/bundle/config/variable/resolve_service_principal_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupServicePrincipal_ResolveSuccess(t *testing.T) { +func TestResolveServicePrincipal_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockServicePrincipalsAPI() @@ -23,13 +23,13 @@ func TestLookupServicePrincipal_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupServicePrincipal{name: "service-principal"} + l := resolveServicePrincipal{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) { +func TestResolveServicePrincipal_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockServicePrincipalsAPI() @@ -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 := resolveServicePrincipal{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"} +func TestResolveServicePrincipal_String(t *testing.T) { + l := resolveServicePrincipal{name: "name"} assert.Equal(t, "service-principal: name", l.String()) } diff --git a/bundle/config/variable/lookup_warehouse.go b/bundle/config/variable/resolve_warehouse.go similarity index 61% rename from bundle/config/variable/lookup_warehouse.go rename to bundle/config/variable/resolve_warehouse.go index c194e5cd2a..fbd3663a27 100644 --- a/bundle/config/variable/lookup_warehouse.go +++ b/bundle/config/variable/resolve_warehouse.go @@ -7,11 +7,11 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type lookupWarehouse struct { +type resolveWarehouse struct { name string } -func (l lookupWarehouse) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { +func (l resolveWarehouse) 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.WorkspaceCli return fmt.Sprint(entity.Id), nil } -func (l lookupWarehouse) String() string { +func (l resolveWarehouse) String() string { return fmt.Sprintf("warehouse: %s", l.name) } diff --git a/bundle/config/variable/lookup_warehouse_test.go b/bundle/config/variable/resolve_warehouse_test.go similarity index 77% rename from bundle/config/variable/lookup_warehouse_test.go rename to bundle/config/variable/resolve_warehouse_test.go index 086d8383a0..68e3925bc7 100644 --- a/bundle/config/variable/lookup_warehouse_test.go +++ b/bundle/config/variable/resolve_warehouse_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLookupWarehouse_ResolveSuccess(t *testing.T) { +func TestResolveWarehouse_ResolveSuccess(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockWarehousesAPI() @@ -23,13 +23,13 @@ func TestLookupWarehouse_ResolveSuccess(t *testing.T) { }, nil) ctx := context.Background() - l := lookupWarehouse{name: "warehouse"} + l := resolveWarehouse{name: "warehouse"} result, err := l.Resolve(ctx, m.WorkspaceClient) require.NoError(t, err) assert.Equal(t, "abcd", result) } -func TestLookupWarehouse_ResolveNotFound(t *testing.T) { +func TestResolveWarehouse_ResolveNotFound(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) api := m.GetMockWarehousesAPI() @@ -38,12 +38,12 @@ func TestLookupWarehouse_ResolveNotFound(t *testing.T) { Return(nil, &apierr.APIError{StatusCode: 404}) ctx := context.Background() - l := lookupWarehouse{name: "warehouse"} + l := resolveWarehouse{name: "warehouse"} _, err := l.Resolve(ctx, m.WorkspaceClient) require.ErrorIs(t, err, apierr.ErrNotFound) } -func TestLookupWarehouse_String(t *testing.T) { - l := lookupWarehouse{name: "name"} +func TestResolveWarehouse_String(t *testing.T) { + l := resolveWarehouse{name: "name"} assert.Equal(t, "warehouse: name", l.String()) } From e8e89b0cbf7d1adb0802ba481b9dbc801a23c452 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 20 Nov 2024 23:24:03 +0100 Subject: [PATCH 7/7] Move and comment --- bundle/config/variable/lookup.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bundle/config/variable/lookup.go b/bundle/config/variable/lookup.go index 4fd08725a5..f8cb671987 100755 --- a/bundle/config/variable/lookup.go +++ b/bundle/config/variable/lookup.go @@ -7,12 +7,6 @@ import ( "github.com/databricks/databricks-sdk-go" ) -type resolver interface { - Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) - - String() string -} - type Lookup struct { Alert string `json:"alert,omitempty"` @@ -37,6 +31,14 @@ type Lookup struct { Warehouse string `json:"warehouse,omitempty"` } +type resolver interface { + // Resolve resolves the underlying entity's ID. + Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) + + // String returns a human-readable representation of the resolver. + String() string +} + func (l *Lookup) constructResolver() (resolver, error) { var resolvers []resolver