명시적 변환 맵 스크립트로 매핑
변환 맵 기록 자체에서 매핑 관계를 명시적으로 정의합니다.
명시적 변환 맵 스크립트는 변환 맵 기록 자체에서 매핑 관계를 명시적으로 정의합니다. 소스 필드 값이 대상 기록에 복사된 후와 데이터베이스에 기록되기 전에 실행됩니다.
명시적 매핑 스크립트로 촉진되는 CMDB 소프트웨어 임포트
소프트웨어 패키지 테이블의 개수에 인스턴스 수를 반영해야 하는 소프트웨어 인스턴스를 CMDB로 쉽게 임포트하기 위해 명시적 맵 스크립팅을 사용할 수 있는 방법을 보여주는 예입니다.
CMDB에는 소프트웨어 패키지와 소프트웨어 인스턴스에 대한 테이블이 있습니다. 소프트웨어 패키지는 Mozilla Firefox와 같은 하나의 개별 소프트웨어 타이틀을 나타냅니다. 소프트웨어 인스턴스는 Jared_T60_Laptop의 Mozilla Firefox와 같은 소프트웨어 타이틀의 개별 인스턴스를 나타냅니다.
좀 더 기술적인 용어로 소프트웨어 인스턴스는 소프트웨어 패키지 및 구성 항목과의 다대다 관계입니다. 이 스크립트는 소프트웨어 인스턴스를 임포트합니다. 이 작업을 수행할 때 인스턴스에 대한 소프트웨어 패키지가 있는지 확인합니다. 소프트웨어 패키지가 없는 경우 소프트웨어가 생성되며, 새로 생성된 패키지의 sys_id 인스턴스 기록의 소프트웨어 필드를 통해 인스턴스와 연결됩니다.
//First we will initialize some temporary variables,
//referencing values from the Import Set table source that will be used to
//reference software package records and create them if necessary.
var name= source.u_name;
var version = source.u_version;
var sid ="";
//Next we will perform a glide query on the software package table (Note: The target table
//for the import is software instances)
var sgr =new GlideRecord("cmdb_ci_spkg");
//Here we are building our query to search for software packages where the name and
//version of the package matches the name and version of the instance being imported
sgr.addQuery("name",name);
sgr.addQuery("version", version);
sgr.query();
//Now if a software package with the correct name and version are found then we record
//the sys_id of the package record otherwise we create the package and then record the sys_id
if(sgr.next()){
sid = sgr.sys_id;}else{// create it
sgr.initialize();
sgr.newRecord();
sgr.name=name;
sgr.version= version;
sid = sgr.insert();
//Here we make an entry in System Import Set Log saying that we had to create a software package
log.info("Created new spkg: "+name+" - "+ version);
}
//Finally we set the reference field on our software instance record to the sys_id we have
//recorded for the software package. In doing so we are also relating the
//software package with the instance and so the count, which keeps track of
//the number of instances associated with a package, will automatically be incremented.
target.software= sid;CMDB로 임포트에서 클래스 이름을 설정하여 하위 테이블 채우기
서버를 구성 관리 데이터베이스로 가져오는 방법을 보여주는 예입니다.
임포트의 대상 테이블은 cmdb_ci_servers 테이블입니다.
var operating_system = source.u_operating_system.toString();
//This if statement uses JavaScript regular expressions to search the operating system
if( operating_system.match(/linux/i)!=null){
target.sys_class_name="cmdb_ci_linux_server";
};
if( operating_system.match(/win/i)!=null){
target.sys_class_name="cmdb_ci_win_server";
};사용자 임포트 데이터 정리
임포트하기 전에 사용자 데이터를 삭제하는 방법을 보여주는 예시 스크립트입니다.
//Note: The field names referenced from an import set are
//prefixed with an "u_", also note that it is necessary to use the java method
// toString() to so that we can use JavaScript functions to
//manipulate the data.
var name= source.u_name.toString();
//Use native JavaScript function split to create an array for each word in the name "splitting" it
//anywhere that there is a space
var split_names =name.split(" ");
//Find the number of of names (i.e., first and last name only, or first middle and last name, etc.)
var num_names = split_names.length;
// If there is only one name then map it to the last name field in the user table
if(num_names ==1){
target.last_name= split_names[0];
}
//if there are two names then map the first one to first name and the last one to last name
if(num_names ==2){
target.first_name= split_names[0];
target.last_name= split_names[1];
}
//if there are more than 3 names then all middle names get combined into one middle name this is done
//by shifting off the first name (array element 0 ) and mapping to first name and popping off the last
// name and returning it to the last name field
if(num_names >=3){
target.first_name= split_names.shift();
target.last_name= split_names.pop();
target.middle_name= split_names.join(" ");
}