|
| 1 | +import {join as joinPath, normalize as normalizePath, extname as pathExtension} from 'path'; |
| 2 | +import mkdirp from 'mkdirp'; |
| 3 | +import FileSystem from '../../common/file_system'; |
| 4 | +import config from '../../common/config'; |
| 5 | +import Logger from '../../common/logger'; |
| 6 | +import {TemplateNotFoundError, TemplateNotReadableError, TemplateNotWritableError, |
| 7 | + TemplatesFolderNotFoundError, TemplatesRootFolderNotCreatableError} from '../../common/errors/template_request_errors'; |
| 8 | + |
| 9 | +let logger = new Logger('TemplatesController'); |
| 10 | + |
| 11 | +export default class TemplatesController { |
| 12 | + constructor() { |
| 13 | + this._fileSystemController = new FileSystem(); |
| 14 | + this.templatesFolder = this._getTemplatesFolder(); |
| 15 | + } |
| 16 | + |
| 17 | + getTemplates(path) { |
| 18 | + const self = this; |
| 19 | + const fullPath = joinPath(self.templatesFolder, path); |
| 20 | + return new Promise(function (resolve, reject) { |
| 21 | + self._fileSystemController.readDirectory(fullPath) |
| 22 | + .then(function (directoryIndex) { |
| 23 | + |
| 24 | + directoryIndex.templates = directoryIndex.files.filter(function (fileName) { |
| 25 | + return pathExtension(fileName).toLowerCase() === '.yaml'; |
| 26 | + }).map(function (fileName) { |
| 27 | + return fileName.slice(0, -5); |
| 28 | + }); |
| 29 | + |
| 30 | + delete directoryIndex.files; |
| 31 | + resolve(directoryIndex); |
| 32 | + }) |
| 33 | + .catch(function (error) { |
| 34 | + |
| 35 | + // Check if the requested folder is the templates root folder |
| 36 | + if (normalizePath(self.templatesFolder) === fullPath) { |
| 37 | + |
| 38 | + // Try to create the root folder |
| 39 | + mkdirp(fullPath, function (error) { |
| 40 | + if (error) { |
| 41 | + reject(new TemplatesRootFolderNotCreatableError()); |
| 42 | + logger.warn(`The templates root folder (${fullPath}) couldn't be found nor could it be created by the file system.`); |
| 43 | + } else { |
| 44 | + resolve(self._fileSystemController.getEmptyDirectoryIndex()); |
| 45 | + } |
| 46 | + }); |
| 47 | + } else { |
| 48 | + logger.warn(`The requested folder (${fullPath}) couldn't be found / read by the server. Error:`, error); |
| 49 | + reject(new TemplatesFolderNotFoundError(path)); |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + template(id) { |
| 56 | + const self = this; |
| 57 | + return new Promise(function (resolve, reject) { |
| 58 | + self._findTemplate(id) |
| 59 | + .then(function (access) { |
| 60 | + console.log('template resolved'); |
| 61 | + resolve({ |
| 62 | + get: function () { |
| 63 | + if (access.read) { |
| 64 | + return self._getTemplate(id); |
| 65 | + } |
| 66 | + return self._getErrorPromise(new TemplateNotReadableError(id)); |
| 67 | + }, |
| 68 | + edit: function (body) { |
| 69 | + if (access.write) { |
| 70 | + return self._editTemplate(id, body); |
| 71 | + } |
| 72 | + return self._getErrorPromise(new TemplateNotWritableError(id)); |
| 73 | + }, |
| 74 | + delete: function () { |
| 75 | + return self._deleteTemplate(id); |
| 76 | + } |
| 77 | + }); |
| 78 | + }) |
| 79 | + .catch(function () { |
| 80 | + console.log('catched'); |
| 81 | + reject(new TemplateNotFoundError(id)); |
| 82 | + }); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + createTemplate(id, content) { |
| 87 | + return this._editTemplate(id, content); |
| 88 | + } |
| 89 | + |
| 90 | + _findTemplate(id) { |
| 91 | + let fileName = id + '.yaml'; |
| 92 | + const self = this; |
| 93 | + return new Promise(function (resolve, reject) { |
| 94 | + self._fileSystemController.fileExists(joinPath(self.templatesFolder, fileName)) |
| 95 | + .then(function (exists) { |
| 96 | + if (!exists) { |
| 97 | + reject(); |
| 98 | + } else { |
| 99 | + //TODO: Get real permissions |
| 100 | + //resolve(permissions); |
| 101 | + resolve({ |
| 102 | + read: true, |
| 103 | + write: true |
| 104 | + }); |
| 105 | + } |
| 106 | + }) |
| 107 | + .catch(function (error) { |
| 108 | + reject(error); |
| 109 | + }); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + _getTemplate(id) { |
| 114 | + const path = joinPath(this.templatesFolder, id + '.yaml'); |
| 115 | + return this._fileSystemController.readFile(path); |
| 116 | + } |
| 117 | + |
| 118 | + _editTemplate(id, body) { |
| 119 | + const path = joinPath(this.templatesFolder, id + '.yaml'); |
| 120 | + return this._fileSystemController.writeFile(path, body); |
| 121 | + } |
| 122 | + |
| 123 | + _deleteTemplate(id) { |
| 124 | + const path = joinPath(this.templatesFolder, id + '.yaml'); |
| 125 | + return this._fileSystemController.deleteFile(path); |
| 126 | + } |
| 127 | + |
| 128 | + _getErrorPromise(error) { |
| 129 | + return new Promise(function (resolve, reject) { |
| 130 | + reject(error); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + _getTemplatesFolder() { |
| 135 | + const templateFolderSettings = config.get('templatesPath'); |
| 136 | + |
| 137 | + if (templateFolderSettings.relative) { |
| 138 | + return joinPath(config.get('elastalertPath'), templateFolderSettings.path); |
| 139 | + } else { |
| 140 | + return templateFolderSettings.path; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments