I thought one day, someone might find this asset linking script useful. It takes quoted, comma-separated Names and URI’s and links the URI as a component of asset with Name.
It’s a butchered version of the asset import script mentioned earlier in the thread written by Jean-Sebastien Morisset.
Input file should look like:
“Name”,“URI”
“computer1”,"at://rt.someplace.com/asset/1234
“computer2”,“at://rt.someplace.com/asset/4321”
-Mike
#!/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: don’t use this it doesn’t do anything anymore\n”;
print “\t-d: don’t use this it doesn’t do anything anymore \n”;
exit 0;
}
open(CSV, “< $opt_f”) or die “$!\n”;
while () {
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 “Name”,”URI”
if ($ln == 1) {
if ($lv !~ /“Name”/ ||
$lv !~ /“URI”/) {
print “error: need the Name and URI 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;
}
print $csv[$csv_map{“Name”}].“:\n”;
print $csv[$csv_map{“URI”}].“:\n”;
# load the asset, and on failure, create the asset.
if (my $id = $at->Load($csv[$csv_map{“Name”}])) {
$at->AddLink(Type=>‘ComponentOf’,Base=>$csv[$csv_map{“URI”}]);
}
}From: Michael Ellis [mailto:Michael_Ellis@UManitoba.CA]
Sent: August-17-09 9:01 AM
To: ‘Michael Ellis’
Cc: ‘Todd Chapman’; ‘rt Users’; jsmoriss@mvlan.net; william_moore@UManitoba.CA
Subject: RE: [rt-users] Asset Tracker API
All of a sudden this morning it’s working. I can’t explain, but am happy.
-Mike
From: Michael Ellis [mailto:michael_ellis@umanitoba.ca]
Sent: August-14-09 10:11 PM
To: Michael Ellis
Cc: Todd Chapman; rt Users; jsmoriss@mvlan.net
Subject: Re: [rt-users] Asset Tracker API
I should add that in the history for the asset in the ui it shows the
adding of the asset, but not in the links area.
Mike