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

HCK-8703: hckFetch #45

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions esbuild.package.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ esbuild
}),
addReleaseFlag(path.resolve(RELEASE_FOLDER_PATH, 'package.json')),
],
external: ['electron'],
})
.catch(() => process.exit(1));
53 changes: 35 additions & 18 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
const aws = require('aws-sdk');
const path = require('path');
const {
SchemasClient,
ListRegistriesCommand,
UpdateRegistryCommand,
DescribeRegistryCommand,
CreateRegistryCommand,
UpdateSchemaCommand,
CreateSchemaCommand,
} = require('@aws-sdk/client-schemas');
const { hckFetchAwsSdkHttpHandler } = require('@hackolade/fetch');
const validationHelper = require('./helpers/validationHelper');
const getInfo = require('./helpers/infoHelper');
const { getPaths } = require('./helpers/pathHelper');
Expand All @@ -7,10 +17,10 @@ const commonHelper = require('./helpers/commonHelper');
const { getServers } = require('./helpers/serversHelper');
const getExtensions = require('./helpers/extensionsHelper');
const { getRegistryCreateCLIStatement, getSchemaCreateCLIStatement } = require('./helpers/awsCLIHelpers/awsCLIHelper');
const { getApiStatements, getItemUpdateParamiters } = require('./helpers/awsCLIHelpers/applyToInstanceHelper');
const { getApiStatements, getItemUpdateParameters } = require('./helpers/awsCLIHelpers/applyToInstanceHelper');
const handleReferencePath = require('./helpers/handleReferencePath');
const mapJsonSchema = require('../reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema');
const path = require('path');
const { SCHEMAS_CLIENT_API_VERSION, NOT_FOUND_RESPONSE_CODE } = require('../shared/constants');

module.exports = {
generateModelScript(data, logger, cb) {
Expand Down Expand Up @@ -57,7 +67,7 @@ module.exports = {
};
const extensions = getExtensions(data.modelData[0].scopesExtensions);

const resultSchema = Object.assign({}, openApiSchema, extensions);
const resultSchema = { ...openApiSchema, ...extensions };
let schema = addCommentsSigns(JSON.stringify(resultSchema, null, 2), 'json');
schema = removeCommentLines(schema);

Expand Down Expand Up @@ -90,10 +100,7 @@ module.exports = {
}
},

async applyToInstance(data, logger, callback, app) {
const NOT_FOUND_RESPONSE_CODE = 'NotFoundException';
const BAD_REQUEST_RESPONSE_CODE = 'BadRequestException';
const NOTHING_TO_UPDATE_RESPONSE_MESSAGE = 'Invalid request. Please provide at least one field to update.';
async applyToInstance(data, logger, callback) {
if (!data.script) {
return callback({ message: 'Empty script' });
}
Expand All @@ -108,24 +115,26 @@ module.exports = {
if (registry) {
try {
if (registry.Description) {
await schemasInstance.updateRegistry(getItemUpdateParamiters(registry)).promise();
await schemasInstance.send(new UpdateRegistryCommand(getItemUpdateParameters(registry)));
} else {
await schemasInstance.describeRegistry({ RegistryName: registry.RegistryName }).promise();
await schemasInstance.send(
new DescribeRegistryCommand({ RegistryName: registry.RegistryName }),
);
}
} catch (err) {
if (err.code === NOT_FOUND_RESPONSE_CODE) {
await schemasInstance.createRegistry(registry).promise();
await schemasInstance.send(new CreateRegistryCommand(registry));
} else {
return callback(err);
}
}
}
if (schema) {
try {
await schemasInstance.updateSchema(getItemUpdateParamiters(schema)).promise();
await schemasInstance.send(new UpdateSchemaCommand(getItemUpdateParameters(schema)));
} catch (err) {
if (err.code === NOT_FOUND_RESPONSE_CODE) {
await schemasInstance.createSchema(schema).promise();
await schemasInstance.send(new CreateSchemaCommand(schema));
} else {
return callback(err);
}
Expand All @@ -141,7 +150,7 @@ module.exports = {
logger.log('info', connectionInfo, 'Test connection', connectionInfo.hiddenKeys);
const schemasInstance = getSchemasInstance(connectionInfo);
try {
await schemasInstance.listRegistries().promise();
await schemasInstance.send(new ListRegistriesCommand());
callback();
} catch (err) {
logger.log('error', { message: err.message, stack: err.stack, error: err }, 'Connection failed');
Expand Down Expand Up @@ -221,7 +230,7 @@ const removeCommentLines = scriptString => {
.split('\n')
.filter(line => !isCommentedLine.test(line))
.join('\n')
.replace(/(.*?),\s*(\}|])/g, '$1$2');
.replace(/(.*?),\s*([}\]])/g, '$1$2');
};

const buildAWSCLIScript = (modelMetadata, openAPISchema, targetScriptOptions = {}) => {
Expand All @@ -238,9 +247,17 @@ const buildAWSCLIScript = (modelMetadata, openAPISchema, targetScriptOptions = {
};

const getSchemasInstance = connectionInfo => {
const { accessKeyId, secretAccessKey, region, sessionToken } = connectionInfo;
aws.config.update({ accessKeyId, secretAccessKey, region, sessionToken });
return new aws.Schemas({ apiVersion: '2019-12-02' });
const { accessKeyId, secretAccessKey, region, sessionToken, queryRequestTimeout } = connectionInfo;
return new SchemasClient({
credentials: {
accessKeyId,
secretAccessKey,
sessionToken,
},
region,
apiVersion: SCHEMAS_CLIENT_API_VERSION,
requestHandler: hckFetchAwsSdkHttpHandler({ requestTimeout: queryRequestTimeout }),
});
};

const handleRefInContainers = (containers, externalDefinitions, resolveApiExternalRefs) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ const getApiStatements = script => {
return cliStatements.reduce((acc, statement) => {
const oneLineStatement = statement.replace(/\n/g, '');
if (registryRegexp.test(oneLineStatement)) {
const parsedInput = getStatementInput(registryRegexp, oneLineStatement);
acc.registry = parsedInput;
acc.registry = getStatementInput(registryRegexp, oneLineStatement);
} else if (schemaRegexp.test(oneLineStatement)) {
const parsedInput = getStatementInput(schemaRegexp, oneLineStatement);
acc.schema = parsedInput;
acc.schema = getStatementInput(schemaRegexp, oneLineStatement);
}
return acc;
}, {});
Expand All @@ -24,12 +22,12 @@ const getStatementInput = (regExp, statement) => {
return JSON.parse(jsonInput);
};

const getItemUpdateParamiters = data => {
const getItemUpdateParameters = data => {
const { Tags, ...parameters } = data;
return parameters;
};

module.exports = {
getApiStatements,
getItemUpdateParamiters,
getItemUpdateParameters,
};
Loading