-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update google_cloud_function implementation as per custom desti…
…nation (#2652) * feat: update google_cloud_function implementation as per custom destination flow * fix: remove old test suit
- Loading branch information
Showing
8 changed files
with
850 additions
and
312 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,47 @@ | ||
const { defaultBatchRequestConfig } = require('../../util'); | ||
const { JSON_MIME_TYPE } = require('../../util/constant'); | ||
const { ConfigurationError } = require('../../util/errorTypes'); | ||
|
||
const { TRIGGERTYPE } = require('./config'); | ||
|
||
/** | ||
* validate destination config | ||
* @param {*} param0 | ||
*/ | ||
const validateDestinationConfig = ({ Config }) => { | ||
// throw error if google Cloud Function is not provided | ||
// throw error if google Cloud Function URL is not provided | ||
if (!Config.googleCloudFunctionUrl) { | ||
throw new ConfigurationError('[GCF]:: Url not found. Aborting'); | ||
} | ||
if ( | ||
Config.triggerType === 'https' && // for triggerType https gcloud Authorization is mandatory | ||
!Config.gcloudAuthorization | ||
) { | ||
throw new ConfigurationError('[GCF]:: Access Token not found. Aborting'); | ||
if (Config.requireAuthentication && !Config.credentials) { | ||
throw new ConfigurationError( | ||
'[GCF]:: Service Account credentials are required if your function required authentication. Aborting', | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
* add headers in the payload that is provided in destination config | ||
* @param {*} response | ||
* @param {*} Config | ||
*/ | ||
function addHeader(response, Config) { | ||
const { triggerType, apiKeyId, gcloudAuthorization } = Config; | ||
|
||
response.headers = { 'content-type': JSON_MIME_TYPE }; | ||
if (apiKeyId) { | ||
const basicAuth = Buffer.from(`apiKey:${apiKeyId}`).toString('base64'); | ||
response.headers.ApiKey = `Basic ${basicAuth}`; | ||
} | ||
if (TRIGGERTYPE.HTTPS === triggerType.toLowerCase()) { | ||
response.headers.Authorization = `bearer ${gcloudAuthorization}`; | ||
} | ||
} | ||
|
||
/** | ||
* Create GoogleCloudFunction Batch payload based on the passed events | ||
* @param {*} events | ||
* @returns | ||
*/ | ||
|
||
function generateBatchedPayload(events) { | ||
const batchResponseList = []; | ||
const metadata = []; | ||
// extracting destination | ||
// from the first event in a batch | ||
const { destination } = events[0]; | ||
const { googleCloudFunctionUrl } = destination.Config; | ||
let batchEventResponse = defaultBatchRequestConfig(); | ||
let batchEventResponse = events.map((event) => event.message); | ||
// Batch event into dest batch structure | ||
events.forEach((ev) => { | ||
batchResponseList.push(ev.message.body.JSON); | ||
// batchResponseList.push(ev.message.body.JSON); | ||
metadata.push(ev.metadata); | ||
}); | ||
batchEventResponse.batchedRequest.body.JSON_ARRAY = { | ||
batch: JSON.stringify(batchResponseList), | ||
}; | ||
batchEventResponse.batchedRequest.endpoint = googleCloudFunctionUrl; | ||
addHeader(batchEventResponse.batchedRequest, destination.Config); | ||
batchEventResponse = { | ||
...batchEventResponse, | ||
metadata, | ||
message: batchEventResponse, | ||
destination, | ||
metadata, | ||
}; | ||
return batchEventResponse; | ||
} | ||
|
||
module.exports = { | ||
validateDestinationConfig, | ||
addHeader, | ||
generateBatchedPayload, | ||
}; |
170 changes: 0 additions & 170 deletions
170
test/__tests__/data/google_cloud_function_router_input.json
This file was deleted.
Oops, something went wrong.
55 changes: 0 additions & 55 deletions
55
test/__tests__/data/google_cloud_function_router_output.json
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.