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