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