Skip to content

Commit

Permalink
delete registrations not part of config on --force-events flag enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sangeetha5491 committed Jul 13, 2023
1 parent 3090d75 commit 17af58e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/hooks/pre-deploy-event-reg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
JOURNAL, getDeliveryType
} = require('./utils/hook-utils')

module.exports = async function ({ appConfig, force }) {
module.exports = async function ({ appConfig }) {
if (appConfig && appConfig.events) {
const registrations = appConfig.events.registrations
for (const registrationName in registrations) {
Expand Down
60 changes: 51 additions & 9 deletions src/hooks/utils/hook-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ function getRegistrationNameToRegistrationsMap (eventRegistrations) {
return registrationNameToRegistrations
}

/**
* @private
* @param {Array.<string>} workspaceRegistrationNames Registration names from the Console workspace
* @param {Array.<string>} appConfigRegistrationNames Registration names defined in the app.config.yaml file
* @returns {Array.<string>} Registrations that are part of the workspace, but not part of the app.config.yaml
*/
function getWorkspaceRegistrationsToBeDeleted (workspaceRegistrationNames, appConfigRegistrationNames) {
return workspaceRegistrationNames.filter(wsRegistration => !appConfigRegistrationNames.includes(wsRegistration))
}

/**
* @private
* @param {object} body - Registration Create/Update Model
Expand All @@ -115,11 +125,25 @@ async function createOrUpdateRegistration (body, eventsSDK, existingRegistration
if (existingRegistration) {
const response = await eventsSDK.eventsClient.updateRegistration(eventsSDK.orgId, project.id, project.workspace.id,
existingRegistration.registration_id, body)
console.log('Updated registration:' + JSON.stringify(response))
console.log('Updated registration with name:' + response.name + ' and id:' + response.registration_id)
} else {
const response = await eventsSDK.eventsClient.createRegistration(eventsSDK.orgId, project.id,
project.workspace.id, body)
console.log('Created registration:' + JSON.stringify(response))
console.log('Created registration:' + response.name + ' and id:' + response.registration_id)
}
}

/**
* @private
* @param {object} eventsSDK - eventsSDK object containing eventsClient and orgId
* @param {object} existingRegistration - existing registration obtained from .aio config if exists
* @param {object} project - project details from .aio config file
*/
async function deleteRegistration (eventsSDK, existingRegistration, project) {
if (existingRegistration) {
await eventsSDK.eventsClient.deleteRegistration(eventsSDK.orgId, project.id, project.workspace.id,
existingRegistration.registration_id)
console.log('Deleted registration with name:' + existingRegistration.name + ' and id:' + existingRegistration.registration_id)
}
}

Expand Down Expand Up @@ -160,23 +184,24 @@ async function deployRegistration ({ appConfig: { events, project } }, expectedD
throw new Error(
`Events SDK could not be initialised correctly. Skipping event registration in ${hookType} hook`)
}
const registrations = events.registrations
const registrationsFromConfig = events.registrations
const providerMetadataToProviderIdMapping = getProviderMetadataToProviderIdMapping()
const existingRegistrations = await getAllRegistrationsForWorkspace(eventsSDK, project)
for (const registrationName in registrations) {
const deliveryType = getDeliveryType(registrations[registrationName])
const registrationsFromWorkspace = await getAllRegistrationsForWorkspace(eventsSDK, project)
for (const registrationName in registrationsFromConfig) {
const deliveryType = getDeliveryType(registrationsFromConfig[registrationName])
if (deliveryType === expectedDeliveryType) {
const body = {
name: registrationName,
client_id: eventsSDK.X_API_KEY,
description: registrations[registrationName].description,
description: registrationsFromConfig[registrationName].description,
delivery_type: deliveryType,
runtime_action: registrationsFromConfig[registrationName].runtime_action,
events_of_interest: getEventsOfInterestForRegistration(
registrations[registrationName], providerMetadataToProviderIdMapping)
registrationsFromConfig[registrationName], providerMetadataToProviderIdMapping)
}
try {
let existingRegistration
if (existingRegistrations && existingRegistrations[registrationName]) { existingRegistration = existingRegistrations[registrationName] }
if (registrationsFromWorkspace && registrationsFromWorkspace[registrationName]) { existingRegistration = registrationsFromWorkspace[registrationName] }
await createOrUpdateRegistration(body, eventsSDK,
existingRegistration, project)
} catch (e) {
Expand All @@ -186,6 +211,23 @@ async function deployRegistration ({ appConfig: { events, project } }, expectedD
}
}
}

if (forceEventsFlag) {
const registrationsToDeleted = getWorkspaceRegistrationsToBeDeleted(Object.keys(registrationsFromWorkspace), Object.keys(registrationsFromConfig))
console.log('The following registrations will be deleted: ', registrationsToDeleted)
for (const registrationName of registrationsToDeleted) {
try {
if (registrationsFromWorkspace && registrationsFromWorkspace[registrationName]) {
await deleteRegistration(eventsSDK,
registrationsFromWorkspace[registrationName], project)
}
} catch (e) {
throw new Error(
e + '\ncode:' + e.code + '\nDetails:' + JSON.stringify(
e.sdkDetails))
}
}
}
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions test/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ const hookDecodedEventRegistration1 = {
client_id: 'serviceApiKey',
description: 'Registration for IO Events 1',
delivery_type: 'webhook',
runtime_action: 'poc-event-1',
events_of_interest: [{
provider_id: 'providerId1',
event_code: 'eventCode1'
Expand Down

0 comments on commit 17af58e

Please sign in to comment.