Skip to content

Commit 25c2734

Browse files
committed
More stuff to Projects feature - added support for adding resources (vms, templates, isos) to the project
1 parent 9985682 commit 25c2734

117 files changed

Lines changed: 1377 additions & 2052 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/src/com/cloud/acl/SecurityChecker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ public enum AccessType {
4646
* Checks if the account owns the object.
4747
*
4848
* @param caller account to check against.
49-
* @param accessType TODO
5049
* @param object object that the account is trying to access.
5150
* @return true if access allowed. false if this adapter cannot authenticate ownership.
5251
* @throws PermissionDeniedException if this adapter is suppose to authenticate ownership and the check failed.
5352
*/
54-
boolean checkAccess(Account caller, Domain domain, AccessType accessType) throws PermissionDeniedException;
53+
boolean checkAccess(Account caller, Domain domain) throws PermissionDeniedException;
5554

5655
/**
5756
* Checks if the user belongs to an account that owns the object.

api/src/com/cloud/agent/api/ClusterSyncAnswer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.cloud.agent.api;
1919

2020
import java.util.HashMap;
21-
import java.util.Map;
2221

2322
import com.cloud.utils.Pair;
2423
import com.cloud.vm.VirtualMachine.State;

api/src/com/cloud/agent/api/ClusterSyncCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.cloud.agent.api;
1919

20-
import java.util.Map;
21-
22-
import com.cloud.vm.VirtualMachine.State;
2320

2421
public class ClusterSyncCommand extends Command implements CronCommand {
2522
int _interval;

api/src/com/cloud/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public class ApiConstants {
261261
public static final String HYPERVISOR_VERSION = "hypervisorversion";
262262
public static final String MAX_GUESTS_LIMIT = "maxguestslimit";
263263
public static final String PROJECT_ID = "projectid";
264+
public static final String PROJECT_IDS = "projectids";
264265
public static final String PROJECT = "project";
265266
public static final String ROLE = "role";
266267
public static final String USER = "user";

api/src/com/cloud/api/BaseCmd.java

Lines changed: 1 addition & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.cloud.exception.ConcurrentOperationException;
3636
import com.cloud.exception.InsufficientCapacityException;
3737
import com.cloud.exception.InvalidParameterValueException;
38-
import com.cloud.exception.PermissionDeniedException;
3938
import com.cloud.exception.ResourceAllocationException;
4039
import com.cloud.exception.ResourceUnavailableException;
4140
import com.cloud.network.NetworkService;
@@ -56,7 +55,6 @@
5655
import com.cloud.user.AccountService;
5756
import com.cloud.user.DomainService;
5857
import com.cloud.user.ResourceLimitService;
59-
import com.cloud.user.UserContext;
6058
import com.cloud.utils.Pair;
6159
import com.cloud.utils.component.ComponentLocator;
6260
import com.cloud.vm.BareMetalVmService;
@@ -194,124 +192,6 @@ public static String getDateString(Date date) {
194192
return formattedString;
195193
}
196194

197-
protected Account getValidOwner(String accountName, Long domainId) {
198-
Account owner = null;
199-
if (accountName != null) {
200-
owner = _responseGenerator.findAccountByNameDomain(accountName, domainId);
201-
} else {
202-
owner = UserContext.current().getCaller();
203-
}
204-
if (owner == null) {
205-
throw new InvalidParameterValueException("Invalid value for owner specified: " + accountName);
206-
}
207-
if (owner.getState() == Account.State.disabled || owner.getState() == Account.State.locked) {
208-
throw new PermissionDeniedException("Account disabled.");
209-
}
210-
return owner;
211-
}
212-
213-
public Map<String, Object> validateParams(Map<String, String> params, boolean decode) {
214-
// List<Pair<Enum, Boolean>> properties = getProperties();
215-
216-
// step 1 - all parameter names passed in will be converted to lowercase
217-
Map<String, Object> processedParams = lowercaseParams(params, decode);
218-
return processedParams;
219-
220-
/*
221-
// step 2 - make sure all required params exist, and all existing params adhere to the appropriate data type
222-
Map<String, Object> validatedParams = new HashMap<String, Object>();
223-
for (Pair<Enum, Boolean> propertyPair : properties) {
224-
Properties prop = (Properties)propertyPair.first();
225-
Object param = processedParams.get(prop.getName());
226-
// possible validation errors are
227-
// - NULL (not specified)
228-
// - MALFORMED
229-
if (param != null) {
230-
short propertyType = prop.getDataType();
231-
String decodedParam = null;
232-
if ((propertyType != TYPE_OBJECT) && (propertyType != TYPE_OBJECT_MAP)) {
233-
decodedParam = (String)param;
234-
if (decode) {
235-
try {
236-
decodedParam = URLDecoder.decode((String)param, "UTF-8");
237-
} catch (UnsupportedEncodingException usex) {
238-
s_logger.warn(prop.getName() + " could not be decoded, value = " + param);
239-
throw new ServerApiException(PARAM_ERROR, prop.getName() + " could not be decoded");
240-
}
241-
}
242-
}
243-
244-
switch (propertyType) {
245-
case TYPE_INT:
246-
try {
247-
validatedParams.put(prop.getName(), Integer.valueOf(Integer.parseInt(decodedParam)));
248-
} catch (NumberFormatException ex) {
249-
s_logger.warn(prop.getName() + " (type is int) is malformed, value = " + decodedParam);
250-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getName() + " is malformed");
251-
}
252-
break;
253-
case TYPE_LONG:
254-
try {
255-
validatedParams.put(prop.getName(), Long.valueOf(Long.parseLong(decodedParam)));
256-
} catch (NumberFormatException ex) {
257-
s_logger.warn(prop.getName() + " (type is long) is malformed, value = " + decodedParam);
258-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getName() + " is malformed");
259-
}
260-
break;
261-
case TYPE_DATE:
262-
try {
263-
synchronized(_format) { // SimpleDataFormat is not thread safe, synchronize on it to avoid parse errors
264-
validatedParams.put(prop.getName(), _format.parse(decodedParam));
265-
}
266-
} catch (ParseException ex) {
267-
s_logger.warn(prop.getName() + " (type is date) is malformed, value = " + decodedParam);
268-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getName() + " uses an unsupported date format");
269-
}
270-
break;
271-
case TYPE_TZDATE:
272-
try {
273-
validatedParams.put(prop.getName(), DateUtil.parseTZDateString(decodedParam));
274-
} catch (ParseException ex) {
275-
s_logger.warn(prop.getName() + " (type is date) is malformed, value = " + decodedParam);
276-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getName() + " uses an unsupported date format");
277-
}
278-
break;
279-
case TYPE_FLOAT:
280-
try {
281-
validatedParams.put(prop.getName(), Float.valueOf(Float.parseFloat(decodedParam)));
282-
} catch (NumberFormatException ex) {
283-
s_logger.warn(prop.getName() + " (type is float) is malformed, value = " + decodedParam);
284-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getName() + " is malformed");
285-
}
286-
break;
287-
case TYPE_BOOLEAN:
288-
validatedParams.put(prop.getName(), Boolean.valueOf(Boolean.parseBoolean(decodedParam)));
289-
break;
290-
case TYPE_STRING:
291-
validatedParams.put(prop.getName(), decodedParam);
292-
break;
293-
default:
294-
validatedParams.put(prop.getName(), param);
295-
break;
296-
}
297-
} else if (propertyPair.second().booleanValue() == true) {
298-
s_logger.warn("missing parameter, " + prop.getTagName() + " is not specified");
299-
throw new ServerApiException(MALFORMED_PARAMETER_ERROR, prop.getTagName() + " is not specified");
300-
}
301-
}
302-
303-
return validatedParams;
304-
*/
305-
}
306-
307-
private Map<String, Object> lowercaseParams(Map<String, String> params, boolean decode) {
308-
Map<String, Object> lowercaseParams = new HashMap<String, Object>();
309-
for (String key : params.keySet()) {
310-
lowercaseParams.put(key.toLowerCase(), params.get(key));
311-
}
312-
return lowercaseParams;
313-
}
314-
315195
// FIXME: move this to a utils method so that maps can be unpacked and integer/long values can be appropriately cast
316196
@SuppressWarnings({"unchecked", "rawtypes"})
317197
public Map<String, Object> unpackParams(Map<String, String> params) {
@@ -584,7 +464,7 @@ public Long getAccountId(String accountName, Long domainId, Long projectId) {
584464
}
585465

586466
Domain domain = _domainService.getDomain(domainId);
587-
if (domain == null || domain.getType() == Domain.Type.Project) {
467+
if (domain == null) {
588468
throw new InvalidParameterValueException("Unable to find domain by id=" + domainId);
589469
}
590470

api/src/com/cloud/api/commands/AddAccountToProjectCmd.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.cloud.api.response.SuccessResponse;
2929
import com.cloud.exception.InvalidParameterValueException;
3030
import com.cloud.projects.Project;
31-
import com.cloud.user.Account;
3231
import com.cloud.user.UserContext;
3332

3433
@Implementation(description="Adds acoount to a project", responseObject=SuccessResponse.class)

api/src/com/cloud/api/commands/AddVpnUserCmd.java

Lines changed: 19 additions & 35 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.ApiConstants;
2324
import com.cloud.api.BaseAsyncCreateCmd;
2425
import com.cloud.api.BaseCmd;
2526
import com.cloud.api.Implementation;
@@ -41,16 +42,19 @@ public class AddVpnUserCmd extends BaseAsyncCreateCmd {
4142
/////////////////////////////////////////////////////
4243
//////////////// API parameters /////////////////////
4344
/////////////////////////////////////////////////////
44-
@Parameter(name="username", type=CommandType.STRING, required=true, description="username for the vpn user")
45+
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="username for the vpn user")
4546
private String userName;
4647

47-
@Parameter(name="password", type=CommandType.STRING, required=true, description="password for the username")
48+
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required=true, description="password for the username")
4849
private String password;
4950

50-
@Parameter(name="account", type=CommandType.STRING, description="an optional account for the vpn user. Must be used with domainId.")
51+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="an optional account for the vpn user. Must be used with domainId.")
5152
private String accountName;
53+
54+
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="add vpn user to the specific project")
55+
private Long projectId;
5256

53-
@Parameter(name="domainid", type=CommandType.LONG, description="an optional domainId for the vpn user. If the account parameter is used, domainId must also be used.")
57+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="an optional domainId for the vpn user. If the account parameter is used, domainId must also be used.")
5458
private Long domainId;
5559

5660
/////////////////////////////////////////////////////
@@ -69,17 +73,13 @@ public Long getDomainId() {
6973
public String getUserName() {
7074
return userName;
7175
}
72-
73-
public void setUserName(String userName) {
74-
this.userName = userName;
75-
}
76-
76+
7777
public String getPassword() {
7878
return password;
7979
}
80-
81-
public void setPassword(String password) {
82-
this.password = password;
80+
81+
public Long getProjectId() {
82+
return projectId;
8383
}
8484

8585
/////////////////////////////////////////////////////
@@ -93,30 +93,19 @@ public String getCommandName() {
9393

9494
@Override
9595
public long getEntityOwnerId() {
96-
Account account = UserContext.current().getCaller();
97-
if ((account == null) || isAdmin(account.getType())) {
98-
if ((domainId != null) && (accountName != null)) {
99-
Account userAccount = _responseGenerator.findAccountByNameDomain(accountName, domainId);
100-
if (userAccount != null) {
101-
return userAccount.getId();
102-
}
103-
}
96+
Long accountId = getAccountId(accountName, domainId, projectId);
97+
if (accountId == null) {
98+
return UserContext.current().getCaller().getId();
10499
}
105-
106-
if (account != null) {
107-
return account.getId();
108-
}
109-
110-
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
100+
101+
return accountId;
111102
}
112103

113104
@Override
114105
public String getEventDescription() {
115106
return "Add Remote Access VPN user for account " + getEntityOwnerId() + " username= " + getUserName();
116107
}
117108

118-
119-
120109
@Override
121110
public String getEventType() {
122111
return EventTypes.EVENT_VPN_USER_ADD;
@@ -145,13 +134,8 @@ public void execute(){
145134

146135
@Override
147136
public void create() {
148-
Account owner = null;
149-
if (accountName != null) {
150-
owner = _responseGenerator.findAccountByNameDomain(accountName, domainId);
151-
} else {
152-
owner = UserContext.current().getCaller();
153-
}
154-
137+
Account owner = _accountService.getAccount(getEntityOwnerId());
138+
155139
VpnUser vpnUser = _ravService.addVpnUser(owner.getId(), userName, password);
156140
if (vpnUser == null) {
157141
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to add vpn user");

api/src/com/cloud/api/commands/AssociateIPAddrCmd.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.cloud.network.IpAddress;
4343
import com.cloud.network.Network;
4444
import com.cloud.network.Networks.TrafficType;
45-
import com.cloud.user.Account;
4645
import com.cloud.user.UserContext;
4746

4847
@Implementation(description="Acquires and associates a public IP to an account.", responseObject=IPAddressResponse.class)
@@ -65,7 +64,9 @@ public class AssociateIPAddrCmd extends BaseAsyncCreateCmd {
6564

6665
@Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.LONG, description="The network this ip address should be associated to.")
6766
private Long networkId;
68-
67+
68+
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="Deploy vm for the project")
69+
private Long projectId;
6970

7071
/////////////////////////////////////////////////////
7172
/////////////////// Accessors ///////////////////////
@@ -96,7 +97,7 @@ public Long getNetworkId() {
9697

9798
DataCenter zone = _configService.getZone(getZoneId());
9899
if (zone.getNetworkType() == NetworkType.Advanced) {
99-
List<? extends Network> networks = _networkService.getVirtualNetworksOwnedByAccountInZone(getAccountName(), getDomainId(), getZoneId());
100+
List<? extends Network> networks = _networkService.getVirtualNetworksOwnedByAccountInZone(getZoneId(), _accountService.getAccount(getEntityOwnerId()));
100101
if (networks.size() == 0) {
101102
String domain = _domainService.getDomain(getDomainId()).getName();
102103
throw new InvalidParameterValueException("Account name=" + getAccountName() + " domain=" + domain + " doesn't have virtual networks in zone=" + zone.getName());
@@ -116,8 +117,12 @@ public Long getNetworkId() {
116117

117118
@Override
118119
public long getEntityOwnerId() {
119-
Account caller = UserContext.current().getCaller();
120-
return _accountService.finalizeOwner(caller, accountName, domainId).getAccountId();
120+
Long accountId = getAccountId(accountName, domainId, projectId);
121+
if (accountId == null) {
122+
return UserContext.current().getCaller().getId();
123+
}
124+
125+
return accountId;
121126
}
122127

123128
@Override

api/src/com/cloud/api/commands/AttachIsoCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public String getEventDescription() {
9393
@Override
9494
public void execute(){
9595
UserContext.current().setEventDetails("Vm Id: " +getVirtualMachineId()+ " ISO Id: "+getId());
96-
boolean result = _templateService.attachIso(this);
96+
boolean result = _templateService.attachIso(id, virtualMachineId);
9797
if (result) {
9898
UserVm userVm = _responseGenerator.findUserVmById(virtualMachineId);
9999
if (userVm != null) {

0 commit comments

Comments
 (0)