Skip to content

Commit 33a0dec

Browse files
committed
CLOUDSTACK-6221:
Publish first class objects involved in an operation (for now vm uuid) on the event bus . Example - during attach/detachIso along with iso id, vm id should be available as well.
1 parent 466825a commit 33a0dec

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

api/src/com/cloud/event/EventTypes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ public class EventTypes {
483483

484484
// TODO: need a way to force author adding event types to declare the entity details as well, with out braking
485485
// current ActionEvent annotation semantics
486+
// TODO #2 - The map should be from event type to class.
486487

487488
entityEventDetails = new HashMap<String, String>();
488489

server/src/com/cloud/api/ApiDispatcher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import javax.inject.Inject;
2323

2424
import com.cloud.event.EventTypes;
25+
import com.cloud.utils.ReflectUtil;
26+
import com.cloud.vm.VirtualMachine;
2527
import org.apache.cloudstack.api.ApiConstants;
2628
import org.apache.cloudstack.api.BaseAsyncCmd;
2729
import org.apache.cloudstack.api.BaseAsyncCreateCmd;
@@ -83,13 +85,18 @@ public void dispatch(final BaseCmd cmd, final Map<String, String> params, final
8385

8486
final BaseAsyncCmd asyncCmd = (BaseAsyncCmd)cmd;
8587
final String startEventId = params.get(ApiConstants.CTX_START_EVENT_ID);
86-
String uuid = params.get("uuid");
88+
String uuid = params.get(ApiConstants.UUID);
8789
ctx.setStartEventId(Long.valueOf(startEventId));
8890

8991
// Fow now use the key from EventTypes.java rather than getInstanceType bcz the later doesn't refer to the interfaces
92+
// Add the resource id in the call context, also add some other first class object ids (for now vm) if available.
93+
// TODO - this should be done for all the uuids passed in the cmd - so should be moved where uuid to id conversion happens.
9094
if(EventTypes.getEntityForEvent(asyncCmd.getEventType()) != null){
9195
ctx.putContextParameter(EventTypes.getEntityForEvent(asyncCmd.getEventType()), uuid);
9296
}
97+
if(params.get(ApiConstants.VIRTUAL_MACHINE_ID) != null){
98+
ctx.putContextParameter(ReflectUtil.getEntityName(VirtualMachine.class), params.get(ApiConstants.VIRTUAL_MACHINE_ID));
99+
}
93100

94101
// Synchronise job on the object if needed
95102
if (asyncCmd.getJob() != null && asyncCmd.getSyncObjId() != null && asyncCmd.getSyncObjType() != null) {

server/src/com/cloud/api/ApiServer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import javax.servlet.http.HttpSession;
5555

5656
import com.cloud.event.EventTypes;
57+
import com.cloud.utils.ReflectUtil;
58+
import com.cloud.vm.VirtualMachine;
5759
import org.apache.cloudstack.acl.APIChecker;
5860
import org.apache.cloudstack.api.APICommand;
5961
import org.apache.cloudstack.api.ApiConstants;
@@ -503,6 +505,7 @@ private String queueCommand(final BaseCmd cmdObj, final Map<String, String> para
503505
final CallContext ctx = CallContext.current();
504506
final Long callerUserId = ctx.getCallingUserId();
505507
final Account caller = ctx.getCallingAccount();
508+
String vmUUID = params.get(ApiConstants.VIRTUAL_MACHINE_ID);
506509

507510
// Queue command based on Cmd super class:
508511
// BaseCmd: cmd is dispatched to ApiDispatcher, executed, serialized and returned.
@@ -519,7 +522,7 @@ private String queueCommand(final BaseCmd cmdObj, final Map<String, String> para
519522
params.put("id", objectId.toString());
520523
} else {
521524
// Extract the uuid before params are processed and id reflects internal db id
522-
objectUuid = params.get("id");
525+
objectUuid = params.get(ApiConstants.ID);
523526
dispatchChainFactory.getStandardDispatchChain().dispatch(new DispatchTask(cmdObj, params));
524527
}
525528

@@ -538,9 +541,15 @@ private String queueCommand(final BaseCmd cmdObj, final Map<String, String> para
538541
long startEventId = ctx.getStartEventId();
539542
asyncCmd.setStartEventId(startEventId);
540543

544+
// Add the resource id in the call context, also add some other first class object ids (for now vm) if available.
545+
// TODO - this should be done for all the uuids passed in the cmd - so should be moved where uuid to id conversion happens.
541546
if(EventTypes.getEntityForEvent(asyncCmd.getEventType()) != null){
542547
ctx.putContextParameter(EventTypes.getEntityForEvent(asyncCmd.getEventType()), objectUuid);
543548
}
549+
if(vmUUID != null){
550+
ctx.putContextParameter(ReflectUtil.getEntityName(VirtualMachine.class), vmUUID);
551+
}
552+
544553
// save the scheduled event
545554
final Long eventId =
546555
ActionEventUtils.onScheduledActionEvent((callerUserId == null) ? User.UID_SYSTEM : callerUserId, asyncCmd.getEntityOwnerId(), asyncCmd.getEventType(),

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import javax.annotation.PostConstruct;
2626
import javax.inject.Inject;
2727

28+
import com.cloud.utils.ReflectUtil;
29+
import com.cloud.vm.VirtualMachine;
2830
import org.apache.log4j.Logger;
2931
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3032

@@ -186,10 +188,12 @@ private static void publishOnEventBus(long userId, long accountId, String eventC
186188
String entityType = null;
187189
String entityUuid = null;
188190
CallContext context = CallContext.current();
191+
String vmEntityName = ReflectUtil.getEntityName(VirtualMachine.class);
192+
String vmuuid = (String) context.getContextParameter(vmEntityName);
189193
Class entityKey = getEntityKey(eventType);
190194
if (entityKey != null)
191195
{
192-
//FIXME - Remove this
196+
//FIXME - Remove this since it should be covered by the else if condition below.
193197
entityUuid = (String)context.getContextParameter(entityKey);
194198
if (entityUuid != null)
195199
entityType = entityKey.getName();
@@ -220,6 +224,8 @@ private static void publishOnEventBus(long userId, long accountId, String eventC
220224
eventDescription.put("status", state.toString());
221225
eventDescription.put("entity", entityType);
222226
eventDescription.put("entityuuid", entityUuid);
227+
//Put all the first class entities that are touched during the action. For now atleast put in the vmid.
228+
eventDescription.put(vmEntityName, vmuuid);
223229
eventDescription.put("description", description);
224230

225231
String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());

utils/src/com/cloud/utils/ReflectUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,17 @@ private static List<String> flattenProperties(final Object target, final Class<?
189189

190190
}
191191

192+
public static String getEntityName(Class clz){
193+
if(clz == null)
194+
return null;
195+
196+
String entityName = clz.getName();
197+
int index = entityName.lastIndexOf(".");
198+
if (index != -1) {
199+
return entityName.substring(index + 1);
200+
}else{
201+
return entityName;
202+
}
203+
}
204+
192205
}

0 commit comments

Comments
 (0)