Skip to content
Draft
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
42 changes: 24 additions & 18 deletions api/src/main/java/com/cloud/configuration/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,45 @@ public interface Resource {
String UNLIMITED = "Unlimited";

enum ResourceType { // All storage type resources are allocated_storage and not the physical storage.
user_vm("user_vm", 0),
public_ip("public_ip", 1),
volume("volume", 2),
snapshot("snapshot", 3),
template("template", 4),
project("project", 5),
network("network", 6),
vpc("vpc", 7),
cpu("cpu", 8),
memory("memory", 9),
primary_storage("primary_storage", 10),
secondary_storage("secondary_storage", 11),
backup("backup", 12),
backup_storage("backup_storage", 13),
bucket("bucket", 14),
object_storage("object_storage", 15),
gpu("gpu", 16);
user_vm("user_vm", "Instance", 0),
public_ip("public_ip", "Public IP", 1),
volume("volume", "Volume", 2),
snapshot("snapshot", "Snapshot", 3),
template("template", "Template", 4),
project("project", "Project", 5),
network("network", "Network", 6),
vpc("vpc", "VPC", 7),
cpu("cpu", "CPU", 8),
memory("memory", "Memory", 9),
primary_storage("primary_storage", "Primary Storage", 10),
secondary_storage("secondary_storage", "Secondary Storage", 11),
backup("backup", "Backup", 12),
backup_storage("backup_storage", "Backup Storage", 13),
bucket("bucket", "Bucket", 14),
object_storage("object_storage", "Object Storage", 15),
gpu("gpu", "GPU", 16);

private String name;
private String displayName;
private int ordinal;
public static final long bytesToKiB = 1024;
public static final long bytesToMiB = bytesToKiB * 1024;
public static final long bytesToGiB = bytesToMiB * 1024;

ResourceType(String name, int ordinal) {
ResourceType(String name, String displayName, int ordinal) {
this.name = name;
this.displayName = displayName;
this.ordinal = ordinal;
}

public String getName() {
return name;
}

public String getDisplayName() {
return displayName;
}

public int getOrdinal() {
return ordinal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
// under the License.
package com.cloud.exception;

import java.util.Map;

import org.apache.cloudstack.context.ErrorMessageResolver;

import com.cloud.utils.exception.CloudRuntimeException;

public class InvalidParameterValueException extends CloudRuntimeException {
Expand All @@ -26,4 +30,8 @@ public InvalidParameterValueException(String message) {
super(message);
}

public InvalidParameterValueException(String key, Map<String, Object> metadata) {
super(ErrorMessageResolver.getMessage(key, metadata), key, metadata);
}

}
6 changes: 4 additions & 2 deletions api/src/main/java/com/cloud/user/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import java.util.List;
import java.util.Map;

import com.cloud.utils.Pair;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.command.admin.account.CreateAccountCmd;
import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.RegisterUserKeyCmd;
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;

import com.cloud.dc.DataCenter;
import com.cloud.domain.Domain;
Expand All @@ -35,7 +35,7 @@
import com.cloud.offering.DiskOffering;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.ServiceOffering;
import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;
import com.cloud.utils.Pair;

public interface AccountService {

Expand Down Expand Up @@ -85,6 +85,8 @@ User createUser(String userName, String password, String firstName, String lastN

boolean isRootAdmin(Long accountId);

boolean isRootAdmin(Account account);

boolean isDomainAdmin(Long accountId);

boolean isResourceDomainAdmin(Long accountId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.cloudstack.api;

import java.util.ArrayList;
import java.util.Map;

import org.apache.cloudstack.context.ErrorMessageResolver;

import com.cloud.exception.CloudException;
import com.cloud.utils.exception.CSExceptionErrorCode;
Expand All @@ -34,6 +37,14 @@ public ServerApiException() {
setCSErrorCode(CSExceptionErrorCode.getCSErrCode(ServerApiException.class.getName()));
}

public ServerApiException(ApiErrorCode errorCode, String messageKey, Map<String, Object> errorMetadata) {
_errorCode = errorCode;
this.messageKey = messageKey;
this.metadata = errorMetadata;
_description = ErrorMessageResolver.getMessage(messageKey, errorMetadata);
setCSErrorCode(CSExceptionErrorCode.getCSErrCode(ServerApiException.class.getName()));
}

public ServerApiException(ApiErrorCode errorCode, String description) {
_errorCode = errorCode;
_description = description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ private Long getNetworkIdFomIpMap(HashMap<String, String> ips) {
try {
networkId = Long.parseLong(networkid);
} catch (NumberFormatException e) {
throw new InvalidParameterValueException("Unable to translate and find entity with networkId: " + networkid);
throw new InvalidParameterValueException("vm.deploy.network.not.found.ip.map",
Map.of("networkId", networkid));
}
}
return networkId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.
package org.apache.cloudstack.api.command.user.vm;

import java.util.Collections;
import java.util.Objects;
import java.util.stream.Stream;

Expand Down Expand Up @@ -157,7 +158,18 @@ public void create() throws ResourceAllocationException {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (ResourceAllocationException ex) {
logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
handleCreateResourceAllocationException(ex);
}
}

protected void handleCreateResourceAllocationException(ResourceAllocationException ex) {
if (ex.getMessage() != null && ex.getMessage().startsWith("Maximum amount")) {
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR,
"vm.deploy.resourcelimit.exceeded.account", Collections.emptyMap());
} else if (ex.getMessage() != null && ex.getMessage().startsWith("Maximum domain resource limits")) {
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR,
"vm.deploy.resourcelimit.exceeded.domain", Collections.emptyMap());
}
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.SerializedName;
import java.util.Map;

import org.apache.cloudstack.api.BaseResponse;

import com.cloud.serializer.Param;
import com.cloud.utils.exception.ExceptionProxyObject;
import com.google.gson.annotations.SerializedName;

public class ExceptionResponse extends BaseResponse {

Expand All @@ -44,6 +44,14 @@ public class ExceptionResponse extends BaseResponse {
@Param(description = "The text associated with this error")
private String errorText = "Command failed due to Internal Server Error";

@SerializedName("errortextkey")
@Param(description = "the key for the text associated with this error")
private String errorTextKey;

@SerializedName("errormetadata")
@Param(description = "the metadata associated with this error")
private Map<String, String> errorMetadata;

public ExceptionResponse() {
idList = new ArrayList<ExceptionProxyObject>();
}
Expand All @@ -64,6 +72,22 @@ public void setErrorText(String errorText) {
this.errorText = errorText;
}

public String getErrorTextKey() {
return errorTextKey;
}

public void setErrorTextKey(String errorTextKey) {
this.errorTextKey = errorTextKey;
}

public Map<String, String> getErrorMetadata() {
return errorMetadata;
}

public void setErrorMetadata(Map<String, String> errorMetadata) {
this.errorMetadata = errorMetadata;
}

public void addProxyObject(ExceptionProxyObject id) {
idList.add(id);
return;
Expand Down
40 changes: 38 additions & 2 deletions api/src/main/java/org/apache/cloudstack/context/CallContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@

import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.managed.threadlocal.ManagedThreadLocal;
import org.apache.logging.log4j.Logger;
import org.apache.commons.collections.MapUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import com.cloud.exception.CloudAuthenticationException;
import com.cloud.projects.Project;
import com.cloud.user.Account;
import com.cloud.user.AccountService;
import com.cloud.user.User;
import com.cloud.utils.UuidUtils;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.logging.log4j.ThreadContext;

/**
* CallContext records information about the environment the call is made. This
Expand All @@ -53,6 +57,7 @@ protected Stack<CallContext> initialValue() {
private String contextId;
private Account account;
private long accountId;
private Boolean isAccountRootAdmin = null;
private long startEventId = 0;
private String eventDescription;
private String eventDetails;
Expand All @@ -63,6 +68,7 @@ protected Stack<CallContext> initialValue() {
private User user;
private long userId;
private final Map<Object, Object> context = new HashMap<Object, Object>();
private final Map<String, Object> errorContext = new HashMap<String, Object>();
private Project project;
private String apiName;

Expand Down Expand Up @@ -134,6 +140,21 @@ public Account getCallingAccount() {
return account;
}

public boolean isCallingAccountRootAdmin() {
if (isAccountRootAdmin == null) {
AccountService accountService;
try {
accountService = ComponentContext.getDelegateComponentOfType(AccountService.class);
} catch (NoSuchBeanDefinitionException e) {
LOGGER.warn("Falling back to account type check for isRootAdmin for account ID: {} as no AccountService bean found: {}", accountId, e.getMessage());
Account caller = getCallingAccount();
return caller != null && caller.getType() == Account.Type.ADMIN;
}
isAccountRootAdmin = accountService.isRootAdmin(getCallingAccount());
}
return Boolean.TRUE.equals(isAccountRootAdmin);
}

public static CallContext current() {
CallContext context = s_currentContext.get();

Expand Down Expand Up @@ -405,6 +426,21 @@ public void putContextParameters(Map<Object, Object> details){
}
}

public Map<String, Object> getErrorContextParameters() {
return errorContext;
}

public void putErrorContextParameter(String key, Object value) {
errorContext.put(key, value);
}

public void putErrorContextParameters(Map<String, Object> details) {
if (MapUtils.isEmpty(details)) {
return;
}
errorContext.putAll(details);
}

public static void setActionEventInfo(String eventType, String description) {
CallContext context = CallContext.current();
if (context != null) {
Expand Down
Loading
Loading