[rt-users] RTAT Import?

Jean-Sebastien Morisset jsmoriss at mvlan.net
Fri May 2 14:38:00 EDT 2008


On Wed, Apr 30, 2008 at 07:19:09PM +0000, Jean-Sebastien Morisset wrote:
> On Wed, Apr 30, 2008 at 05:51:39PM +0000, Jean-Sebastien Morisset wrote:
>> 
>> Aha! Thanks! Ok, so far this seems to work well:
> [snip!]
> 
> Alright, so I spent a little more time on this script, and added some
> validation against the customfield select values. Here's an example:
[snip!]

Well, I couldn't leave well enough alone, and kept tweeking the import
script. I know a few people are already using my first script, so you'll
probably enjoy this little update (script attached). :-)

[root:/opt/rt3/bin]
# ./rt-import-assets.pl
syntax: ./rt-import-assets.pl [-h] [-c] [-d] -f {csvfile}
        -c: create missing assets (script only updates by default)
        -d: change empty Description csv fields to Manufacturer and Model

js.
-- 
Jean-Sebastien Morisset, Sr. UNIX Administrator <jsmoriss at mvlan.net>
-------------- next part --------------
#!/usr/bin/perl

use lib qw(/opt/rt3/local/lib /opt/rt3/lib);

use RT;
use RTx::AssetTracker::Asset;
use Getopt::Std;
use strict;

our ($opt_h, $opt_d, $opt_c, $opt_f);
getopts('hcdf:');

RT::LoadConfig();
RT::Init();
my $at = RTx::AssetTracker::Asset->new(RT->SystemUser);
my %csv_map;
my $csv_col;
my $ln;
my $status;
my $msg;

if ($opt_h || !$opt_f) {
	print "syntax: $0 [-h] [-c] [-d] -f {csvfile}\n";
	print "\t-c: create missing assets (script only updates by default)\n";
	print "\t-d: change empty Description csv fields to Manufacturer and Model\n";
	exit 0;
}

open(CSV, "< $opt_f") or die "$!\n";

while (<CSV>) {
	chomp;
	$ln++;
	my $lv = $_;
	# fix empty fields without double-quotes
	while (s/",(,+")/",""$1/g) {};
	# csv fields should be padded with double quotes,
	# which allows the use of commas in field values.
	s/^"//; s/"$//;
	my @csv = split(/","/);
	# the first line should be a header. header names
	# must match customfield names, plus the Name,
	# Status and Type headers.
	if ($ln == 1) {
		if ($lv !~ /"Name"/ || 
			$lv !~ /"Status"/ || 
			$lv !~ /"Type"/) {
			print "error: need at least the Name, Status and Type headers\n";
			exit 1;
		}
		# remember the column number for each field name.
		# this allows us to include only those columns we
		# want in the csv, and in whatever order we want.
		for (@csv) { $csv_map{$_} = $csv_col++; }
		next;
	}
	if (!$csv[$csv_map{"Name"}]) {
		print "error: cannot load blank asset Name in $lv\n";
		next;
	}
	# change empty description csv fields to manufacturer and model
	if ($opt_d && defined $csv_map{"Description"} && !$csv[$csv_map{"Description"}] &&
		$csv[$csv_map{"Manufacturer"}] && $csv[$csv_map{"Model"}]) {
		$csv[$csv_map{"Description"}] = $csv[$csv_map{"Manufacturer"}]." ".$csv[$csv_map{"Model"}];
	}

	print $csv[$csv_map{"Name"}].":\n";
	# load the asset, and on failure, create the asset.
	if (my $id = $at->Load($csv[$csv_map{"Name"}])) {
		# the New Name column, if present, allows us to change the asset name.
		for my $fn ("New Name", "Status", "Type", "Description") {
			if (defined $csv_map{$fn}) {
				# if we have a New Name defined, make sure it's not
				# empty and it's different from the current Name
				if ($fn eq "New Name" && $csv[$csv_map{$fn}] && 
					$csv[$csv_map{$fn}] ne $csv["Name"]) {
			  		($status, $msg) = $at->_Set(
						Field => "Name",
						Value => $csv[$csv_map{$fn}],
						RecordTransaction => 0);
				} else {
			  		($status, $msg) = $at->_Set(
						Field => $fn,
						Value => $csv[$csv_map{$fn}],
						RecordTransaction => 0);
				}
				print "\t$msg\n" if ($status);
			}
		}
	} else {
		if (!$csv[$csv_map{"Status"}] || !$csv[$csv_map{"Type"}]) {
			print "\terror: need a Status and Type to create an asset!\n";
			next;
		}
		if ($opt_c) {
			# create using only the essential fields
			my ($id, $t, $msg) = $at->Create (
				Name => $csv[$csv_map{"Name"}],
				Status => $csv[$csv_map{"Status"}],
				Type => $csv[$csv_map{"Type"}],
				RecordTransaction => 0,
			);
			print "\t$msg\n" if ($msg);
			# set the description - if we have this field in the csv
			if (defined $csv_map{"Description"}) {
				($status, $msg) = $at->_Set(
					Field => "Description",
					Value => $csv[$csv_map{"Description"}],
					RecordTransaction => 0);
				print "\t$msg\n" if ($status);
			}
		} else {
			print "\tno create argument - skipping asset creation\n";
			next;
		}
	}
	my $atcf = $at->CustomFields();
	# check each customfield for this asset
	while (my $cf = $atcf->Next()) {
		# check to see if we have a CSV column for this customfield
 		if ($csv_col = $csv_map{$cf->Name}) {
			my $accept;
			if ($cf->Type eq "Select") {
				# read all selectable values for this customfield
				my $cfvs = $cf->Values;
				while (my $value = $cfvs->Next) {
					# check to see if new values is defined in select
					if ($value->Name eq $csv[$csv_col]) {
						$accept = 1;
						last;
					}
				}
			} elsif ($cf->Type eq "Freeform") {
				$accept = 1;
			} else {
				print "\tneed more code for ".$cf->Type." customfield type\n";
			}
			if ($accept) {
  		     		($status, $msg) = $at->AddCustomFieldValue(
					Field => $cf->Id,
					Value => $csv[$csv_col],
					RecordTransaction => 0,
				);
				print "\t$msg\n" if ($msg);
			} else {
				# don't print an error for empty select values
				print "\t\"$csv[$csv_col]\" value not ok for ".$cf->Name." customfield\n"
					if ($csv[$csv_col]);
			}
		}
	}
}


More information about the rt-users mailing list