# "Developed by Rutul Shah , GSDC Engineer, Netapp Inc." #!/usr/bin/env perl -w use strict; use warnings; use MIME::Base64; use XML::Simple; use JSON qw( decode_json ); # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm # Example install using cpanm: # sudo cpanm -i REST::Client use REST::Client; # Set the request parameters #my $host = 'https://sandbox.service-now.com'; my $auth_file="C:/Users/rutuls/Desktop/SN_WFA/authentication.txt"; # This is configuration file which contains servicenow instance name,username and password.Change the instance name,username,password and file path accordingly. my @inputs; my $host; my $user; my $pwd; my @logonname; my $description; my $firstvalue; my $secondvalue; my @names; my @openedby; my $count=0; open FILE, "<$auth_file" or die "Error reading file $auth_file: $!\n"; while () { chomp; ($host,$user,$pwd)=split("\t"); } my $encoded_auth = encode_base64("$user:$pwd", ''); print $encoded_auth; my $client = REST::Client->new(host => $host); # Get the incident with sys_id declared above my $json_data=$client->GET("/incident.do?JSONv2", {'Authorization' => "Basic $encoded_auth", 'Accept' => 'application/json'})->responseContent(); my $decoded =decode_json($json_data); my @results = @{$decoded->{'records'}}; my $file="C:/Users/rutuls/Desktop/SN_WFA/workflow_input.txt"; my @res; my @temp; foreach my $r(@results) { if($r->{"short_description"} =~ /Create a Home Directory/ && $r->{"active"} =~ /true/) { my $sd=$r->{"short_description"}; @res=split(':',$sd); @names=split(',',$res[1]); push(@temp,@names); } } foreach my $s(@temp) { $count++; print $count; $logonname[$count]=$s; print $logonname[$count]; } open FILE, "<$file" or die "Error reading file $file: $!\n"; while () { chomp; my @inputs = $_; $firstvalue=$inputs[0]; #$secondvalue=$inputs[1]; } my $i; if($firstvalue =~ /ShareName=/) { foreach $i(1..$count) { open(FILE,">>$file"); if($i==$count) { print FILE $logonname[$i]; } else { print FILE "$logonname[$i]".","; } } print FILE ";ChangeUsers="; foreach $i(1..$count) { open(FILE,">>$file"); if($i==$count) { print FILE $logonname[$i]; } else { print FILE "$logonname[$i]".","; } } callWFAWorkflow(); } sub callWFAWorkflow { # Global variables. my ($server,$user,$pass,$workflow,$file); my ($headers,$rest,$wfa,$uuid,$input_key_value); $server="10.216.50.81:81"; $user="admin"; $pass="Netapp123!"; $file="C:/Users/rutuls/Desktop/SN_WFA/workflow_input.txt"; # Setup our connection to the WFA server. $headers = { Authorization => 'Basic ' . encode_base64($user . ':' . $pass), 'Content-type' => 'application/xml', }; $rest = REST::Client->new( { host => "http://$server/rest", } ); # If we get here, we need to execute workflows based on the input file. open FILE, "<$file" or die "Error reading file $file: $!\n"; while () { chomp; # Parse the input file. my @input = split /;/, $_; $workflow ="Create Home Directories on Clustered Data ONTAP Qtree" ; #print $workflow; for my $x (0..$#input) { my ($key,$value) = split /=/, $input[$x], 2; $input_key_value->{$key} = $value if ($key); #print $value; } # Get the workflow uuid. $wfa = get_workflow_xml($rest,$headers,$workflow); $uuid = $wfa->{'workflow'}{'uuid'}; print $uuid; # Call the workflow for this line. run_workflow($rest,$headers,$uuid,$input_key_value); } close FILE; exit; } # Subroutine to get workflow XML specification. sub get_workflow_xml { my ($rest,$headers,$workflow) = (@_); my ($wfa,$uuid); $workflow =~ s/\s/%20/g; $rest->GET("/workflows?name=${workflow}", $headers); if ($rest->responseCode() > 201) { my $error = $rest->responseContent(); print "Error connecting to WFA: $error\n"; exit; } $wfa = XMLin($rest->responseContent()); return $wfa; } # Subroutine to run the worklow sub run_workflow { my ($rest,$headers,$uuid,$inputs) = (@_); my ($postdata,$key,$value,$sleep,$date); # This is how long we sleep in between checking job status. $sleep = 15; # Setup workflow input in XML format. $postdata = qq{ }; # Loop through our user inputs and add them in the XML. for $key (keys %$inputs) { $postdata .= "\n"; $value = $inputs->{$key}; $postdata .= qq{ }; } # Finish the XML. $postdata .= qq{ }; # Submit the workflow. $rest->POST( "/workflows/$uuid/jobs" , $postdata , $headers); open FILE, ">$file" or die "Error reading file $file: $!\n"; print FILE "ShareName="; close FILE; # Get results of workflow submission, sleeping between each time we check, # until it is finished. if ($rest->responseCode() > 201) { $date = localtime; print "$date: Response Code: " . $rest->responseCode() . ". "; print "Error executing workflow: "; print $rest->responseContent() . "\n";; } else { my $result = XMLin($rest->responseContent()); my $jobId = $result->{'jobId'}; $date = localtime; print "$date: JobId $jobId submitted.\n"; my $jobfinished = 0; sleep $sleep; while (! $jobfinished) { $date = localtime; $rest->GET("/workflows/${uuid}/jobs/$jobId", $headers); my $jobs = XMLin($rest->responseContent()); my $jobStatus = $jobs->{'jobStatus'}{'jobStatus'}; my $startTime = $jobs->{'jobStatus'}{'startTime'}; my $endTime = $jobs->{'jobStatus'}{'endTime'}; my $error = $jobs->{'jobStatus'}{'errorMessage'} if ($jobs->{'jobStatus'}{'errorMessage'}); if ($jobStatus =~ /completed/i) { print "$date: Job $jobId finished successfully. Started at $startTime and ended at $endTime.\n"; $jobfinished++; } elsif ($jobStatus =~ /failed/i) { print "$date: Job $jobId failed. Started at $startTime and ended at $endTime. Error message was: $error\n"; $jobfinished++; } else { print "$date: Job $jobId still running. Status is $jobStatus. Waiting $sleep seconds.\n"; } sleep $sleep; } } return 1; }