{"id":5940,"date":"2016-07-11T17:55:27","date_gmt":"2016-07-11T07:55:27","guid":{"rendered":"http:\/\/nsrd.info\/blog\/?p=5940"},"modified":"2018-12-11T10:52:59","modified_gmt":"2018-12-11T00:52:59","slug":"basics-experimenting-with-the-rest-api-in-networker","status":"publish","type":"post","link":"https:\/\/nsrd.info\/blog\/2016\/07\/11\/basics-experimenting-with-the-rest-api-in-networker\/","title":{"rendered":"Basics &#8211; Experimenting with the REST API in NetWorker"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>As I mentioned&nbsp;in the <a href=\"https:\/\/nsrd.info\/blog\/2016\/07\/08\/networker-9-0-sp1\/\" target=\"_blank\">previous article, NetWorker 9 SP1<\/a> has introduced a REST API. I&#8217;ve never previously got&nbsp;around to playing with REST API interfaces, but as is always the case with programming, you either do it because you&#8217;re getting paid to or because&nbsp;it&#8217;s something that&nbsp;strikes you as interesting.<\/p>\n<p>Accessing NetWorker via a REST API does indeed strike me as interesting. Even more so if I can do it using my favourite language, Perl.<\/p>\n<p>This is by no means meant to be a programming tutorial, nor am I claiming&nbsp;to be the first to experiment with it.&nbsp;If you want to check out an in-development use of the REST API, check out Karsten Bott&#8217;s PowerShell work to date <a href=\"https:\/\/community.emc.com\/blogs\/bottk\/2016\/07\/07\/nwpsresttoolkitgetting-started-with-powershell-and-the-networker-901-rest-api\" target=\"_blank\">over at the NetWorker Community Page<\/a>. This post covers just the process of bootstrapping myself to the point I have&nbsp;working code \u2013 the real fun and work comes next!<\/p>\n<p><a href=\"https:\/\/nsrd.info\/blog\/2016\/07\/11\/basics-experimenting-with-the-rest-api-in-networker\/male-hand-pressing-api-key-button\/\" rel=\"attachment wp-att-5945\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5945\" src=\"https:\/\/nsrd.info\/blog\/wp-content\/uploads\/2016\/07\/bigStock-API.jpg\" alt=\"REST API\" width=\"900\" height=\"675\" srcset=\"https:\/\/nsrd.info\/blog\/wp-content\/uploads\/2016\/07\/bigStock-API.jpg 900w, https:\/\/nsrd.info\/blog\/wp-content\/uploads\/2016\/07\/bigStock-API-300x225.jpg 300w, https:\/\/nsrd.info\/blog\/wp-content\/uploads\/2016\/07\/bigStock-API-768x576.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<h3>What you&#8217;ll need<\/h3>\n<p>For this to work, you&#8217;ll need a suitably recent&nbsp;Perl 5.x implementation. I&#8217;m&nbsp;practicing on my&nbsp;Mac laptop, running Perl 5.18.2.<\/p>\n<p>You&#8217;ll also need the following modules:<\/p>\n<ul>\n<li>MIME::Base64<\/li>\n<li>REST::Client<\/li>\n<li>Data::Dumper<\/li>\n<li>JSON<\/li>\n<\/ul>\n<p>And of&nbsp;course, you&#8217;ll need a NetWorker server running NetWorker 9, SP1.<\/p>\n<h3>Getting started<\/h3>\n<p>I&#8217;m getting old an&nbsp;crotchety when it comes to resolving dependencies. When I was younger I used to manually download each CPAN module I needed, try to compile, strike dependency requirements, recurse down&nbsp;<em>those<\/em> modules and keep going until&nbsp;I&#8217;d either solved all the dependencies or threw the computer out the window and became a monk.<\/p>\n<p>So to get the above modules I invoked the&nbsp;<em>cpan install<\/em> function on my Mac as follows:<\/p>\n<pre>pmdg@ganymede$ <strong>cpan install MIME::Base64<\/strong>\npmdg@ganymede$ <strong>cpan install REST::Client<\/strong>\npmdg@ganymede$ <strong>cpan install Data::Dumper<\/strong>\npmdg@ganymede$ <strong>cpan install JSON<\/strong><\/pre>\n<p>There was a little bit of an exception thrown in the REST::Client installation about packages that could be used for testing, but overall the CPAN based installer worked well and saved me a lot of headaches.<\/p>\n<h3>The code<\/h3>\n<p>The code itself is extremely simple \u2013 as I mentioned this is a proof of concept, not intended to be an interface as such. It&#8217;s from here I&#8217;ll start&nbsp;as I play around in greater detail. My goal for the code was as follows:<\/p>\n<ul>\n<li>Prompt for username and password<\/li>\n<li>Connect via REST API<\/li>\n<li>Retrieve a complete list of clients<\/li>\n<li>Dump out the data in a basic format to confirm it was successful<\/li>\n<\/ul>\n<p>The actual code therefore is:<\/p>\n<pre>pmdg@ganymede$ <strong>cat tester.pl<\/strong>\n\n#!\/usr\/bin\/perl -w\n\nuse strict;\nuse MIME::Base64();\nuse REST::Client;\nuse Data::Dumper;\nuse JSON;\n\nmy $username = \"\";\nmy $password = \"\";\n\nprint \"Username: \";\n$username = &lt;&gt;;\nchomp $username;\n\nprint \"Password: \";\n$password = &lt;&gt;;\nchomp $password;\n\nmy $encoded = MIME::Base64::encode($username . \":\" . $password);\n$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;\nmy $client = REST::Client-&gt;new();\nmy $headers = { Accept =&gt; 'application\/json', Authorization =&gt; 'Basic ' . $encoded};\n$client-&gt;setHost('https:\/\/orilla.turbamentis.int:9090');\n$client-&gt;GET('\/nwrestapi\/v1\/global\/clients',$headers);\nmy $response = from_json($client-&gt;responseContent);\nprint Dumper($response);\n\n<\/pre>\n<h3>Notes on the Code<\/h3>\n<p>If you&#8217;re copying and pasting the code, about&nbsp;the only thing you should need to change is the hostname in the line starting <strong>$client-&gt;setHost<\/strong>.<\/p>\n<p>It&#8217;s not particularly secure in the password prompt as Perl will&nbsp;automatically echo the password as you&#8217;re entering it. There are ways of disabling this echo, but they require the Term::Readkey library and that may not be readily available on all systems.&nbsp;So just keep this in mind&#8230;<\/p>\n<h3>The Results<\/h3>\n<p>Here&#8217;s the starting output for the code:<\/p>\n<pre>pmdg@ganymede$ <strong>.\/tester.pl<\/strong>\nUsername: <strong>administrator<\/strong>\nPassword:&nbsp;<strong>MySuperSecretPassword<\/strong>\n$VAR1 = {\n          'clients' =&gt; [\n                         {\n                           'ndmpMultiStreamsEnabled' =&gt; bless( do{\\(my $o = 0)}, 'JSON::PP::Boolean' ),\n                           'ndmpVendorInformation' =&gt; [],\n                           'protectionGroups' =&gt; [],\n                           'resourceId' =&gt; {\n                                                'sequence' =&gt; 79,\n                                                'id' =&gt; '198.0.72.12.0.0.0.0.132.105.45.87.192.168.100.4'\n                                           },\n                           'links' =&gt; [\n                                        {\n                                           'rel' =&gt; 'item',\n                                           'href' =&gt; 'https:\/\/orilla.turbamentis.int:9090\/nwrestapi\/v1\/global\/clients\/198.0.72.12.0.0.0.0.132.105.45.87.192.168.100.4'\n                                        }\n                                      ],\n                           'parallelSaveStreamsPerSaveSet' =&gt; $VAR1-&gt;{'clients'}[0]{'ndmpMultiStreamsEnabled'},\n                           'hostname' =&gt; 'archon.turbamentis.int',\n\n<\/pre>\n<p><em>And so on&#8230;<\/em><\/p>\n<h3>In Summary<\/h3>\n<p>The&nbsp;script isn&#8217;t pretty at the moment, but I&nbsp;wanted to get it out there as an example. As I hack&nbsp;around with it and get more functionality, I&#8217;ll provide updates.<\/p>\n<p>Hopefully however you can see that it&#8217;s pretty straight-forward overall to access the REST API!<\/p>\n<h3>References<\/h3>\n<ul>\n<li><a href=\"https:\/\/support.emc.com\/docu71091_NetWorker_9.0.1_REST_API_Getting_Started_Guide.pdf?language=en_US&amp;language=en_US\" target=\"_blank\"><strong>REST API Getting Started Guide<\/strong><\/a><\/li>\n<li><strong><a href=\"https:\/\/support.emc.com\/docu71092_NetWorker_9.0.1_REST_API_Reference_Guide.pdf?language=en_US&amp;language=en_US\" target=\"_blank\">REST API Reference Guide<\/a><\/strong><\/li>\n<li><a href=\"https:\/\/developer.atlassian.com\/fecrudev\/integration-tutorials\/writing-a-rest-client-in-perl\" target=\"_blank\"><strong>Writing a REST Client in Perl<\/strong><\/a> &#8211; I basically&nbsp;used this as supplementary&nbsp;explanations to the CPAN documentation for the REST::Client module.<\/li>\n<li><strong><a href=\"https:\/\/community.emc.com\/blogs\/bottk\/2016\/07\/07\/nwpsresttoolkitgetting-started-with-powershell-and-the-networker-901-rest-api\" target=\"_blank\">Karsten&#8217;s PowerShell REST Interface<\/a><\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview As I mentioned&nbsp;in the previous article, NetWorker 9 SP1 has introduced a REST API. I&#8217;ve never previously got&nbsp;around to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,16,20],"tags":[1249,1318,1317],"class_list":["post-5940","post","type-post","status-publish","format-standard","hentry","category-basics","category-networker","category-scripting","tag-networker","tag-perl","tag-rest-api"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pKpIN-1xO","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/5940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/comments?post=5940"}],"version-history":[{"count":7,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/5940\/revisions"}],"predecessor-version":[{"id":7409,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/5940\/revisions\/7409"}],"wp:attachment":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/media?parent=5940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/categories?post=5940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/tags?post=5940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}