Node JS Security Group Creation AWS - json

I have a JSON file that I'm calling into a file:
{
"SecurityGroups": [
{
"IpPermissionsEgress": [],
"Description": "My security group",
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 22,
"IpRanges": [
{
"CidrIp": "203.0.113.0/24"
}
],
"ToPort": 22,
"IpProtocol": "tcp",
"UserIdGroupPairs": []
}
],
"GroupName": "MySecurityGroup",
"VPCId": "123456789012",
"GroupId": "sg-903004f8",
}
]
}
My JSON file looks like this:
SyntaxError: /Users/testuser/Documents/testsecuritygroups.json: Unexpected token } in JSON at position 696
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (module.js:588:27)
The error group name and description is clearly there. Not sure why I'm getting these errors. This is what my code looks like:
'use strict';
process.env.AWS_PROFILE
var PropertiesReader = require('properties-reader');
var AWS = require('aws-sdk')
var properties = PropertiesReader('/Users/testuser/.aws/credentials');
AWS.config.update({
accessKeyId : properties.get('aws_access_key_id'),
secretAccessKey : properties.get('aws_secret_access_key'),
region : 'us-east-1'
})
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
// Load credentials and set region from JSON file
// Load in security group parameters
let securityParams = require('./securityParams.json');
let securityParamsJSON = JSON.stringify(securityParams);
module.exports = {
//Exports creation of Security Groups
createSecurityGroup: (req, res) => {
ec2.createSecurityGroup(securityParams[0].SecurityGroups[0], function(err, data) {
if (err) {
return (console.log("Error", err));
}
// Pass the Json as a parameter in this function
ec2.authorizeSecurityGroupIngress(securityParams.IpPermissions, function(err, data) {
if (err) {
res.serverError(err, err.stack);
} else {
res.ok(data);
console.log('Ingress Security Rules Created');
}
})
// Pass the Json as a parameter in this function
ec2.authorizeSecurityGroupEgress(securityParams.IpPermissionsEgress[0], function(err, data) {
if (err) {
res.serverError(err, err.stack);
} else {
res.ok(data);
console.log('Egress Security Rules Created');
}
})
})
}
}
module.exports.createSecurityGroup();
I'm just trying to create security groups in AWS through this script.

Related

Retrieving data from MongoDB ánd MySQL simultaneously

I am trying to retrieve data from my MongoDB database which stores chat conversations. This works fine and returns what I want. However, I only save userIDs in MongoDB, so I need to query profile picture, username etc from my MySQL database. I tried the following:
app.get('/api/retrieveAllChats', (req, res) => {
var Conversation = mongoose.model('Conversation', ConversationSchema);
var ChatMessage = mongoose.model('Message', ChatMessageSchema);
var userID = req.query.userID.toString()
var members = []
var conversationData = []
var retrieveAllChats = new Promise(function(resolve, reject) {
Conversation.aggregate([{ $match: { "members.uID": userID } }, { $lookup: { foreignField: "c_ID", from: "messages", localField: "_id", as: "messages" } }, { "$unwind": "$messages" }, { "$sort": { "messages.t": -1 } }, { "$group": { "_id": "$_id", "lastMessage": { "$first": "$messages" }, "allFields": { "$first": "$$ROOT" } } }, { "$replaceRoot": { "newRoot": { "$mergeObjects": [ "$allFields", { "lastMessage": "$lastMessage" } ] } } }, { "$project": { "messages": 0 } }], function (err, conversations) {
if (err) return handleError(err);
conversations.forEach((conversation, i) => {
return new Promise(function (resolveConversations, rejectConversations) {
var membersPromise = conversation.members.forEach((member, x) => {
return new Promise(function (resolveUserData, rejectUserData) {
getUserData(member["uID"], function(userData) {
members.push({userID: member["uID"], joinDate: member["j"], userName: userData["userName"], userDisplayName: userData["userDisplayName"], userVerified: userData["userVerified"], userProfilePicURL: userData["userProfilePicURL"]})
console.log("userData: ", userData)
conversations[i].members[x].userData = userData
conversationData = conversations
resolveUserData({userID: member["uID"], joinDate: member["j"], userName: userData["userName"], userDisplayName: userData["userDisplayName"], userVerified: userData["userVerified"], userProfilePicURL: userData["userProfilePicURL"]})
})
})
})
resolveConversations()
})
})
resolve()
})
}).catch(error => {
console.log(error)
res.json({ errorCode: 500 })
})
retrieveAllChats.then(function() {
res.header("Content-Type",'application/json');
res.send(JSON.stringify(conversationData, null, 4));
})
})
However, the conversationData array is always empty. So I need a way to resolve the retrieveAllChats promise and pass the data I added to the existing conversations object to return it with all information I need. Any ideas on how I can do this? (getUserData is a function to retrieve the MySQL data, this one works fine and returns what I want)
You are trying to do async operation inside forEach which wouldn't work. You need to either use for...of or Promise.all.
Also, you can make this code much cimpler by using .exec() at the end of running any query or aggregation as that is supported by mongoose. Something like this should work. Make sure you change your routte line to this to tell it is an async function
app.get("/api/retrieveAllChats", async (req, res) => {
core logic
const conversions = await Conversation.aggregate([{"$match": {"members.uID": userID}}, {"$lookup": {"foreignField": "c_ID", "from": "messages", "localField": "_id", "as": "messages"}}, {"$unwind": "$messages"}, {"$sort": {"messages.t": -1}}, {"$group": {"_id": "$_id", "lastMessage": {"$first": "$messages"}, "allFields": {"$first": "$$ROOT"}}}, {"$replaceRoot": {"newRoot": {"$mergeObjects": ["$allFields", {"lastMessage": "$lastMessage"}]}}}, {"$project": {"messages": 0}}]);
for(const conversation of conversations) {
for(const member of conversation.members) {
// add your promise call here and either await it or use then to get the promise value.
}
}

I want to get Affinity Interest from Google analytics api v4

I put the tracking code of google analytics in my web Application, now i want to get the Interests of the user from Google analytics API, I am using Nodejs, here is my request's code and the JSON response I get.
const dimensions_rows = [{
name: 'ga:interestAffinityCategory'
}, ];
const date_filters = [{
startDate: '7daysAgo',
endDate: 'today',
}];
const req = {
reportRequests: [{
viewId: viewId,
dateRanges: date_filters,
dimensions: dimensions_rows,
}],
};
analytics.reports.batchGet({
auth: oauth2Client,
requestBody: req
},
function(err, response) {
if (err) {
console.log('Failed to get Report');
console.log(err);
return;
}
// response.data is where the good stuff is located
console.log('Success - got something back from the Googlez');
console.log("responseee: ", JSON.stringify(response.data));
}
);
//JSON response
{
"reports": [{
"columnHeader": {
"dimensions": ["ga:interestAffinityCategory"],
"metricHeader": {
"metricHeaderEntries": [{
"name": "ga:visits",
"type": "INTEGER"
}]
}
},
"data": {
"totals": [{
"values": ["0"]
}]
}
}]
}

Insertion issue in Json Array object mongodb with Nodejs?

I am new to mongodb , I have below Json structure in mongodb ,
{
"_id" : ObjectId("59d62452a164b51d64b714c2"),
"folderName" : "Avinash 1234",
"tag" : "search",
"ismainFolder" : true,
"innerFolder" : [
{
"ismainFolder" : false,
"foldername" : "Test12",
"_id" : ObjectId("59d72246e66adf2cfcfdd6e6")
}
],
"innerFiles" : [
{
"filelocation" : "",
"isFolder" : false,
"filename" : "Penguins.jpg",
"_id" : ObjectId("59d7223de66adf2cfcfdd6e5")
},
{
"filelocation" : "",
"isFolder" : false,
"filename" : "Desert.jpg",
"_id" : ObjectId("59d72ff4e66adf2cfcfdd6ec")
},
{
"filelocation" : "",
"isFolder" : false,
"filename" : "Hydrangeas.jpg",
"_id" : ObjectId("59d731dfe66adf2cfcfdd6ed")
},
{
"filelocation" : "",
"isFolder" : false,
"filename" : "Chrysanthemum.jpg",
"_id" : ObjectId("59d73252e66adf2cfcfdd6ee")
}
],
"__v" : 0
}
For innerFiles array i need to insert the Tag field depending on the id ("_id" : ObjectId("59d7223de66adf2cfcfdd6e5")) . I used following nodeJs code but it adding as a new object . Please give me the solution .
exports.addTagForSearch = function (req, res, next) {
var tagDetails = req.body.tagDetails;
console.log("tagDetails", tagDetails);
console.log("tagDetails", tagDetails._id);
Repository.find({ _id: tagDetails._id, }, { innerFiles: { $elemMatch: { _id: tagDetails._id } } },function (err, response) {
$push: {
innerFiles: {
"tagName": tagDetails.tagname,
}
//"filelocation": tagDetails.filelocation
}
}, { upsert: true, new: true }, function (err, post) {
if (err) return next(err);
return res.status(200).json("success");
});
}
but above code inserting as a new object , Please give me solution please .
First I need to create a database for that I had a config.js file . Here is the code
module.exports = {
'secretKey': '12345-67890-09876-54321',
'mongoUrl' : 'mongodb://localhost:27017/innerFiles'
}
Next create a models folder and keep this order.js in it
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var folderSchema=new Schema({
ismainFolder:{
type:String,
//required:true,
default:''
},
foldername:{
type:String,
//required:true,
default:''
}
});
var innerSchema=new Schema({
filelocation:{
type:String,
//required:true,
default:''
},
isFolder:{
type:String,
//required:true,
default:''
},
filename:{
type:String,
//required:true,
default:''
}
});
var main= new Schema({
folderName:{type:String},
tag:{type:String},
ismainFolder:{type:String},
innerFolder:[folderSchema],
innerFiles:[innerSchema]
},{ strict: false });
var Order= mongoose.model('main', main);
// make this available to our Node applications
module.exports = Order;
Next create a routes folder and keep this orderRouter.js file in it
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Orders = require('../models/orders');
var app = express();
var orderRouter = express.Router();
orderRouter.use(bodyParser.json());
orderRouter.get('/get',function (req, res, next) {
Orders.find({}, function (err, order) {
if (err) throw err;
res.json(order);
});
})
orderRouter.post('/post',function (req, res, next) {
Orders.create(req.body, function (err, order) {
if (err) {
res.status(400).send('Bad request');
}
else{
console.log('order created!');
var id = order._id;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Added the order with id: ' + id);
}
});
})
orderRouter.get('/:orderId',function (req, res, next) {
Orders.findById(req.params.orderId, function (err, order) {
if (err) {
res.status(404).send('OrderId not found');
}
else{
res.json(order);
}
});
})
orderRouter.put('/addingField',function(req,res){
//var tagDetails = req.body;
console.log("tagDetails:"+req.body.subId);
console.log("tagname:"+req.body.tagname);
Orders.update(
{_id:req.body.mainId,'innerFiles._id':req.body.subId},
{$set:{'innerFiles.$.tagName':req.body.tagname}},
function (err, article) {
if (err) return console.log(err);
res.json(article);
});
});
app.use('/orders',orderRouter);
app.use(express.static(__dirname+'/public'));
module.exports = orderRouter;
Next create a app.js file this is the server code
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var config = require('./config');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
});
var orderRouter = require('./routes/orderRouter');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// passport config
app.use(passport.initialize());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/orders',orderRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
app.listen(3000,function(){
console.log("Server listening on 3000");
});
module.exports = app;
And run the server as node app.js.You can post data using this api http://localhost:3000/orders/post you need to use post method.Here is the sample input example for posting
{
"folderName" : "Avinash 1234",
"tag" : "search",
"ismainFolder" : "true",
"innerFolder" : [
{
"ismainFolder" : "false",
"foldername" : "Test12"
}
],
"innerFiles" : [
{
"filelocation" : "a",
"isFolder" : "false",
"filename" : "Penguins.jpg"
},
{
"filelocation" : "b",
"isFolder" : "false",
"filename" : "Desert.jpg"
},
{
"filelocation" : "c",
"isFolder" : "false",
"filename" : "Hydrangeas.jpg"
},
{
"filelocation" : "d",
"isFolder" : "false",
"filename" : "Chrysanthemum.jpg"
}
]
}
and here is the image for it
After posting data check that your data is stored in db or not.Here whatever the id I am giving in response is mainId . For that run this api http://localhost:3000/orders/get use get method for this. Collect the sub document id which is subId in our code.Sample Image for getting
After this here is the task of adding a new field to sub document for that use this api http://localhost:3000/orders/addingField and you need to use put method for this.Here is the input example
{
"mainId":"59dca6aff968a98478aaaa96",
"subId":"59dca6aff968a98478aaaa9a",
"tagname":"hello"
}
And Image for it
After completion of all these steps check into db.Here is the sample image for
it
That's it. Hope it helps.

How to get results from MySql DB using node.js MySQL and send them back to API.ai - DialogFlow

I am having issues retrieving and sending results from a MySql database to API.ai. The concrete question is how to wait for the results to be available, and then send the results in the Json object back to API.ai
This is what I have:
In the webhook or service, after receiving the Json request, I call a method:
if (action === 'get.data') {
// Call the callDBJokes method
callDB().then((output) => {
// Return the results to API.AI
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(output));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(error));
});
}
which calls the method callDB() where the database call is executed:
function callDB() {
return new Promise((resolve, reject) => {
try {
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "x",
database: 'y'
});
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (!error) {
let response = "The solution is: " + results[0].solution;
response = response.toString();
let output = {'speech': response, 'displayText': response};
console.log(output);
resolve(output);
} else {
let output = {'speech': 'Error. Query Failed.', 'displayText': 'Error. Query Failed.'};
console.log(output);
reject(output);
}
});
connection.end();
} catch (err) {
let output = {'speech': 'try-cacth block error', 'displayText': 'try-cacth block error'};
console.log(output);
reject(output);
}
}
);
}
I get a Json response in API.ai like:
{
"id": "5daf182b-009f-4c11-a654-f2c65caa415e",
"timestamp": "2017-08-29T07:24:39.709Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "get data",
"action": "get.data",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "location",
"parameters": {
"date": "",
"geo-city": "Perth",
"date.original": "",
"geo-city.original": "perth"
},
"lifespan": 2
},
{
"name": "smalltalkagentgeneral-followup",
"parameters": {},
"lifespan": 2
}
],
"metadata": {
"intentId": "4043ad70-289f-441c-9381-e82fdd9a9985",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 387,
"intentName": "smalltalk.agent.general"
},
**"fulfillment": {
"speech": "error",
"displayText": "error",
"messages": [
{
"type": 0,
"speech": "error"**
}
]
},
"score": 1
},
**"status": {
"code": 200,
"errorType": "success"**
},
"sessionId": "c326c828-aa47-490c-9ca0-37827a4e348a"
}
I am getting only the error message but not the result from the database. I read that it could be done using callbacks as well, but I could not figure it out yet. I can see that the database connection is working, because the logs of the connections shows the connection attempts.
Any help will be appreciated. Thanks.
Solved by declaring the var mysql = require('mysql'); as const mysql = require('mysql'); not inside the function, but before the exports.myfunction declaration. Working example code to get results from MySql DB using node.js MySQL, and send them back to API.ai is as follows:
'use strict';
const mysql = require('mysql');
exports.her_goes_your_function_name = (req, res) => { //add your function name
//Determine the required action
let action = req.body.result['action'];
if (action === 'get.data') {
// Call the callDBJokes method
callDB().then((output) => {
// Return the results of the weather API to API.AI
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(output));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(error));
});
}
};
function callDB() {
return new Promise((resolve, reject) => {
try {
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "your_user",
password: "your_pass",
database: "your_DB"
});
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (!error) {
let response = "The solution is: " + results[0].solution;
response = response.toString();
let output = {'speech': response, 'displayText': response};
console.log(output);
resolve(output);
} else {
let output = {'speech': 'Error. Query Failed.', 'displayText': 'Error. Query Failed.'};
console.log(output);
reject(output);
}
});
connection.end();
} catch (err) {
let output = {'speech': 'try-cacth block error', 'displayText': 'try-cacth block error'};
console.log(output);
reject(output);
}
}
);
}

Parse JSON in NodeJS

I am trying to fetch a JSON from a remote API using a simple NodeJS HTTP server (it uses request to make HTTP calls). When I run the HTTP response through some JSON validator, it says it's a valid JSON, but I'm not able to manage accessing keys or even keys which are arrays etc.
JSON looks like:
{
"submissions": [{
"token": "db7fa11f970f376cc17c5f8d1760ab98",
"created_at": "2017-03-02T13:01:35Z",
"saved_at": "2017-03-02T12:50:35Z",
"changed_at": "2017-03-02T12:50:35Z",
"email": "",
"data": {
"CompName": "",
"Name": "TestFirma01",
"MA_Name": "Robert Dotzlaff",
"CASFunction": "TestFunktion01",
"CreateDate": "02.03.2017",
"Street1": "TestStrasse",
"Zip1": "12345",
"Town1": "Berlin",
"PhoneFieldStr4": "07225919241",
"MailFieldStr1": "tes#mpl.de",
"Category1": [
"CRM"
],
"Category2": [
"mpl Finance"
],
"gwBranch": [
"B2B",
"B2C"
],
"ITDANZAHLMA": "<25",
"MPLUSERPOT": "<5",
"TurnOver": "<50.000",
"gwBranch_Product": "Maschinen",
"gwBranch_Solution": "Keine",
"Konkurenz": "Nein",
"MPLEINFUEHRUNG1": null,
"MPLEINFUEHRUNG2": [
"> 12 Monate"
],
"MPLEINFUEHRUNG3": "02.03.2017",
"MPLINFRASTRUKTUR1": [
"ERP"
],
"MPLINFRASTRUKTUR2": [
"Lotus"
],
"MPLINFRASTRUKTUR3": [
"RDP-Anbindung"
],
"MPLINTTHEMA1": [
"Projektmanagement",
"Vertrieb"
],
"MPLINTTHEMA2": [
"Auswertungen",
"Zeiterfassung"
],
"MPLINTTHEMA3": [
"Sonstiges"
],
"MPLSONSTIGEINFOS": "Es muss schnell sein",
"MPLKONKPRODUKT": "",
"ANSPR_TEAM": "Robert D",
"ANSPR_Entscheider": "Ptrick R",
"MPLENTSCHEIDUNG": "02.03.2017",
"ITDKLASSIFIZIERUNG": [
"sehr gut"
],
"NEXT_ACTION": [
"Testzugang"
]
},
"attachments": []
}]
}
NodeJS script as follows:
'use strict'
const Hapi = require('hapi');
const Express = require('express');
const Request = require('request');
const Vision = require('vision');
const Handlebars = require('handlebars');
const _ = require('lodash');
const LodashFilter = require('lodash.filter');
const LodashTake = require('lodash.take');
const JSONStream = require('JSONStream');
const jQuery = require('jsdom');
const server = new Hapi.Server();
server.connection({
host: '127.0.0.1',
port: 3000,
routes: {
cors: {
origin: ['*'],
additionalHeaders: ['X-API-KEY']
},
}
});
server.register(Vision, (err) => {
server.views({
engines: {
html: Handlebars
},
relativeTo: __dirname,
path: './views',
});
});
server.start((err) => {
if (err) {
throw err;
}
getJSON();
console.log(`Server running at: ${server.info.uri}`);
});
function getJSON() {
// URL and APIKEY ommitted for security reasons
var as_host = '';
var as_apiKey = ''
var as_endpoint = '/api/public/v1/projects/';
var as_projectId = '736';
var as_endpointExt = '/submissions';
var as_url = as_host + as_endpoint + as_projectId + as_endpointExt;
var options = {
url: as_url,
headers: {
'X-API-KEY': as_apiKey,
},
json: true
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var jsonString1 = JSON.stringify(body);
var jsonObject = JSON.parse(jsonString1);
console.log("BODY2: " + jsonObject);
}
}
Request(options, callback);
}
The console.log("BODY2: " + jsonObject); outputs BODY2: [object Object] which is not what I want. When I remove json: true from the options variable for request, it outputs the JSON (or at least what looks like one), but I am still unable to access key / value pairs in the JSON. I need to access especially the data part of the JSON which contains the relevant data sets that need to be handed over to a second remote REST API (accepts only a special format, which is why I may not simply hand the retrieved JSON over to the other API). I have already tried several solutions and read a lot of posts on here, none of them seems to work for me (JSON.parse(), JSONStream.parse(), _.get() etc). Any help is much appreciated!