Skip to content

Commit 674129c

Browse files
Update project account for all the events with project account owner, except for create project event (#9572)
1 parent 6c04923 commit 674129c

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/account/AddUserToProjectCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public String getEventType() {
103103

104104
@Override
105105
public String getEventDescription() {
106-
return "Adding user "+getUsername()+" to Project "+getProjectId();
106+
return "Adding user " + getUsername() + " to project: " + getProjectId();
107107
}
108108

109109
/////////////////////////////////////////////////////

api/src/main/java/org/apache/cloudstack/api/command/user/account/DeleteUserFromProjectCmd.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public String getEventDescription() {
8383
return "Removing user " + userId + " from project: " + projectId;
8484
}
8585

86-
8786
@Override
8887
public long getEntityOwnerId() {
8988
Project project = _projectService.getProject(projectId);

server/src/main/java/com/cloud/event/ActionEventInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ public void interceptComplete(Method method, Object target, Object event) {
8888
for (ActionEvent actionEvent : getActionEvents(method)) {
8989
CallContext ctx = CallContext.current();
9090
long userId = ctx.getCallingUserId();
91-
long accountId = ctx.getProject() != null ? ctx.getProject().getProjectAccountId() : ctx.getCallingAccountId(); //This should be the entity owner id rather than the Calling User Account Id.
9291
long startEventId = ctx.getStartEventId();
9392
String eventDescription = getEventDescription(actionEvent, ctx);
9493
Long eventResourceId = getEventResourceId(actionEvent, ctx);
9594
String eventResourceType = getEventResourceType(actionEvent, ctx);
9695
String eventType = getEventType(actionEvent, ctx);
9796
boolean isEventDisplayEnabled = ctx.isEventDisplayEnabled();
97+
long accountId = ActionEventUtils.getOwnerAccountId(ctx, eventType, ctx.getCallingAccountId());
9898

9999
if (eventType.equals(""))
100100
return;
@@ -118,13 +118,13 @@ public void interceptException(Method method, Object target, Object event) {
118118
for (ActionEvent actionEvent : getActionEvents(method)) {
119119
CallContext ctx = CallContext.current();
120120
long userId = ctx.getCallingUserId();
121-
long accountId = ctx.getCallingAccountId();
122121
long startEventId = ctx.getStartEventId();
123122
String eventDescription = getEventDescription(actionEvent, ctx);
124123
Long eventResourceId = getEventResourceId(actionEvent, ctx);
125124
String eventResourceType = getEventResourceType(actionEvent, ctx);
126125
String eventType = getEventType(actionEvent, ctx);
127126
boolean isEventDisplayEnabled = ctx.isEventDisplayEnabled();
127+
long accountId = ActionEventUtils.getOwnerAccountId(ctx, eventType, ctx.getCallingAccountId());
128128

129129
if (eventType.equals(""))
130130
return;

server/src/main/java/com/cloud/event/ActionEventUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.text.SimpleDateFormat;
2323
import java.util.Date;
2424
import java.util.HashMap;
25+
import java.util.List;
2526
import java.util.Map;
2627

2728
import javax.annotation.PostConstruct;
@@ -110,6 +111,8 @@ public static Long onActionEvent(Long userId, Long accountId, Long domainId, Str
110111
*/
111112
public static Long onScheduledActionEvent(Long userId, Long accountId, String type, String description, Long resourceId, String resourceType, boolean eventDisplayEnabled, long startEventId) {
112113
Ternary<Long, String, String> resourceDetails = getResourceDetails(resourceId, resourceType, type);
114+
CallContext ctx = CallContext.current();
115+
accountId = getOwnerAccountId(ctx, type, accountId);
113116
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type, com.cloud.event.Event.State.Scheduled, description, resourceDetails.second(), resourceDetails.third());
114117
Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Scheduled, eventDisplayEnabled, description, resourceDetails.first(), resourceDetails.third(), startEventId);
115118
return event.getId();
@@ -123,7 +126,7 @@ public static void startNestedActionEvent(String eventType, String eventDescript
123126
public static void onStartedActionEventFromContext(String eventType, String eventDescription, Long resourceId, String resourceType, boolean eventDisplayEnabled) {
124127
CallContext ctx = CallContext.current();
125128
long userId = ctx.getCallingUserId();
126-
long accountId = ctx.getProject() != null ? ctx.getProject().getProjectAccountId() : ctx.getCallingAccountId(); //This should be the entity owner id rather than the Calling User Account Id.
129+
long accountId = getOwnerAccountId(ctx, eventType, ctx.getCallingAccountId());
127130
long startEventId = ctx.getStartEventId();
128131

129132
if (!eventType.equals(""))
@@ -393,7 +396,11 @@ private static void populateFirstClassEntities(Map<String, String> eventDescript
393396
s_logger.trace("Caught exception while populating first class entities for event bus, moving on");
394397
}
395398
}
396-
397399
}
398400

401+
public static long getOwnerAccountId(CallContext ctx, String eventType, long callingAccountId) {
402+
List<String> mainProjectEvents = List.of(EventTypes.EVENT_PROJECT_CREATE, EventTypes.EVENT_PROJECT_UPDATE, EventTypes.EVENT_PROJECT_DELETE);
403+
long accountId = ctx.getProject() != null && !mainProjectEvents.stream().anyMatch(eventType::equalsIgnoreCase) ? ctx.getProject().getProjectAccountId() : callingAccountId; //This should be the entity owner id rather than the Calling User Account Id.
404+
return accountId;
405+
}
399406
}

server/src/main/java/com/cloud/projects/ProjectManagerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,16 @@ public Project doInTransaction(TransactionStatus status) {
279279
assignAccountToProject(project, ownerFinal.getId(), ProjectAccount.Role.Admin,
280280
Optional.ofNullable(finalUser).map(User::getId).orElse(null), null);
281281

282-
if (project != null) {
283-
CallContext.current().setEventDetails("Project id=" + project.getId());
284-
CallContext.current().putContextParameter(Project.class, project.getUuid());
285-
}
282+
if (project != null) {
283+
CallContext.current().setEventDetails("Project id=" + project.getId());
284+
CallContext.current().putContextParameter(Project.class, project.getUuid());
285+
}
286286

287-
//Increment resource count
287+
//Increment resource count
288288
_resourceLimitMgr.incrementResourceCount(ownerFinal.getId(), ResourceType.project);
289289

290-
return project;
291-
}
290+
return project;
291+
}
292292
});
293293

294294
messageBus.publish(_name, ProjectManager.MESSAGE_CREATE_TUNGSTEN_PROJECT_EVENT, PublishScope.LOCAL, project);
@@ -1275,7 +1275,7 @@ public List<Long> listPermittedProjectAccounts(long accountId) {
12751275
}
12761276

12771277
@Override
1278-
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACTIVATE, eventDescription = "activating project")
1278+
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACTIVATE, eventDescription = "activating project", async = true)
12791279
@DB
12801280
public Project activateProject(final long projectId) {
12811281
Account caller = CallContext.current().getCallingAccount();

0 commit comments

Comments
 (0)