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