Skip to content

Commit 1e25886

Browse files
CLOUDSTACK-7908: Add user_id column to vm_instance table
- Adds column to VMInstance DAO - Adds column in vm_instance table - Adds column in the UserVMJoinVO - Adds default admin user which has UID = 2 - Adds migration path that sets user_id to first user of the accountId that owns the vm in vm_instance table - Add arg on list VMs API to query by userId, add support in query layer - Refactor VMInstanceVO and child classes to accept userId - Add code to let service layer pass userId if loggedIn user belongs to same account as the owner executing an API call or use first user from owner account - In case of CPVM and SSVM use system user ID - Fix unit tests and spring injections Signed-off-by: Rohit Yadav <[email protected]>
1 parent 0128d62 commit 1e25886

File tree

30 files changed

+346
-42
lines changed

30 files changed

+346
-42
lines changed

api/src/com/cloud/user/User.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
public interface User extends OwnedBy, InternalIdentity {
2424
public static final long UID_SYSTEM = 1;
25+
public static final long UID_ADMIN = 2;
2526

2627
@Override
2728
public long getId();

api/src/org/apache/cloudstack/api/command/user/vm/ListVMsCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.EnumSet;
2121
import java.util.List;
2222

23+
import org.apache.cloudstack.api.response.UserResponse;
2324
import org.apache.log4j.Logger;
2425

2526
import org.apache.cloudstack.acl.RoleType;
@@ -126,6 +127,9 @@ public class ListVMsCmd extends BaseListTaggedResourcesCmd {
126127
@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})
127128
private Boolean display;
128129

130+
@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")
131+
private Long userId;
132+
129133
/////////////////////////////////////////////////////
130134
/////////////////// Accessors ///////////////////////
131135
/////////////////////////////////////////////////////
@@ -142,6 +146,10 @@ public List<Long> getIds() {
142146
return ids;
143147
}
144148

149+
public Long getUserId() {
150+
return userId;
151+
}
152+
145153
public String getName() {
146154
return name;
147155
}

engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import java.io.File;
2121
import java.sql.Connection;
22+
import java.sql.PreparedStatement;
23+
import java.sql.ResultSet;
24+
import java.sql.SQLException;
2225

2326
import org.apache.log4j.Logger;
2427

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

5659
@Override
5760
public void performDataMigration(Connection conn) {
61+
updateVMInstanceUserId(conn);
62+
}
63+
64+
public void updateVMInstanceUserId(Connection conn) {
65+
// For schemas before this, copy first user from an account_id which deployed already running VMs
66+
s_logger.debug("Updating vm_instance column user_id using first user in vm_instance's account_id");
67+
String vmInstanceSql = "SELECT id, account_id FROM `cloud`.`vm_instance`";
68+
String userSql = "SELECT id FROM `cloud`.`user` where account_id=?";
69+
String userIdUpdateSql = "update `cloud`.`vm_instance` set user_id=? where id=?";
70+
try(PreparedStatement selectStatement = conn.prepareStatement(vmInstanceSql)) {
71+
ResultSet results = selectStatement.executeQuery();
72+
while (results.next()) {
73+
long vmId = results.getLong(1);
74+
long accountId = results.getLong(2);
75+
try (PreparedStatement selectUserStatement = conn.prepareStatement(userSql)) {
76+
selectUserStatement.setLong(1, accountId);
77+
ResultSet userResults = selectUserStatement.executeQuery();
78+
if (userResults.next()) {
79+
long userId = userResults.getLong(1);
80+
try (PreparedStatement updateStatement = conn.prepareStatement(userIdUpdateSql)) {
81+
updateStatement.setLong(1, userId);
82+
updateStatement.setLong(2, vmId);
83+
updateStatement.executeUpdate();
84+
} catch (SQLException e) {
85+
throw new CloudRuntimeException("Unable to update user ID " + userId + " on vm_instance id=" + vmId, e);
86+
}
87+
}
88+
89+
} catch (SQLException e) {
90+
throw new CloudRuntimeException("Unable to update user ID using accountId " + accountId + " on vm_instance id=" + vmId, e);
91+
}
92+
}
93+
} catch (SQLException e) {
94+
throw new CloudRuntimeException("Unable to update user Ids for previously deployed VMs", e);
95+
}
96+
s_logger.debug("Done updating user Ids for previously deployed VMs");
5897
}
5998

6099

engine/schema/src/com/cloud/vm/ConsoleProxyVO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public class ConsoleProxyVO extends VMInstanceVO implements ConsoleProxy {
144144
*
145145
*/
146146
public ConsoleProxyVO(long id, long serviceOfferingId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long dataCenterId, long domainId,
147-
long accountId, int activeSession, boolean haEnabled) {
148-
super(id, serviceOfferingId, name, name, Type.ConsoleProxy, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
147+
long accountId, long userId, int activeSession, boolean haEnabled) {
148+
super(id, serviceOfferingId, name, name, Type.ConsoleProxy, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
149149
this.activeSession = activeSession;
150150
}
151151

engine/schema/src/com/cloud/vm/DomainRouterVO.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public class DomainRouterVO extends VMInstanceVO implements VirtualRouter {
7676
private Long vpcId;
7777

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

9191
public DomainRouterVO(long id, long serviceOfferingId, long elementId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long domainId,
92-
long accountId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
93-
VirtualMachine.Type vmType, Long vpcId) {
94-
super(id, serviceOfferingId, name, name, vmType, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
92+
long accountId, long userId, boolean isRedundantRouter, int priority, boolean isPriorityBumpUp, RedundantState redundantState, boolean haEnabled, boolean stopPending,
93+
Type vmType, Long vpcId) {
94+
super(id, serviceOfferingId, name, name, vmType, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
9595
this.elementId = elementId;
9696
this.isRedundantRouter = isRedundantRouter;
9797
this.priority = priority;

engine/schema/src/com/cloud/vm/SecondaryStorageVmVO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public class SecondaryStorageVmVO extends VMInstanceVO implements SecondaryStora
6464
private Date lastUpdateTime;
6565

6666
public SecondaryStorageVmVO(long id, long serviceOfferingId, String name, long templateId, HypervisorType hypervisorType, long guestOSId, long dataCenterId,
67-
long domainId, long accountId, Role role, boolean haEnabled) {
68-
super(id, serviceOfferingId, name, name, Type.SecondaryStorageVm, templateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
67+
long domainId, long accountId, long userId, Role role, boolean haEnabled) {
68+
super(id, serviceOfferingId, name, name, Type.SecondaryStorageVm, templateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
6969
this.role = role;
7070
}
7171

engine/schema/src/com/cloud/vm/UserVmVO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public long getServiceOfferingId() {
7070
}
7171

7272
public UserVmVO(long id, String instanceName, String displayName, long templateId, HypervisorType hypervisorType, long guestOsId, boolean haEnabled,
73-
boolean limitCpuUse, long domainId, long accountId, long serviceOfferingId, String userData, String name, Long diskOfferingId) {
74-
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled, limitCpuUse, diskOfferingId);
73+
boolean limitCpuUse, long domainId, long accountId, long userId, long serviceOfferingId, String userData, String name, Long diskOfferingId) {
74+
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, userId, haEnabled, limitCpuUse, diskOfferingId);
7575
this.userData = userData;
7676
this.displayName = displayName;
7777
this.details = new HashMap<String, String>();

engine/schema/src/com/cloud/vm/VMInstanceVO.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
141141
@Column(name = "account_id")
142142
protected long accountId;
143143

144+
@Column(name = "user_id")
145+
protected long userId;
146+
144147
@Column(name = "service_offering_id")
145148
protected long serviceOfferingId;
146149

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

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

215218
public VMInstanceVO(long id, long serviceOfferingId, String name, String instanceName, Type type, Long vmTemplateId, HypervisorType hypervisorType, long guestOSId,
216-
long domainId, long accountId, boolean haEnabled, boolean limitResourceUse, Long diskOfferingId) {
217-
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
219+
long domainId, long accountId, long userId, boolean haEnabled, boolean limitResourceUse, Long diskOfferingId) {
220+
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, userId, haEnabled);
218221
limitCpuUse = limitResourceUse;
219222
this.diskOfferingId = diskOfferingId;
220223
}

plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/LoadBalanceRuleHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.inject.Inject;
2929

30+
import com.cloud.user.dao.UserDao;
3031
import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
3132
import org.apache.cloudstack.context.CallContext;
3233
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
@@ -140,6 +141,8 @@ public class LoadBalanceRuleHandler {
140141
private PhysicalNetworkServiceProviderDao _physicalProviderDao;
141142
@Inject
142143
private VirtualRouterProviderDao _vrProviderDao;
144+
@Inject
145+
private UserDao _userDao;
143146

144147
static final private String ELB_VM_NAME_PREFIX = "l";
145148

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

278+
long userId = CallContext.current().getCallingUserId();
279+
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
280+
userId = _userDao.listByAccount(owner.getAccountId()).get(0).getId();
281+
}
282+
275283
elbVm = new DomainRouterVO(id, _elasticLbVmOffering.getId(), vrProvider.getId(), VirtualMachineName.getSystemVmName(id, _instance, ELB_VM_NAME_PREFIX),
276-
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false, RedundantState.UNKNOWN,
284+
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, false, 0, false, RedundantState.UNKNOWN,
277285
_elasticLbVmOffering.getOfferHA(), false, VirtualMachine.Type.ElasticLoadBalancerVm, null);
278286
elbVm.setRole(Role.LB);
279287
elbVm = _routerDao.persist(elbVm);

plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerVMManagerImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import javax.inject.Inject;
2828
import javax.naming.ConfigurationException;
2929

30+
import com.cloud.user.dao.UserDao;
31+
import org.apache.cloudstack.context.CallContext;
3032
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
3133
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3234
import org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO;
@@ -164,6 +166,8 @@ public class InternalLoadBalancerVMManagerImpl extends ManagerBase implements In
164166
VMTemplateDao _templateDao;
165167
@Inject
166168
ResourceManager _resourceMgr;
169+
@Inject
170+
UserDao _userDao;
167171

168172
@Override
169173
public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) {
@@ -760,9 +764,14 @@ protected DomainRouterVO deployInternalLbVm(Account owner, DeployDestination des
760764
continue;
761765
}
762766

767+
long userId = CallContext.current().getCallingUserId();
768+
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
769+
userId = _userDao.listByAccount(owner.getAccountId()).get(0).getId();
770+
}
771+
763772
internalLbVm =
764773
new DomainRouterVO(id, routerOffering.getId(), internalLbProviderId, VirtualMachineName.getSystemVmName(id, _instance, InternalLbVmNamePrefix),
765-
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false,
774+
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, false, 0, false,
766775
RedundantState.UNKNOWN, false, false, VirtualMachine.Type.InternalLoadBalancerVm, vpcId);
767776
internalLbVm.setRole(Role.INTERNAL_LB_VM);
768777
internalLbVm = _internalLbVmDao.persist(internalLbVm);

0 commit comments

Comments
 (0)