Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

Commit a7bf66b

Browse files
amoghvDaanHoogland
authored andcommitted
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>
1 parent 7a13afc commit a7bf66b

7 files changed

Lines changed: 69 additions & 1 deletion

File tree

api/src/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ public class ApiConstants {
290290
public static final String SPECIFY_VLAN = "specifyvlan";
291291
public static final String IS_DEFAULT = "isdefault";
292292
public static final String IS_SYSTEM = "issystem";
293+
public static final String IS_USER_DEFINED = "isuserdefined";
293294
public static final String AVAILABILITY = "availability";
294295
public static final String NETWORKRATE = "networkrate";
295296
public static final String HOST_TAGS = "hosttags";

api/src/org/apache/cloudstack/api/response/GuestOSResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class GuestOSResponse extends BaseResponse {
3939
@Param(description = "the name/description of the OS type")
4040
private String description;
4141

42+
@SerializedName(ApiConstants.IS_USER_DEFINED)
43+
@Param(description = "is the guest OS user defined")
44+
private String isUserDefined;
45+
4246
public String getId() {
4347
return id;
4448
}
@@ -62,4 +66,13 @@ public String getDescription() {
6266
public void setDescription(String description) {
6367
this.description = description;
6468
}
69+
70+
public String getIsUserDefined() {
71+
return isUserDefined;
72+
}
73+
74+
public void setIsUserDefined(String isUserDefined) {
75+
this.isUserDefined = isUserDefined;
76+
}
77+
6578
}

api/src/org/apache/cloudstack/api/response/GuestOsMappingResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public class GuestOsMappingResponse extends BaseResponse {
5252
@Param(description = "hypervisor specific name for the Guest OS")
5353
private String osNameForHypervisor;
5454

55+
@SerializedName(ApiConstants.IS_USER_DEFINED)
56+
@Param(description = "is the mapping user defined")
57+
private String isUserDefined;
58+
59+
public String getIsUserDefined() {
60+
return isUserDefined;
61+
}
62+
63+
public void setIsUserDefined(String isUserDefined) {
64+
this.isUserDefined = isUserDefined;
65+
}
66+
5567
public String getId() {
5668
return id;
5769
}

engine/schema/src/com/cloud/storage/dao/GuestOSHypervisorDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public interface GuestOSHypervisorDao extends GenericDao<GuestOSHypervisorVO, Lo
2727
GuestOSHypervisorVO findByOsIdAndHypervisor(long guestOsId, String hypervisorType, String hypervisorVersion);
2828

2929
boolean removeGuestOsMapping(Long id);
30+
31+
GuestOSHypervisorVO findByOsIdAndHypervisorAndUserDefined(long guestOsId, String hypervisorType, String hypervisorVersion, boolean isUserDefined);
3032
}

engine/schema/src/com/cloud/storage/dao/GuestOSHypervisorDaoImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
3434

3535
protected final SearchBuilder<GuestOSHypervisorVO> guestOsSearch;
3636
protected final SearchBuilder<GuestOSHypervisorVO> mappingSearch;
37+
protected final SearchBuilder<GuestOSHypervisorVO> userDefinedMappingSearch;
3738

3839
protected GuestOSHypervisorDaoImpl() {
3940
guestOsSearch = createSearchBuilder();
@@ -45,6 +46,13 @@ protected GuestOSHypervisorDaoImpl() {
4546
mappingSearch.and("hypervisor_type", mappingSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
4647
mappingSearch.and("hypervisor_version", mappingSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
4748
mappingSearch.done();
49+
50+
userDefinedMappingSearch = createSearchBuilder();
51+
userDefinedMappingSearch.and("guest_os_id", userDefinedMappingSearch.entity().getGuestOsId(), SearchCriteria.Op.EQ);
52+
userDefinedMappingSearch.and("hypervisor_type", userDefinedMappingSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
53+
userDefinedMappingSearch.and("hypervisor_version", userDefinedMappingSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
54+
userDefinedMappingSearch.and("is_user_defined", userDefinedMappingSearch.entity().getIsUserDefined(), SearchCriteria.Op.EQ);
55+
userDefinedMappingSearch.done();
4856
}
4957

5058
@Override
@@ -68,6 +76,20 @@ public GuestOSHypervisorVO findByOsIdAndHypervisor(long guestOsId, String hyperv
6876
return findOneBy(sc);
6977
}
7078

79+
@Override
80+
public GuestOSHypervisorVO findByOsIdAndHypervisorAndUserDefined(long guestOsId, String hypervisorType, String hypervisorVersion, boolean isUserDefined) {
81+
SearchCriteria<GuestOSHypervisorVO> sc = userDefinedMappingSearch.create();
82+
String version = "default";
83+
if (!(hypervisorVersion == null || hypervisorVersion.isEmpty())) {
84+
version = hypervisorVersion;
85+
}
86+
sc.setParameters("guest_os_id", guestOsId);
87+
sc.setParameters("hypervisor_type", hypervisorType);
88+
sc.setParameters("hypervisor_version", version);
89+
sc.setParameters("is_user_defined", isUserDefined);
90+
return findOneBy(sc);
91+
}
92+
7193
@Override
7294
public boolean removeGuestOsMapping(Long id) {
7395
GuestOSHypervisorVO guestOsHypervisor = findById(id);

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,7 @@ public GuestOSResponse createGuestOSResponse(GuestOS guestOS) {
31583158
GuestOSResponse response = new GuestOSResponse();
31593159
response.setDescription(guestOS.getDisplayName());
31603160
response.setId(guestOS.getUuid());
3161+
response.setIsUserDefined(Boolean.valueOf(guestOS.getIsUserDefined()).toString());
31613162
GuestOSCategoryVO category = ApiDBUtils.findGuestOsCategoryById(guestOS.getCategoryId());
31623163
if (category != null) {
31633164
response.setOsCategoryId(category.getUuid());
@@ -3174,6 +3175,7 @@ public GuestOsMappingResponse createGuestOSMappingResponse(GuestOSHypervisor gue
31743175
response.setHypervisor(guestOSHypervisor.getHypervisorType());
31753176
response.setHypervisorVersion(guestOSHypervisor.getHypervisorVersion());
31763177
response.setOsNameForHypervisor((guestOSHypervisor.getGuestOsName()));
3178+
response.setIsUserDefined(Boolean.valueOf(guestOSHypervisor.getIsUserDefined()).toString());
31773179
GuestOS guestOs = ApiDBUtils.findGuestOSById(guestOSHypervisor.getGuestOsId());
31783180
if (guestOs != null) {
31793181
response.setOsStdName(guestOs.getDisplayName());

server/src/com/cloud/server/ManagementServerImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ else if (osStdName != null) {
20292029
throw new InvalidParameterValueException("Unable to find the guest OS by name or UUID");
20302030
}
20312031
//check for duplicates
2032-
GuestOSHypervisorVO duplicate = _guestOSHypervisorDao.findByOsIdAndHypervisor(guestOs.getId(), hypervisorType.toString(), hypervisorVersion);
2032+
GuestOSHypervisorVO duplicate = _guestOSHypervisorDao.findByOsIdAndHypervisorAndUserDefined(guestOs.getId(), hypervisorType.toString(), hypervisorVersion, true);
20332033

20342034
if (duplicate != null) {
20352035
throw new InvalidParameterValueException("Mapping from hypervisor : " + hypervisorType.toString() + ", version : " + hypervisorVersion + " and guest OS : "
@@ -2096,6 +2096,10 @@ public GuestOS updateGuestOs(UpdateGuestOsCmd cmd) {
20962096
throw new InvalidParameterValueException("Guest OS not found. Please specify a valid ID for the Guest OS");
20972097
}
20982098

2099+
if (!guestOsHandle.getIsUserDefined()) {
2100+
throw new InvalidParameterValueException("Unable to modify system defined guest OS");
2101+
}
2102+
20992103
//Check if update is needed
21002104
if (displayName.equals(guestOsHandle.getDisplayName())) {
21012105
return guestOsHandle;
@@ -2127,6 +2131,10 @@ public boolean removeGuestOs(RemoveGuestOsCmd cmd) {
21272131
throw new InvalidParameterValueException("Guest OS not found. Please specify a valid ID for the Guest OS");
21282132
}
21292133

2134+
if (!guestOs.getIsUserDefined()) {
2135+
throw new InvalidParameterValueException("Unable to remove system defined guest OS");
2136+
}
2137+
21302138
return _guestOSDao.remove(id);
21312139
}
21322140

@@ -2143,6 +2151,10 @@ public GuestOSHypervisor updateGuestOsMapping(UpdateGuestOsMappingCmd cmd) {
21432151
throw new InvalidParameterValueException("Guest OS Mapping not found. Please specify a valid ID for the Guest OS Mapping");
21442152
}
21452153

2154+
if (!guestOsHypervisorHandle.getIsUserDefined()) {
2155+
throw new InvalidParameterValueException("Unable to modify system defined Guest OS mapping");
2156+
}
2157+
21462158
GuestOSHypervisorVO guestOsHypervisor = _guestOSHypervisorDao.createForUpdate(id);
21472159
guestOsHypervisor.setGuestOsName(osNameForHypervisor);
21482160
if (_guestOSHypervisorDao.update(id, guestOsHypervisor)) {
@@ -2164,6 +2176,10 @@ public boolean removeGuestOsMapping(RemoveGuestOsMappingCmd cmd) {
21642176
throw new InvalidParameterValueException("Guest OS Mapping not found. Please specify a valid ID for the Guest OS Mapping");
21652177
}
21662178

2179+
if (!guestOsHypervisorHandle.getIsUserDefined()) {
2180+
throw new InvalidParameterValueException("Unable to remove system defined Guest OS mapping");
2181+
}
2182+
21672183
return _guestOSHypervisorDao.removeGuestOsMapping(id);
21682184

21692185
}

0 commit comments

Comments
 (0)