Rubrique précédente: Translation Scripts - User Function Scripts; Examples

Rubrique suivante: Launcher Page Examples

CMDB Example

Option Explicit
 
' The following constants are names that the script uses for creating new entities.
' In the first running you must: 
'    1. Create a New Contract Party called "CMDB Template"
'    2. Verify that a suitable Adapter with a suitable file already run.

' In every run at the same environment you must change the name of 
' the constant and the Resource name in the input file of the Adapter.
' CA recommends incrementing the number contained within the 
  name, e.g., "CMDB CS3" would become "CMDB CS4"


Const CHANGE_SET_NAME= "CMDB CS"
Const RESOURCE_TYPE1 = "CMDB RT1"
Const RESOURCE_TYPE2 = "CMDB RT2" ' This RT will be used for Attach Function
Const SERVICE = "CMDB Service1"
Const RESOURCE_GROUP1 = "CMDB Group1"
Const RESOURCE_GROUP2 = "CMDB Group2"  ' This RG will be used for Attach Function
Const CONTRACT_PARTY = "CMDB CP1"
Const CONTRACT_PARTY_TEMPLATE = "CMDB Template"
Const RESOURCE_NAME = "CMDB Resource1"
Const SECOND_RESOURCE_NAME = "CMDB Based on CMDB Resource1"

Dim added
Dim translated
Dim ignored
Dim deleted

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The OnLoad sub routine adds several entities to the system, if they 
  do not already exist.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Sub OnLoad()
  added = 0
  translated = 0
  ignored = 0
  deleted = 0

  ' Update general details for all the resource groups and resources where their name starts with YYY
  IntResourcesUpdateGeneralDetails "YYY"

  ' Add two Resource Types
  IntAddResourceType RESOURCE_TYPE1, "Resource Type was added
  automatically by a script."
  IntAddResourceType RESOURCE_TYPE2, "Resource Type was added 
  automatically by a script."
  
  ' Add Change Set  
  IntAddChangeSet CHANGE_SET_NAME, "01/01/2006", "Change Set 
  was added automatically by a script."
  
  ' Add Service
  IntAddService Service, "Service was added automatically by 
  a script.", 100
  
  ' Add Contract Party
  IntAddContractPartyFromTemplate CONTRACT_PARTY, "CP was 
  added automatically by a script.", "Provider", "01/01/2006"
                                    
  
  'Add resource groups
  IntAddResourceGroup RESOURCE_GROUP1, CHANGE_SET_NAME, _
                   "Resource Group was added automatically by 
                    a script.", SERVICE, CONTRACT_PARTY

  IntDuplicateResourceGroup RESOURCE_GROUP1, RESOURCE_GROUP2, 
                            CHANGE_SET_NAME

  'Add resources
  IntAddResource RESOURCE_NAME, CHANGE_SET_NAME, _
              "Resource was added automatically by a script.", _
              RESOURCE_TYPE1,RESOURCE_GROUP1, SERVICE, CONTRACT_PARTY    

  IntDuplicateResource RESOURCE_NAME, SECOND_RESOURCE_NAME, CHANGE_SET_NAME
 
  Tools.Commit
    
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get one translation entry. The details are in EntryDetails parameter and the relevant fields are:
' - EntryId - the translation entry ID. The Tools.TranslateEntry, Tools.IgnoreEntry and Tools.DeleteEntry
'   methods get this id.
' - FieldValue(1) - the value to be translated. 
' The method gets the value from FieldValue(1) and its process depends on the field value:
' - If there is a resource with the same name then translate to this resource
' - When it starts with "Ignore me" - ignore the entry
' - When it starts with "Delete me" - delete the entry
' - When it starts with A then add a resource with all details.
' - When it starts with B then add a resource with only resource 
'   type and attach the other data with the specific methods.
' - When it starts with F'F' then add a resource with only resource type and then attach two resource \
'   types and two resource groups.
' - In all other cases add the resource and translate the entry
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  


Sub OnTranslationEvent(EntryDetails)
  Tools.log "Translation entry details : " & EntryDetails.dump, "D"

  Dim TranslateFrom
  Dim ResourceName
  Dim EntryID
  
  Dim ResourceTypesMap
  Dim ResourceGroupsMap
  Dim ResourceMap
  
  Set ResourceTypesMap = Tools.CreateMap
  Set ResourceGroupsMap = Tools.CreateMap
  Set ResourceMap= Tools.CreateMap
  
  ResourceTypesMap(RESOURCE_TYPE1) = RESOURCE_TYPE1
  ResourceTypesMap(RESOURCE_TYPE2) = RESOURCE_TYPE2
  
  ResourceGroupsMap(RESOURCE_GROUP1) = RESOURCE_GROUP1
  ResourceGroupsMap(RESOURCE_GROUP2) = RESOURCE_GROUP2
    
  TranslateFrom = EntryDetails.FieldValue(1)
  ResourceName= "CMDB " & TranslateFrom 
  Tools.log "Resource name : " & ResourceName, "D"
  EntryID= EntryDetails.EntryId

  ' If the resource already exists then translate the entry to the resource
  If Tools.IsResourceExists (ResourceName) Then
    Tools.TranslateEntry EntryID,ResourceName
    translated = translated + 1
    Tools.Log "The entry id " & EntryID & " Translated to the resource: " & ResourceName,"D"

  ' If the resource name starts with 'Ignore me' substring then ignore the entry
  ElseIf mid(TranslateFrom ,1,9) = "Ignore me" Then
    Tools.IgnoreEntry EntryID
    ignored = ignored + 1
    Tools.Log "The entry id " & EntryID & " (resource: " & ResourceName & ") ignored","D"

  ' If the resource name starts with 'Delete me' substring then ignore the entry
  ElseIf mid(TranslateFrom ,1,9) = "Delete me" Then
    Tools.DeleteEntry EntryID
    deleted = deleted + 1
    Tools.Log "The entry id " & EntryID & " (resource: " & ResourceName & ") deleted","D"

  ' If the resource name starts with 'A' then add resource with all details
  ElseIf mid(TranslateFrom ,1,1) = "A" Then
    IntAddResource ResourceName, CHANGE_SET_NAME, "Resource was added automatically by a script", _
                RESOURCE_TYPE1,RESOURCE_GROUP1, SERVICE, CONTRACT_PARTY    
    added = added + 1
    Tools.TranslateEntry EntryID,ResourceName
    translated = translated + 1

  ' If the resource name starts with 'B' then add resource only with resource type and attach the
  ' other data with the specific methods.
  ElseIf mid(TranslateFrom ,1,1) = "B" Then
    IntAddResource ResourceName, CHANGE_SET_NAME, "Resource was added automatically by a script", _
                RESOURCE_TYPE1, Null, Null, Null   
    added = added + 1
    Tools.TranslateEntry EntryID,ResourceName
    translated = translated + 1

    Tools.AttachResourcesToContractParties ResourceName, CHANGE_SET_NAME, CONTRACT_PARTY
    Tools.AttachResourcesToResourceGroups  ResourceName, CHANGE_SET_NAME, RESOURCE_GROUP1
    Tools.AttachResourcesToServices        ResourceName, CHANGE_SET_NAME, SERVICE
    IntLogEntityDetails "resource", "new", Tools.GetResourceDetails (ResourceName,CHANGE_SET_NAME).Dump

  ' If the resource name starts with 'F' then add resource only with resource type and then attach 
  ' two resource types, two resource groups.
  ElseIf mid(TranslateFrom ,1,1) = "F" Then
    IntAddResource ResourceName, CHANGE_SET_NAME, "Resource was added automatically by a script", _
                RESOURCE_TYPE1, Null, Null, Null   
    added = added + 1
    Tools.TranslateEntry EntryID,ResourceName
    translated = translated + 1

    Tools.AttachResourcesToResourceTypes   ResourceName, CHANGE_SET_NAME, ResourceTypesMap
    Tools.AttachResourcesToResourceGroups   ResourceName, CHANGE_SET_NAME, ResourceGroupsMap
    
  ' In all the other cases add the resource and translate the entry
  Else
    ResourceMap("ResourceName") = ResourceName
    ResourceMap("ResourceDisplayName") = ResourceName
    ResourceMap("ChangeSetName") = CHANGE_SET_NAME
    ResourceMap("ResourceDescription") = "Resource was added automatically (AddResourceByMap) by a script"
    ResourceMap("ResourceTypes") = ResourceTypesMap
    ResourceMap("ResourceGroups") = ResourceGroupsMap
    ResourceMap("Services") = SERVICE
    ResourceMap("ContractParties") = CONTRACT_PARTY
    ResourceMap("Effective") = "Yes"
    ResourceMap("CustomAttributes") = Null
    

    Tools.AddResourceByMap ResourceMap   
    added = added + 1
    Tools.TranslateEntry EntryID,ResourceName
    translated = translated + 1

    IntLogEntityDetails "resource", "new", Tools.GetResourceDetails (ResourceName,CHANGE_SET_NAME).Dump
  End If

  Tools.commit

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub Main()
  'Tools.log "Translation Script : In Main procedure", "I"
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add change set. The function checks if the change set already exists.
Sub IntAddChangeSet(Name, EffectiveDate, Description)
  If Not Tools.IsChangeSetExists(Name) Then
    Tools.AddResourceVersion Name, EffectiveDate,Description
    Tools.Log "Change Set was added successfully: " & Name, "D"
    IntLogEntityDetails "change set", "new", Tools.GetChangeSetDetails(Name).Dump
  End If

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add resource type. The function checks if the resource type already exists.
Sub IntAddResourceType(Name, Description)
  If Not Tools.IsResourceTypeExists (Name) Then
    Tools.AddResourceType Name, Description, Null
    Tools.Log "Resource Type was added successfully: " & Name, "D"
    IntLogEntityDetails "resource type", "new", Tools.GetResourceTypeDetails(Name).Dump
  End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add Service. The function checks if the service already exists.
Sub IntAddService(Name, Description, Fee)
  If Not Tools.IsServiceExists (Name) Then
    Tools.AddService Name, Description, Fee, Null
    Tools.Log "Service was added successfully: " & Name, "D"
    IntLogEntityDetails "service", "new", Tools.GetServiceDetails(Name).Dump
  End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add Contract Party. The function checks if the contract party already exists.
Sub IntAddContractPartyFromTemplate(Name, Description, Provider, RegistrationDate)

  Dim ContractPartyMap

  If Not Tools.IsContractPartyExists (Name) Then
    Set ContractPartyMap = Tools.GetContractPartyDetails (CONTRACT_PARTY_TEMPLATE)
    ContractPartyMap("ContractPartyName")=Name
    ContractPartyMap("ContractPartyDescription")=Description
    ContractPartyMap("ContractPartyType")=Provider
    ContractPartyMap("ContractPartyRegistrationDate")=RegistrationDate
    Tools.AddContractPartyByMap ContractPartyMap
    Tools.Log "Contract Party was added successfully: " & Name, "D"
    IntLogEntityDetails "contract party", "new", Tools.GetContractPartyDetails(Name).Dump
  End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add Resource Group. The function checks if the resource group already exists.
Sub IntAddResourceGroup(Name, ChangeSetName, Description, Service, ContractParty)
  If Not Tools.IsResourceGroupExists (Name) Then
    Tools.AddResourceGroup Name, ChangeSetName, Description, Null, Service, ContractParty
    Tools.Log "Resource Group was added successfully: " & Name, "D"
    IntLogEntityDetails "resource group", "new", Tools.GetResourceGroupDetails (Name,ChangeSetName).Dump
  End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Duplicate Resource Group. The function checks if the resource group already exists.
Sub IntDuplicateResourceGroup(OldName, NewName, ChangeSetName)

  Dim ResourceGroupMap

  If Not Tools.IsResourceGroupExists (OldName) Then
    Tools.Log "Resource Group " & OldName & " does not exist. ", "E"

  Else
    If Not Tools.IsResourceGroupExists (NewName) Then
      Set ResourceGroupMap = Tools.GetResourceGroupDetails (OldName,ChangeSetName)
      ResourceGroupMap("ResourceGroupName")=NewName
      Tools.AddResourceGroupByMap ResourceGroupMap
      Tools.Log "Resource Group was duplicated successfully: " & NewName, "D"
      IntLogEntityDetails "resource group", "new", Tools.GetResourceGroupDetails (NewName,ChangeSetName).Dump
    End If
  End If 
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Add Resource. The function checks if the resource already exists.
Sub IntAddResource(Name, ChangeSetName, Description, ResourceType, ResourceGroup, Service, ContractParty)
  If Not Tools.IsResourceGroupExists (Name) Then
    Tools.AddResource Name, ChangeSetName, Description, ResourceType, ResourceGroup, Service, ContractParty
    Tools.Log "Resource was added successfully: " & Name, "D"
    IntLogEntityDetails "resource", "new", Tools.GetResourceDetails (Name,ChangeSetName).Dump
  End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Duplicate Resource. The function checks if the resource group already exists.
Sub IntDuplicateResource(OldName, NewName, ChangeSetName)
  
  Dim ResourceMap
  
  If Not Tools.IsResourceExists (OldName) Then
    Tools.Log "Resource " & OldName & " does not exist. ", "E"

  Else
    If Not Tools.IsResourceExists (NewName) Then
      Set ResourceMap = Tools.GetResourceDetails (OldName,ChangeSetName)
      ResourceMap("ResourceName")=NewName
      Tools.AddResourceByMap ResourceMap
      Tools.Log "Resource was duplicated successfully: " & NewName, "D"
      IntLogEntityDetails "resource", "new", Tools.GetResourceDetails (NewName,ChangeSetName).Dump
    End If
  End If 
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Run on all the resource groups and resources with the given prefix and 
' get their general details, change the details, and update them in the DB
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub IntResourcesUpdateGeneralDetails(NamePrefix)

  Dim map
  Dim generalDetails
  Dim resourceName
  Set map = Tools.SearchResourceGroups ("", NamePrefix + "*")
  If (map.count >0) Then
    For Each resourceName In map
      Set generalDetails = Tools.GetResourceGroupGeneralDetails (resourceName)
      Tools.Log "General details before update:" 
      Tools.Log GeneralDetails.dump
      
      GeneralDetails("ResourceGroupName") = resourceName + " Renamed"
      GeneralDetails("ResourceGroupDisplayName") = resourceName + " Renamed"
      GeneralDetails("ResourceGroupDescription") = "New description"
      
      Tools.UpdateResourceGroupGeneralDetails resourceName, GeneralDetails
      Tools.Log "General details after update" 
      Tools.Log GeneralDetails.dump
    Next 
  End If

  Set map = Tools.SearchResources("", NamePrefix + "*")
  If (map.count >0) Then
    For Each resourceName In map
      Set generalDetails = Tools.GetResourceGeneralDetails(resourceName)
      Tools.Log "General details before update:" 
      Tools.Log GeneralDetails.dump
      
      GeneralDetails("ResourceName") = resourceName + " Renamed"
      GeneralDetails("ResourceDisplayName") = resourceName + " Renamed" 
      GeneralDetails("ResourceDescription") = "New description"
      
      Tools.UpdateResourceGeneralDetails resourceName, GeneralDetails
      Tools.Log "General details after update" 
      Tools.Log GeneralDetails.dump
    Next 
  End If

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub IntLogEntityDetails(EntityType, ActionType, Details)
  Tools.Log "The " & ActionType & " " & EntityType & " details: " & chr(10) & chr(13) & Details,"D"
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function Result
  Result = added & " resources had been added, " & _
           translated & " resources had been translated, " & _
           ignored & " resources had been Ignored and " & _
           deleted & " resources had been deleted."
End Function