-
Notifications
You must be signed in to change notification settings - Fork 0
/
databricks_service.go
63 lines (53 loc) · 2.05 KB
/
databricks_service.go
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
63
package smile_databricks_gateway
import (
"context"
"fmt"
"time"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/service/pipelines"
)
type DatabricksService struct {
wClient *databricks.WorkspaceClient
smileDLTPipeName string
}
func NewDatabricksService(dbInstance, token, smileDLTPipeName string) (*DatabricksService, error) {
w, err := databricks.NewWorkspaceClient(&databricks.Config{
Host: fmt.Sprintf("https://%s", dbInstance),
Token: token,
Credentials: config.PatCredentials{},
})
if err != nil {
return nil, fmt.Errorf("Cannot create a databricks workspace client: %v", err)
}
return &DatabricksService{wClient: w, smileDLTPipeName: smileDLTPipeName}, nil
}
func (d *DatabricksService) ExecutePipeline(ctx context.Context) error {
pipelineId, err := d.getPipelineIdByName(ctx)
if err != nil {
return fmt.Errorf("Failed to get SMILE DLT Pipeline id: '%s': %q", d.smileDLTPipeName, err)
}
if _, err = d.wClient.Pipelines.WaitGetPipelineIdle(ctx, pipelineId, 15*time.Minute, nil); err != nil {
return fmt.Errorf("Error waiting for pipeline to get in idle state: '%s': %q", d.smileDLTPipeName, err)
}
_, err = d.wClient.Pipelines.StartUpdate(ctx, pipelines.StartUpdate{PipelineId: pipelineId})
if err != nil {
return fmt.Errorf("Failed to run SMILE DLT Pipeline id: '%s': %q", d.smileDLTPipeName, err)
}
if _, err := d.wClient.Pipelines.WaitGetPipelineRunning(ctx, pipelineId, 15*time.Minute, nil); err != nil {
return fmt.Errorf("Error waiting for pipeline to start running: '%s': %q", d.smileDLTPipeName, err)
}
return nil
}
func (d *DatabricksService) getPipelineIdByName(ctx context.Context) (string, error) {
pipelines, err := d.wClient.Pipelines.ListPipelinesAll(ctx, pipelines.ListPipelinesRequest{})
if err != nil {
return "", err
}
for _, pipeline := range pipelines {
if pipeline.Name == d.smileDLTPipeName {
return pipeline.PipelineId, nil
}
}
return "", fmt.Errorf("pipeline '%s' not found", d.smileDLTPipeName)
}