github.com/databricks/cli@v0.203.0/bundle/config/mutator/override_compute.go (about)

     1  package mutator
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/databricks/cli/bundle"
     9  	"github.com/databricks/cli/bundle/config"
    10  	"github.com/databricks/cli/bundle/config/resources"
    11  )
    12  
    13  type overrideCompute struct{}
    14  
    15  func OverrideCompute() bundle.Mutator {
    16  	return &overrideCompute{}
    17  }
    18  
    19  func (m *overrideCompute) Name() string {
    20  	return "OverrideCompute"
    21  }
    22  
    23  func overrideJobCompute(j *resources.Job, compute string) {
    24  	for i := range j.Tasks {
    25  		task := &j.Tasks[i]
    26  		if task.NewCluster != nil {
    27  			task.NewCluster = nil
    28  			task.ExistingClusterId = compute
    29  		} else if task.ExistingClusterId != "" {
    30  			task.ExistingClusterId = compute
    31  		}
    32  	}
    33  }
    34  
    35  func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) error {
    36  	if b.Config.Bundle.Mode != config.Development {
    37  		if b.Config.Bundle.ComputeID != "" {
    38  			return fmt.Errorf("cannot override compute for an environment that does not use 'mode: development'")
    39  		}
    40  		return nil
    41  	}
    42  	if os.Getenv("DATABRICKS_CLUSTER_ID") != "" {
    43  		b.Config.Bundle.ComputeID = os.Getenv("DATABRICKS_CLUSTER_ID")
    44  	}
    45  
    46  	if b.Config.Bundle.ComputeID == "" {
    47  		return nil
    48  	}
    49  
    50  	r := b.Config.Resources
    51  	for i := range r.Jobs {
    52  		overrideJobCompute(r.Jobs[i], b.Config.Bundle.ComputeID)
    53  	}
    54  
    55  	return nil
    56  }