2022-03-10 06:59:48 +00:00
|
|
|
import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common';
|
2022-01-04 09:32:57 +00:00
|
|
|
import { Client as MinioClient, ClientOptions } from 'minio';
|
2022-12-06 10:10:19 +00:00
|
|
|
import {
|
|
|
|
|
getObject,
|
|
|
|
|
uploadObject,
|
|
|
|
|
listBuckets,
|
|
|
|
|
listObjects,
|
|
|
|
|
signedUrlForGet,
|
|
|
|
|
signedUrlForPut,
|
|
|
|
|
removeObject,
|
|
|
|
|
} from './operations';
|
2022-03-10 06:59:48 +00:00
|
|
|
import { SourceOptions, QueryOptions } from './types';
|
2022-01-17 07:08:17 +00:00
|
|
|
|
2022-01-04 09:32:57 +00:00
|
|
|
export default class MinioService implements QueryService {
|
2022-01-24 13:59:21 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, _dataSourceId: string): Promise<QueryResult> {
|
2022-01-04 09:32:57 +00:00
|
|
|
const operation = queryOptions.operation;
|
|
|
|
|
const minioClient = await this.getConnection(sourceOptions, { operation });
|
|
|
|
|
let result = {};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
switch (operation) {
|
|
|
|
|
case 'list_buckets':
|
|
|
|
|
result = await listBuckets(minioClient, queryOptions);
|
|
|
|
|
break;
|
|
|
|
|
case 'list_objects':
|
|
|
|
|
result = await listObjects(minioClient, queryOptions);
|
|
|
|
|
break;
|
|
|
|
|
case 'get_object':
|
|
|
|
|
result = await getObject(minioClient, queryOptions);
|
|
|
|
|
break;
|
|
|
|
|
case 'put_object':
|
|
|
|
|
result = await uploadObject(minioClient, queryOptions);
|
|
|
|
|
break;
|
|
|
|
|
case 'signed_url_for_get':
|
|
|
|
|
result = await signedUrlForGet(minioClient, queryOptions);
|
|
|
|
|
break;
|
|
|
|
|
case 'signed_url_for_put':
|
|
|
|
|
result = await signedUrlForPut(minioClient, queryOptions);
|
|
|
|
|
break;
|
2022-12-06 10:10:19 +00:00
|
|
|
case 'remove_object':
|
|
|
|
|
result = await removeObject(minioClient, queryOptions);
|
|
|
|
|
break;
|
2022-01-04 09:32:57 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
|
|
|
|
data: result,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
2022-01-04 09:32:57 +00:00
|
|
|
const minioClient: MinioClient = await this.getConnection(sourceOptions);
|
|
|
|
|
await minioClient.listBuckets();
|
|
|
|
|
|
|
|
|
|
return { status: 'ok' };
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async getConnection(sourceOptions: SourceOptions, options?: object): Promise<any> {
|
2022-01-04 09:32:57 +00:00
|
|
|
const credentials: ClientOptions = {
|
|
|
|
|
endPoint: sourceOptions['host'],
|
2023-12-21 06:25:35 +00:00
|
|
|
port: +sourceOptions['port'],
|
|
|
|
|
useSSL: !!sourceOptions['ssl_enabled'],
|
2022-01-04 09:32:57 +00:00
|
|
|
accessKey: sourceOptions['access_key'],
|
|
|
|
|
secretKey: sourceOptions['secret_key'],
|
|
|
|
|
};
|
|
|
|
|
return new MinioClient(credentials);
|
|
|
|
|
}
|
2022-03-10 06:59:48 +00:00
|
|
|
}
|