/* # <******************* # # Copyright 2017 Juniper Networks, Inc. All rights reserved. # Licensed under the Juniper Networks Script Software License (the "License"). # You may not use this script file except in compliance with the License, which is located at # http://www.juniper.net/support/legal/scriptlicense/ # Unless required by applicable law or otherwise agreed to in writing by the parties, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # *******************> */ version 1.2; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; ns str = "http://exslt.org/strings"; ns func extension = "http://exslt.org/functions"; ns curl extension = "http://xml.libslax.org/curl"; ns xutil extension = "http://xml.libslax.org/xutil"; ns jspace = "http://jspace-utils/asharp@juniper.net"; import "../import/junos.xsl"; /* Junos Space specific context, name and description */ /* @CONTEXT = "/device" */ /* @NAME = "Add route target to policy" */ /* @DESCRIPTION = "Add route target to policy" */ /* @ISLOCAL = "true" */ /* @PASSSPACEAUTHHEADER = "true" */ /* @PASSDEVICECREDENTIALS = "true" */ /* @EXECUTIONTYPE = "GROUPEDEXECUTION" */ var $curl = curl:open(); var $fabric = jspace:fabric(); var $arguments = { { "termname"; "Name of term to add"; } { "commname"; "Name of community to add"; } { "vnitarget"; "Name of vnitarget"; } } main { { if ($fabric) { var $devices = jspace:credentials(); if ($devices) { for-each ($devices/device) { sort host; var $myhost = host; /* find the device using API call */ /* * url, content-type, item-type, condition, item */ var $findDevice = curl:perform($curl,jspace:findItem( "http://" _ $fabric _ ":8080/api/space/device-management/devices", "", "name", "eq", $myhost )); /* get the key of the selected device */ var $deviceKey = $findDevice/data/devices/device/@key; /* make changeRequest using the API */ /* * url, content-type, deviceKey, orig-name, new-name, resync */ var $results = curl:perform($curl,jspace:addTerm( "http://" _ $fabric _ ":8080/api/space/configuration-management/change-requests", "application/vnd.net.juniper.space.configuration-management.change-request+xml;version=2;charset=UTF-8", $deviceKey, $termname, $commname, $vnitarget, "true" )); if ($results/headers/code == "200") { /* job accepted */ expr "Adding term " _ $termname _ "\n"; /* need to see if the commit was successful or not */ /* to get the XML data which is embedded within more xml needs lots of parsing... */ /* parse and convert the raw-data node */ var $parseOne = { uexpr $results/raw-data/.; } var $parseTwo = xutil:string-to-xml($parseOne); var $parseThree := {copy-of $parseTwo;} /* parse and convert the change-request/xmlData node this step is only required if you want to display the configuration that was pushed to the device. */ var $xmlDataOne = { uexpr $parseThree/change-request/xmlData/.; } var $xmlDataTwo = xutil:string-to-xml($xmlDataOne); var $xmlDataThree := {copy-of $xmlDataTwo;} copy-of $xmlDataThree; /* display the change-request/result */ uexpr substring-before($parseThree/change-request/result/.,"rpc-reply: "); } else { expr "Job failed with error code " _ $results/headers/code _ "\n"; } } } } else { expr "Could not identify the fabric!"; } } } /* * Used to identify the fabric that the script has been executed on * knowing this is essential in a multi fabric environment when making * RESTful API calls to Junos Space and using the session cookies for * authentication. */ { if( $CONTEXT ) { var $fabric = { if ( contains( $JSESSIONID, "server") ) { expr jcs:regex("\.(space-[A-Za-z0-9]+):server",$JSESSIONID)[2]; } else { expr jcs:regex("\.(space-[A-Za-z0-9]+)",$JSESSIONID)[2]; } } ; } else { ; } } /* findItem */ { param $url; param $content-type; param $item-type; param $condition; param $item; var $curlData := { "get"; ; $url;
"JSESSIONID=" _ $JSESSIONID _ ";Path=/;";
"JSESSIONIDSSO=" _ $JSESSIONIDSSO _ ";Path=/;"; "(" _ $item-type _ " " _ $condition _ " '" _ $item _ "')"; $content-type; "xml"; } ; } /* add term */ { param $url; param $content-type; param $deviceId; param $termname; param $commname; param $vnitarget; param $syncAfterPush; /* true | false */ var $curlData := { "post"; ; $url; $content-type;
"JSESSIONID=" _ $JSESSIONID _ ";Path=/;";
"JSESSIONIDSSO=" _ $JSESSIONIDSSO _ ";Path=/;"; "xml"; { uexpr ' Add_route_target_to_policy Add_route_target_to_policy vrf-imp ' _ $termname _ ' ' _ $commname _ ' ' _ $commname _ ' ' _ $vnitarget _ ' ]]> ' _ $syncAfterPush _ ' '; } } ; } /* * Can be used to identify selected devices, and their credentials. * Must be used with PASSDEVICECREDENTIALS = "true" annotation for credentials. * Must be used with EXECUTIONTYPE = "GROUPEDEXECUTION" annotation for multiple devices */ { if( $CONTEXT ) { var $splitCredentials = str:split( $credentials, "\\;" ); var $targets := { for-each ( $splitCredentials ) { var $splitCredential = str:split( ., "\\:" ); var $user-target = str:split($splitCredential[1], "\@"); var $host = substring-before( substring-after( $deviceipmap, substring-after( $splitCredential[1], "@" ) _ "\":\"" ), "\"" ); { { expr $user-target[2]; } { expr $user-target[1]; } { expr $splitCredential[2]; } { expr $host; } } } } ; } else { ; } }