Skip to content

Commit 3df026b

Browse files
committed
ApiDiscoveryServiceImpl: Implement listApis to return response based on role
Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent 74bb043 commit 3df026b

1 file changed

Lines changed: 57 additions & 18 deletions

File tree

‎plugins/api/discovery/src/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java‎

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616
// under the License.
1717
package org.apache.cloudstack.discovery;
1818

19-
import com.cloud.utils.PropertiesUtil;
19+
import com.cloud.server.ManagementServer;
2020
import com.cloud.utils.ReflectUtil;
21+
import com.cloud.utils.component.Adapters;
22+
import com.cloud.utils.component.ComponentLocator;
23+
import com.cloud.utils.component.Inject;
24+
import com.cloud.utils.component.PluggableService;
25+
import org.apache.cloudstack.acl.APIChecker;
2126
import org.apache.cloudstack.acl.RoleType;
2227
import org.apache.cloudstack.api.APICommand;
2328
import org.apache.cloudstack.api.BaseCmd;
@@ -42,30 +47,50 @@
4247
public class ApiDiscoveryServiceImpl implements ApiDiscoveryService {
4348
private static final Logger s_logger = Logger.getLogger(ApiDiscoveryServiceImpl.class);
4449

45-
private ListResponse<ApiDiscoveryResponse> _discoveryResponse = new ListResponse<ApiDiscoveryResponse>();
50+
private static Map<String, ApiDiscoveryResponse> _apiNameDiscoveryResponseMap =
51+
new HashMap<String, ApiDiscoveryResponse>();
4652

47-
private Map<String, Class<?>> _apiNameCmdClassMap = new HashMap<String, Class<?>>();
53+
private static Map<RoleType, List<ApiDiscoveryResponse>> _roleTypeDiscoveryResponseListMap =
54+
new HashMap<RoleType, List<ApiDiscoveryResponse>>();
55+
56+
private static Map<String, List<RoleType>> _apiNameRoleTypeListMap = null;
4857

4958
protected ApiDiscoveryServiceImpl() {
5059
super();
51-
generateApiNameCmdClassMap();
60+
for (RoleType roleType: RoleType.values())
61+
_roleTypeDiscoveryResponseListMap.put(roleType, new ArrayList<ApiDiscoveryResponse>());
5262
cacheListApiResponse();
5363
}
5464

55-
private void generateApiNameCmdClassMap() {
56-
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
57-
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
58-
59-
for(Class<?> cmdClass: cmdClasses)
60-
_apiNameCmdClassMap.put(cmdClass.getAnnotation(APICommand.class).name(), cmdClass);
65+
private Map<String, List<RoleType>> getApiNameRoleTypeListMap() {
66+
Map<String, List<RoleType>> apiNameRoleTypeMap = new HashMap<String, List<RoleType>>();
67+
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
68+
List<PluggableService> services = locator.getAllPluggableServices();
69+
services.add((PluggableService) ComponentLocator.getComponent(ManagementServer.Name));
70+
for (PluggableService service : services) {
71+
for (Map.Entry<String, String> entry: service.getProperties().entrySet()) {
72+
String apiName = entry.getKey();
73+
String roleMask = entry.getValue();
74+
try {
75+
short cmdPermissions = Short.parseShort(roleMask);
76+
if (!apiNameRoleTypeMap.containsKey(apiName))
77+
apiNameRoleTypeMap.put(apiName, new ArrayList<RoleType>());
78+
for (RoleType roleType: RoleType.values()) {
79+
if ((cmdPermissions & roleType.getValue()) != 0)
80+
apiNameRoleTypeMap.get(apiName).add(roleType);
81+
}
82+
} catch (NumberFormatException nfe) {
83+
}
84+
}
85+
}
86+
return apiNameRoleTypeMap;
6187
}
6288

6389
private void cacheListApiResponse() {
90+
Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class,
91+
new String[]{"org.apache.cloudstack.api", "com.cloud.api"});
6492

65-
List<ApiDiscoveryResponse> apiDiscoveryResponses = new ArrayList<ApiDiscoveryResponse>();
66-
67-
for(String key: _apiNameCmdClassMap.keySet()) {
68-
Class<?> cmdClass = _apiNameCmdClassMap.get(key);
93+
for(Class<?> cmdClass: cmdClasses) {
6994
APICommand apiCmdAnnotation = cmdClass.getAnnotation(APICommand.class);
7095
if (apiCmdAnnotation == null)
7196
apiCmdAnnotation = cmdClass.getSuperclass().getAnnotation(APICommand.class);
@@ -74,8 +99,9 @@ private void cacheListApiResponse() {
7499
|| apiCmdAnnotation.name().isEmpty())
75100
continue;
76101

102+
String apiName = apiCmdAnnotation.name();
77103
ApiDiscoveryResponse response = new ApiDiscoveryResponse();
78-
response.setName(apiCmdAnnotation.name());
104+
response.setName(apiName);
79105
response.setDescription(apiCmdAnnotation.description());
80106
response.setSince(apiCmdAnnotation.since());
81107

@@ -104,14 +130,27 @@ private void cacheListApiResponse() {
104130
}
105131
}
106132
response.setObjectName("apis");
107-
apiDiscoveryResponses.add(response);
133+
_apiNameDiscoveryResponseMap.put(apiName, response);
108134
}
109-
_discoveryResponse.setResponses(apiDiscoveryResponses);
110135
}
111136

112137
@Override
113138
public ListResponse<? extends BaseResponse> listApis(RoleType roleType) {
114-
return _discoveryResponse;
139+
// Creates roles based response list cache the first time listApis is called
140+
// Due to how adapters work, this cannot be done when mgmt loads
141+
if (_apiNameRoleTypeListMap == null) {
142+
_apiNameRoleTypeListMap = getApiNameRoleTypeListMap();
143+
for (Map.Entry<String, List<RoleType>> entry: _apiNameRoleTypeListMap.entrySet()) {
144+
String apiName = entry.getKey();
145+
for (RoleType roleTypeInList: entry.getValue()) {
146+
_roleTypeDiscoveryResponseListMap.get(roleTypeInList).add(
147+
_apiNameDiscoveryResponseMap.get(apiName));
148+
}
149+
}
150+
}
151+
ListResponse<ApiDiscoveryResponse> response = new ListResponse<ApiDiscoveryResponse>();
152+
response.setResponses(_roleTypeDiscoveryResponseListMap.get(roleType));
153+
return response;
115154
}
116155

117156
@Override

0 commit comments

Comments
 (0)