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