Skip to content

Instantly share code, notes, and snippets.

@jasper07
Last active November 2, 2022 11:03
Show Gist options
  • Save jasper07/84f3dc545cc51b707802c0306feb0717 to your computer and use it in GitHub Desktop.
Save jasper07/84f3dc545cc51b707802c0306feb0717 to your computer and use it in GitHub Desktop.
alternative to tmsUpload - add a MTA file to BTP Cloud Transport Node
const FormData = require('form-data');
const fs = require('fs');
const qs = require('qs');
const axios = require('axios');
// const dotenv = require('dotenv');
// dotenv.config();
const TMS_API = 'https://transport-service-app-backend.ts.cfapps.ap10.hana.ondemand.com/v2';
const TOKEN_URL = 'https://secondphasetooling.authentication.ap10.hana.ondemand.com/oauth/token';
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const CTMS_NODE = { nodeName: "MTA_QA", nodeId: 206 }
const USER_NAME = process.env.USER_NAME;
const STATUS = { Initial: 'in' };
const MTA_PATH = process.argv[2]; //passed in via cmd line
console.log(USER_NAME);
const getBearerToken = async () => {
const encodedToken = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64');
try {
const res = await axios.request({
url: TOKEN_URL,
method: "post",
headers: {
'Content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + encodedToken
},
data: qs.stringify({
"grant_type": "client_credentials",
"client_id": CLIENT_ID
})
})
return res.data;
} catch (err) {
console.error(err);
}
}
const uploadMTA = async (namedUser, filePath) => {
const form = new FormData();
form.append('namedUser', namedUser);
form.append('file', fs.createReadStream(filePath));
try {
const { data } = await axios.request({
url: `${TMS_API}/files/upload`,
method: "post",
headers: {
...form.getHeaders()
},
data: form
});
return data;
} catch (err) {
console.error(err);
}
};
const getTransportsInNode = async (nodeId, status) => {
let url = `${TMS_API}/nodes/${nodeId}/transportRequests?status=${status}&nodeId=${nodeId}`;
try {
const { data } = await axios.get(url);
return data;
} catch (err) {
console.error(err);
}
};
const uploadFileToNode = async (uri, namedUser, nodeName) => {
let body = {
"nodeName": nodeName,
"contentType": "MTA",
"storageType": "FILE",
"entries": [
{
"uri": uri
}
],
"description": `upload via API for ${USER_NAME}`,
"namedUser": namedUser
}
let url = `${TMS_API}/nodes/upload`;
try {
const res = await axios.request({
url: url,
headers: {
'Content-type': 'application/json'
},
method: "post",
data: JSON.stringify(body)
});
return res.data;
} catch (err) {
console.error(err);
}
};
const run = async () => {
const { access_token } = await getBearerToken();
if (access_token) {
axios.defaults.headers.common = { 'Authorization': `Bearer ${access_token}` };
const { fileId, fileName } = await uploadMTA(USER_NAME, MTA_PATH);
const uploadFileToNodeResponse = await uploadFileToNode(fileId, USER_NAME, CTMS_NODE.nodeName);
//'hello-world_1.0.0.mtar' successfully uploaded to Node 'MTA_DEV (Id: 204)'
console.log(`'${fileName}' successfully uploaded to Node '${uploadFileToNodeResponse.queueEntries[0].nodeName} (Id: ${uploadFileToNodeResponse.queueEntries[0].nodeId})'`);
//verify request created and in queue
const { transportRequests } = await getTransportsInNode(CTMS_NODE.nodeId, STATUS.Initial);
const transportRequest = transportRequests.find(tr => tr.id === uploadFileToNodeResponse.transportRequestId);
//Tranpsort Request '591' created in Node 'MTA_DEV'
console.log(`Tranpsort Request '${transportRequest.id}' created in Node '${transportRequest.origin}'`);
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment