Macros for SAS Application Developers
https://github.com/sasjs/core
mv_createfolder.sas
Go to the documentation of this file.
1 /**
2  @file mv_createfolder.sas
3  @brief Creates a viya folder if that folder does not already exist
4  @details Creates a viya folder by checking if each parent folder exists, and
5  recursively creating children if needed.
6  Usage:
7 
8  %mv_createfolder(path=/Public)
9 
10 
11  @param [in] path= The full path of the folder to be created
12  @param [in] access_token_var= The global macro variable to contain the access
13  token, if using authorization_code grant type.
14  @param [in] grant_type= (sas_services) Valid values are:
15  @li password
16  @li authorization_code
17  @li sas_services
18 
19  @param [in] mdebug=(0) set to 1 to enable DEBUG messages
20 
21  @version VIYA V.03.04
22  @author Allan Bowe, source: https://github.com/sasjs/core
23 
24  <h4> SAS Macros </h4>
25  @li mp_abort.sas
26  @li mf_getuniquefileref.sas
27  @li mf_getuniquelibref.sas
28  @li mf_isblank.sas
29  @li mf_getplatform.sas
30  @li mfv_existfolder.sas
31 
32 
33 **/
34 
35 %macro mv_createfolder(path=
36  ,access_token_var=ACCESS_TOKEN
37  ,grant_type=sas_services
38  ,mdebug=0
39  );
40 %local dbg;
41 %if &mdebug=1 %then %do;
42  %put &sysmacroname entry vars:;
43  %put _local_;
44 %end;
45 %else %let dbg=*;
46 
47 %if %mfv_existfolder(&path)=1 %then %do;
48  %put &sysmacroname: &path already exists;
49  %return;
50 %end;
51 
52 %local oauth_bearer;
53 %if &grant_type=detect %then %do;
54  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
55  %else %let grant_type=sas_services;
56 %end;
57 %if &grant_type=sas_services %then %do;
58  %let oauth_bearer=oauth_bearer=sas_services;
59  %let &access_token_var=;
60 %end;
61 
62 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
63  and &grant_type ne sas_services
64  )
65  ,mac=&sysmacroname
66  ,msg=%str(Invalid value for grant_type: &grant_type)
67 )
68 
69 %mp_abort(iftrue=(%mf_isblank(&path)=1)
70  ,mac=&sysmacroname
71  ,msg=%str(path value must be provided)
72 )
73 %mp_abort(iftrue=(%length(&path)=1)
74  ,mac=&sysmacroname
75  ,msg=%str(path value must be provided)
76 )
77 
78 options noquotelenmax;
79 
80 %local subfolder_cnt; /* determine the number of subfolders */
81 %let subfolder_cnt=%sysfunc(countw(&path,/));
82 
83 %local href; /* resource address (none for root) */
84 %let href="/folders/folders?parentFolderUri=/folders/folders/none";
85 
86 %local base_uri; /* location of rest apis */
87 %let base_uri=%mf_getplatform(VIYARESTAPI);
88 
89 %local x newpath subfolder;
90 %do x=1 %to &subfolder_cnt;
91  %let subfolder=%scan(&path,&x,%str(/));
92  %let newpath=&newpath/&subfolder;
93 
94  %local fname1;
95  %let fname1=%mf_getuniquefileref();
96 
97  %put &sysmacroname checking to see if &newpath exists;
98  proc http method='GET' out=&fname1 &oauth_bearer
99  url="&base_uri/folders/folders/@item?path=&newpath";
100  %if &grant_type=authorization_code %then %do;
101  headers "Authorization"="Bearer &&&access_token_var";
102  %end;
103  run;
104  %local libref1;
105  %let libref1=%mf_getuniquelibref();
106  libname &libref1 JSON fileref=&fname1;
107  %mp_abort(
108  iftrue=(
109  &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 404
110  )
111  ,mac=&sysmacroname
112  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
113  )
114  %if &mdebug=1 %then %do;
115  %put &sysmacroname following check to see if &newpath exists:;
116  %put _local_;
117  data _null_;
118  set &fname1;
119  input;
120  putlog _infile_;
121  run;
122  %end;
123  %if &SYS_PROCHTTP_STATUS_CODE=200 %then %do;
124  %*put &sysmacroname &newpath exists so grab the follow on link ;
125  data _null_;
126  set &libref1..links;
127  if rel='createChild' then
128  call symputx('href',quote(cats("&base_uri",href)),'l');
129  run;
130  %end;
131  %else %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
132  %put &sysmacroname &newpath not found - creating it now;
133  %local fname2;
134  %let fname2=%mf_getuniquefileref();
135  data _null_;
136  length json $1000;
137  json=cats("'"
138  ,'{"name":'
139  ,quote(trim(symget('subfolder')))
140  ,',"description":'
141  ,quote("&subfolder, created by &sysmacroname")
142  ,',"type":"folder"}'
143  ,"'"
144  );
145  call symputx('json',json,'l');
146  run;
147 
148  proc http method='POST'
149  in=&json
150  out=&fname2
151  &oauth_bearer
152  url=%unquote(%superq(href));
153  headers
154  %if &grant_type=authorization_code %then %do;
155  "Authorization"="Bearer &&&access_token_var"
156  %end;
157  'Content-Type'='application/vnd.sas.content.folder+json'
158  'Accept'='application/vnd.sas.content.folder+json';
159  run;
160  %put &=SYS_PROCHTTP_STATUS_CODE;
161  %put &=SYS_PROCHTTP_STATUS_PHRASE;
162  %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
163  ,mac=&sysmacroname
164  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
165  )
166  %local libref2;
167  %let libref2=%mf_getuniquelibref();
168  libname &libref2 JSON fileref=&fname2;
169  %put &sysmacroname &newpath now created. Grabbing the follow on link ;
170  data _null_;
171  set &libref2..links;
172  if rel='createChild' then do;
173  call symputx('href',quote(cats("&base_uri",href)),'l');
174  &dbg put (_all_)(=);
175  end;
176  run;
177 
178  libname &libref2 clear;
179  filename &fname2 clear;
180  %end;
181  filename &fname1 clear;
182  libname &libref1 clear;
183 %end;
184 %mend mv_createfolder;