github.com/databricks/cli@v0.203.0/bundle/run/keys.go (about)

     1  package run
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/databricks/cli/bundle"
     7  )
     8  
     9  // RunnerLookup maps identifiers to a list of workloads that match that identifier.
    10  // The list can have more than 1 entry if resources of different types use the
    11  // same key. When this happens, the user should disambiguate between them.
    12  type RunnerLookup map[string][]Runner
    13  
    14  // ResourceKeys computes a map with
    15  func ResourceKeys(b *bundle.Bundle) (keyOnly RunnerLookup, keyWithType RunnerLookup) {
    16  	keyOnly = make(RunnerLookup)
    17  	keyWithType = make(RunnerLookup)
    18  
    19  	r := b.Config.Resources
    20  	for k, v := range r.Jobs {
    21  		kt := fmt.Sprintf("jobs.%s", k)
    22  		w := jobRunner{key: key(kt), bundle: b, job: v}
    23  		keyOnly[k] = append(keyOnly[k], &w)
    24  		keyWithType[kt] = append(keyWithType[kt], &w)
    25  	}
    26  	for k, v := range r.Pipelines {
    27  		kt := fmt.Sprintf("pipelines.%s", k)
    28  		w := pipelineRunner{key: key(kt), bundle: b, pipeline: v}
    29  		keyOnly[k] = append(keyOnly[k], &w)
    30  		keyWithType[kt] = append(keyWithType[kt], &w)
    31  	}
    32  	return
    33  }
    34  
    35  // ResourceCompletions returns a list of keys that unambiguously reference resources in the bundle.
    36  func ResourceCompletions(b *bundle.Bundle) []string {
    37  	seen := make(map[string]bool)
    38  	comps := []string{}
    39  	keyOnly, keyWithType := ResourceKeys(b)
    40  
    41  	// First add resources that can be identified by key alone.
    42  	for k, v := range keyOnly {
    43  		// Invariant: len(v) >= 1. See [ResourceKeys].
    44  		if len(v) == 1 {
    45  			seen[v[0].Key()] = true
    46  			comps = append(comps, k)
    47  		}
    48  	}
    49  
    50  	// Then add resources that can only be identified by their type and key.
    51  	for k, v := range keyWithType {
    52  		// Invariant: len(v) == 1. See [ResourceKeys].
    53  		_, ok := seen[v[0].Key()]
    54  		if ok {
    55  			continue
    56  		}
    57  		comps = append(comps, k)
    58  	}
    59  
    60  	return comps
    61  }