明示的な変換マップスクリプトを使用したマップ

  • リリースバージョン: Washingtondc
  • 更新日 2024年02月01日
  • 読む6読むのに数分
  • 変換マップレコード自体でマッピング関係を明示的に定義します。

    明示的な変換マップスクリプトは、変換マップレコード自体にマッピング関係を明示的に定義します。ソースフィールド値がターゲットレコードにコピーされた後、データベースに書き込まれる前に実行されます。

    明示的なマッピングスクリプトによって促進される CMDB ソフトウェアインポート

    ソフトウェアパッケージテーブルのカウントにインスタンスの数を反映させる必要がある場合に、明示的なマップスクリプティングを使用して CMDB へのインポートを促進する方法を示す例。

    CMDB には、ソフトウェアパッケージとソフトウェアインスタンスのテーブルが存在します。ソフトウェアパッケージは、Mozilla Firefox などの 1 つの個々のソフトウェアタイトルを参照します。ソフトウェアインスタンスは、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 へのインポートでクラス名を設定することによる子テーブル

    サーバーを Configuration Management Database にインポートする方法を示す例。

    インポート先のテーブルは 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(" ");
    }