2024-01-04 21:04:42 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-01-04 21:04:42 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type resolveResourceReferences struct{}
|
|
|
|
|
|
|
|
func ResolveResourceReferences() bundle.Mutator {
|
|
|
|
return &resolveResourceReferences{}
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *resolveResourceReferences) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-01-04 21:04:42 +00:00
|
|
|
errs, errCtx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
for k := range b.Config.Variables {
|
|
|
|
v := b.Config.Variables[k]
|
|
|
|
if v == nil || v.Lookup == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.HasValue() {
|
|
|
|
log.Debugf(ctx, "Ignoring '%s' lookup for the variable '%s' because the value is set", v.Lookup, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
errs.Go(func() error {
|
|
|
|
id, err := v.Lookup.Resolve(errCtx, b.WorkspaceClient())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to resolve %s, err: %w", v.Lookup, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Set(id)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(errs.Wait())
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*resolveResourceReferences) Name() string {
|
|
|
|
return "ResolveResourceReferences"
|
|
|
|
}
|