Loading...
Searching...
No Matches
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 @param [out] outds=(_null_) Optionally create an output dataset which will
21 contain the uri (self_uri) of the created (and parent) folder.
22
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 mfv_getpathuri.sas
30 @li mf_getplatform.sas
31 @li mfv_existfolder.sas
32
33
34**/
35
36%macro mv_createfolder(path=
37 ,access_token_var=ACCESS_TOKEN
38 ,grant_type=sas_services
39 ,mdebug=0
40 ,outds=_null_
41 );
42%local dbg;
43%if &mdebug=1 %then %do;
44 %put &sysmacroname entry vars:;
45 %put _local_;
46%end;
47%else %let dbg=*;
48
49%mp_abort(
50 iftrue=(&syscc ne 0),
51 msg=Cannot enter &sysmacroname with syscc=&syscc
52)
53
54%if %mfv_existfolder(&path)=1 %then %do;
55 %&dbg.put &sysmacroname: &path already exists;
56 data &outds;
57 self_uri="%mfv_getpathuri(&path)";
58 output;
59 stop;
60 run;
61 %return;
62%end;
63%mp_abort(iftrue=(&syscc ne 0),msg=syscc=&syscc when folder checking)
64
65%local oauth_bearer;
66%if &grant_type=detect %then %do;
67 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
68 %else %let grant_type=sas_services;
69%end;
70%if &grant_type=sas_services %then %do;
71 %let oauth_bearer=oauth_bearer=sas_services;
72 %let &access_token_var=;
73%end;
74
75%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
76 and &grant_type ne sas_services
77 )
78 ,mac=&sysmacroname
79 ,msg=%str(Invalid value for grant_type: &grant_type)
80)
81
82%mp_abort(iftrue=(%mf_isblank(&path)=1)
83 ,mac=&sysmacroname
84 ,msg=%str(path value must be provided)
85)
86%mp_abort(iftrue=(%length(&path)=1)
87 ,mac=&sysmacroname
88 ,msg=%str(path value must be provided)
89)
90
91options noquotelenmax;
92
93%local subfolder_cnt; /* determine the number of subfolders */
94%let subfolder_cnt=%sysfunc(countw(&path,/));
95
96%local base_uri; /* location of rest apis */
97%let base_uri=%mf_getplatform(VIYARESTAPI);
98
99%local href; /* resource address (none for root) */
100%let href="&base_uri/folders/folders?parentFolderUri=/folders/folders/none";
101
102%local x newpath subfolder;
103%do x=1 %to &subfolder_cnt;
104 %let subfolder=%scan(&path,&x,%str(/));
105 %let newpath=&newpath/&subfolder;
106
107 %local fname1;
108 %let fname1=%mf_getuniquefileref();
109
110 %put &sysmacroname checking to see if &newpath exists;
111 proc http method='GET' out=&fname1 &oauth_bearer
112 url="&base_uri/folders/folders/@item?path=&newpath";
113 %if &grant_type=authorization_code %then %do;
114 headers "Authorization"="Bearer &&&access_token_var";
115 %end;
116 run;
117 %if &SYS_PROCHTTP_STATUS_CODE=401 %then %do;
118 /* relates to: https://github.com/sasjs/core/issues/400 */
119 %put 401 thrown in &sysmacroname;
120 %put sleeping: %sysfunc(sleep(12,1)) secs - will try again;
121 proc http method='GET' out=&fname1 &oauth_bearer
122 url="&base_uri/folders/folders/@item?path=&newpath";
123 %if &grant_type=authorization_code %then %do;
124 headers "Authorization"="Bearer &&&access_token_var";
125 %end;
126 run;
127 %end;
128 %local libref1;
129 %let libref1=%mf_getuniquelibref();
130 libname &libref1 JSON fileref=&fname1;
131 %mp_abort(
132 iftrue=(
133 &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 404
134 )
135 ,mac=mv_createfolder124
136 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
137 )
138 %if &mdebug=1 %then %do;
139 %put &sysmacroname following check to see if &newpath exists:;
140 %put _local_;
141 data _null_;
142 infile &fname1;
143 input;
144 putlog _infile_;
145 run;
146 %end;
147 %if &SYS_PROCHTTP_STATUS_CODE=200 %then %do;
148 %*put &sysmacroname &newpath exists so grab the follow on link ;
149 data _null_;
150 set &libref1..links;
151 if rel='createChild' then
152 call symputx('href',quote(cats("&base_uri",href)),'l');
153 run;
154 %end;
155 %else %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
156 %put &sysmacroname &newpath not found - creating it now;
157 %local fname2;
158 %let fname2=%mf_getuniquefileref();
159 data _null_;
160 length json $1000;
161 json=cats("'"
162 ,'{"name":'
163 ,quote(trim(symget('subfolder')))
164 ,',"description":'
165 ,quote("&subfolder, created by &sysmacroname")
166 ,',"type":"folder"}'
167 ,"'"
168 );
169 call symputx('json',json,'l');
170 run;
171
172 proc http method='POST'
173 in=&json
174 out=&fname2
175 &oauth_bearer
176 url=%unquote(%superq(href));
177 headers
178 %if &grant_type=authorization_code %then %do;
179 "Authorization"="Bearer &&&access_token_var"
180 %end;
181 'Content-Type'='application/vnd.sas.content.folder+json'
182 'Accept'='application/vnd.sas.content.folder+json';
183 run;
184 %if &SYS_PROCHTTP_STATUS_CODE ne 201 %then %do;
185 %put &=SYS_PROCHTTP_STATUS_CODE &=SYS_PROCHTTP_STATUS_PHRASE;
186 %end;
187 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
188 ,mac=&sysmacroname
189 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
190 )
191 %local libref2;
192 %let libref2=%mf_getuniquelibref();
193 libname &libref2 JSON fileref=&fname2;
194 %put &sysmacroname &newpath now created. Grabbing the follow on link ;
195 data &outds;
196 set &libref2..links;
197 if rel='createChild' then do;
198 call symputx('href',quote(cats("&base_uri",href)),'l');
199 &dbg put (_all_)(=);
200 end;
201 if method='GET' and rel='self' then do;
202 self_uri=uri;
203 output;
204 end;
205 keep self_uri ;
206 run;
207
208 libname &libref2 clear;
209 filename &fname2 clear;
210 %end;
211 filename &fname1 clear;
212 libname &libref1 clear;
213%end;
214%mp_abort(
215 iftrue=(&syscc ne 0),
216 msg=Cannot leave &sysmacroname with syscc=&syscc
217)
218%mend mv_createfolder;