Macros for SAS Application Developers
https://github.com/sasjs/core
mv_deletejes.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Deletes a Viya Job, if it exists
4  @details If not executed in Studio 5+ will expect oauth token in a global
5  macro variable (default ACCESS_TOKEN).
6 
7  filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
8  %inc mc;
9 
10  %mv_createwebservice(path=/Public/test, name=blah)
11  %mv_deletejes(path=/Public/test, name=blah)
12 
13 
14  @param [in] path= ()
15  The full path of the folder containing the Job Execution Service
16  @param [in] name= The name of the Job Execution Service to be deleted
17  @param [in] access_token_var= (ACCESS_TOKEN)
18  The global macro variable to contain the access token
19  @param [in] grant_type= (sas_services)
20  Valid values are "password" or "authorization_code" (unquoted).
21 
22 
23  @version VIYA V.03.04
24  @author Allan Bowe, source: https://github.com/sasjs/core
25 
26  <h4> SAS Macros </h4>
27  @li mp_abort.sas
28  @li mf_getplatform.sas
29  @li mf_getuniquefileref.sas
30  @li mf_getuniquelibref.sas
31  @li mf_isblank.sas
32 
33 **/
34 
35 %macro mv_deletejes(path=
36  ,name=
37  ,access_token_var=ACCESS_TOKEN
38  ,grant_type=sas_services
39  );
40 %local oauth_bearer;
41 %if &grant_type=detect %then %do;
42  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
43  %else %let grant_type=sas_services;
44 %end;
45 %if &grant_type=sas_services %then %do;
46  %let oauth_bearer=oauth_bearer=sas_services;
47  %let &access_token_var=;
48 %end;
49 
50 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
51  and &grant_type ne sas_services
52  )
53  ,mac=&sysmacroname
54  ,msg=%str(Invalid value for grant_type: &grant_type)
55 )
56 %mp_abort(iftrue=(%mf_isblank(&path)=1)
57  ,mac=&sysmacroname
58  ,msg=%str(path value must be provided)
59 )
60 %mp_abort(iftrue=(%mf_isblank(&name)=1)
61  ,mac=&sysmacroname
62  ,msg=%str(name value must be provided)
63 )
64 %mp_abort(iftrue=(%length(&path)=1)
65  ,mac=&sysmacroname
66  ,msg=%str(path value must be provided)
67 )
68 
69 options noquotelenmax;
70 %local base_uri; /* location of rest apis */
71 %let base_uri=%mf_getplatform(VIYARESTAPI);
72 
73 %put &sysmacroname: fetching details for &path ;
74 %local fname1;
75 %let fname1=%mf_getuniquefileref();
76 proc http method='GET' out=&fname1 &oauth_bearer
77  url="&base_uri/folders/folders/@item?path=&path";
78 %if &grant_type=authorization_code %then %do;
79  headers "Authorization"="Bearer &&&access_token_var";
80 %end;
81 run;
82 %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
83  %put &sysmacroname: Folder &path NOT FOUND - nothing to delete!;
84  %return;
85 %end;
86 %else %if &SYS_PROCHTTP_STATUS_CODE ne 200 %then %do;
87  /*data _null_;infile &fname1;input;putlog _infile_;run;*/
88  %mp_abort(mac=&sysmacroname
89  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
90  )
91 %end;
92 
93 %put &sysmacroname: grab the follow on link ;
94 %local libref1;
95 %let libref1=%mf_getuniquelibref();
96 libname &libref1 JSON fileref=&fname1;
97 data _null_;
98  set &libref1..links;
99  if rel='members' then call symputx('mref',quote("&base_uri"!!trim(href)),'l');
100 run;
101 
102 /* get the children */
103 %local fname1a;
104 %let fname1a=%mf_getuniquefileref();
105 proc http method='GET' out=&fname1a &oauth_bearer
106  url=%unquote(%superq(mref));
107 %if &grant_type=authorization_code %then %do;
108  headers "Authorization"="Bearer &&&access_token_var";
109 %end;
110 run;
111 %put &=SYS_PROCHTTP_STATUS_CODE;
112 %local libref1a;
113 %let libref1a=%mf_getuniquelibref();
114 libname &libref1a JSON fileref=&fname1a;
115 %local uri found;
116 %let found=0;
117 %put Getting object uri from &libref1a..items;
118 data _null_;
119  length contenttype name $1000;
120  set &libref1a..items;
121  if contenttype='jobDefinition' and upcase(name)="%upcase(&name)" then do;
122  call symputx('uri',cats("&base_uri",uri),'l');
123  call symputx('found',1,'l');
124  end;
125 run;
126 %if &found=0 %then %do;
127  %put NOTE:;%put NOTE- &sysmacroname: &path/&name NOT FOUND;%put NOTE- ;
128  %return;
129 %end;
130 proc http method="DELETE" url="&uri" &oauth_bearer;
131  headers
132 %if &grant_type=authorization_code %then %do;
133  "Authorization"="Bearer &&&access_token_var"
134 %end;
135  "Accept"="*/*";/**/
136 run;
137 %if &SYS_PROCHTTP_STATUS_CODE ne 204 %then %do;
138  data _null_; infile &fname2; input; putlog _infile_;run;
139  %mp_abort(mac=&sysmacroname
140  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
141  )
142 %end;
143 %else %put &sysmacroname: &path/&name successfully deleted;
144 
145 /* clear refs */
146 filename &fname1 clear;
147 libname &libref1 clear;
148 filename &fname1a clear;
149 libname &libref1a clear;
150 
151 %mend mv_deletejes;