This patch disallows deleting / modifying system defined guest OS mappings

Local env
1. Create user defined mapping
2. Delete / modify user defined mapping. Should pass
3. Delete / modify system defined mapping. Should fail

Signed off by :- Nitin Mehta<nitin.mehta@citrix.com>
This commit is contained in:
Amogh Vasekar 2014-05-12 22:43:06 -07:00 committed by Nitin Mehta
parent 0a62eb8380
commit 9b6d430171
7 changed files with 69 additions and 1 deletions

View File

@ -291,6 +291,7 @@ public class ApiConstants {
public static final String SPECIFY_VLAN = "specifyvlan";
public static final String IS_DEFAULT = "isdefault";
public static final String IS_SYSTEM = "issystem";
public static final String IS_USER_DEFINED = "isuserdefined";
public static final String AVAILABILITY = "availability";
public static final String NETWORKRATE = "networkrate";
public static final String HOST_TAGS = "hosttags";

View File

@ -39,6 +39,10 @@ public class GuestOSResponse extends BaseResponse {
@Param(description = "the name/description of the OS type")
private String description;
@SerializedName(ApiConstants.IS_USER_DEFINED)
@Param(description = "is the guest OS user defined")
private String isUserDefined;
public String getId() {
return id;
}
@ -62,4 +66,13 @@ public class GuestOSResponse extends BaseResponse {
public void setDescription(String description) {
this.description = description;
}
public String getIsUserDefined() {
return isUserDefined;
}
public void setIsUserDefined(String isUserDefined) {
this.isUserDefined = isUserDefined;
}
}

View File

@ -52,6 +52,18 @@ public class GuestOsMappingResponse extends BaseResponse {
@Param(description = "hypervisor specific name for the Guest OS")
private String osNameForHypervisor;
@SerializedName(ApiConstants.IS_USER_DEFINED)
@Param(description = "is the mapping user defined")
private String isUserDefined;
public String getIsUserDefined() {
return isUserDefined;
}
public void setIsUserDefined(String isUserDefined) {
this.isUserDefined = isUserDefined;
}
public String getId() {
return id;
}

View File

@ -27,4 +27,6 @@ public interface GuestOSHypervisorDao extends GenericDao<GuestOSHypervisorVO, Lo
GuestOSHypervisorVO findByOsIdAndHypervisor(long guestOsId, String hypervisorType, String hypervisorVersion);
boolean removeGuestOsMapping(Long id);
GuestOSHypervisorVO findByOsIdAndHypervisorAndUserDefined(long guestOsId, String hypervisorType, String hypervisorVersion, boolean isUserDefined);
}

View File

@ -34,6 +34,7 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
protected final SearchBuilder<GuestOSHypervisorVO> guestOsSearch;
protected final SearchBuilder<GuestOSHypervisorVO> mappingSearch;
protected final SearchBuilder<GuestOSHypervisorVO> userDefinedMappingSearch;
protected GuestOSHypervisorDaoImpl() {
guestOsSearch = createSearchBuilder();
@ -45,6 +46,13 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
mappingSearch.and("hypervisor_type", mappingSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
mappingSearch.and("hypervisor_version", mappingSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
mappingSearch.done();
userDefinedMappingSearch = createSearchBuilder();
userDefinedMappingSearch.and("guest_os_id", userDefinedMappingSearch.entity().getGuestOsId(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.and("hypervisor_type", userDefinedMappingSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.and("hypervisor_version", userDefinedMappingSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.and("is_user_defined", userDefinedMappingSearch.entity().getIsUserDefined(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.done();
}
@Override
@ -64,6 +72,20 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
return findOneBy(sc);
}
@Override
public GuestOSHypervisorVO findByOsIdAndHypervisorAndUserDefined(long guestOsId, String hypervisorType, String hypervisorVersion, boolean isUserDefined) {
SearchCriteria<GuestOSHypervisorVO> sc = userDefinedMappingSearch.create();
String version = "default";
if (!(hypervisorVersion == null || hypervisorVersion.isEmpty())) {
version = hypervisorVersion;
}
sc.setParameters("guest_os_id", guestOsId);
sc.setParameters("hypervisor_type", hypervisorType);
sc.setParameters("hypervisor_version", version);
sc.setParameters("is_user_defined", isUserDefined);
return findOneBy(sc);
}
@Override
public boolean removeGuestOsMapping(Long id) {
GuestOSHypervisorVO guestOsHypervisor = findById(id);

View File

@ -2890,6 +2890,7 @@ public class ApiResponseHelper implements ResponseGenerator {
GuestOSResponse response = new GuestOSResponse();
response.setDescription(guestOS.getDisplayName());
response.setId(guestOS.getUuid());
response.setIsUserDefined(Boolean.valueOf(guestOS.getIsUserDefined()).toString());
GuestOSCategoryVO category = ApiDBUtils.findGuestOsCategoryById(guestOS.getCategoryId());
if (category != null) {
response.setOsCategoryId(category.getUuid());
@ -2906,6 +2907,7 @@ public class ApiResponseHelper implements ResponseGenerator {
response.setHypervisor(guestOSHypervisor.getHypervisorType());
response.setHypervisorVersion(guestOSHypervisor.getHypervisorVersion());
response.setOsNameForHypervisor((guestOSHypervisor.getGuestOsName()));
response.setIsUserDefined(Boolean.valueOf(guestOSHypervisor.getIsUserDefined()).toString());
GuestOS guestOs = ApiDBUtils.findGuestOSById(guestOSHypervisor.getGuestOsId());
if (guestOs != null) {
response.setOsStdName(guestOs.getDisplayName());

View File

@ -2029,7 +2029,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
throw new InvalidParameterValueException("Unable to find the guest OS by name or UUID");
}
//check for duplicates
GuestOSHypervisorVO duplicate = _guestOSHypervisorDao.findByOsIdAndHypervisor(guestOs.getId(), hypervisorType.toString(), hypervisorVersion);
GuestOSHypervisorVO duplicate = _guestOSHypervisorDao.findByOsIdAndHypervisorAndUserDefined(guestOs.getId(), hypervisorType.toString(), hypervisorVersion, true);
if (duplicate != null) {
throw new InvalidParameterValueException("Mapping from hypervisor : " + hypervisorType.toString() + ", version : " + hypervisorVersion + " and guest OS : "
@ -2096,6 +2096,10 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
throw new InvalidParameterValueException("Guest OS not found. Please specify a valid ID for the Guest OS");
}
if (!guestOsHandle.getIsUserDefined()) {
throw new InvalidParameterValueException("Unable to modify system defined guest OS");
}
//Check if update is needed
if (displayName.equals(guestOsHandle.getDisplayName())) {
return guestOsHandle;
@ -2127,6 +2131,10 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
throw new InvalidParameterValueException("Guest OS not found. Please specify a valid ID for the Guest OS");
}
if (!guestOs.getIsUserDefined()) {
throw new InvalidParameterValueException("Unable to remove system defined guest OS");
}
return _guestOSDao.remove(id);
}
@ -2143,6 +2151,10 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
throw new InvalidParameterValueException("Guest OS Mapping not found. Please specify a valid ID for the Guest OS Mapping");
}
if (!guestOsHypervisorHandle.getIsUserDefined()) {
throw new InvalidParameterValueException("Unable to modify system defined Guest OS mapping");
}
GuestOSHypervisorVO guestOsHypervisor = _guestOSHypervisorDao.createForUpdate(id);
guestOsHypervisor.setGuestOsName(osNameForHypervisor);
if (_guestOSHypervisorDao.update(id, guestOsHypervisor)) {
@ -2164,6 +2176,10 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
throw new InvalidParameterValueException("Guest OS Mapping not found. Please specify a valid ID for the Guest OS Mapping");
}
if (!guestOsHypervisorHandle.getIsUserDefined()) {
throw new InvalidParameterValueException("Unable to remove system defined Guest OS mapping");
}
return _guestOSHypervisorDao.removeGuestOsMapping(id);
}