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:

# bin/import-assets.sh /tmp/js.csv
gessolx1:
uasolx1:
        "" value not ok for OS Name customfield
BDM-MTL-FWSM-PROD-1:
        "X" value not ok for Client customfield

And here's the script:

---BEGIN---
#!/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_f);
getopts('hf:');

RT::LoadConfig();
RT::Init();
my $at = RTx::AssetTracker::Asset->new(RT->SystemUser);
my $line;
my @csv_def = (
        'Name',
        'Status',
        'Type',
        'Description',
        'Client',
        'Location',
        'Manufacturer',
        'Model',
        'Serial Number',
        'OS Name',
        'Switch Type',
);

if ($opt_h || !$opt_f) {
        print "    Syntax: $0 [-h] -f {csvfile}\n";
        print "CSV Format: ";
        for (@csv_def) { print "\"$_\","; }
        print "\n";
        exit 0;
}

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

# customfield columns in csv file
my (%cf_map, $cf_item);
for (@csv_def) { $cf_map{$_} = $cf_item++; }

while (<CSV>) {
        chomp; $line++;
        next if ($line == 1);   # Skip CSV Header
        s/^"//; s/"$//;
        my @csv = split(/"?,"?/);
        print $csv[0], ":\n";
        unless (my $id = $at->Load($csv[0])) {
                my ($id, $t, $msg) = $at->Create (
                        Name => $csv[0],
                        Status => $csv[1],
                        Type => $csv[2],
                        Description => $csv[3],
                );
                print "\t$msg\n" if ($msg);
        }
        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 (my $col = $cf_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[$col]) {
                                                $accept = 1;
                                                last;
                                        }
                                }
                        } elsif ($cf->Type eq "Freeform") {
                                $accept = 1;
                        } else {
                                print "\tneed more code for ".$cf->Type." 
customfield type\n";
                        }
                        if ($accept) {
                                my ($status, $msg) = $at->AddCustomFieldValue(
                                        Field => $cf->Id,
                                        Value => $csv[$col],
                                        RecordTransaction => 1,
                                );
                                print "\t$msg\n" if ($msg);
                        } else {
                                print "\t\"$csv[$col]\" value not ok for 
".$cf->Name." customfield\n";
                        }
                }
        }
}
---END---

-- 
Jean-Sebastien Morisset, Sr. UNIX Administrator <[EMAIL PROTECTED]>
_______________________________________________
http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-users

Community help: http://wiki.bestpractical.com
Commercial support: [EMAIL PROTECTED]


Discover RT's hidden secrets with RT Essentials from O'Reilly Media. 
Buy a copy at http://rtbook.bestpractical.com

Reply via email to