Search in sources :

Example 1 with ApiDiscoveryResponse

use of com.cloud.api.response.ApiDiscoveryResponse in project cosmic by MissionCriticalCloud.

the class ListApisCmd method execute.

@Override
public void execute() throws ServerApiException {
    if (_apiDiscoveryService != null) {
        final User user = CallContext.current().getCallingUser();
        final ListResponse<ApiDiscoveryResponse> response = (ListResponse<ApiDiscoveryResponse>) _apiDiscoveryService.listApis(user, name);
        if (response == null) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Api Discovery plugin was unable to find an api by that name or process any apis");
        }
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    }
}
Also used : User(com.cloud.user.User) ListResponse(com.cloud.api.response.ListResponse) ServerApiException(com.cloud.api.ServerApiException) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse)

Example 2 with ApiDiscoveryResponse

use of com.cloud.api.response.ApiDiscoveryResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryServiceImpl method listApis.

@Override
public ListResponse<? extends BaseResponse> listApis(final User user, final String name) {
    final ListResponse<ApiDiscoveryResponse> response = new ListResponse<>();
    final List<ApiDiscoveryResponse> responseList = new ArrayList<>();
    if (user == null) {
        return null;
    }
    if (name != null) {
        if (!s_apiNameDiscoveryResponseMap.containsKey(name)) {
            return null;
        }
        for (final APIChecker apiChecker : _apiAccessCheckers) {
            try {
                apiChecker.checkAccess(user, name);
            } catch (final Exception ex) {
                s_logger.debug("API discovery access check failed for " + name + " with " + ex.getMessage());
                return null;
            }
        }
        responseList.add(s_apiNameDiscoveryResponseMap.get(name));
    } else {
        for (final String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
            boolean isAllowed = true;
            for (final APIChecker apiChecker : _apiAccessCheckers) {
                try {
                    apiChecker.checkAccess(user, apiName);
                } catch (final Exception ex) {
                    isAllowed = false;
                }
            }
            if (isAllowed) {
                responseList.add(s_apiNameDiscoveryResponseMap.get(apiName));
            }
        }
    }
    response.setResponses(responseList);
    return response;
}
Also used : ListResponse(com.cloud.api.response.ListResponse) ArrayList(java.util.ArrayList) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse) APIChecker(com.cloud.acl.APIChecker)

Example 3 with ApiDiscoveryResponse

use of com.cloud.api.response.ApiDiscoveryResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryServiceImpl method cacheResponseMap.

protected Map<String, List<String>> cacheResponseMap(final Set<Class<?>> cmdClasses) {
    final Map<String, List<String>> responseApiNameListMap = new HashMap<>();
    for (final Class<?> cmdClass : cmdClasses) {
        APICommand apiCmdAnnotation = cmdClass.getAnnotation(APICommand.class);
        if (apiCmdAnnotation == null) {
            apiCmdAnnotation = cmdClass.getSuperclass().getAnnotation(APICommand.class);
        }
        if (apiCmdAnnotation == null || !apiCmdAnnotation.includeInApiDoc() || apiCmdAnnotation.name().isEmpty()) {
            continue;
        }
        final String apiName = apiCmdAnnotation.name();
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Found api: " + apiName);
        }
        final ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
        final String responseName = apiCmdAnnotation.responseObject().getName();
        if (!responseName.contains("SuccessResponse")) {
            if (!responseApiNameListMap.containsKey(responseName)) {
                responseApiNameListMap.put(responseName, new ArrayList<>());
            }
            responseApiNameListMap.get(responseName).add(apiName);
        }
        response.setRelated(responseName);
        final Field[] responseFields = apiCmdAnnotation.responseObject().getDeclaredFields();
        for (final Field responseField : responseFields) {
            final ApiResponseResponse responseResponse = getFieldResponseMap(responseField);
            response.addApiResponse(responseResponse);
        }
        response.setObjectName("api");
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    for (final String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
        final ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
        final Set<ApiParameterResponse> processedParams = new HashSet<>();
        for (final ApiParameterResponse param : response.getParams()) {
            if (responseApiNameListMap.containsKey(param.getRelated())) {
                final List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
                param.setRelated(StringUtils.join(relatedApis, ","));
            } else {
                param.setRelated(null);
            }
            processedParams.add(param);
        }
        response.setParams(processedParams);
        if (responseApiNameListMap.containsKey(response.getRelated())) {
            final List<String> relatedApis = responseApiNameListMap.get(response.getRelated());
            relatedApis.remove(apiName);
            response.setRelated(StringUtils.join(relatedApis, ","));
        } else {
            response.setRelated(null);
        }
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    return responseApiNameListMap;
}
Also used : HashMap(java.util.HashMap) ApiResponseResponse(com.cloud.api.response.ApiResponseResponse) ApiParameterResponse(com.cloud.api.response.ApiParameterResponse) APICommand(com.cloud.api.APICommand) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse) HashSet(java.util.HashSet)

Example 4 with ApiDiscoveryResponse

use of com.cloud.api.response.ApiDiscoveryResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryTest method verifyListApis.

@Test
public void verifyListApis() throws Exception {
    final ListResponse<ApiDiscoveryResponse> responses = (ListResponse<ApiDiscoveryResponse>) s_discoveryService.listApis(testUser, null);
    if (responses != null) {
        assertTrue("No. of response items > 1", responses.getCount().intValue() == 1);
        for (final ApiDiscoveryResponse response : responses.getResponses()) {
            assertFalse("API name is empty", response.getName().isEmpty());
            assertFalse("API description is empty", response.getDescription().isEmpty());
        }
    }
}
Also used : ListResponse(com.cloud.api.response.ListResponse) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse) Test(org.junit.Test)

Example 5 with ApiDiscoveryResponse

use of com.cloud.api.response.ApiDiscoveryResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryServiceImpl method getCmdRequestMap.

private ApiDiscoveryResponse getCmdRequestMap(final Class<?> cmdClass, final APICommand apiCmdAnnotation) {
    final String apiName = apiCmdAnnotation.name();
    final ApiDiscoveryResponse response = new ApiDiscoveryResponse();
    response.setName(apiName);
    response.setGroupName(apiCmdAnnotation.group().name());
    response.setGroupDescription(apiCmdAnnotation.group().toString());
    response.setDescription(apiCmdAnnotation.description());
    if (!apiCmdAnnotation.since().isEmpty()) {
        response.setSince(apiCmdAnnotation.since());
    }
    final Set<Field> fields = ReflectUtil.getAllFieldsForClass(cmdClass, new Class<?>[] { BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
    final boolean isAsync = ReflectUtil.isCmdClassAsync(cmdClass, new Class<?>[] { BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
    response.setAsync(isAsync);
    for (final Field field : fields) {
        final Parameter parameterAnnotation = field.getAnnotation(Parameter.class);
        if (parameterAnnotation != null && parameterAnnotation.expose() && parameterAnnotation.includeInApiDoc()) {
            final ApiParameterResponse paramResponse = new ApiParameterResponse();
            paramResponse.setName(parameterAnnotation.name());
            paramResponse.setDescription(parameterAnnotation.description());
            paramResponse.setType(parameterAnnotation.type().toString().toLowerCase());
            paramResponse.setLength(parameterAnnotation.length());
            paramResponse.setRequired(parameterAnnotation.required());
            if (!parameterAnnotation.since().isEmpty()) {
                paramResponse.setSince(parameterAnnotation.since());
            }
            paramResponse.setRelated(parameterAnnotation.entityType()[0].getName());
            response.addParam(paramResponse);
        }
    }
    return response;
}
Also used : Field(java.lang.reflect.Field) ApiParameterResponse(com.cloud.api.response.ApiParameterResponse) Parameter(com.cloud.api.Parameter) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse)

Aggregations

ApiDiscoveryResponse (com.cloud.api.response.ApiDiscoveryResponse)6 ListResponse (com.cloud.api.response.ListResponse)4 ApiParameterResponse (com.cloud.api.response.ApiParameterResponse)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 APIChecker (com.cloud.acl.APIChecker)1 APICommand (com.cloud.api.APICommand)1 Parameter (com.cloud.api.Parameter)1 ServerApiException (com.cloud.api.ServerApiException)1 ApiResponseResponse (com.cloud.api.response.ApiResponseResponse)1 User (com.cloud.user.User)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1