Argomento precedente: Case study 10: conversione automatica di baseArgomento successivo: Esempi di implementazione di business logic


Case study 11: aggiornamenti del modello di risorsa

Un altro possibile utilizzo degli script di conversione consiste nell'aggiornamento del modello di risorsa CA Business Service Insight con valori ricavati da un'origine dati esterna. Anche se non più strettamente correlato alla conversione, questo esempio delinea una funzione molto utile per gli aggiornamenti automatici del sistema.

Nella sezione precedente sugli attributi personalizzati è stato descritto uno scenario in cui i dispositivi di infrastruttura hardware di un'organizzazione vengono archiviati in un CMDB esterno, insieme alla destinazione di disponibilità prevista per ogni dispositivo. Queste informazioni devono essere replicate nel modello di risorsa CA Business Service Insight in modo da tenere aggiornato il mapping dell'infrastruttura (insieme alle destinazioni dei dispositivi).

In questo caso, è necessario che lo script esegua le seguenti attività:

Di seguito è riportato lo script:

Option Explicit

'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'Variabili globali e costanti
'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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/07"

'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'OnLoad secondario:
'Preparazione delle entità di infrastruttura basilari
'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Sub OnLoad()
   Tools.log "Translation Script : In OnLoad procedure", "D"

   'Cercare la versione di risorsa preliminare esistente
   Dim ChangeSetMap
   Set ChangeSetMap=Tools.SearchChangeSets(CHANGESET_EFFECTIVE_DATE, CHANGESET_NAME)

   'Se non esiste alcuna versione creare una nuova versione
   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")
      'Aggiungere risorse all'ultima versione se non esiste già
      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
   Ciclo

   rs.Close
   conn.Close
   Set rs = Nothing
   Set conn = Nothing
End Sub

Function Result
   ' Confermare la transazione
   Tools.CommitChangeSets CHANGESET_NAME
   Result = added & " resources added and " & updated & " resources updated."
End Function

'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *