Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions api/src/com/cloud/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public interface User extends OwnedBy, InternalIdentity {
public static final long UID_SYSTEM = 1;
public static final long UID_ADMIN = 2;

@Override
public long getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.EnumSet;
import java.util.List;

import org.apache.cloudstack.api.response.UserResponse;
import org.apache.log4j.Logger;

import org.apache.cloudstack.acl.RoleType;
Expand Down Expand Up @@ -126,6 +127,9 @@ public class ListVMsCmd extends BaseListTaggedResourcesCmd {
@Parameter(name = ApiConstants.DISPLAY_VM, type = CommandType.BOOLEAN, description = "list resources by display flag; only ROOT admin is eligible to pass this parameter", since = "4.4", authorized = {RoleType.Admin})
private Boolean display;

@Parameter(name = ApiConstants.USER_ID, type = CommandType.UUID, entityType = UserResponse.class, required = true, description = "the user ID that created the VM and is under the account that owns the VM")
private Long userId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand All @@ -142,6 +146,10 @@ public List<Long> getIds() {
return ids;
}

public Long getUserId() {
return userId;
}

public String getName() {
return name;
}
Expand Down
39 changes: 39 additions & 0 deletions engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

Expand Down Expand Up @@ -55,6 +58,42 @@ public File[] getPrepareScripts() {

@Override
public void performDataMigration(Connection conn) {
updateVMInstanceUserId(conn);
}

public void updateVMInstanceUserId(Connection conn) {
// For schemas before this, copy first user from an account_id which deployed already running VMs
s_logger.debug("Updating vm_instance column user_id using first user in vm_instance's account_id");
String vmInstanceSql = "SELECT id, account_id FROM `cloud`.`vm_instance`";
String userSql = "SELECT id FROM `cloud`.`user` where account_id=?";
String userIdUpdateSql = "update `cloud`.`vm_instance` set user_id=? where id=?";
try(PreparedStatement selectStatement = conn.prepareStatement(vmInstanceSql)) {
ResultSet results = selectStatement.executeQuery();
while (results.next()) {
long vmId = results.getLong(1);
long accountId = results.getLong(2);
try (PreparedStatement selectUserStatement = conn.prepareStatement(userSql)) {
selectUserStatement.setLong(1, accountId);
ResultSet userResults = selectUserStatement.executeQuery();
if (userResults.next()) {
long userId = userResults.getLong(1);
try (PreparedStatement updateStatement = conn.prepareStatement(userIdUpdateSql)) {
updateStatement.setLong(1, userId);
updateStatement.setLong(2, vmId);
updateStatement.executeUpdate();
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update user ID " + userId + " on vm_instance id=" + vmId, e);
}
}

} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update user ID using accountId " + accountId + " on vm_instance id=" + vmId, e);
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update user Ids for previously deployed VMs", e);
}
s_logger.debug("Done updating user Ids for previously deployed VMs");
}


Expand Down
4 changes: 2 additions & 2 deletions engine/schema/src/com/cloud/vm/ConsoleProxyVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public class ConsoleProxyVO extends VMInstanceVO implements ConsoleProxy {
*
*/
public ConsoleProxyVO(long id, long serviceOfferingId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long dataCenterId, long domainId,
long accountId, int activeSession, boolean haEnabled) {
super(id, serviceOfferingId, name, name, Type.ConsoleProxy, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
long accountId, long userId, int activeSession, boolean haEnabled) {
super(id, serviceOfferingId, name, name, Type.ConsoleProxy, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
this.activeSession = activeSession;
}

Expand Down
12 changes: 6 additions & 6 deletions engine/schema/src/com/cloud/vm/DomainRouterVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public class DomainRouterVO extends VMInstanceVO implements VirtualRouter {
private Long vpcId;

public DomainRouterVO(long id, long serviceOfferingId, long elementId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long domainId,
long accountId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
Long vpcId) {
super(id, serviceOfferingId, name, name, Type.DomainRouter, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
long accountId, long userId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
Long vpcId) {
super(id, serviceOfferingId, name, name, Type.DomainRouter, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
this.elementId = elementId;
this.isRedundantRouter = isRedundantRouter;
this.priority = priority;
Expand All @@ -89,9 +89,9 @@ public DomainRouterVO(long id, long serviceOfferingId, long elementId, String na
}

public DomainRouterVO(long id, long serviceOfferingId, long elementId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long domainId,
long accountId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
VirtualMachine.Type vmType, Long vpcId) {
super(id, serviceOfferingId, name, name, vmType, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
long accountId, long userId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
Type vmType, Long vpcId) {
super(id, serviceOfferingId, name, name, vmType, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
this.elementId = elementId;
this.isRedundantRouter = isRedundantRouter;
this.priority = priority;
Expand Down
4 changes: 2 additions & 2 deletions engine/schema/src/com/cloud/vm/SecondaryStorageVmVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public class SecondaryStorageVmVO extends VMInstanceVO implements SecondaryStora
private Date lastUpdateTime;

public SecondaryStorageVmVO(long id, long serviceOfferingId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long dataCenterId,
long domainId, long accountId, Role role, boolean haEnabled) {
super(id, serviceOfferingId, name, name, Type.SecondaryStorageVm, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
long domainId, long accountId, long userId, Role role, boolean haEnabled) {
super(id, serviceOfferingId, name, name, Type.SecondaryStorageVm, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
this.role = role;
}

Expand Down
4 changes: 2 additions & 2 deletions engine/schema/src/com/cloud/vm/UserVmVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public long getServiceOfferingId() {
}

public UserVmVO(long id, String instanceName, String displayName, long templateId, HypervisorType hypervisorType, long guestOsId, boolean haEnabled,
boolean limitCpuUse, long domainId, long accountId, long serviceOfferingId, String userData, String name, Long diskOfferingId) {
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled, limitCpuUse, diskOfferingId);
boolean limitCpuUse, long domainId, long accountId, long userId, long serviceOfferingId, String userData, String name, Long diskOfferingId) {
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, userId, haEnabled, limitCpuUse, diskOfferingId);
this.userData = userData;
this.displayName = displayName;
this.details = new HashMap<String, String>();
Expand Down
9 changes: 6 additions & 3 deletions engine/schema/src/com/cloud/vm/VMInstanceVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
@Column(name = "account_id")
protected long accountId;

@Column(name = "user_id")
protected long userId;

@Column(name = "service_offering_id")
protected long serviceOfferingId;

Expand Down Expand Up @@ -186,7 +189,7 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
protected Long powerHostId;

public VMInstanceVO(long id, long serviceOfferingId, String name, String instanceName, Type type, Long vmTemplateId, HypervisorType hypervisorType, long guestOSId,
long domainId, long accountId, boolean haEnabled) {
long domainId, long accountId, long userId, boolean haEnabled) {
this.id = id;
hostName = name != null ? name : uuid;
if (vmTemplateId != null) {
Expand All @@ -213,8 +216,8 @@ public VMInstanceVO(long id, long serviceOfferingId, String name, String instanc
}

public VMInstanceVO(long id, long serviceOfferingId, String name, String instanceName, Type type, Long vmTemplateId, HypervisorType hypervisorType, long guestOSId,
long domainId, long accountId, boolean haEnabled, boolean limitResourceUse, Long diskOfferingId) {
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
long domainId, long accountId, long userId, boolean haEnabled, boolean limitResourceUse, Long diskOfferingId) {
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
limitCpuUse = limitResourceUse;
this.diskOfferingId = diskOfferingId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.inject.Inject;

import com.cloud.user.dao.UserDao;
import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
Expand Down Expand Up @@ -140,6 +141,8 @@ public class LoadBalanceRuleHandler {
private PhysicalNetworkServiceProviderDao _physicalProviderDao;
@Inject
private VirtualRouterProviderDao _vrProviderDao;
@Inject
private UserDao _userDao;

static final private String ELB_VM_NAME_PREFIX = "l";

Expand Down Expand Up @@ -272,8 +275,13 @@ private DomainRouterVO deployELBVm(Network guestNetwork, DeployDestination dest,
throw new CloudRuntimeException("Cannot find virtual router provider " + typeString + " as service provider " + provider.getId());
}

long userId = CallContext.current().getCallingUserId();
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
userId = _userDao.listByAccount(owner.getAccountId()).get(0).getId();
}

elbVm = new DomainRouterVO(id, _elasticLbVmOffering.getId(), vrProvider.getId(), VirtualMachineName.getSystemVmName(id, _instance, ELB_VM_NAME_PREFIX),
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false, RedundantState.UNKNOWN,
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, false, 0, false, RedundantState.UNKNOWN,
_elasticLbVmOffering.getOfferHA(), false, VirtualMachine.Type.ElasticLoadBalancerVm, null);
elbVm.setRole(Role.LB);
elbVm = _routerDao.persist(elbVm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.user.dao.UserDao;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO;
Expand Down Expand Up @@ -164,6 +166,8 @@ public class InternalLoadBalancerVMManagerImpl extends ManagerBase implements In
VMTemplateDao _templateDao;
@Inject
ResourceManager _resourceMgr;
@Inject
UserDao _userDao;

@Override
public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) {
Expand Down Expand Up @@ -760,9 +764,14 @@ protected DomainRouterVO deployInternalLbVm(Account owner, DeployDestination des
continue;
}

long userId = CallContext.current().getCallingUserId();
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
userId = _userDao.listByAccount(owner.getAccountId()).get(0).getId();
}

internalLbVm =
new DomainRouterVO(id, routerOffering.getId(), internalLbProviderId, VirtualMachineName.getSystemVmName(id, _instance, InternalLbVmNamePrefix),
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false,
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, false, 0, false,
RedundantState.UNKNOWN, false, false, VirtualMachine.Type.InternalLoadBalancerVm, vpcId);
internalLbVm.setRole(Role.INTERNAL_LB_VM);
internalLbVm = _internalLbVmDao.persist(internalLbVm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void setUp() {
ComponentContext.initComponentsLifeCycle();

vm =
new DomainRouterVO(1L, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, false, 0, false, null, false, false,
new DomainRouterVO(1L, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, 1, false, 0, false, null, false, false,
VirtualMachine.Type.InternalLoadBalancerVm, null);
vm.setRole(Role.INTERNAL_LB_VM);
vm = setId(vm, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public void setUp() {
CallContext.register(_accountMgr.getSystemUser(), _accountMgr.getSystemAccount());

DomainRouterVO validVm =
new DomainRouterVO(validVmId, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, false, 0, false, null, false, false,
new DomainRouterVO(validVmId, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, 1, false, 0, false, null, false, false,
VirtualMachine.Type.InternalLoadBalancerVm, null);
validVm.setRole(Role.INTERNAL_LB_VM);
DomainRouterVO nonInternalLbVm =
new DomainRouterVO(validVmId, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, false, 0, false, null, false, false,
new DomainRouterVO(validVmId, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, 1, false, 0, false, null, false, false,
VirtualMachine.Type.DomainRouter, null);
nonInternalLbVm.setRole(Role.VIRTUAL_ROUTER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
<bean id="InternalLoadBalancerVMManager" class="org.apache.cloudstack.network.lb.InternalLoadBalancerVMManagerImpl">
<property name="name" value="InternalLoadBalancerVMManager"/>
</bean>


<bean id="UserDao" class="com.cloud.user.dao.UserDaoImpl">
<property name="name" value="UserDao"/>
</bean>

<bean class="org.apache.cloudstack.internallbvmmgr.LbChildTestConfiguration" />

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<bean id="InternalLoadBalancerVMService" class="org.apache.cloudstack.network.lb.InternalLoadBalancerVMManagerImpl">
<property name="name" value="InternalLoadBalancerVMService"/>
</bean>

<bean id="UserDao" class="com.cloud.user.dao.UserDaoImpl">
<property name="name" value="UserDao"/>
</bean>

<bean class="org.apache.cloudstack.internallbvmmgr.LbChildTestConfiguration" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ private ServiceVirtualMachine createServiceVM(DataCenter zone, Account owner, Vi
networks.put((NetworkVO)left, new ArrayList<NicProfile>());
networks.put((NetworkVO)right, new ArrayList<NicProfile>());
String instanceName = VirtualMachineName.getVmName(id, owner.getId(), "SRV");

long userId = CallContext.current().getCallingUserId();
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
userId = _userDao.listByAccount(owner.getAccountId()).get(0).getId();
}

ServiceVirtualMachine svm =
new ServiceVirtualMachine(id, instanceName, name, template.getId(), serviceOffering.getId(), template.getHypervisorType(), template.getGuestOSId(),
zone.getId(), owner.getDomainId(), owner.getAccountId(), false);
zone.getId(), owner.getDomainId(), owner.getAccountId(), userId, false);

// database synchronization code must be able to distinguish service instance VMs.
Map<String, String> kvmap = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class ServiceVirtualMachine extends UserVmVO {
public ServiceVirtualMachine(long id, String instanceName, String name, long templateId, long serviceOfferingId, HypervisorType hypervisorType, long guestOSId,
long dataCenterId, long domainId, long accountId, boolean haEnabled) {
super(id, instanceName, name, templateId, hypervisorType, guestOSId, false, false, domainId, accountId, serviceOfferingId, null, name, null);
long dataCenterId, long domainId, long accountId, long userId, boolean haEnabled) {
super(id, instanceName, name, templateId, hypervisorType, guestOSId, false, false, domainId, accountId, userId, serviceOfferingId, null, name, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public Object answer(InvocationOnMock invocation) {
long id = _userVmDao.getNextInSequence(Long.class, "id");
UserVmVO vm =
new UserVmVO(id, name, name, tmpl.getId(), HypervisorType.XenServer, tmpl.getGuestOSId(), false, false, _zone.getDomainId(), Account.ACCOUNT_ID_SYSTEM,
small.getId(), null, name, null);
1, small.getId(), null, name, null);
vm.setState(com.cloud.vm.VirtualMachine.State.Running);
vm.setHostId(_hostId);
vm.setDataCenterId(network.getDataCenterId());
Expand Down
5 changes: 5 additions & 0 deletions server/src/com/cloud/api/query/QueryManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm

boolean listAll = cmd.listAll();
Long id = cmd.getId();
Long userId = cmd.getUserId();
Map<String, String> tags = cmd.getTags();
Boolean display = cmd.getDisplay();
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
Expand Down Expand Up @@ -862,6 +863,10 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
}

if (userId != null) {
sb.and("userId", sb.entity().getUserId(), SearchCriteria.Op.EQ);
}

if (networkId != null) {
sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
}
Expand Down
Loading