marvin_refactor: The deleteXxx APIs should have their entity in the id field

All delete APIs have an id field and the entityType the delete is
removing from the system is found here. Include the entity Type for
these in the API discovery service.

Signed-off-by: Prasanna Santhanam <tsp@apache.org>
This commit is contained in:
Prasanna Santhanam 2013-05-28 16:35:07 +05:30
parent c3246f4309
commit 32542ecf6c
4 changed files with 41 additions and 39 deletions

View File

@ -7,16 +7,6 @@
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

View File

@ -50,6 +50,10 @@ public class ApiParameterResponse extends BaseResponse {
this.name = name;
}
public String getName() {
return this.name;
}
public void setDescription(String description) {
this.description = description;
}

View File

@ -16,19 +16,16 @@
// under the License.
package org.apache.cloudstack.discovery;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Local;
import javax.inject.Inject;
import com.cloud.serializer.Param;
import com.cloud.user.User;
import com.cloud.utils.ReflectUtil;
import com.cloud.utils.StringUtils;
import com.cloud.utils.component.ComponentLifecycleBase;
import com.cloud.utils.component.PluggableService;
import com.google.gson.annotations.SerializedName;
import org.apache.cloudstack.acl.APIChecker;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.BaseAsyncCreateCmd;
import org.apache.cloudstack.api.BaseCmd;
@ -42,13 +39,15 @@ import org.apache.cloudstack.api.response.ListResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.serializer.Param;
import com.cloud.user.User;
import com.cloud.utils.ReflectUtil;
import com.cloud.utils.StringUtils;
import com.cloud.utils.component.ComponentLifecycleBase;
import com.cloud.utils.component.PluggableService;
import com.google.gson.annotations.SerializedName;
import javax.ejb.Local;
import javax.inject.Inject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
@Local(value = ApiDiscoveryService.class)
@ -102,6 +101,7 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
}
ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
String responseName = apiCmdAnnotation.responseObject().getName();
response.setResponseName(responseName);
if (!responseName.contains("SuccessResponse")) {
if (!responseApiNameListMap.containsKey(responseName)) {
responseApiNameListMap.put(responseName, new ArrayList<String>());
@ -110,9 +110,6 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
responseApiNameListMap.get(responseName).add(apiName);
String entity = apiCmdAnnotation.responseObject().getSimpleName().replace("Response", "");
response.setEntity(entity);
} else {
String entity = deriveRelatedEntity(apiName, response).replace("Response", "");
response.setEntity(entity);
}
Field[] responseFields = apiCmdAnnotation.responseObject().getDeclaredFields();
@ -129,6 +126,14 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
for (ApiParameterResponse param: response.getParams()) {
//Derive entity for unmapped apis
if (response.getEntity() == null) {
String entity = deriveRelatedEntity(apiName, param);
if (entity != null && !entity.isEmpty()) {
response.setEntity(entity);
}
}
if (responseApiNameListMap.containsKey(param.getRelated())) {
List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
param.setRelated(StringUtils.join(relatedApis, ","));
@ -146,24 +151,29 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
} else {
response.setRelated(null);
}
s_apiNameDiscoveryResponseMap.put(apiName, response);
}
return responseApiNameListMap;
}
private String deriveRelatedEntity(String currentApi, ApiDiscoveryResponse response) {
private String deriveRelatedEntity(String api, ApiParameterResponse param) {
//Guess the entity from the related APIs
//For APIs that are deleteXxx, the right entityType is in the `id` field of the Cmd
String entity = "";
for (ApiParameterResponse param : response.getParams()) {
if (param.getRelated() != null) {
if (param.getName() != null && param.getName().equals(ApiConstants.ID)) {
if (api.startsWith("delete")) {
entity = param.getRelated();
}
} else {
if (param.getRelated() != null && !param.getRelated().equals("Object")) {
entity = param.getRelated();
}
}
if (entity.isEmpty()) {
s_logger.warn("Couldn't find entity for API: " + currentApi + " from related APIs");
s_logger.warn("Couldn't find entity for API: " + api + " from related APIs");
return null;
}
return entity;
return entity.replace("Response", "");
}
private ApiResponseResponse getFieldResponseMap(Field responseField) {

View File

@ -309,8 +309,6 @@ class codeGenerator(object):
getText(param.getElementsByTagName('name'))
assert paramProperty.name
required = param.getElementsByTagName('required')
if required:
paramProperty.required = getText(required)