|
| 1 | +package exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 10 | + sdk_workspace "github.com/databricks/databricks-sdk-go/service/workspace" |
| 11 | +) |
| 12 | + |
| 13 | +func ImportNotebook(ic *importContext, r *resource) error { |
| 14 | + ic.emitUserOrServicePrincipalForPath(r.ID, "/Users") |
| 15 | + resp, err := ic.workspaceClient.Workspace.Export(ic.Context, sdk_workspace.ExportRequest{ |
| 16 | + Path: r.ID, |
| 17 | + Format: sdk_workspace.ExportFormat(ic.notebooksFormat), |
| 18 | + }) |
| 19 | + if err != nil { |
| 20 | + if apierr.IsMissing(err) { |
| 21 | + ic.addIgnoredResource(fmt.Sprintf("databricks_notebook. path=%s", r.ID)) |
| 22 | + } |
| 23 | + return err |
| 24 | + } |
| 25 | + var fileExtension string |
| 26 | + if ic.notebooksFormat == "SOURCE" { |
| 27 | + language := r.Data.Get("language").(string) |
| 28 | + fileExtension = fileExtensionLanguageMapping[language] |
| 29 | + r.Data.Set("language", "") |
| 30 | + } else { |
| 31 | + fileExtension = fileExtensionFormatMapping[ic.notebooksFormat] |
| 32 | + } |
| 33 | + r.Data.Set("format", ic.notebooksFormat) |
| 34 | + objectId := r.Data.Get("object_id").(int) |
| 35 | + name := fileNameNormalizationRegex.ReplaceAllString(r.ID[1:], "_") + "_" + strconv.Itoa(objectId) + fileExtension |
| 36 | + content, _ := base64.StdEncoding.DecodeString(resp.Content) |
| 37 | + fileName, err := ic.saveFileIn("notebooks", name, []byte(content)) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + if ic.notebooksFormat == "SOURCE" && r.Data.Get("language").(string) == "PYTHON" { |
| 42 | + analyzeNotebook(ic, string(content)) |
| 43 | + } |
| 44 | + ic.emitPermissionsIfNotIgnored(r, fmt.Sprintf("/notebooks/%d", objectId), |
| 45 | + "notebook_"+ic.Importables["databricks_notebook"].Name(ic, r.Data)) |
| 46 | + // TODO: it's not completely correct condition - we need to make emit smarter - |
| 47 | + // emit only if permissions are different from their parent's permission. |
| 48 | + ic.emitWorkspaceObjectParentDirectory(r) |
| 49 | + return r.Data.Set("source", fileName) |
| 50 | +} |
| 51 | + |
| 52 | +func analyzeNotebook(ic *importContext, content string) { |
| 53 | + lines := strings.Split(content, "\n") |
| 54 | + for _, line := range lines { |
| 55 | + if strings.HasPrefix(strings.TrimSpace(line), "#") { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + if strings.HasPrefix(strings.TrimSpace(line), "%pip") && strings.Contains(line, " install ") { |
| 60 | + parts := strings.Fields(line) |
| 61 | + if len(parts) < 3 { |
| 62 | + continue |
| 63 | + } |
| 64 | + for _, part := range parts[2:] { |
| 65 | + if strings.HasPrefix(part, "-") || strings.Contains(part, "==") { |
| 66 | + continue |
| 67 | + } |
| 68 | + if strings.HasPrefix(part, "/dbfs/") { |
| 69 | + ic.Emit(&resource{ |
| 70 | + Resource: "databricks_dbfs_file", |
| 71 | + ID: strings.TrimPrefix(part, "/dbfs"), |
| 72 | + }) |
| 73 | + } else if strings.HasPrefix(part, "/Workspace/") { |
| 74 | + ic.Emit(&resource{ |
| 75 | + Resource: "databricks_workspace_file", |
| 76 | + ID: strings.TrimPrefix(part, "/Workspace"), |
| 77 | + }) |
| 78 | + } else if strings.HasPrefix(part, "/Volumes/") { |
| 79 | + ic.Emit(&resource{ |
| 80 | + Resource: "databricks_file", |
| 81 | + ID: part, |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments