Skip to content

Commit 5c8d2c9

Browse files
committed
use separate errorcontext
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 2693714 commit 5c8d2c9

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

api/src/main/java/org/apache/cloudstack/context/CallContext.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import java.util.Map;
2121
import java.util.Stack;
2222
import java.util.UUID;
23-
import java.util.stream.Collectors;
2423

2524
import org.apache.cloudstack.api.ApiCommandResourceType;
2625
import org.apache.cloudstack.managed.threadlocal.ManagedThreadLocal;
26+
import org.apache.commons.collections.MapUtils;
2727
import org.apache.logging.log4j.LogManager;
2828
import org.apache.logging.log4j.Logger;
2929
import org.apache.logging.log4j.ThreadContext;
@@ -68,6 +68,7 @@ protected Stack<CallContext> initialValue() {
6868
private User user;
6969
private long userId;
7070
private final Map<Object, Object> context = new HashMap<Object, Object>();
71+
private final Map<String, Object> errorContext = new HashMap<String, Object>();
7172
private Project project;
7273
private String apiName;
7374

@@ -418,19 +419,28 @@ public Map<Object, Object> getContextParameters() {
418419
return context;
419420
}
420421

421-
public Map<String, Object> getContextStringKeyParameters() {
422-
return context.entrySet().stream()
423-
.filter(entry -> entry.getKey() instanceof String)
424-
.collect(Collectors.toMap(entry -> (String) entry.getKey(), Map.Entry::getValue));
425-
}
426-
427422
public void putContextParameters(Map<Object, Object> details){
428423
if (details == null) return;
429424
for(Map.Entry<Object,Object>entry : details.entrySet()){
430425
putContextParameter(entry.getKey(), entry.getValue());
431426
}
432427
}
433428

429+
public Map<String, Object> getErrorContextParameters() {
430+
return errorContext;
431+
}
432+
433+
public void putErrorContextParameter(String key, Object value) {
434+
errorContext.put(key, value);
435+
}
436+
437+
public void putErrorContextParameters(Map<String, Object> details) {
438+
if (MapUtils.isEmpty(details)) {
439+
return;
440+
}
441+
errorContext.putAll(details);
442+
}
443+
434444
public static void setActionEventInfo(String eventType, String description) {
435445
CallContext context = CallContext.current();
436446
if (context != null) {

api/src/main/java/org/apache/cloudstack/context/ErrorMessageResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected static Map<String, Object> getCombinedMetadataFromErrorTemplate(String
8585
if (variableNames.isEmpty()) {
8686
return metadata;
8787
}
88-
Map<String, Object> contextMetadata = CallContext.current().getContextStringKeyParameters();
88+
Map<String, Object> contextMetadata = CallContext.current().getErrorContextParameters();
8989
if (MapUtils.isEmpty(contextMetadata)) {
9090
return metadata;
9191
}

api/src/test/java/org/apache/cloudstack/context/ErrorMessageResolverTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void getCombinedMetadataFromErrorTemplate_shouldReturnMetadataWhenNoVaria
135135
public void getCombinedMetadataFromErrorTemplate_shouldReturnEmptyMapWhenContextMetadataIsEmpty() {
136136
String template = "This template has {{var1}}.";
137137
Map<String, Object> metadata = Map.of();
138-
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of());
138+
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of());
139139
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
140140
Assert.assertTrue("Result should be an empty map", result.isEmpty());
141141
}
@@ -144,7 +144,7 @@ public void getCombinedMetadataFromErrorTemplate_shouldReturnEmptyMapWhenContext
144144
public void getCombinedMetadataFromErrorTemplate_shouldCombineContextAndProvidedMetadata() {
145145
String template = "This template has {{var1}} and {{var2}}.";
146146
Map<String, Object> metadata = Map.of("key1", "value1");
147-
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("var1", "valueVar1", "var2", "valueVar2"));
147+
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("var1", "valueVar1", "var2", "valueVar2"));
148148
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
149149
Assert.assertEquals("Result should contain combined metadata", 3, result.size());
150150
Assert.assertEquals("Result should contain context metadata for var1", "valueVar1", result.get("var1"));
@@ -156,7 +156,7 @@ public void getCombinedMetadataFromErrorTemplate_shouldCombineContextAndProvided
156156
public void getCombinedMetadataFromErrorTemplate_shouldIgnoreVariablesNotInContextMetadata() {
157157
String template = "This template has {{var1}} and {{var2}}.";
158158
Map<String, Object> metadata = Map.of("key1", "value1");
159-
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("var1", "valueVar1"));
159+
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("var1", "valueVar1"));
160160
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
161161
Assert.assertEquals("Result should contain combined metadata", 2, result.size());
162162
Assert.assertEquals("Result should contain context metadata for var1", "valueVar1", result.get("var1"));
@@ -332,7 +332,7 @@ public void getMessage_shouldReturnExpandedMessageWhenTemplateExists() {
332332
String template = "Error occurred: {{field}}";
333333
Map<String, Object> metadata = Map.of("field", "value");
334334
createTempFileWithTemplate(errorKey, template);
335-
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of());
335+
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of());
336336
String result = ErrorMessageResolver.getMessage(errorKey, metadata);
337337
Assert.assertEquals("Error occurred: value", result);
338338
}
@@ -361,7 +361,7 @@ public void getMessage_shouldCombineContextAndProvidedMetadata() {
361361
String template = "Error in {{field1}} and {{field2}}.";
362362
Map<String, Object> metadata = Map.of("field1", "value1");
363363
createTempFileWithTemplate(errorKey, template);
364-
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("field2", "value2"));
364+
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("field2", "value2"));
365365
String result = ErrorMessageResolver.getMessage(errorKey, metadata);
366366
Assert.assertEquals("Error in value1 and value2.", result);
367367
}

server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ protected void checkDomainResourceLimit(final Account account, final Project pro
559559

560560
if (domainResourceLimit != Resource.RESOURCE_UNLIMITED && requestedDomainResourceCount > domainResourceLimit) {
561561
String message = "Maximum" + messageSuffix;
562-
Map<Object, Object> details = new HashMap<>();
562+
Map<String, Object> details = new HashMap<>();
563563
details.put("resourceTypeDisplay", StringUtils.isBlank(tag) ? type.getDisplayName() : type.getDisplayName() + " (tag: " + tag + ")");
564564
details.put("resourceOwner", ObjectUtils.firstNonNull(project, account));
565565
details.put("resourceOwnerDomain", domain);
@@ -568,7 +568,7 @@ protected void checkDomainResourceLimit(final Account account, final Project pro
568568
details.put("resourceAmount", convCurrentDomainResourceCount);
569569
details.put("resourceReserved", convCurrentResourceReservation);
570570
details.put("resourceRequested", convNumResources);
571-
CallContext.current().putContextParameters(details);
571+
CallContext.current().putErrorContextParameters(details);
572572
ResourceAllocationException e = new ResourceAllocationException(message, type);
573573
logger.error(message, e);
574574
throw e;
@@ -608,15 +608,15 @@ protected void checkAccountResourceLimit(final Account account, final Project pr
608608

609609
if (accountResourceLimit != Resource.RESOURCE_UNLIMITED && requestedResourceCount > accountResourceLimit) {
610610
String message = "Maximum" + messageSuffix;
611-
Map<Object, Object> details = new HashMap<>();
611+
Map<String, Object> details = new HashMap<>();
612612
details.put("resourceTypeDisplay", StringUtils.isBlank(tag) ? type.getDisplayName() : type.getDisplayName() + " (tag: " + tag + ")");
613613
details.put("resourceOwner", ObjectUtils.firstNonNull(project, account));
614614
details.put("resourceOwnerType", project == null ? "Account" : "Project");
615615
details.put("resourceLimit", convertedAccountResourceLimit);
616616
details.put("resourceAmount", convertedCurrentResourceCount);
617617
details.put("resourceReserved", convertedCurrentResourceReservation);
618618
details.put("resourceRequested", convertedNumResources);
619-
CallContext.current().putContextParameters(details);
619+
CallContext.current().putErrorContextParameters(details);
620620
ResourceAllocationException e = new ResourceAllocationException(message, type);
621621
logger.error(message, e);
622622
throw e;

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6264,7 +6264,7 @@ public String finalizeUserData(String userData, Long userDataId, VirtualMachineT
62646264
}
62656265

62666266
private void verifyServiceOffering(BaseDeployVMCmd cmd, ServiceOffering serviceOffering) {
6267-
CallContext.current().putContextParameter("serviceOffering", serviceOffering);
6267+
CallContext.current().putErrorContextParameter("serviceOffering", serviceOffering);
62686268
if (ServiceOffering.State.Inactive.equals(serviceOffering.getState())) {
62696269
throw new InvalidParameterValueException("vm.deploy.serviceoffering.inactive",
62706270
Collections.emptyMap());

0 commit comments

Comments
 (0)