Does anyone have examples of transform scripts they have used to import multiple CI classes into the CMDB that they are willing to share?

jamestrezza
Giga Contributor

I have over 40 different CI classes that are in flat file format that need ongoing import to the CMDB and want to avoid having one import/transform per CI Class.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Michael.Fry solution is probably the simplest, BUT make sure that the value for the sys_class_name is valid or you will mess things up real quick (typos will kill you).   You could add something like this to the end of Michael's script in order to validate the data before it's inserted:



var test = new GlideRecord(target.getValue("sys_class_name"));


if (!test.isValid()) {


      ignore = true;   //will skip this one row


      gs.addErrorMessage("whatever message you want to add to the logs, if any");


}



It just tries to create a new record (will not actually save it though) and then checks to see if the record is valid.


View solution in original post

10 REPLIES 10

Michael Fry1
Kilo Patron

On your Transform Map, make sure your Target table is cmdb_ci. Then check Run Script and you can use something like below. You need to have a field that can be used for mapping the class. We use assettype. We also set other data, but gives you the idea.



if (source.u_assettype == 1) {
    target.sys_class_name = "cmdb_ci_ip_network";
    target.category = 'Enterprise Hardware';
    target.subcategory = 'other';
    target.install_status = 1;

      }


      else if (source.u_assettype == 2) {


    target.sys_class_name = "cmdb_ci_ip_firewall";
    target.category = 'Enterprise Hardware';
    target.subcategory = 'firewall';
    target.install_status = 1;

      }


      else if (source.u_assettype == 3) {


    target.sys_class_name = "cmdb_ci_nas_file_system";
    target.category = 'Enterprise Hardware';
    target.subcategory = 'tape/disk devices';
    target.install_status = 1;

      }


      else if (source.u_assettype == 4) {


    target.sys_class_name = "cmdb_ci_ip_router";
    target.category = 'Enterprise Hardware';
    target.subcategory = 'router';
    target.install_status = 1;

      }


Jim Coyne
Kilo Patron

Michael.Fry solution is probably the simplest, BUT make sure that the value for the sys_class_name is valid or you will mess things up real quick (typos will kill you).   You could add something like this to the end of Michael's script in order to validate the data before it's inserted:



var test = new GlideRecord(target.getValue("sys_class_name"));


if (!test.isValid()) {


      ignore = true;   //will skip this one row


      gs.addErrorMessage("whatever message you want to add to the logs, if any");


}



It just tries to create a new record (will not actually save it though) and then checks to see if the record is valid.


What do I do if my imported file has the display value for the Class rather than the actual value?


Then your source would be Class, for example:


if (source.class == Windows) {


Sorry I should have been more clear, I don't want to have to build out IF statements for every potential Class - I'd like to have the Class I'm given resolve correctly on its own.   I didn't think the data I have would work without some scripting help...