Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_deletefoldermember.sas
Go to the documentation of this file.
1 /**
2  @file mv_deletefoldermember.sas
3  @brief Deletes an item in a Viya folder
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 item to be deleted
15  @param name= The name of the item to be deleted
16  @param contenttype= The contenttype of the item, eg: file, jobDefinition
17  @param access_token_var= The global macro variable to contain the access token
18  @param grant_type= valid values are "password" or "authorization_code" (unquoted).
19  The default is "detect" (which will run in Studio 5+ without a token).
20 
21 
22  @version VIYA V.03.04
23  @author Allan Bowe
24  @source https://github.com/sasjs/core
25 
26  <h4> Dependencies </h4>
27  @li mp_abort.sas
28  @li mf_getplatform.sas
29  @li mf_getuniquefileref.sas
30  @li mf_getuniquelibref.sas
31  @li mf_isblank.sas
32 
33 **/
34 
35 %macro mv_deletefoldermember(path=
36  ,name=
37  ,contenttype=
38  ,access_token_var=ACCESS_TOKEN
39  ,grant_type=sas_services
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 %put &sysmacroname: grant_type=&grant_type;
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=(%mf_isblank(&name)=1)
62  ,mac=&sysmacroname
63  ,msg=%str(name value must be provided)
64 )
65 %mp_abort(iftrue=(%length(&path)=1)
66  ,mac=&sysmacroname
67  ,msg=%str(path value must be provided)
68 )
69 
70 options noquotelenmax;
71 
72 %local base_uri; /* location of rest apis */
73 %let base_uri=%mf_getplatform(VIYARESTAPI);
74 
75 %put &sysmacroname: fetching details for &path ;
76 %local fname1;
77 %let fname1=%mf_getuniquefileref();
78 proc http method='GET' out=&fname1 &oauth_bearer
79  url="&base_uri/folders/folders/@item?path=&path";
80 %if &grant_type=authorization_code %then %do;
81  headers "Authorization"="Bearer &&&access_token_var";
82 %end;
83 run;
84 %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
85  %put &sysmacroname: Folder &path NOT FOUND - nothing to delete!;
86  %return;
87 %end;
88 %else %if &SYS_PROCHTTP_STATUS_CODE ne 200 %then %do;
89  /*data _null_;infile &fname1;input;putlog _infile_;run;*/
90  %mp_abort(mac=&sysmacroname
91  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
92  )
93 %end;
94 
95 %put &sysmacroname: grab the follow on link ;
96 %local libref1;
97 %let libref1=%mf_getuniquelibref();
98 libname &libref1 JSON fileref=&fname1;
99 data _null_;
100  set &libref1..links;
101  if rel='members' then call symputx('mref',quote("&base_uri"!!trim(href)),'l');
102 run;
103 
104 /* get the children */
105 %local fname1a;
106 %let fname1a=%mf_getuniquefileref();
107 proc http method='GET' out=&fname1a &oauth_bearer
108  url=%unquote(%superq(mref));
109 %if &grant_type=authorization_code %then %do;
110  headers "Authorization"="Bearer &&&access_token_var";
111 %end;
112 run;
113 %put &=SYS_PROCHTTP_STATUS_CODE;
114 %local libref1a;
115 %let libref1a=%mf_getuniquelibref();
116 libname &libref1a JSON fileref=&fname1a;
117 %local uri found;
118 %let found=0;
119 %put Getting object uri from &libref1a..items;
120 data _null_;
121  set &libref1a..items;
122  if contenttype="&contenttype" and upcase(name)="%upcase(&name)" then do;
123  call symputx('uri',uri,'l');
124  call symputx('found',1,'l');
125  end;
126 run;
127 %if &found=0 %then %do;
128  %put NOTE:;%put NOTE- &sysmacroname: &path/&name NOT FOUND;%put NOTE- ;
129  %return;
130 %end;
131 proc http method="DELETE" url="&base_uri&uri" &oauth_bearer;
132  headers
133 %if &grant_type=authorization_code %then %do;
134  "Authorization"="Bearer &&&access_token_var"
135 %end;
136  "Accept"="*/*";/**/
137 run;
138 %if &SYS_PROCHTTP_STATUS_CODE ne 204 %then %do;
139  data _null_; infile &fname2; input; putlog _infile_;run;
140  %mp_abort(mac=&sysmacroname
141  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
142  )
143 %end;
144 %else %put &sysmacroname: &path/&name(&contenttype) successfully deleted;
145 
146 /* clear refs */
147 filename &fname1 clear;
148 libname &libref1 clear;
149 filename &fname1a clear;
150 libname &libref1a clear;
151 
152 %mend;