diff --git a/kubernetes.js b/kubernetes.js index d435823..9d3b32d 100644 --- a/kubernetes.js +++ b/kubernetes.js @@ -1,4 +1,5 @@ const got = require('got') +const FormData = require('form-data') const k8s = require('@kubernetes/client-node') const _ = require('lodash') const awsEFS = require('./lib/aws-efs.js') @@ -498,6 +499,11 @@ const getEndpoints = async (project) => { } } +const getStaticFileUrl = async (instance, filePath) => { + const prefix = instance.safeName.match(/^[0-9]/) ? 'srv-' : '' + return `http://${prefix}${instance.safeName}.${this._namespace}:2880/flowforge/files/_/${encodeURIComponent(filePath)}` +} + module.exports = { /** * Initialises this driver @@ -1117,5 +1123,64 @@ module.exports = { } return properties + }, + + // Static Assets API + listFiles: async (instance, filePath) => { + const fileUrl = await getStaticFileUrl(instance, filePath) + console.log(fileUrl) + try { + return got.get(fileUrl).json() + } catch (err) { + console.log(err) + err.statusCode = err.response.statusCode + throw err + } + }, + + updateFile: async (instance, filePath, update) => { + const fileUrl = await getStaticFileUrl(instance, filePath) + try { + return got.put(fileUrl, { + json: update + }) + } catch (err) { + err.statusCode = err.response.statusCode + throw err + } + }, + + deleteFile: async (instance, filePath) => { + const fileUrl = await getStaticFileUrl(instance, filePath) + try { + return got.delete(fileUrl) + } catch (err) { + err.statusCode = err.response.statusCode + throw err + } + }, + createDirectory: async (instance, filePath, directoryName) => { + const fileUrl = await getStaticFileUrl(instance, filePath) + try { + return got.post(fileUrl, { + json: { path: directoryName } + }) + } catch (err) { + err.statusCode = err.response.statusCode + throw err + } + }, + uploadFile: async (instance, filePath, fileBuffer) => { + const form = new FormData() + form.append('file', fileBuffer, { filename: filePath }) + const fileUrl = await getStaticFileUrl(instance, filePath) + try { + return got.post(fileUrl, { + body: form + }) + } catch (err) { + err.statusCode = err.response.statusCode + throw err + } } } diff --git a/package-lock.json b/package-lock.json index f8bb377..2e05e06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@aws-sdk/client-efs": "^3.600.0", "@kubernetes/client-node": "^0.18.1", + "form-data": "^4.0.0", "got": "^11.8.0", "lodash": "^4.17.21" }, @@ -1408,6 +1409,19 @@ "form-data": "^2.5.0" } }, + "node_modules/@types/request/node_modules/form-data": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", + "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 0.12" + } + }, "node_modules/@types/responselike": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/@types/responselike/-/responselike-1.0.0.tgz", @@ -2529,16 +2543,16 @@ } }, "node_modules/form-data": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", - "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "dependencies": { "asynckit": "^0.4.0", - "combined-stream": "^1.0.6", + "combined-stream": "^1.0.8", "mime-types": "^2.1.12" }, "engines": { - "node": ">= 0.12" + "node": ">= 6" } }, "node_modules/fs-minipass": { @@ -5543,6 +5557,18 @@ "@types/node": "*", "@types/tough-cookie": "*", "form-data": "^2.5.0" + }, + "dependencies": { + "form-data": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", + "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + } + } } }, "@types/responselike": { @@ -6381,12 +6407,12 @@ "integrity": "sha512-j0KLYPhm6zeac4lz3oJ3o65qvgQCcPubiyotZrXqEaG4hNagNYO8qdlUrX5vwqv9ohqeT/Z3j6+yW067yWWdUw==" }, "form-data": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", - "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "requires": { "asynckit": "^0.4.0", - "combined-stream": "^1.0.6", + "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, diff --git a/package.json b/package.json index f2d2fe2..410c74b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dependencies": { "@aws-sdk/client-efs": "^3.600.0", "@kubernetes/client-node": "^0.18.1", + "form-data": "^4.0.0", "got": "^11.8.0", "lodash": "^4.17.21" },