4 Replies Latest reply: Jun 14, 2013 3:51 PM by gstewart RSS

Adding node to a container (group) with Perl?

gstewart

I'm attempting to add a specific nodeID to a container using the Perl SDK.

 

None of the following seem to be working... I must be missing something obvious!

 

my %UPDATEGROUP = (

   'ContainerID' => $_[1],

  'MemberPrimaryID' => $_[2]

  );

 

my $swis = SW::InformationService->new();

$swis->outputxml("true");

$swis->proxy("https://".$hostname.$endpoint);

$swis->proxy->http_request->authorization_basic($username, $password);

 

return $swis->Create('Orion.Container', \%UPDATEGROUP);

 

..or

 

return $swis->Create('Orion.ContainerMembers', \%UPDATEGROUP)

 

Any help / guidance woudl be very much appreciated.

 

Thanks,

 

Graeme

 
  • Re: Adding node to a container (group) with Perl?
    tdanner

    To add a member to an Orion group, you need to call the Orion.Container.AddDefinition verb. Here's some perl code for doing that:

     

    use SW::InformationService;

    use SOAP::Lite;

    use XML::LibXML;

     

    my $hostname = "localhost"; # fill in a hostname

    my $username = "admin";

    my $password = "";

     

    my $groupID = 3;

    my $nodeID = 3;

     

    my $endpoint = "https://$hostname:17778/SolarWinds/InformationService/v3/OrionBasic";

    my $swis = SW::InformationService->new();

    $swis->outputxml("true");

    $swis->proxy($endpoint);

    $swis->proxy->ssl_opts(verify_hostname => 0);

    $swis->proxy->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE);

    $swis->proxy->http_request->authorization_basic($username, $password);

     

    # Example of Query operation: Query for a caption of a node 1

    my $queryResult = $swis->QueryXml('SELECT Uri FROM Orion.Nodes WHERE NodeID=@id', {'id'=>$nodeID});

    my $dom = XML::LibXML->load_xml(string => $queryResult);

    my $xpc = XML::LibXML::XPathContext->new($dom);

    $xpc->registerNs("s", "http://schemas.solarwinds.com/2007/08/informationservice");

    my $nodeUri = $xpc->findvalue("//s:data/s:row/s:c0");

     

    my $memberDefinitionInfo = SOAP::Data->type('xml'=>

      "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

      <Name></Name>

      <Definition>$nodeUri</Definition>

        </MemberDefinitionInfo>");

     

    print $swis->Invoke("Orion.Container", "AddDefinition", [$groupID, $memberDefinitionInfo]);

    • Re: Adding node to a container (group) with Perl?
      gstewart

      Awesome. Many thanks.

       

      That was a lot more non-obvious than I initially expected.

      • Re: Adding node to a container (group) with Perl?
        tdanner

        We have a few samples for how to do various tasks in the Samples\PowerShell directory. When you want to do something with the API, I recommend you start by seeing if there is a PowerShell sample for that task. If you find one, you can do the same operations in Perl. You may have to do a bit more XML-wrangling in Perl than in PowerShell since the PowerShell plugin we provide (SwisPowerShell.dll) does more than the Perl module (SW::InformationService).

        • Re: Adding node to a container (group) with Perl?
          gstewart

          Great advice. Allowed me to throw together the add/delete from group function without too much difficulty:

           

          sub manageGroup {

               my $parser = XML::LibXML->new();

               my $dom = $parser->parse_string($_[0]->QueryXml('SELECT Uri FROM Orion.Nodes WHERE NodeID=@id', {'id'=>$_[3]}));

           

               my $xpc = XML::LibXML::XPathContext->new($dom);

               $xpc->registerNs("s", "http://schemas.solarwinds.com/2007/08/informationservice");

               my $nodeUri = $xpc->findvalue("//s:data/s:row/s:c0");

           

               if($_[1] eq "Add"){

                    my $memberDefinitionInfo = SOAP::Data->type('xml'=>

                         "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                         <Name></Name>

                         <Definition>".$nodeUri."</Definition>

                         </MemberDefinitionInfo>"

                    );

                    return $_[0]->Invoke("Orion.Container", "AddDefinition", [$_[2], $memberDefinitionInfo]);

           

              }elsif($_[1] eq "Delete"){

                    $dom = $parser->parse_string($_[0]->QueryXml('SELECT DefinitionID FROM Orion.ContainerMemberDefinition WHERE Definition=@id AND ContainerID=@container', {'id'=>$nodeUri, 'container'=>$_[2]}));

                    my @DEFIDS = $dom->getElementsByTagName("c0");

           

                    if (scalar(@DEFIDS) == 1){

                         return $_[0]->Invoke("Orion.Container", "DeleteDefinition", [scalar($DEFIDS[0]->textContent)]);

                    }else{

                         print "Error! Found ".scalar(@DEFIDS)." definition ID's for that node / container combo.\n";

                    }

               }

          }

           

          my $swis = SW::InformationService->new();

          $swis->outputxml("true");

          $swis->proxy("https://".$hostname.$endpoint);

          $swis->proxy->http_request->authorization_basic($username, $password);

           

          #print manageGroup($swis, "Add", "84", "12929");

          #print manageGroup($swis, "Delete", "84", "12929");

           

          Thanks again.