Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/storage/GuestOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public interface GuestOS extends InternalIdentity, Identity {
Date getRemoved();

boolean getIsUserDefined();

boolean getForDisplay();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.GuestOSCategoryResponse;
import org.apache.cloudstack.api.response.GuestOSResponse;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.context.CallContext;

import com.cloud.event.EventTypes;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class AddGuestOsCmd extends BaseAsyncCreateCmd {
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, required = false, description = "Map of (key/value pairs)")
private Map details;

@Parameter(name="forDisplay", type=CommandType.BOOLEAN, description="whether this guest OS is available for end users", authorized = {RoleType.Admin})
private Boolean display;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
Expand All @@ -78,7 +81,7 @@ public String getOsName() {
}

public Map getDetails() {
Map<String, String> detailsMap = new HashMap<String, String>();
Map<String, String> detailsMap = new HashMap<>();
if (!details.isEmpty()) {
Collection<?> servicesCollection = details.values();
Iterator<?> iter = servicesCollection.iterator();
Expand All @@ -92,6 +95,9 @@ public Map getDetails() {
return detailsMap;
}

public Boolean getForDisplay() {
return display;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.GuestOSResponse;
import org.apache.cloudstack.acl.RoleType;

import com.cloud.event.EventTypes;
import com.cloud.storage.GuestOS;
Expand Down Expand Up @@ -56,8 +57,10 @@ public class UpdateGuestOsCmd extends BaseAsyncCmd {
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, required = true, description = "Map of (key/value pairs)")
private Map details;

@Parameter(name="forDisplay", type=CommandType.BOOLEAN, description="whether this guest OS is available for end users", authorized = {RoleType.Admin})
private Boolean display;

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

Expand All @@ -70,7 +73,7 @@ public String getOsDisplayName() {
}

public Map getDetails() {
Map<String, String> detailsMap = new HashMap<String, String>();;
Map<String, String> detailsMap = new HashMap<>();;
if (!details.isEmpty()) {
Collection<?> servicesCollection = details.values();
Iterator<?> iter = servicesCollection.iterator();
Expand All @@ -84,6 +87,10 @@ public Map getDetails() {
return detailsMap;
}

public Boolean getForDisplay() {
return display;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.log4j.Logger;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseListCmd;
Expand Down Expand Up @@ -51,6 +53,10 @@ public class ListGuestOsCmd extends BaseListCmd {
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "list os by description", since = "3.0.1")
private String description;

@Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, description = "list resources by display flag; only ROOT admin is eligible to pass this parameter",
since = "4.18.1", authorized = {RoleType.Admin})
private Boolean display;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand All @@ -67,15 +73,19 @@ public String getDescription() {
return description;
}

public Boolean getDisplay() {
return BooleanUtils.toBooleanDefaultIfNull(display, true);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() {
Pair<List<? extends GuestOS>, Integer> result = _mgr.listGuestOSByCriteria(this);
ListResponse<GuestOSResponse> response = new ListResponse<GuestOSResponse>();
List<GuestOSResponse> osResponses = new ArrayList<GuestOSResponse>();
ListResponse<GuestOSResponse> response = new ListResponse<>();
List<GuestOSResponse> osResponses = new ArrayList<>();
for (GuestOS guestOS : result.first()) {
GuestOSResponse guestOSResponse = _responseGenerator.createGuestOSResponse(guestOS);
osResponses.add(guestOSResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class GuestOSResponse extends BaseResponse {
@Param(description = "is the guest OS user defined")
private Boolean isUserDefined;

@SerializedName(ApiConstants.FOR_DISPLAY)
@Param(description = "is the guest OS visible for the users")
private Boolean forDisplay;

public String getId() {
return id;
}
Expand Down Expand Up @@ -75,4 +79,12 @@ public void setIsUserDefined(Boolean isUserDefined) {
this.isUserDefined = isUserDefined;
}

public Boolean getForDisplay() {
return this.forDisplay;
}

public void setForDisplay(final Boolean forDisplay) {
this.forDisplay = forDisplay;
}

}
14 changes: 14 additions & 0 deletions engine/schema/src/main/java/com/cloud/storage/GuestOSVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class GuestOSVO implements GuestOS {
@Column(name = "is_user_defined")
private boolean isUserDefined;

@Column(name = "display", updatable = true, nullable = false)
protected boolean display = true;

@Override
public long getId() {
return id;
Expand Down Expand Up @@ -120,4 +123,15 @@ public boolean getIsUserDefined() {
public void setIsUserDefined(boolean isUserDefined) {
this.isUserDefined = isUserDefined;
}

public boolean getForDisplay() {
return isDisplay();
}
public boolean isDisplay() {
return display;
}

public void setDisplay(boolean display) {
this.display = display;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@
// under the License.
package com.cloud.storage.dao;

import com.cloud.storage.GuestOS;
import com.cloud.storage.GuestOSVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericDao;

import java.util.List;
import java.util.Set;

public interface GuestOSDao extends GenericDao<GuestOSVO, Long> {

GuestOSVO listByDisplayName(String displayName);
GuestOSVO findOneByDisplayName(String displayName);

GuestOSVO findByCategoryIdAndDisplayNameOrderByCreatedDesc(long categoryId, String displayName);

Set<String> findDoubleNames();

List<GuestOSVO> listByDisplayName(String displayName);

Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, Long id, Long osCategoryId, String description, String keyword, Boolean forDisplay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@
package com.cloud.storage.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.cloud.storage.GuestOS;
import com.cloud.utils.Pair;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

Expand All @@ -42,7 +53,7 @@ public GuestOSDaoImpl() {
}

@Override
public GuestOSVO listByDisplayName(String displayName) {
public GuestOSVO findOneByDisplayName(String displayName) {
SearchCriteria<GuestOSVO> sc = Search.create();
sc.setParameters("display_name", displayName);
return findOneBy(sc);
Expand All @@ -62,4 +73,69 @@ public GuestOSVO findByCategoryIdAndDisplayNameOrderByCreatedDesc(long categoryI
}
return null;
}

/**
+ "select display_name from"
+ "(select display_name, count(1) as count from guest_os go1 where removed is null group by display_name having count > 1) tab0";
*
* @return
*/
@Override
@DB
public Set<String> findDoubleNames() {
String selectSql = "SELECT display_name FROM (SELECT display_name, count(1) AS count FROM guest_os go1 WHERE removed IS NULL GROUP BY display_name HAVING count > 1) tab0";
Set<String> names = new HashSet<>();
Connection conn = TransactionLegacy.getStandaloneConnection();
try {
PreparedStatement stmt = conn.prepareStatement(selectSql);
ResultSet rs = stmt.executeQuery();
while (rs != null && rs.next()) {
names.add(rs.getString(1));
}
} catch (SQLException ex) {
throw new CloudRuntimeException("Error while trying to find duplicate guest OSses", ex);
}
return names;
}

/**
* get all with a certain display name
* @param displayName
* @return a list with GuestOS objects
*/
@Override
public List<GuestOSVO> listByDisplayName(String displayName) {
SearchCriteria<GuestOSVO> sc = Search.create();
sc.setParameters("display_name", displayName);
return listBy(sc);
}

public Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, Long id, Long osCategoryId, String description, String keyword, Boolean forDisplay) {
final Filter searchFilter = new Filter(GuestOSVO.class, "displayName", true, startIndex, pageSize);
final SearchCriteria<GuestOSVO> sc = createSearchCriteria();

if (id != null) {
sc.addAnd("id", SearchCriteria.Op.EQ, id);
}

if (osCategoryId != null) {
sc.addAnd("categoryId", SearchCriteria.Op.EQ, osCategoryId);
}

if (description != null) {
sc.addAnd("displayName", SearchCriteria.Op.LIKE, "%" + description + "%");
}

if (keyword != null) {
sc.addAnd("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
}

if (forDisplay != null) {
sc.addAnd("display", SearchCriteria.Op.EQ, forDisplay);
}

final Pair<List<GuestOSVO>, Integer> result = searchAndCount(sc, searchFilter);
return new Pair<>(result.first(), result.second());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
// under the License.
package com.cloud.storage.dao;

import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.GuestOSHypervisorVO;
import com.cloud.utils.db.GenericDao;

import java.util.List;

public interface GuestOSHypervisorDao extends GenericDao<GuestOSHypervisorVO, Long> {

HypervisorType findHypervisorTypeByGuestOsId(long guestOsId);
/**
* list all the mappings for a guesos id
* @param guestOsId the guestos to look for
* @return a list of mappings
*/
List<GuestOSHypervisorVO> listByGuestOsId(long guestOsId);

GuestOSHypervisorVO findByOsIdAndHypervisor(long guestOsId, String hypervisorType, String hypervisorVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import java.util.Date;
import java.util.List;

import com.cloud.utils.db.QueryBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.GuestOSHypervisorVO;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.QueryBuilder;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
Expand Down Expand Up @@ -82,11 +81,10 @@ public GuestOSHypervisorDaoImpl() {
}

@Override
public HypervisorType findHypervisorTypeByGuestOsId(long guestOsId) {
public List<GuestOSHypervisorVO> listByGuestOsId(long guestOsId) {
SearchCriteria<GuestOSHypervisorVO> sc = guestOsSearch.create();
sc.setParameters("guest_os_id", guestOsId);
GuestOSHypervisorVO goh = findOneBy(sc);
return HypervisorType.getType(goh.getHypervisorType());
return listBy(sc);
}

@Override
Expand Down
Loading