Create Event Definitions
Here's a PowerShell script template to create/update event definitions. Adapt as needed.
Template eventdefs.json file referenced by the script: eventdefs.json. Include all event definitions for the product in this file.
#Powershell script to create/update event definitions for a product in AIP
#All the event definitions are expected to be in the eventdefs.json file in the same path as this script file
#Incorporate this script in the ci/cd pipeline to keep all the event definitions current
#IAM service account - store client secret in a secure vault
$clientId = ""
$secret = ""
# for staging/dev
$tokenUrl = "https://apteaniamapp-dev.apteancloud.dev/realms/aptean-staging/protocol/openid-connect/token"
# for production
# $tokenUrl = "https://apteaniamapp.apteancloud.com/realms/aptean/protocol/openid-connect/token"
#use the product id from AIP for which the event definitions are to be created/updated
$productId = ""
$aipEventUrl = "https://stg.integration-graph.apteansharedservices.com/v2/event-definitions"
# for production
# $aipEventUrl = "https://integration-graph.apteansharedservices.com/v2/event-definitions"
function Request-Token {
$parameters = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $secret
}
$response = Invoke-RestMethod -Uri $tokenURL -Method Post -Body $parameters
if ($response) {
return $response.access_token
} else {
return $null
}
}
function GetEventDefinitions() {
try
{
$listEventUrl = $aipEventUrl + "?productId=$productId"
$response = Invoke-WebRequest $listEventUrl -Method 'GET' -Headers $headers
return $response
} catch {
Write-Host "Error Details: $($_.Exception)"
Write-Host "HTTP Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "HTTP Status Description: $($_.Exception.Response.StatusDescription)"
return $null
}
}
function CreateEventDefinition([string]$jsonString) {
$body = $jsonString
try
{
$createResponse = Invoke-WebRequest $aipEventUrl -Method 'POST' -Headers $headers -Body $body
Write-Host "Event Definition Created:{{$createResponse}}"
} catch {
Write-Host "Error Details: $($_.Exception)"
Write-Host "HTTP Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "HTTP Status Description: $($_.Exception.Response.StatusDescription)"
}
}
function UpdateEventDefinition([string]$jsonString, [string]$eventId) {
try
{
$response = Invoke-WebRequest "$aipEventUrl/$eventId" -Method 'PUT' -Headers $headers -Body $jsonString
Write-Host "Event Definition Updated:{$response}"
} catch {
Write-Host "Error Details: $($_.Exception)"
Write-Host "HTTP Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "HTTP Status Description: $($_.Exception.Response.StatusDescription)"
}
}
$token = Request-Token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Bearer $token")
$defs = GetEventDefinitions
#declare an array to store all the event definitions for product from AIP
$allDefs = @()
if ($defs.StatusCode -eq 200) {
$content = $defs.Content | ConvertFrom-Json
$allDefs = $content.items
}
# Read the file and parse the JSON
$json = Get-Content -Path 'eventdefs.json' | ConvertFrom-Json
$jsonString = $json | ConvertTo-Json -Depth 100
foreach ($event in $json.eventDefinitions) {
#check whether definition exists
$existingEvent = $allDefs | Where-Object { $_.type -eq $event.type }
if ($existingEvent -eq $null) {
$newBody = @{
productId = $productId
productVersion = ""
eventDefinitions = @()
}
$newBody.eventDefinitions +=$event
$body = $newBody | ConvertTo-Json -Depth 100
CreateEventDefinition $body
} else {
$body = $event | ConvertTo-Json -Depth 100
UpdateEventDefinition $body $existingEvent.id
}
}