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