Forum Discussion

ahmed_2340's avatar
ahmed_2340
Icon for Nimbostratus rankNimbostratus
Sep 26, 2012

How to Add new member to string class without recreating the class

How do I add a new member to a class without updating the entire class in Perl? On the lab there is an Add button and you can add new members without disturbing the older ones. i am looking for the same functionality in a perl API.

 

Ther perl query string class on devCentral has a script but this recreates the entire class and deletes all the previous members when adding a new one,

 

1 Reply

  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus

     

    here's a fragment of the code I've used in the past.

     

     

    To add a new entry.. (v9)

     

     

    
    sub addClassEntry {
      my($classAPI, $className, $classEntry)=@_;
      my @stringInfoList=();
    
      print "addClassEntry:\n";
    
      my $stringInfo = {name => $className, members => [$classEntry]};
      push(@stringInfoList, $stringInfo);
    
      print "addClassEntry: calling class->add_string_class_member()\n";
    
      $soapResponse=$classAPI->add_string_class_member(SOAP::Data->name(class_members => [$stringInfo]));
    
      my($status,$text)=&checkResponse($soapResponse);
    
      print "addClassEntry: status[$status] text [$text]\n";
      if(($status!=1)||($text ne "OK")) {
        print "addClassEntry: status $status error [$text] from add_string_class_member()\n";
        exit(-1);
      }
      print "addClassEntry: status $status success [$text] from add_string_class_member()\n";
    
    }
    
     

     

     

    Or you can update an existing entry (With key/value pairs only which means v10+) (Or add a new key/value) with these two

     

     

     
    sub updateClass {
      my($classAPI, $className, %files)=@_;
    
      print "updateClass:\n";
      my @members=();
      foreach $filename (keys %files) {
        my $encode64=$files{$filename}->{BASE64};
    
        print "updateClass: pushing [$encode64]\n";
    
        push(@members, $encode64);
      }
    
      my $stringInfo = {name => $className, members => [@members]};
    
    
      print "updateClass: calling class->modify_string_class() with\n";
      print Dumper($stringInfo);
    
      $soapResponse=$classAPI->modify_string_class(SOAP::Data->name(classes => [$stringInfo]));
    
      my($status,$text)=&checkResponse($soapResponse);
      if(($status!=1)||($text ne "OK")) {
        print "updateClass: status $status error [$text] from modify_string_class()\n";
        exit(-1);
      }
      print "updateClass: status $status success [$text] from modify_string_class()\n";
    }
    
    sub addClassEntry {
      my($classAPI, $className, $classMembers, $classValues)=@_;
      my @stringInfoList=();
    
      print "addClassEntry: ($className) ($classMembers) ($classValues)\n";
    
      my $class_members = {name => $className, members => [$classMembers]};
      push(@stringInfoList, $class_members);
    
      print "addClassEntry: calling class->set_string_class_member_data_value()\n";
    
      $soapResponse=$classAPI->add_string_class_member(SOAP::Data->name(class_members => [$class_members]));
      $soapResponse=$classAPI->set_string_class_member_data_value(SOAP::Data->name(class_members => [$class_members]), SOAP::Data->name(val
    ues => [[$classValues]]));
    
      my($status,$text)=&checkResponse($soapResponse);
    
      print "addClassEntry: status[$status] text [$text]\n";
      if(($status!=1)||($text ne "OK")) {
        print "addClassEntry: status $status error [$text] from add_string_class_member()\n";
        exit(-1);
      }
      print "addClassEntry: status $status success [$text] from add_string_class_member()\n";
    
    }
    
    

     

     

     

    Don't forget to save & sync afterwards though...

     

     

     
    
    ----------------------------------------------------------------------------
     sub saveConfig
    ----------------------------------------------------------------------------
    sub saveConfig {
      my($configAPI)=@_;
    
      print "saveConfig:\n";
      my $soapResponse = $configAPI->save_configuration(SOAP::Data->name(filename => ""), SOAP::Data->name(save_flag => "SAVE_HIGH_LEVEL_CO
    NFIG"));
      my($status,$text)=&checkResponse($soapResponse);
      if(($status!=1)||($text ne "OK")) {
        print "saveConfig: status $status error [$text] from save_configuration(SAVE_HIGH_LEVEL_CONFIG)\n";
        return;
      }
      print "saveConfig: status $status success [$text] from save_configuration(SAVE_HIGH_LEVEL_CONFIG)\n";
    
      $soapResponse = $configAPI->save_configuration(SOAP::Data->name(filename => ""), SOAP::Data->name(save_flag => "SAVE_BASE_LEVEL_CONFI
    G"));
      ($status,$text)=&checkResponse($soapResponse);
      if(($status!=1)||($text ne "OK")) {
        print "saveConfig: status $status error [$text] from save_configuration(SAVE_BASE_LEVEL_CONFIG)\n";
        return;
      }
      print "saveConfig: status $status success [$text] from save_configuration(SAVE_BASE_LEVEL_CONFIG)\n";
    
    
      print "saveConfig:\n";
    }
    
    ----------------------------------------------------------------------------
     sub syncConfig
    ----------------------------------------------------------------------------
    sub syncConfig {
      my($configAPI, $synctype)=@_;
    
      print "syncConfig:\n";
      my $soapResponse = $configAPI->synchronize_configuration(SOAP::Data->name(sync_flag => $synctype));
      my($status,$text)=&checkResponse($soapResponse);
      if(($status!=1)||($text ne "OK")) {
        print "syncConfig: status $status error [$text] from synchronize_configuration($synctype)\n";
        return;
      }
      print "syncConfig: status $status success [$text] from synchronize_configuration($synctype)\n";
    
      print "syncConfig:\n";
    }
    
    

     

     

    And you probably want to only do this on the active unit... (Syncing from standby to active can lead to nasty surprises)

     

     

     
    
    sub activeUnit {
      my ($FailoverAPI, $icServer)=@_;
    
      print "activeUnit:\n";
      my $soapResponse = $FailoverAPI->get_failover_state();
      my($status,$text)=&checkResponse($soapResponse);
      if(($status!=1)||($text ne "OK")) {
        print "activeUnit: status $status error [$text] from get_failover_state()\n";
        return;
      }
      print "activeUnit: status $status success [$text] from get_failover_state()\n";
      print Dumper($soapResponse->result);
    
      $fostate=$soapResponse->result;
      print "activeUnit: status $status fostate [$fostate] from get_failover_state()\n";
    
      return ($fostate eq "FAILOVER_STATE_ACTIVE");
    }