Loading...
Searching...
No Matches
mv_getgroups.sas
Go to the documentation of this file.
1/**
2 @file mv_getgroups.sas
3 @brief Creates a dataset with a list of viya groups
4 @details Returns exactly GROUPURI, GROUPNAME and GROUPDESC, all character
5 variables of length 256. They contain the Viya group ID, name and description
6 respectively. An empty response returns the same columns with zero rows.
7 Values exceeding 256 bytes cause failure rather than being truncated.
8 The request returns at most 10000 groups.
9
10 First, load the macros:
11
12 filename mc url
13 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
14 %inc mc;
15
16 Next, execute:
17
18 %mv_getgroups(outds=work.groups)
19
20 @param [in] access_token_var= (ACCESS_TOKEN)
21 The global macro variable to contain the access token
22 @param [in] grant_type= (sas_services)
23 valid values are "password" or "authorization_code" (unquoted).
24 @param [out] outds= (work.viyagroups)
25 The library.dataset to be created that contains the list of groups
26
27
28 @version VIYA V.03.04
29 @author Allan Bowe, source: https://github.com/sasjs/core
30
31 <h4> SAS Macros </h4>
32 @li mddl_getgroups.sas
33 @li mp_abort.sas
34 @li mf_getplatform.sas
35 @li mf_getuniquefileref.sas
36 @li mf_getuniquelibref.sas
37 @li mf_getuniquename.sas
38
39**/
40
41%macro mv_getgroups(access_token_var=ACCESS_TOKEN
42 ,grant_type=sas_services
43 ,outds=work.viyagroups
44 );
45%local oauth_bearer base_uri fname1 libref1 dsid rc idpos namepos descpos
46 invalid overflow schema empty;
47%if &grant_type=detect %then %do;
48 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
49 %else %let grant_type=sas_services;
50%end;
51%if &grant_type=sas_services %then %do;
52 %let oauth_bearer=oauth_bearer=sas_services;
53 %let &access_token_var=;
54%end;
55
56%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
57 and &grant_type ne sas_services
58 )
59 ,mac=&sysmacroname
60 ,msg=%str(Invalid value for grant_type: &grant_type)
61)
62
63options noquotelenmax;
64/* location of rest apis */
65%let base_uri=%mf_getplatform(VIYARESTAPI);
66
67/* fetching folder details for provided path */
68%let fname1=%mf_getuniquefileref();
69%let libref1=%mf_getuniquelibref();
70
71proc http method='GET' out=&fname1 &oauth_bearer
72 url="&base_uri/identities/groups?limit=10000";
73 headers
74 %if &grant_type=authorization_code %then %do;
75 "Authorization"="Bearer &&&access_token_var"
76 %end;
77 "Accept"="application/json";
78run;
79/*data _null_;infile &fname1;input;putlog _infile_;run;*/
80%mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
81 ,mac=&sysmacroname
82 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
83)
84libname &libref1 JSON fileref=&fname1;
85%let schema=work.%mf_getuniquename(prefix=mv_groups_);
86%mddl_getgroups(&schema)
87
88/* A JSON response without items has no ITEMS table. */
89%if not %sysfunc(exist(&libref1..items)) %then %do;
90 data &outds;
91 if 0 then set &schema;
92 stop;
93 run;
94%end;
95%else %do;
96 /* Check actual values, not the JSON engine's inferred variable length,
97 to avoid rejecting padded short values. */
98 %let dsid=%sysfunc(open(&libref1..items,i));
99 %let invalid=0;
100 %let empty=0;
101 %let idpos=0;
102 %let namepos=0;
103 %let descpos=0;
104 %if &dsid=0 %then %let invalid=1;
105 %else %do;
106 %let idpos=%sysfunc(varnum(&dsid,id));
107 %let namepos=%sysfunc(varnum(&dsid,name));
108 %let descpos=%sysfunc(varnum(&dsid,description));
109 /* An empty nested array can have an ordinal-only ITEMS table. */
110 %if &idpos=0 and &namepos=0 and &descpos=0 %then %let empty=1;
111 %else %if &idpos=0 or &namepos=0 %then %let invalid=1;
112 %else %if %sysfunc(vartype(&dsid,&idpos)) ne C or
113 %sysfunc(vartype(&dsid,&namepos)) ne C %then %let invalid=1;
114 %if &descpos>0 %then %do;
115 %if %sysfunc(vartype(&dsid,&descpos)) ne C %then %let invalid=1;
116 %end;
117 %let rc=%sysfunc(close(&dsid));
118 %end;
119 %mp_abort(iftrue=(&invalid ne 0),mac=mv_getgroups,
120 msg=%str(Invalid groups response: expected character ID and NAME))
121 %if &empty %then %do;
122 data &outds;
123 if 0 then set &schema;
124 stop;
125 run;
126 %end;
127 %else %do;
128 %let overflow=0;
129 data _null_;
130 set &libref1..items;
131 if lengthn(id)>256 or lengthn(name)>256
132 %if &descpos>0 %then %do;
133 or lengthn(description)>256
134 %end;
135 then call symputx('overflow',1,'L');
136 run;
137 %mp_abort(iftrue=(&overflow ne 0),mac=mv_getgroups,
138 msg=%str(Group ID name or description exceeds 256 bytes))
139 data &outds;
140 if 0 then set &schema;
141 set &libref1..items;
142 groupuri=id;
143 groupname=name;
144 %if &descpos>0 %then %do;
145 groupdesc=description;
146 %end;
147 keep groupuri groupname groupdesc;
148 run;
149 %end;
150%end;
151
152
153/* clear refs */
154filename &fname1 clear;
155libname &libref1 clear;
156
157%mend mv_getgroups;