Another use of translation scripts is to update the CA Business Service Insight resource model with values taken from an external data source. While strictly not related to translation any longer, this sample is a highly valuable facility for automatic updates to the system.
The previous section on Custom Attributes described a scenario where the hardware infrastructure devices of an organization are stored in an external CMDB, along with an expected availability target for each device. This information needs to be replicated into the CA Business Service Insight resource model so as to keep the infrastructure mapping (and the device targets) up to date.
In this case, the script is required to perform the following tasks:
The script is as follows:
Option Explicit
'******************************************************************
'Global Variables and constants
'******************************************************************
Dim added
Dim updated
Dim ChangeSetName
added = 0
updated = 0
Const RESOURCE_TYPE = "Infrastructure Devices"
Const RESOURCE_GROUP = "InfraServers"
Const CHANGESET_NAME = "Infrastructure Devices"
Const CHANGESET_EFFECTIVE_DATE = "01/01/2007"
'******************************************************************
'Sub OnLoad :
'Preparing the foundation infrustructure enteties
'******************************************************************
Sub OnLoad()
Tools.log "Translation Script : In OnLoad procedure", "D"
'Search for existing preliminary resource version
Dim ChangeSetMap
Set ChangeSetMap=Tools.SearchChangeSets(CHANGESET_EFFECTIVE_DATE, CHANGESET_NAME)
'If no existing version create a new version
If ChangeSetMap.EMPTY Then
Tools.AddChangeSet CHANGESET_NAME, CHANGESET_EFFECTIVE_DATE, CHANGESET_NAME
Tools.Log "Changes set '" & CHANGESET_NAME & "' added."
End If
Set ChangeSetMap = Nothing
End Sub
Sub OnTranslationEvent(EntryDetails)
End Sub
Sub Main()
Dim conn, rs, resource, deviceTarget, resource_id, resMap, custAttrib, custAttribValue
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.RecordSet")
conn.open "DSN=HardwareDevices"
rs.Open "select * from tblServers", conn
Do While Not rs.EOF
resource = rs("serverName")
deviceTarget= rs("DeviceTarget")
'Add resources to latest version if it doesnt exist already
If Not Tools.IsResourceExists(resource) Then
resource_id = Tools.AddResource(resource, CHANGESET_NAME, "Infrastructure Device", RESOURCE_TYPE, RESOURCE_GROUP, Null, Null)
Tools.UpdateResourcesCustomAttribute resource, CHANGESET_NAME, "DeviceTarget", deviceTarget
Tools.Log "AddingResource: " & resource & " with target: " & deviceTarget & " ; assigned ID= " & resource_id
added = added + 1
Else
Set resMap = Tools.GetResourceDetails(resource,CHANGESET_NAME, False)
Set custAttrib = resMap("CustomAttributes")
custAttribValue = CDbl(custAttrib("DeviceTarget")("CustomAttributeValue"))
If CDbl(deviceTarget) <> custAttribValue Then
Tools.UpdateResourcesCustomAttribute resource, CHANGESET_NAME, "DeviceTarget", deviceTarget
Tools.Log "Updating Resource target for : " & resource & " from: " & deviceTarget & " to " & custAttribValue
updated = updated + 1
End If
End If
Tools.Commit
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub
Function Result
' Commit the transaction
Tools.CommitChangeSets CHANGESET_NAME
Result = added & " resources added and " & updated & " resources updated."
End Function
'********************************************************************************
|
Copyright © 2013 CA.
All rights reserved.
|
|