From 0eccc169f5dfb9b838893f39efccffe91059416d Mon Sep 17 00:00:00 2001 From: Allan Bowe Date: Sun, 29 Nov 2020 13:56:51 +0100 Subject: [PATCH] feat: adding mv_jobexecute macro (and a fix for mv_getfoldermembers where there are no members) --- viya/mv_getfoldermembers.sas | 8 +- viya/mv_jobexecute.sas | 141 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 viya/mv_jobexecute.sas diff --git a/viya/mv_getfoldermembers.sas b/viya/mv_getfoldermembers.sas index 51dbf71..223b6af 100644 --- a/viya/mv_getfoldermembers.sas +++ b/viya/mv_getfoldermembers.sas @@ -42,7 +42,7 @@ %let &access_token_var=; %end; %put &sysmacroname: grant_type=&grant_type; -%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password +%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password and &grant_type ne sas_services ) ,mac=&sysmacroname @@ -85,10 +85,16 @@ options noquotelenmax; /*data _null_;infile &fname1;input;putlog _infile_;run;*/ libname &libref1 JSON fileref=&fname1; /* now get the followon link to list members */ + %local href; + %let href=0; data _null_; set &libref1..links; if rel='members' then call symputx('href',quote("&base_uri"!!trim(href)),'l'); run; + %if &href=0 %then %do; + %put NOTE:;%put NOTE- No members found in &root!!;%put NOTE-; + %return; + %end; %local fname2 libref2; %let fname2=%mf_getuniquefileref(); %let libref2=%mf_getuniquelibref(); diff --git a/viya/mv_jobexecute.sas b/viya/mv_jobexecute.sas new file mode 100644 index 0000000..4334b23 --- /dev/null +++ b/viya/mv_jobexecute.sas @@ -0,0 +1,141 @@ +/** + @file + @brief Executes a SAS Viya Job + @details Triggers a SAS Viya Job, with optional URL parameters, using + the JES web app. + + First, compile the macros: + + filename mc url + "https://raw.githubusercontent.com/sasjs/core/main/all.sas"; + %inc mc; + + Then, execute the job! + + %mv_jobexecute(path=/Public/folder + ,name=somejob + ) + + + @param access_token_var= The global macro variable to contain the access token + @param grant_type= valid values: + * password + * authorization_code + * detect - will check if access_token exists, if not will use sas_services if + a SASStudioV session else authorization_code. Default option. + * sas_services - will use oauth_bearer=sas_services + + @param path= The SAS Drive path to the job being executed + @param name= The name of the job to execute + @param params= A macro quoted string to append to the URL + @param contextName= Context name with which to run the job. + Default = `SAS Job Execution compute context` + + + @version VIYA V.03.04 + @author Allan Bowe, source: https://github.com/sasjs/core + +

Dependencies

+ @li mp_abort.sas + @li mf_getplatform.sas + @li mf_getuniquefileref.sas + @li mv_getfoldermembers.sas + +**/ + +%macro mv_jobexecute(path=0 + ,name=0 + ,contextName=SAS Job Execution compute context + ,access_token_var=ACCESS_TOKEN + ,grant_type=sas_services + ); +%local oauth_bearer; +%if &grant_type=detect %then %do; + %if %symexist(&access_token_var) %then %let grant_type=authorization_code; + %else %let grant_type=sas_services; +%end; +%if &grant_type=sas_services %then %do; + %let oauth_bearer=oauth_bearer=sas_services; + %let &access_token_var=; +%end; +%put &sysmacroname: grant_type=&grant_type; +%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password + and &grant_type ne sas_services + ) + ,mac=&sysmacroname + ,msg=%str(Invalid value for grant_type: &grant_type) +) + +%mp_abort(iftrue=("&path"="0") + ,mac=&sysmacroname + ,msg=%str(Path not provided) +) +%mp_abort(iftrue=("&name"="0") + ,mac=&sysmacroname + ,msg=%str(Job Name not provided) +) + +options noquotelenmax; + +%local base_uri; /* location of rest apis */ +%let base_uri=%mf_getplatform(VIYARESTAPI); + +data;run; +%local foldermembers; +%let foldermembers=&syslast; +%mv_getfoldermembers(root=&path + ,access_token_var=&access_token_var + ,grant_type=&grant_type + ,outds=&foldermembers +) + +%local joburi; +%let joburi=0; +data _null_; + set &foldermembers; + if name="&name" and uri=:'/jobDefinitions/definitions' + then call symputx('joburi',uri); +run; + +%mp_abort(iftrue=("&joburi"="0") + ,mac=&sysmacroname + ,msg=%str(Job &path/&name not found) +) + +/* prepare request*/ +%local fname0 fname1; +%let fname0=%mf_getuniquefileref(); +%let fname1=%mf_getuniquefileref(); + +data _null_; + file &fname0; + put '{"jobDefinitionUri": "'@@; + put "&joburi"@@; + put '","arguments":{"_contextName":"'@@; + put "&contextName"@@; + put '"}}'; +run; + +proc http method='POST' in=&fname0 out=&fname1 &oauth_bearer + url="&base_uri/jobExecution/jobs"; + headers "Content-Type"="application/vnd.sas.job.execution.job.request+json" + "Accept"="application/vnd.sas.job.execution.job+json" + %if &grant_type=authorization_code %then %do; + "Authorization"="Bearer &&&access_token_var" + %end; + ; +run; +/*data _null_;infile &fname1;input;putlog _infile_;run;*/ + +%mp_abort(iftrue=( + &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 + ) + ,mac=&sysmacroname + ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE) +) + +/* clear refs */ +filename &fname0 clear; +filename &fname1 clear; + +%mend; \ No newline at end of file