diff --git a/engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java b/engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java index 94fbb7a80af8..331bad8b5acd 100644 --- a/engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java +++ b/engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java @@ -26,6 +26,8 @@ import javax.inject.Inject; import com.cloud.network.Network; + +import org.apache.cloudstack.context.CallContext; import org.apache.commons.collections.MapUtils; import org.springframework.beans.factory.NoSuchBeanDefinitionException; @@ -214,7 +216,9 @@ private static void publishUsageEvent(String usageEventType, Long accountId, Lon return; // no provider is configured to provide events distributor, so just return } - Account account = s_accountDao.findById(accountId); + Account account = (accountId == CallContext.current().getCallingAccountId()) + ? CallContext.current().getCallingAccount() + : s_accountDao.findById(accountId); DataCenterVO dc = s_dcDao.findById(zoneId); // if account has been deleted, this might be called during cleanup of resources and results in null pointer diff --git a/server/src/main/java/com/cloud/event/ActionEventUtils.java b/server/src/main/java/com/cloud/event/ActionEventUtils.java index ae77446a8561..c7206e791599 100644 --- a/server/src/main/java/com/cloud/event/ActionEventUtils.java +++ b/server/src/main/java/com/cloud/event/ActionEventUtils.java @@ -227,7 +227,9 @@ private static void publishOnEventBus(Event eventRecord, long userId, long accou Map eventDescription = new HashMap(); Project project = s_projectDao.findByProjectAccountId(accountId); - Account account = s_accountDao.findById(accountId); + Account account = (accountId == CallContext.current().getCallingAccountId()) + ? CallContext.current().getCallingAccount() + : s_accountDao.findById(accountId); User user = s_userDao.findById(userId); // if account has been deleted, this might be called during cleanup of resources and results in null pointer if (account == null) diff --git a/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java b/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java index f655c0d74c0b..3540f2aca4a9 100644 --- a/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java +++ b/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java @@ -175,8 +175,6 @@ public EventVO answer(InvocationOnMock invocation) throws Throwable { user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN); CallContext.register(user, account); - - Mockito.when(accountDao.findById(ACCOUNT_ID)).thenReturn(account); } /** diff --git a/server/src/test/java/com/cloud/event/ActionEventUtilsTest.java b/server/src/test/java/com/cloud/event/ActionEventUtilsTest.java index aba8acf59c26..e63e55fed3c9 100644 --- a/server/src/test/java/com/cloud/event/ActionEventUtilsTest.java +++ b/server/src/test/java/com/cloud/event/ActionEventUtilsTest.java @@ -177,9 +177,8 @@ public EventVO answer(InvocationOnMock invocation) throws Throwable { account.setId(ACCOUNT_ID); user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN); - - Mockito.when(accountDao.findById(ACCOUNT_ID)).thenReturn(account); Mockito.when(userDao.findById(USER_ID)).thenReturn(user); + CallContext.register(user, account); } /** @@ -204,12 +203,11 @@ public void teardown() { utils.init(); componentContextMocked.close(); + CallContext.unregister(); } @Test public void testPopulateFirstClassEntities() { - CallContext.register(user, account); - //Inject some entity UUIDs into the call context String instanceUuid = UUID.randomUUID().toString(); String ipUuid = UUID.randomUUID().toString(); @@ -231,8 +229,6 @@ public void testPopulateFirstClassEntities() { Assert.assertTrue(json.has("IpAddress")); Assert.assertEquals(json.get("VirtualMachine").getAsString(), instanceUuid); Assert.assertEquals(json.get("IpAddress").getAsString(), ipUuid); - - CallContext.unregister(); } private void checkEventResourceAndUnregisterContext(Long resourceId, String resourceUuid, String resourceType) { @@ -248,14 +244,10 @@ private void checkEventResourceAndUnregisterContext(Long resourceId, String reso EventVO eventVO = persistedEvents.get(0); Assert.assertEquals(eventVO.getResourceType(), resourceType); Assert.assertEquals(eventVO.getResourceId(), resourceId); - - CallContext.unregister(); } @Test public void testPublishedEventResource() { - CallContext.register(user, account); - final Long resourceId = 1L; final String resourceType = ApiCommandResourceType.VirtualMachine.toString(); final String resourceUuid = UUID.randomUUID().toString(); @@ -270,8 +262,6 @@ public void testPublishedEventResource() { @Test public void testPublishedEventResourceWithCallContext() { - CallContext.register(user, account); - final Long resourceId = 1L; final String resourceType = ApiCommandResourceType.VirtualMachine.toString(); final String resourceUuid = UUID.randomUUID().toString(); @@ -287,8 +277,6 @@ public void testPublishedEventResourceWithCallContext() { @Test public void testScheduledEvent() { - CallContext.register(user, account); - final Long resourceId = 1L; final String resourceType = ApiCommandResourceType.VirtualMachine.toString(); final String resourceUuid = UUID.randomUUID().toString(); @@ -304,14 +292,10 @@ public void testScheduledEvent() { Assert.assertEquals(persistedEvents.size(), 1); EventVO eventVO = persistedEvents.get(0); Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Scheduled); - - CallContext.unregister(); } @Test public void testCreatedEvent() { - CallContext.register(user, account); - final Long resourceId = 1L; final String resourceType = ApiCommandResourceType.VirtualMachine.toString(); final String resourceUuid = UUID.randomUUID().toString(); @@ -327,14 +311,10 @@ public void testCreatedEvent() { Assert.assertEquals(persistedEvents.size(), 1); EventVO eventVO = persistedEvents.get(0); Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Created); - - CallContext.unregister(); } @Test public void testNestedEvent() { - CallContext.register(user, account); - final Long resourceId = 1L; final String resourceType = ApiCommandResourceType.VirtualMachine.toString(); final String resourceUuid = UUID.randomUUID().toString(); @@ -344,14 +324,10 @@ public void testNestedEvent() { Assert.assertEquals(persistedEvents.size(), 1); EventVO eventVO = persistedEvents.get(0); Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Started); - - CallContext.unregister(); } @Test public void testSnapshotEventResource() { - CallContext.register(user, account); - final Long snapshotResourceId = 100L; final String snapshotResourceType = ApiCommandResourceType.Snapshot.toString(); final String snapshotResourceUuid = UUID.randomUUID().toString(); @@ -366,7 +342,6 @@ public void testSnapshotEventResource() { @Test public void testVmSnapshotEventResource() { - CallContext.register(user, account); final Long vmSnapshotResourceId = 100L; final String vmSnapshotResourceType = ApiCommandResourceType.VmSnapshot.toString();