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