Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_getgroupmembers.sas
Go to the documentation of this file.
1/**
2 @file mv_getgroupmembers.sas
3 @brief Creates a dataset with a list of group members
4 @details First, be sure you have an access token (which requires an app token)
5
6 Using the macros here:
7
8 filename mc url
9 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
10 %inc mc;
11
12 Now we can run the macro!
13
14 %mv_getgroupmembers(All Users)
15
16 outputs:
17
18 ordinal_root num,
19 ordinal_items num,
20 version num,
21 id char(256),
22 name char(256),
23 providerId char(32),
24 implicit num,
25 type char(32)
26
27 `type` is the member type (user or group) and is declared on every path.
28 `implicit` is declared so a caller reading it does not fail with "Variable
29 implicit not found", but the identities API returns no such field, so the
30 column is present and always missing. A group with no members, and a group
31 id the server does not know, both return the columns above with zero rows.
32
33 @param [in] group Group id for which to return group members
34 @param [in] access_token_var= (ACCESS_TOKEN)
35 The global macro variable to contain the access token
36 @param [in] grant_type= (sas_services)
37 valid values are "password" or "authorization_code" (unquoted).
38 @param [out] outds= (work.viyagroupmembers)
39 The library.dataset to be created that contains the list of members
40
41 id and name are declared at $256, the longest active directory can hold for
42 a display name or a SAM account name. The $43 they used to carry truncated
43 anything longer, and a truncation fails the step on some platforms.
44
45
46 @version VIYA V.03.04
47 @author Allan Bowe, source: https://github.com/sasjs/core
48
49 <h4> SAS Macros </h4>
50 @li mp_abort.sas
51 @li mf_getplatform.sas
52 @li mf_getuniquefileref.sas
53 @li mf_getuniquelibref.sas
54 @li mf_existvar.sas
55
56**/
57
58%macro mv_getgroupmembers(group
59 ,access_token_var=ACCESS_TOKEN
60 ,grant_type=sas_services
61 ,outds=work.viyagroupmembers
62 );
63%local oauth_bearer;
64%if &grant_type=detect %then %do;
65 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
66 %else %let grant_type=sas_services;
67%end;
68%if &grant_type=sas_services %then %do;
69 %let oauth_bearer=oauth_bearer=sas_services;
70 %let &access_token_var=;
71%end;
72
73%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
74 and &grant_type ne sas_services
75 )
76 ,mac=&sysmacroname
77 ,msg=%str(Invalid value for grant_type: &grant_type)
78)
79
80options noquotelenmax;
81
82%local base_uri; /* location of rest apis */
83%let base_uri=%mf_getplatform(VIYARESTAPI);
84
85/* fetching folder details for provided path */
86%local fname1;
87%let fname1=%mf_getuniquefileref();
88proc http method='GET' out=&fname1 &oauth_bearer
89 url="&base_uri/identities/groups/&group/members?limit=10000";
90 headers
91 %if &grant_type=authorization_code %then %do;
92 "Authorization"="Bearer &&&access_token_var"
93 %end;
94 "Accept"="application/json";
95run;
96/*data _null_;infile &fname1;input;putlog _infile_;run;*/
97%if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
98 %put NOTE: Group &group not found!!;
99 /* the same documented columns and zero rows as a group with no members -
100 call missing alone would output one all-missing observation */
101 data &outds;
102 length ordinal_root 8 ordinal_items 8 version 8
103 id $256 name $256 providerId $32 implicit 8 type $32;
104 call missing(of _all_);
105 stop;
106 run;
107%end;
108%else %do;
109 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
110 ,mac=&sysmacroname
111 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
112 )
113 %let libref1=%mf_getuniquelibref();
114 libname &libref1 JSON fileref=&fname1;
115 /* a group with no members returns a collection whose items dataset carries no
116 member columns, so a straight SET would raise "Variable id is uninitialized"
117 and leave syscc dirty. Only copy when there is something to copy. The
118 documented columns are declared on both paths, so an empty group hands the
119 caller the same PDV as a populated one. The lengths are declared because
120 the JSON engine sizes its columns to the longest value in the payload:
121 $256 is the longest an active directory display name or SAM account name
122 can hold, and a shorter declaration lets the engine report "Multiple
123 lengths were specified" on a later member. */
124 %local itemsds hasvars;
125 %let itemsds=&libref1..items;
126 %let hasvars=0;
127 %if %sysfunc(exist(&itemsds))=1 %then %do;
128 %if %mf_existvar(&itemsds,id)>0 %then %let hasvars=1;
129 %end;
130 data &outds;
131 length ordinal_root 8 ordinal_items 8 version 8
132 id $256 name $256 providerId $32 implicit 8 type $32;
133 call missing(of _all_);
134 %if &hasvars=1 %then %do;
135 set &itemsds;
136 /* the engine emits one all-missing row for an empty collection */
137 if missing(id) then delete;
138 %end;
139 %else %do;
140 stop;
141 %end;
142 run;
143 libname &libref1 clear;
144%end;
145
146/* clear refs */
147filename &fname1 clear;
148
149%mend mv_getgroupmembers;