Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

30 create oih delete action #33

Merged
merged 21 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## 1.1.0 (November 27, 2019)

* Add custom name for uploaded file

## 1.0.0 (October 8, 2019)

* Initial release
* Add custom port field instead of defaulting to 22
* Add more unit tests
* Fix integration test using `path.resolve` based on test machine
* Format repository according to Airbnb styling
# 1.1.0 (November 27, 2019)
## Actions
* Add Delete Object action
* Add custom name for uploaded file
# 1.0.0 (October 8, 2019)
* Initial release
* Add custom port field instead of defaulting to 22
* Add more unit tests
* Fix integration test using `path.resolve` based on test machine
* Format repository according to Airbnb styling
35 changes: 35 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@
}
}
}
},
"delete": {
"main": "./lib/actions/delete.js",
"title": "Delete files",
kirill-levitskiy marked this conversation as resolved.
Show resolved Hide resolved
"description": "Delete file by name in provided package",
kirill-levitskiy marked this conversation as resolved.
Show resolved Hide resolved
"fields": {
"directory": {
"viewClass": "TextFieldView",
"label": "Directory Name",
denyshld marked this conversation as resolved.
Show resolved Hide resolved
"required": true,
"placeholder": "/foo/bar"
}
},
"metadata": {
"in": {
"type": "object",
"properties": {
"filename": {
"title": "File Name",
"type": "string",
"required": true
}
}
},
"out": {
"type": "object",
"properties": {
"id": {
"title": "File Name",
"type": "string",
"required": true
}
}
}
}
}
},
"triggers": {
Expand Down
11 changes: 11 additions & 0 deletions lib/actions/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { SftpDelete } = require('../utils/deleteUtil');
const Sftp = require('../Sftp');

async function process(msg, cfg, snapshot = {}) {
const sftpClient = new Sftp(this.logger, cfg);
await sftpClient.connect();
const deleteAction = new SftpDelete(this.logger, sftpClient);
return deleteAction.process(msg, cfg, snapshot);
}

module.exports.process = process;
26 changes: 26 additions & 0 deletions lib/utils/deleteUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { DeleteById } = require('@elastic.io/oih-standard-library/lib/actions/delete');

class SftpDelete extends DeleteById {
constructor(logger, client) {
super(logger);
this.client = client;
}

// eslint-disable-next-line class-methods-use-this, no-unused-vars
getId(msg, cfg) {
return msg.body.filename;
}

async deleteObject(filename, cfg) {
const dir = cfg.directory.substring(cfg.directory.length - 1) === '/'
? cfg.directory.substring(0, cfg.directory.length - 1)
: cfg.directory;

const fullFilenameToDelete = `${dir}/${filename}`;
this.logger.info(`Deleting file by name: ${fullFilenameToDelete}`);
await this.client.delete(fullFilenameToDelete);
return filename;
}
}

exports.SftpDelete = SftpDelete;
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "BSD-2-Clause",
"dependencies": {
"@elastic.io/component-commons-library": "0.0.6",
"@elastic.io/oih-standard-library": "^1.0.0",
kirill-levitskiy marked this conversation as resolved.
Show resolved Hide resolved
"elasticio-node": "0.0.9",
"elasticio-rest-node": "1.2.3",
"elasticio-sailor-nodejs": "2.5.1",
Expand Down
50 changes: 50 additions & 0 deletions spec-integration/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { expect } = require('chai');
const EventEmitter = require('events');
const bunyan = require('bunyan');
const Sftp = require('../lib/Sftp');
const deleteAction = require('../lib/actions/delete');
const upload = require('../lib/actions/upload');
const read = require('../lib/triggers/read');
require('dotenv').config();
Expand Down Expand Up @@ -155,6 +156,55 @@ describe('SFTP integration test - upload then download', function () {
await sftp.rmdir(cfg.directory, false);
});

it('Uploads, read and deletes attachments with custom name', async () => {
const cfg = {
host,
username,
password,
port,
directory,
};
sftp = new Sftp(bunyan.createLogger({ name: 'dummy' }), cfg);
await sftp.connect();

await upload.process.call(new TestEmitter(), {
body: { filename: 'custom.svg' },
attachments: {
'logo.svg': {
url: 'https://app.elastic.io/img/logo.svg',
},
'logo2.svg': {
url: 'https://app.elastic.io/img/logo.svg',
},
},
}, cfg);

const receiver = new TestEmitter();
const msg = {};
await read.process.call(receiver, msg, cfg);
expect(receiver.data.length).to.equal(2);
expect(receiver.data[0].body.filename).to.equal('custom_logo.svg');
expect(receiver.data[0].body.size).to.equal(4379);
expect(receiver.data[1].body.filename).to.equal('custom_logo2.svg');
expect(receiver.data[1].body.size).to.equal(4379);

const logoFilename = (await sftp.list(`${cfg.directory}${PROCESSED_FOLDER_NAME}`))[0].name;
const logo2Filename = (await sftp.list(`${cfg.directory}${PROCESSED_FOLDER_NAME}`))[1].name;

const upgradedCfg = JSON.parse(JSON.stringify(cfg));
upgradedCfg.directory = `${cfg.directory}${PROCESSED_FOLDER_NAME}`;
const deleteResult = await deleteAction.process.call(receiver,
{ body: { filename: logoFilename } }, upgradedCfg);
const deleteResult2 = await deleteAction.process.call(receiver,
{ body: { filename: logo2Filename } }, upgradedCfg);

expect(deleteResult.body.id).to.equal(logoFilename);
expect(deleteResult2.body.id).to.equal(logo2Filename);

await sftp.rmdir(`${cfg.directory}${PROCESSED_FOLDER_NAME}`, false);
await sftp.rmdir(cfg.directory, false);
});

it('Uploads, reads, and filters files by pattern match', async () => {
const cfg = {
host,
Expand Down