-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathbind_resource.go
More file actions
62 lines (54 loc) · 1.84 KB
/
bind_resource.go
File metadata and controls
62 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package deployment
import (
"fmt"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/cmd/bundle/utils"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/logdiag"
"github.com/spf13/cobra"
)
// BindResource binds a bundle resource to an existing workspace resource.
// This function is shared between the bind command and generate commands with --bind flag.
func BindResource(cmd *cobra.Command, resourceKey, resourceId string, autoApprove, forceLock, skipInitContext bool) error {
b, stateDesc, err := utils.ProcessBundleRet(cmd, utils.ProcessOptions{
SkipInitContext: skipInitContext,
ReadState: true,
InitFunc: func(b *bundle.Bundle) {
b.Config.Bundle.Deployment.Lock.Force = forceLock
},
})
if err != nil {
return err
}
ctx := cmd.Context()
resource, err := b.Config.Resources.FindResourceByConfigKey(resourceKey)
if err != nil {
return err
}
w := b.WorkspaceClient()
exists, err := resource.Exists(ctx, w, resourceId)
if err != nil {
return fmt.Errorf("failed to fetch the resource, err: %w", err)
}
if !exists {
return fmt.Errorf("%s with an id '%s' is not found", resource.ResourceDescription().SingularName, resourceId)
}
tfName, ok := terraform.GroupToTerraformName[resource.ResourceDescription().PluralName]
if !ok {
tfName = resource.ResourceDescription().PluralName
}
phases.Bind(ctx, b, &terraform.BindOptions{
AutoApprove: autoApprove,
ResourceType: tfName,
ResourceKey: resourceKey,
ResourceId: resourceId,
}, stateDesc.Engine)
if logdiag.HasError(ctx) {
return root.ErrAlreadyPrinted
}
cmdio.LogString(ctx, fmt.Sprintf("Successfully bound %s with an id '%s'", resource.ResourceDescription().SingularName, resourceId))
return nil
}