|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "github.com/databrickslabs/databricks-terraform/client/model" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +// TokensAPI exposes the Secrets API |
| 12 | +type DBFSAPI struct { |
| 13 | + Client DBApiClient |
| 14 | +} |
| 15 | + |
| 16 | +func (a DBFSAPI) init(client DBApiClient) DBFSAPI { |
| 17 | + a.Client = client |
| 18 | + return a |
| 19 | +} |
| 20 | + |
| 21 | +func (a DBFSAPI) Create(path string, overwrite bool, data string) error { |
| 22 | + byteArr, err := base64.StdEncoding.DecodeString(data) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + byteChunks := split(byteArr, 1e6) |
| 27 | + handle, err := a.createHandle(path, overwrite) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + for _, byteChunk := range byteChunks { |
| 32 | + b64Data := base64.StdEncoding.EncodeToString(byteChunk) |
| 33 | + err := a.addBlock(b64Data, handle) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + } |
| 38 | + err = a.closeHandle(handle) |
| 39 | + return err |
| 40 | +} |
| 41 | + |
| 42 | +func (a DBFSAPI) Read(path string) (string, error) { |
| 43 | + var bytesFetched []byte |
| 44 | + fetchLoop := true |
| 45 | + offSet := int64(0) |
| 46 | + length := int64(1e6) |
| 47 | + for fetchLoop == true { |
| 48 | + bytesRead, bytes, err := a.read(path, offSet, length) |
| 49 | + if err != nil { |
| 50 | + return "", err |
| 51 | + } |
| 52 | + log.Println(bytesRead) |
| 53 | + if bytesRead == 0 || bytesRead < length { |
| 54 | + fetchLoop = false |
| 55 | + } |
| 56 | + |
| 57 | + bytesFetched = append(bytesFetched, bytes...) |
| 58 | + offSet = offSet + length |
| 59 | + } |
| 60 | + resp := base64.StdEncoding.EncodeToString(bytesFetched) |
| 61 | + return resp, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (a DBFSAPI) Delete(path string, recursive bool) error { |
| 65 | + deleteRequest := struct { |
| 66 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 67 | + Recursive bool `json:"recursive,omitempty" url:"recursive,omitempty"` |
| 68 | + }{ |
| 69 | + Path: path, |
| 70 | + Recursive: recursive, |
| 71 | + } |
| 72 | + _, err := a.Client.performQuery(http.MethodPost, "/dbfs/delete", "2.0", nil, deleteRequest) |
| 73 | + |
| 74 | + return err |
| 75 | +} |
| 76 | + |
| 77 | +func (a DBFSAPI) read(path string, offset, length int64) (int64, []byte, error) { |
| 78 | + var readBytes struct { |
| 79 | + BytesRead int64 `json:"bytes_read,omitempty" url:"bytes_read,omitempty"` |
| 80 | + Data string `json:"data,omitempty" url:"data,omitempty"` |
| 81 | + } |
| 82 | + readRequest := struct { |
| 83 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 84 | + Offset int64 `json:"offset,omitempty" url:"offset,omitempty"` |
| 85 | + Length int64 `json:"length,omitempty" url:"length,omitempty"` |
| 86 | + }{ |
| 87 | + Path: path, |
| 88 | + Offset: offset, |
| 89 | + Length: length, |
| 90 | + } |
| 91 | + resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/read", "2.0", nil, readRequest) |
| 92 | + if err != nil { |
| 93 | + return readBytes.BytesRead, []byte{}, err |
| 94 | + } |
| 95 | + err = json.Unmarshal(resp, &readBytes) |
| 96 | + if err != nil { |
| 97 | + return 0, []byte{}, err |
| 98 | + } |
| 99 | + dataBytes, err := base64.StdEncoding.DecodeString(readBytes.Data) |
| 100 | + return readBytes.BytesRead, dataBytes, err |
| 101 | +} |
| 102 | + |
| 103 | +func (a DBFSAPI) Status(path string) (model.FileInfo, error) { |
| 104 | + var fileInfo model.FileInfo |
| 105 | + statusRequest := struct { |
| 106 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 107 | + }{ |
| 108 | + Path: path, |
| 109 | + } |
| 110 | + resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/get-status", "2.0", nil, statusRequest) |
| 111 | + if err != nil { |
| 112 | + return fileInfo, err |
| 113 | + } |
| 114 | + err = json.Unmarshal(resp, &fileInfo) |
| 115 | + return fileInfo, err |
| 116 | +} |
| 117 | + |
| 118 | +func (a DBFSAPI) List(path string, recursive bool) ([]model.FileInfo, error) { |
| 119 | + if recursive == true { |
| 120 | + var paths []model.FileInfo |
| 121 | + a.recursiveAddPaths(path, &paths) |
| 122 | + return paths, nil |
| 123 | + } else { |
| 124 | + return a.list(path) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func (a DBFSAPI) recursiveAddPaths(path string, pathList *[]model.FileInfo) { |
| 129 | + fileInfoList, _ := a.list(path) |
| 130 | + for _, v := range fileInfoList { |
| 131 | + if v.IsDir == false { |
| 132 | + *pathList = append(*pathList, v) |
| 133 | + } else if v.IsDir == true { |
| 134 | + a.recursiveAddPaths(v.Path, pathList) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func (a DBFSAPI) list(path string) ([]model.FileInfo, error) { |
| 140 | + var dbfsList struct { |
| 141 | + Files []model.FileInfo `json:"files,omitempty" url:"files,omitempty"` |
| 142 | + } |
| 143 | + listRequest := struct { |
| 144 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 145 | + }{} |
| 146 | + listRequest.Path = path |
| 147 | + |
| 148 | + resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/list", "2.0", nil, listRequest) |
| 149 | + if err != nil { |
| 150 | + return dbfsList.Files, err |
| 151 | + } |
| 152 | + |
| 153 | + err = json.Unmarshal(resp, &dbfsList) |
| 154 | + return dbfsList.Files, err |
| 155 | +} |
| 156 | + |
| 157 | +func (a DBFSAPI) Mkdirs(path string) error { |
| 158 | + mkDirsRequest := struct { |
| 159 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 160 | + }{} |
| 161 | + mkDirsRequest.Path = path |
| 162 | + |
| 163 | + _, err := a.Client.performQuery(http.MethodPost, "/dbfs/mkdirs", "2.0", nil, mkDirsRequest) |
| 164 | + |
| 165 | + return err |
| 166 | +} |
| 167 | + |
| 168 | +func (a DBFSAPI) createHandle(path string, overwrite bool) (int64, error) { |
| 169 | + var handle struct { |
| 170 | + Handle int64 `json:"handle,omitempty" url:"handle,omitempty"` |
| 171 | + } |
| 172 | + createDBFSHandleRequest := struct { |
| 173 | + Path string `json:"path,omitempty" url:"path,omitempty"` |
| 174 | + Overwrite bool `json:"overwrite,omitempty" url:"overwrite,omitempty"` |
| 175 | + }{ |
| 176 | + Path: path, |
| 177 | + Overwrite: overwrite, |
| 178 | + } |
| 179 | + |
| 180 | + resp, err := a.Client.performQuery(http.MethodPost, "/dbfs/create", "2.0", nil, createDBFSHandleRequest) |
| 181 | + if err != nil { |
| 182 | + return handle.Handle, err |
| 183 | + } |
| 184 | + |
| 185 | + err = json.Unmarshal(resp, &handle) |
| 186 | + return handle.Handle, err |
| 187 | +} |
| 188 | + |
| 189 | +func (a DBFSAPI) addBlock(data string, handle int64) error { |
| 190 | + var addDBFSBlockRequest = struct { |
| 191 | + Data string `json:"data,omitempty" url:"data,omitempty"` |
| 192 | + Handle int64 `json:"handle,omitempty" url:"handle,omitempty"` |
| 193 | + }{ |
| 194 | + Data: data, |
| 195 | + Handle: handle, |
| 196 | + } |
| 197 | + _, err := a.Client.performQuery(http.MethodPost, "/dbfs/add-block", "2.0", nil, addDBFSBlockRequest) |
| 198 | + return err |
| 199 | +} |
| 200 | + |
| 201 | +func (a DBFSAPI) closeHandle(handle int64) error { |
| 202 | + closeHandleRequest := struct { |
| 203 | + Handle int64 `json:"handle,omitempty" url:"handle,omitempty"` |
| 204 | + }{ |
| 205 | + Handle: handle, |
| 206 | + } |
| 207 | + |
| 208 | + _, err := a.Client.performQuery(http.MethodPost, "/dbfs/close", "2.0", nil, closeHandleRequest) |
| 209 | + return err |
| 210 | +} |
| 211 | + |
| 212 | +func split(buf []byte, lim int) [][]byte { |
| 213 | + var chunk []byte |
| 214 | + chunks := make([][]byte, 0, len(buf)/lim+1) |
| 215 | + for len(buf) >= lim { |
| 216 | + chunk, buf = buf[:lim], buf[lim:] |
| 217 | + chunks = append(chunks, chunk) |
| 218 | + } |
| 219 | + if len(buf) > 0 { |
| 220 | + chunks = append(chunks, buf[:len(buf)]) |
| 221 | + } |
| 222 | + return chunks |
| 223 | +} |
0 commit comments