Macros for SAS Application Developers
https://github.com/sasjs/core
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%if %mfv_existfolder(&path)=1 %then %do;
50 %put &sysmacroname: &path already exists;
51 data &outds;
52 self_uri="%mfv_getpathuri(&path)";
53 output;
54 stop;
55 run;
56 %return;
57%end;
58
59%local oauth_bearer;
60%if &grant_type=detect %then %do;
61 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
62 %else %let grant_type=sas_services;
63%end;
64%if &grant_type=sas_services %then %do;
65 %let oauth_bearer=oauth_bearer=sas_services;
66 %let &access_token_var=;
67%end;
68
69%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
70 and &grant_type ne sas_services
71 )
72 ,mac=&sysmacroname
73 ,msg=%str(Invalid value for grant_type: &grant_type)
74)
75
76%mp_abort(iftrue=(%mf_isblank(&path)=1)
77 ,mac=&sysmacroname
78 ,msg=%str(path value must be provided)
79)
80%mp_abort(iftrue=(%length(&path)=1)
81 ,mac=&sysmacroname
82 ,msg=%str(path value must be provided)
83)
84
85options noquotelenmax;
86
87%local subfolder_cnt; /* determine the number of subfolders */
88%let subfolder_cnt=%sysfunc(countw(&path,/));
89
90%local base_uri; /* location of rest apis */
91%let base_uri=%mf_getplatform(VIYARESTAPI);
92
93%local href; /* resource address (none for root) */
94%let href="&base_uri/folders/folders?parentFolderUri=/folders/folders/none";
95
96%local x newpath subfolder;
97%do x=1 %to &subfolder_cnt;
98 %let subfolder=%scan(&path,&x,%str(/));
99 %let newpath=&newpath/&subfolder;
100
101 %local fname1;
102 %let fname1=%mf_getuniquefileref();
103
104 %put &sysmacroname checking to see if &newpath exists;
105 proc http method='GET' out=&fname1 &oauth_bearer
106 url="&base_uri/folders/folders/@item?path=&newpath";
107 %if &grant_type=authorization_code %then %do;
108 headers "Authorization"="Bearer &&&access_token_var";
109 %end;
110 run;
111 %local libref1;
112 %let libref1=%mf_getuniquelibref();
113 libname &libref1 JSON fileref=&fname1;
114 %mp_abort(
115 iftrue=(
116 &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 404
117 )
118 ,mac=&sysmacroname
119 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
120 )
121 %if &mdebug=1 %then %do;
122 %put &sysmacroname following check to see if &newpath exists:;
123 %put _local_;
124 data _null_;
125 infile &fname1;
126 input;
127 putlog _infile_;
128 run;
129 %end;
130 %if &SYS_PROCHTTP_STATUS_CODE=200 %then %do;
131 %*put &sysmacroname &newpath exists so grab the follow on link ;
132 data _null_;
133 set &libref1..links;
134 if rel='createChild' then
135 call symputx('href',quote(cats("&base_uri",href)),'l');
136 run;
137 %end;
138 %else %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
139 %put &sysmacroname &newpath not found - creating it now;
140 %local fname2;
141 %let fname2=%mf_getuniquefileref();
142 data _null_;
143 length json $1000;
144 json=cats("'"
145 ,'{"name":'
146 ,quote(trim(symget('subfolder')))
147 ,',"description":'
148 ,quote("&subfolder, created by &sysmacroname")
149 ,',"type":"folder"}'
150 ,"'"
151 );
152 call symputx('json',json,'l');
153 run;
154
155 proc http method='POST'
156 in=&json
157 out=&fname2
158 &oauth_bearer
159 url=%unquote(%superq(href));
160 headers
161 %if &grant_type=authorization_code %then %do;
162 "Authorization"="Bearer &&&access_token_var"
163 %end;
164 'Content-Type'='application/vnd.sas.content.folder+json'
165 'Accept'='application/vnd.sas.content.folder+json';
166 run;
167 %put &=SYS_PROCHTTP_STATUS_CODE;
168 %put &=SYS_PROCHTTP_STATUS_PHRASE;
169 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
170 ,mac=&sysmacroname
171 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
172 )
173 %local libref2;
174 %let libref2=%mf_getuniquelibref();
175 libname &libref2 JSON fileref=&fname2;
176 %put &sysmacroname &newpath now created. Grabbing the follow on link ;
177 data &outds;
178 set &libref2..links;
179 if rel='createChild' then do;
180 call symputx('href',quote(cats("&base_uri",href)),'l');
181 &dbg put (_all_)(=);
182 end;
183 if method='GET' and rel='self' then do;
184 self_uri=uri;
185 output;
186 end;
187 keep self_uri ;
188 run;
189
190 libname &libref2 clear;
191 filename &fname2 clear;
192 %end;
193 filename &fname1 clear;
194 libname &libref1 clear;
195%end;
196%mend mv_createfolder;