Skip to content

Commit dbb2897

Browse files
author
Kris McQueen
committed
Unexposed parameters can now be assigned to commands. This are for internal use of the command, and will be serialized/deserialized during execution/response phases, but will not be accepted as part of the API request. Also create a DB utility file for the API to use which delegates requests to the DAOs. Mostly this utility class will look up objects by ID, and it allows the removal of similar methods from ManagementServer, thereby reducing some of the clutter in ManagementServer.
1 parent f4caf14 commit dbb2897

12 files changed

Lines changed: 198 additions & 45 deletions
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.cloud.api;
2+
3+
import java.util.List;
4+
5+
import com.cloud.dc.DataCenterVO;
6+
import com.cloud.dc.dao.DataCenterDao;
7+
import com.cloud.domain.DomainVO;
8+
import com.cloud.domain.dao.DomainDao;
9+
import com.cloud.host.HostVO;
10+
import com.cloud.host.dao.HostDao;
11+
import com.cloud.network.IPAddressVO;
12+
import com.cloud.network.dao.IPAddressDao;
13+
import com.cloud.network.security.NetworkGroupManager;
14+
import com.cloud.offering.ServiceOffering;
15+
import com.cloud.server.Criteria;
16+
import com.cloud.server.ManagementServer;
17+
import com.cloud.service.dao.ServiceOfferingDao;
18+
import com.cloud.storage.VMTemplateVO;
19+
import com.cloud.storage.VolumeVO;
20+
import com.cloud.storage.dao.VMTemplateDao;
21+
import com.cloud.storage.dao.VolumeDao;
22+
import com.cloud.user.Account;
23+
import com.cloud.user.User;
24+
import com.cloud.user.dao.AccountDao;
25+
import com.cloud.user.dao.UserDao;
26+
import com.cloud.utils.component.ComponentLocator;
27+
import com.cloud.vm.UserVmVO;
28+
29+
public class ApiDBUtils {
30+
private static ManagementServer _ms;
31+
private static NetworkGroupManager _networkGroupMgr;
32+
private static AccountDao _accountDao;
33+
private static DomainDao _domainDao;
34+
private static HostDao _hostDao;
35+
private static IPAddressDao _ipAddressDao;
36+
private static ServiceOfferingDao _serviceOfferingDao;
37+
private static VMTemplateDao _templateDao;
38+
private static UserDao _userDao;
39+
private static VolumeDao _volumeDao;
40+
private static DataCenterDao _zoneDao;
41+
42+
static {
43+
_ms = (ManagementServer)ComponentLocator.getComponent(ManagementServer.Name);
44+
45+
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
46+
_networkGroupMgr = locator.getManager(NetworkGroupManager.class);
47+
_accountDao = locator.getDao(AccountDao.class);
48+
_domainDao = locator.getDao(DomainDao.class);
49+
_hostDao = locator.getDao(HostDao.class);
50+
_ipAddressDao = locator.getDao(IPAddressDao.class);
51+
_serviceOfferingDao = locator.getDao(ServiceOfferingDao.class);
52+
_templateDao = locator.getDao(VMTemplateDao.class);
53+
_userDao = locator.getDao(UserDao.class);
54+
_volumeDao = locator.getDao(VolumeDao.class);
55+
_zoneDao = locator.getDao(DataCenterDao.class);
56+
}
57+
58+
/////////////////////////////////////////////////////////////
59+
// ManagementServer methods //
60+
/////////////////////////////////////////////////////////////
61+
62+
public static List<UserVmVO> searchForUserVMs(Criteria c) {
63+
return _ms.searchForUserVMs(c);
64+
}
65+
66+
/////////////////////////////////////////////////////////////
67+
// Manager methods //
68+
/////////////////////////////////////////////////////////////
69+
70+
public static String getNetworkGroupsNamesForVm(long vmId) {
71+
return _networkGroupMgr.getNetworkGroupsNamesForVm(vmId);
72+
}
73+
74+
/////////////////////////////////////////////////////////////
75+
// Dao methods //
76+
/////////////////////////////////////////////////////////////
77+
78+
public static Account findAccountById(Long accountId) {
79+
return _accountDao.findById(accountId);
80+
}
81+
82+
public static DomainVO findDomainById(Long domainId) {
83+
return _domainDao.findById(domainId);
84+
}
85+
86+
public static HostVO findHostById(Long hostId) {
87+
return _hostDao.findById(hostId);
88+
}
89+
90+
public static IPAddressVO findIpAddressById(String address) {
91+
return _ipAddressDao.findById(address);
92+
}
93+
94+
public static ServiceOffering findServiceOfferingById(Long serviceOfferingId) {
95+
return _serviceOfferingDao.findById(serviceOfferingId);
96+
}
97+
98+
public static VMTemplateVO findTemplateById(Long templateId) {
99+
return _templateDao.findById(templateId);
100+
}
101+
102+
public static User findUserById(Long userId) {
103+
return _userDao.findById(userId);
104+
}
105+
106+
public static VolumeVO findVolumeById(Long volumeId) {
107+
return _volumeDao.findById(volumeId);
108+
}
109+
110+
public static DataCenterVO findZoneById(Long zoneId) {
111+
return _zoneDao.findById(zoneId);
112+
}
113+
}

‎server/src/com/cloud/api/ApiDispatcher.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ private void setupParameters(BaseCmd cmd, Map<String, String> params) {
152152
Field[] fields = cmd.getClass().getDeclaredFields();
153153
for (Field field : fields) {
154154
Parameter parameterAnnotation = field.getAnnotation(Parameter.class);
155-
if (parameterAnnotation == null) {
155+
if ((parameterAnnotation == null) || !parameterAnnotation.expose()) {
156156
continue;
157157
}
158+
158159
Object paramObj = unpackedParams.get(parameterAnnotation.name());
159160
if (paramObj == null) {
160161
if (parameterAnnotation.required()) {

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ private String queueCommand(BaseCmd cmdObj, Map<String, String> params) {
349349
if (cmdObj instanceof BaseAsyncCmd) {
350350
Long objectId = null;
351351
if (cmdObj instanceof BaseAsyncCreateCmd) {
352-
objectId = _dispatcher.dispatchCreateCmd((BaseAsyncCreateCmd)cmdObj, params);
352+
BaseAsyncCreateCmd createCmd = (BaseAsyncCreateCmd)cmdObj;
353+
objectId = _dispatcher.dispatchCreateCmd(createCmd, params);
354+
createCmd.setId(objectId);
353355
}
354356
BaseAsyncCmd asyncCmd = (BaseAsyncCmd)cmdObj;
355357

‎server/src/com/cloud/api/Parameter.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
boolean required() default false;
3434
CommandType type() default CommandType.OBJECT;
3535
CommandType collectionType() default CommandType.OBJECT;
36+
boolean expose() default true;
3637
}
3738

‎server/src/com/cloud/api/commands/CreateSnapshotCmd.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.log4j.Logger;
2222

23+
import com.cloud.api.ApiDBUtils;
2324
import com.cloud.api.BaseAsyncCreateCmd;
2425
import com.cloud.api.BaseCmd.Manager;
2526
import com.cloud.api.Implementation;
@@ -85,14 +86,14 @@ public String getResponse() {
8586
SnapshotResponse response = new SnapshotResponse();
8687
response.setId(snapshot.getId());
8788

88-
Account account = getAsyncJobMgr().getExecutorContext().getAccountDao().findById(snapshot.getAccountId());
89+
Account account = ApiDBUtils.findAccountById(snapshot.getAccountId());
8990
if (account != null) {
9091
response.setAccountName(account.getAccountName());
9192
response.setDomainId(account.getDomainId());
92-
response.setDomainName(getAsyncJobMgr().getExecutorContext().getManagementServer().findDomainIdById(account.getDomainId()).getName());
93+
response.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName());
9394
}
9495

95-
VolumeVO volume = managementServer.findVolumeById(snapshot.getVolumeId());
96+
VolumeVO volume = ApiDBUtils.findVolumeById(snapshot.getVolumeId());
9697
String snapshotTypeStr = SnapshotType.values()[snapshot.getSnapshotType()].name();
9798
response.setSnapshotType(snapshotTypeStr);
9899
response.setVolumeId(snapshot.getVolumeId());

‎server/src/com/cloud/api/commands/DeployVMCmd.java‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222

2323
import org.apache.log4j.Logger;
2424

25+
import com.cloud.api.ApiDBUtils;
2526
import com.cloud.api.BaseAsyncCmd;
2627
import com.cloud.api.BaseCmd;
2728
import com.cloud.api.Implementation;
2829
import com.cloud.api.Parameter;
2930
import com.cloud.api.response.UserVmResponse;
31+
import com.cloud.offering.ServiceOffering;
3032
import com.cloud.serializer.SerializerHelper;
31-
import com.cloud.service.ServiceOfferingVO;
3233
import com.cloud.storage.VMTemplateVO;
3334
import com.cloud.user.Account;
3435
import com.cloud.user.User;
36+
import com.cloud.user.UserContext;
3537
import com.cloud.uservm.UserVm;
3638

3739
@Implementation(method="deployVirtualMachine")
@@ -77,6 +79,9 @@ public class DeployVMCmd extends BaseAsyncCmd {
7779
@Parameter(name="zoneid", type=CommandType.LONG, required=true)
7880
private Long zoneId;
7981

82+
// unexposed parameter needed for serializing/deserializing the command
83+
@Parameter(name="password", type=CommandType.STRING, expose=false)
84+
private String password;
8085

8186
/////////////////////////////////////////////////////
8287
/////////////////// Accessors ///////////////////////
@@ -126,6 +131,14 @@ public Long getZoneId() {
126131
return zoneId;
127132
}
128133

134+
// not exposed parameter
135+
public String getPassword() {
136+
return password;
137+
}
138+
139+
public void setPassword(String password) {
140+
this.password = password;
141+
}
129142

130143
/////////////////////////////////////////////////////
131144
/////////////// API Implementation///////////////////
@@ -149,7 +162,7 @@ public String getResponse() {
149162
response.setName(userVm.getName());
150163
response.setCreated(userVm.getCreated());
151164
response.setZoneId(userVm.getDataCenterId());
152-
response.setZoneName(getManagementServer().findDataCenterById(userVm.getDataCenterId()).getName());
165+
response.setZoneName(ApiDBUtils.findZoneById(userVm.getDataCenterId()).getName());
153166
response.setPrivateIp(userVm.getPrivateIpAddress());
154167
response.setServiceOfferingId(userVm.getServiceOfferingId());
155168
response.setHaEnable(userVm.isHaEnabled());
@@ -164,20 +177,25 @@ public String getResponse() {
164177
response.setState(userVm.getState().toString());
165178
}
166179

167-
VMTemplateVO template = managementServer.findTemplateById(userVm.getTemplateId());
180+
VMTemplateVO template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
168181

169-
Account acct = managementServer.findAccountById(Long.valueOf(userVm.getAccountId()));
182+
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
170183
if (acct != null) {
171184
response.setAccountName(acct.getAccountName());
172185
response.setDomainId(acct.getDomainId());
173-
response.setDomain(managementServer.findDomainIdById(acct.getDomainId()).getName());
186+
response.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
174187
}
175-
176-
User userExecutingCmd = managementServer.getUser(userId);
188+
189+
Long userId = UserContext.current().getUserId();
190+
if (userId == null) {
191+
userId = User.UID_SYSTEM;
192+
}
193+
177194
//this is for the case where the admin deploys a vm for a normal user
178-
Account acctForUserExecutingCmd = managementServer.findAccountById(Long.valueOf(userExecutingCmd.getAccountId()));
195+
User userExecutingCmd = ApiDBUtils.findUserById(userId);
196+
Account acctForUserExecutingCmd = ApiDBUtils.findAccountById(Long.valueOf(userExecutingCmd.getAccountId()));
179197
if ((BaseCmd.isAdmin(acctForUserExecutingCmd.getType()) && (userVm.getHostId() != null)) || (BaseCmd.isAdmin(acct.getType()) && (userVm.getHostId() != null))) {
180-
response.setHostName(managementServer.getHostBy(userVm.getHostId()).getName());
198+
response.setHostName(ApiDBUtils.findHostById(userVm.getHostId()).getName());
181199
response.setHostId(userVm.getHostId());
182200
}
183201

@@ -195,13 +213,13 @@ public String getResponse() {
195213
}
196214

197215
if (templatePasswordEnabled) { // FIXME: where will the password come from in this case?
198-
response.setPassword(param.getPassword());
216+
response.setPassword(getPassword());
199217
}
200218

201219
// ISO Info
202220
Long isoId = userVm.getIsoId();
203221
if (isoId != null) {
204-
VMTemplateVO iso = getManagementServer().findTemplateById(isoId.longValue());
222+
VMTemplateVO iso = ApiDBUtils.findTemplateById(isoId.longValue());
205223
if (iso != null) {
206224
response.setIsoId(isoId.longValue());
207225
response.setIsoName(iso.getName());
@@ -221,15 +239,15 @@ public String getResponse() {
221239
response.setPasswordEnabled(templatePasswordEnabled);
222240
}
223241

224-
ServiceOfferingVO offering = managementServer.findServiceOfferingById(userVm.getServiceOfferingId());
242+
ServiceOffering offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
225243
response.setServiceOfferingId(userVm.getServiceOfferingId());
226244
response.setServiceOfferingName(offering.getName());
227245

228246
response.setCpuNumber(offering.getCpu());
229247
response.setCpuSpeed(offering.getSpeed());
230248
response.setMemory(offering.getRamSize());
231249

232-
response.setNetworkGroupList(managementServer.getNetworkGroupsNamesForVm(userVm.getId()));
250+
response.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(userVm.getId()));
233251

234252
return SerializerHelper.toSerializedString(response);
235253
}

‎server/src/com/cloud/api/commands/ListPortForwardingRulesCmd.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
import org.apache.log4j.Logger;
2727

28+
import com.cloud.api.ApiDBUtils;
2829
import com.cloud.api.BaseCmd.Manager;
2930
import com.cloud.api.BaseListCmd;
3031
import com.cloud.api.Implementation;
3132
import com.cloud.api.Parameter;
3233
import com.cloud.api.response.FirewallRuleResponse;
3334
import com.cloud.network.FirewallRuleVO;
35+
import com.cloud.network.IPAddressVO;
3436
import com.cloud.serializer.SerializerHelper;
3537
import com.cloud.server.Criteria;
3638
import com.cloud.vm.UserVmVO;
@@ -70,6 +72,7 @@ public String getName() {
7072
public String getResponse() {
7173
List<FirewallRuleVO> firewallRules = (List<FirewallRuleVO>)getResponseObject();
7274
Map<String, UserVmVO> userVmCache = new HashMap<String, UserVmVO>();
75+
IPAddressVO ipAddr = ApiDBUtils.findIpAddressById(ipAddress);
7376

7477
List<FirewallRuleResponse> response = new ArrayList<FirewallRuleResponse>();
7578
for (FirewallRuleVO fwRule : firewallRules) {
@@ -83,10 +86,10 @@ public String getResponse() {
8386
UserVmVO userVM = userVmCache.get(fwRule.getPrivateIpAddress());
8487
if (userVM == null) {
8588
Criteria c = new Criteria();
86-
c.addCriteria(Criteria.ACCOUNTID, new Object[] {addrOwner.getId()});
87-
c.addCriteria(Criteria.DATACENTERID, ipAddressVO.getDataCenterId());
89+
c.addCriteria(Criteria.ACCOUNTID, new Object[] {ipAddr.getAccountId()});
90+
c.addCriteria(Criteria.DATACENTERID, ipAddr.getDataCenterId());
8891
c.addCriteria(Criteria.IPADDRESS, fwRule.getPrivateIpAddress());
89-
List<UserVmVO> userVMs = getManagementServer().searchForUserVMs(c);
92+
List<UserVmVO> userVMs = ApiDBUtils.searchForUserVMs(c);
9093

9194
if ((userVMs != null) && (userVMs.size() > 0)) {
9295
userVM = userVMs.get(0);

‎server/src/com/cloud/api/commands/ListVMsCmd.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.log4j.Logger;
2525

26+
import com.cloud.api.ApiDBUtils;
2627
import com.cloud.api.BaseListCmd;
2728
import com.cloud.api.Implementation;
2829
import com.cloud.api.Parameter;
@@ -214,7 +215,7 @@ public String getResponse() {
214215
userVmResponse.setOsTypeId(userVm.getGuestOSId());
215216

216217
//network groups
217-
userVmResponse.setNetworkGroupList(getManagementServer().getNetworkGroupsNamesForVm(userVm.getId()));
218+
userVmResponse.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(userVm.getId()));
218219

219220
response.add(userVmResponse);
220221
}

‎server/src/com/cloud/api/response/SnapshotResponse.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SnapshotResponse implements ResponseObject {
3333
private Long domainId;
3434

3535
@Param(name="domain")
36-
private String domain;
36+
private String domainName;
3737

3838
@Param(name="snapshottype")
3939
private String snapshotType;
@@ -86,12 +86,12 @@ public void setDomainId(Long domainId) {
8686
this.domainId = domainId;
8787
}
8888

89-
public String getDomain() {
90-
return domain;
89+
public String getDomainName() {
90+
return domainName;
9191
}
9292

93-
public void setDomain(String domain) {
94-
this.domain = domain;
93+
public void setDomainName(String domainName) {
94+
this.domainName = domainName;
9595
}
9696

9797
public String getSnapshotType() {

0 commit comments

Comments
 (0)