11package resources
22
33import (
4+ "strings"
5+
46 "github.com/databricks/cli/bundle/config/paths"
57 "github.com/databricks/databricks-sdk-go/marshal"
68 "github.com/databricks/databricks-sdk-go/service/pipelines"
9+ "github.com/imdario/mergo"
710)
811
912type Pipeline struct {
@@ -22,3 +25,50 @@ func (s *Pipeline) UnmarshalJSON(b []byte) error {
2225func (s Pipeline ) MarshalJSON () ([]byte , error ) {
2326 return marshal .Marshal (s )
2427}
28+
29+ // MergeClusters merges cluster definitions with same label.
30+ // The clusters field is a slice, and as such, overrides are appended to it.
31+ // We can identify a cluster by its label, however, so we can use this label
32+ // to figure out which definitions are actually overrides and merge them.
33+ //
34+ // Note: the cluster label is optional and defaults to 'default'.
35+ // We therefore ALSO merge all clusters without a label.
36+ func (p * Pipeline ) MergeClusters () error {
37+ clusters := make (map [string ]* pipelines.PipelineCluster )
38+ output := make ([]pipelines.PipelineCluster , 0 , len (p .Clusters ))
39+
40+ // Normalize cluster labels.
41+ // If empty, this defaults to "default".
42+ // To make matching case insensitive, labels are lowercased.
43+ for i := range p .Clusters {
44+ label := p .Clusters [i ].Label
45+ if label == "" {
46+ label = "default"
47+ }
48+ p .Clusters [i ].Label = strings .ToLower (label )
49+ }
50+
51+ // Target overrides are always appended, so we can iterate in natural order to
52+ // first find the base definition, and merge instances we encounter later.
53+ for i := range p .Clusters {
54+ label := p .Clusters [i ].Label
55+
56+ // Register pipeline cluster with label if not yet seen before.
57+ ref , ok := clusters [label ]
58+ if ! ok {
59+ output = append (output , p .Clusters [i ])
60+ clusters [label ] = & output [len (output )- 1 ]
61+ continue
62+ }
63+
64+ // Merge this instance into the reference.
65+ err := mergo .Merge (ref , & p .Clusters [i ], mergo .WithOverride , mergo .WithAppendSlice )
66+ if err != nil {
67+ return err
68+ }
69+ }
70+
71+ // Overwrite resulting slice.
72+ p .Clusters = output
73+ return nil
74+ }
0 commit comments