DX NetOps

Expand all | Collapse all

Creating containers in Spectrum using RESTFul Guide

  • 1.  Creating containers in Spectrum using RESTFul Guide

    Posted Sep 26, 2013 04:12 AM
    Hi All,

    I posted this in a thread but found that after reading the question again, it was not what they were after! So instead of just deleting it - I thought it might be useful to others to get them into understanding some RESTFul concepts!

    How to create containers in Spectrum using RESTful API and perl!
     + Universe
       + Level 1 Container
         + Level 2 Container
           + Level 3 Container
             + Device 1
             + Device 2
    This first thing you need to do is get the 'Universe' model handle as every level 1 container will need this model handle as it's parent. It's pretty easy as the model for Universe is the landscape handle with the last characters being 00004.

    So for example if your landscape handle is 0x100000 then your Universe model will be 0x100004.

    You then need to create the first level container and set it's parent mh to the model handle of the Universe.

    When you post the restful query to create the first container, it will give you the model handle in the results.

    You can then create the 2nd level container and set it's parent as the one of the previously created container.

    You can keep repeating this for any number of levels you need. We use only 3, but you can use up to 6 or more (I've tested with only 6 but practially it's best using 2-3 in most cases!).

    A sample of how to do this via perl is:
    sub createContainer {
    
      my ($containerName, $parentModelHandle) = @_;
      $containerName =~ s/ /%20/g;
      my $landscape = $parentModelHandle;
      substr($landscape, -5) = "00000";
    
      # This creates a container under the specified model handle. The result is the model Handle of either the created container,
      # or the model handle of the existing container!
      my $restful = "curl -s -X POST --header 'content-type: application/xml' --user $username:$passphrase".
          " \"$oneClickServer/spectrum/restful/model?landscapeid=$landscape&mtypeid=0x1002e&attr=0x1006e&val=$containerName&".
          "parentmh=$parentModelHandle\"";
      my $result = `$restful`;
      # print "$restful\n\n$result\n\n";
      if ($result =~ /error=\".*?\"><model mh=\"(.*?)\"\/>/) {
        return $1;
      } else {
        return 'ERROR';
      }
    }
    As you can see we also need to find the landscape as we have more than 1 landscape server. This is easy as it will always be the landcape that the parent model is on.
    We also change all 'spaces' to '%20' for the URL.

    We don't use the RESTful modules yet but you could use them instead of using the curl command. ie. on the server you are running these scripts from you must be able to type curl and it should be available (it says 'curl: try 'curl --help' for more information' if it's installed on a Linux server. I do all the scripting on RHEL so not sure about the Windows differences you have to consider.

    the variables $oneClickServer contains the full URL of the oneclick Server e.g.: 'http://myoneclick.company.com:8080'
    the $username:$passphrase is the user and password for spectrum or another user which has access to do RESTFul queries. Note: if you have unusual characters in your password such as exclamation points, you need to 'escape' it (use '\!')

    The commented line
    # print "$restful\n\n$result\n\n";' is useful for debugging.
    It is useful to see the build REST call as well as the result - good to see if your call is working and what you get back from OneClick.

    The result returned will be the model handle of the new device if it's successful.

    The last level of container will return a model handle which you need to set as the parent of your device model so Spectrum places the device under the correct container!

    You invoke the routing by using:
    my $mh = &createContainer ('First Level Container', '0x100004');
    Hope this helps!

    Regards,

    Frank


  • 2.  RE: Creating containers in Spectrum using RESTFul Guide

    Posted Oct 21, 2013 11:29 AM
    Thank you Frank for sharing this helpful information with the community!
    Mary