Skip to content

Commit 535ab51

Browse files
committed
Merge pull request #849 from karuturi/CLOUDSTACK-8816-take2
Cloudstack-8816 some of the events do not have resource uuidsThe key objects in the context map are sometimes String and sometimes object. This causes missing uuids when an entity put in the context map with key entity.toString is queried with key entity Testing: manually tested by deploying a vm and checked that the created events in rabbitmq now has uuids. events before and after the change are update at https://issues.apache.org/jira/browse/CLOUDSTACK-8816?focusedCommentId=14805239 unittests ``` $ mvn -pl :cloud-api test -Dtest=CallContextTest ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.apache.cloudstack.context.CallContextTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.152 sec - in org.apache.cloudstack.context.CallContextTest Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.445 s [INFO] Finished at: 2015-09-18T14:58:53+05:30 [INFO] Final Memory: 55M/448M [INFO] ------------------------------------------------------------------------ ``` * pr/849: CLOUDSTACK-8816 added missing events CLOUDSTACK-8816: fixed missing resource uuid in delete network cmd CLOUDSTACK-8816: fixed missing resource uuid in destroy vm event Cloudstack-8816: Fixed missing resource uuid in delete snapshot events CLOUDSTACK-8816: some of the events do not have resource uuids CLOUDSTACK-8816: some of the events do not have resource uuids Signed-off-by: Remi Bergsma <[email protected]>
2 parents fb4e6ed + 2c0af1f commit 535ab51

File tree

7 files changed

+122
-10
lines changed

7 files changed

+122
-10
lines changed

api/src/org/apache/cloudstack/api/ApiCommandJobType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ public enum ApiCommandJobType {
5353
IAMPolicy,
5454
IAMGroup,
5555
GuestOs,
56-
GuestOsMapping
56+
GuestOsMapping,
57+
Network
5758
}

api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkCmd.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.network;
1818

19+
import org.apache.cloudstack.api.ApiCommandJobType;
1920
import org.apache.log4j.Logger;
2021

2122
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -105,6 +106,15 @@ public String getEventDescription() {
105106
return "Deleting network: " + id;
106107
}
107108

109+
@Override
110+
public Long getInstanceId() {
111+
return getId();
112+
}
113+
114+
@Override
115+
public ApiCommandJobType getInstanceType() {
116+
return ApiCommandJobType.Network;
117+
}
108118
@Override
109119
public long getEntityOwnerId() {
110120
Network network = _networkService.getNetwork(id);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,19 @@ public void putContextParameter(Object key, Object value) {
8989
context.put(key, value);
9090
}
9191

92+
/**
93+
* @param key any not null key object
94+
* @return the value of the key from context map
95+
* @throws NullPointerException if the specified key is nul
96+
*/
9297
public Object getContextParameter(Object key) {
93-
return context.get(key);
98+
Object value = context.get(key);
99+
//check if the value is present in the toString value of the key
100+
//due to a bug in the way we update the key by serializing and deserializing, it sometimes gets toString value of the key. @see com.cloud.api.ApiAsyncJobDispatcher#runJob
101+
if(value == null ) {
102+
value = context.get(key.toString());
103+
}
104+
return value;
94105
}
95106

96107
public long getCallingUserId() {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cloudstack.context;
21+
22+
import java.util.UUID;
23+
24+
import org.junit.After;
25+
import org.junit.Assert;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.mockito.Mock;
30+
import org.mockito.Mockito;
31+
import org.mockito.runners.MockitoJUnitRunner;
32+
33+
import com.cloud.user.Account;
34+
import com.cloud.user.User;
35+
import com.cloud.utils.db.EntityManager;
36+
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class CallContextTest {
39+
40+
@Mock
41+
EntityManager entityMgr;
42+
43+
@Before
44+
public void setUp() {
45+
CallContext.init(entityMgr);
46+
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
47+
}
48+
49+
@After
50+
public void tearDown() throws Exception {
51+
CallContext.unregisterAll();
52+
}
53+
54+
@Test
55+
public void testGetContextParameter() {
56+
CallContext currentContext = CallContext.current();
57+
58+
Assert.assertEquals("There is nothing in the context. It should return null", null, currentContext.getContextParameter("key"));
59+
Assert.assertTrue("There is nothing in the context. The map should be empty", currentContext.getContextParameters().isEmpty());
60+
61+
UUID objectUUID = UUID.randomUUID();
62+
UUID stringUUID = UUID.randomUUID();
63+
64+
//Case1: when an entry with the object class is present
65+
currentContext.putContextParameter(User.class, objectUUID);
66+
Assert.assertEquals("it should return objectUUID: " + objectUUID, objectUUID, currentContext.getContextParameter(User.class));
67+
Assert.assertEquals("current context map should have exactly one entry", 1, currentContext.getContextParameters().size());
68+
69+
//Case2: when an entry with the object class name as String is present
70+
currentContext.putContextParameter(Account.class.toString(), stringUUID);
71+
//object is put with key as Account.class.toString but get with key as Account.class
72+
Assert.assertEquals("it should return stringUUID: " + stringUUID, stringUUID, currentContext.getContextParameter(Account.class));
73+
Assert.assertEquals("current context map should have exactly two entries", 2, currentContext.getContextParameters().size());
74+
75+
//Case3: when an entry with both object class and object class name as String is present
76+
//put an entry of account class object in the context
77+
currentContext.putContextParameter(Account.class, objectUUID);
78+
//since both object and string a present in the current context, it should return object value
79+
Assert.assertEquals("it should return objectUUID: " + objectUUID, objectUUID, currentContext.getContextParameter(Account.class));
80+
Assert.assertEquals("current context map should have exactly three entries", 3, currentContext.getContextParameters().size());
81+
}
82+
83+
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ void init() {
778778
// ///////////////////////////////////////////////////////////
779779

780780
public static VMInstanceVO findVMInstanceById(long vmId) {
781-
return s_vmDao.findById(vmId);
781+
return s_vmDao.findByIdIncludingRemoved(vmId);
782782
}
783783

784784
public static long getStorageCapacitybyPool(Long poolId, short capacityType) {
@@ -1033,12 +1033,7 @@ public static ServiceOfferingDetailsVO findServiceOfferingDetail(long serviceOff
10331033
}
10341034

10351035
public static Snapshot findSnapshotById(long snapshotId) {
1036-
SnapshotVO snapshot = s_snapshotDao.findById(snapshotId);
1037-
if (snapshot != null && snapshot.getRemoved() == null && snapshot.getState() == Snapshot.State.BackedUp) {
1038-
return snapshot;
1039-
} else {
1040-
return null;
1041-
}
1036+
return s_snapshotDao.findByIdIncludingRemoved(snapshotId);
10421037
}
10431038

10441039
public static StoragePoolVO findStoragePoolById(Long storagePoolId) {
@@ -1222,7 +1217,7 @@ public static PhysicalNetworkTrafficTypeVO findPhysicalNetworkTrafficTypeById(lo
12221217
}
12231218

12241219
public static NetworkVO findNetworkById(long id) {
1225-
return s_networkDao.findById(id);
1220+
return s_networkDao.findByIdIncludingRemoved(id);
12261221
}
12271222

12281223
public static Map<Service, Map<Capability, String>> getNetworkCapabilities(long networkId, long zoneId) {
@@ -1604,6 +1599,11 @@ public static String findJobInstanceUuid(AsyncJob job) {
16041599
if (group != null) {
16051600
jobInstanceId = group.getUuid();
16061601
}
1602+
} else if (jobInstanceType == ApiCommandJobType.Network) {
1603+
NetworkVO networkVO = ApiDBUtils.findNetworkById(job.getInstanceId());
1604+
if(networkVO != null) {
1605+
jobInstanceId = networkVO.getUuid();
1606+
}
16071607
} else if (jobInstanceType != ApiCommandJobType.None) {
16081608
// TODO : when we hit here, we need to add instanceType -> UUID
16091609
// entity table mapping

server/src/com/cloud/user/AccountManagerImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,10 @@ private boolean doDisableAccount(long accountId) throws ConcurrentOperationExcep
991991
}
992992

993993
@Override
994+
@ActionEvents({
995+
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_CREATE, eventDescription = "creating Account"),
996+
@ActionEvent(eventType = EventTypes.EVENT_USER_CREATE, eventDescription = "creating User")
997+
})
994998
public UserAccount createUserAccount(final String userName, final String password, final String firstName, final String lastName, final String email, final String timezone,
995999
String accountName, final short accountType, Long domainId, final String networkDomain, final Map<String, String> details, String accountUUID, final String userUUID) {
9961000

@@ -1132,6 +1136,7 @@ public UserVO createUser(String userName, String password, String firstName, Str
11321136
}
11331137

11341138
@Override
1139+
@ActionEvent(eventType = EventTypes.EVENT_USER_CREATE, eventDescription = "creating User")
11351140
public UserVO createUser(String userName, String password, String firstName, String lastName, String email, String timeZone, String accountName, Long domainId,
11361141
String userUUID) {
11371142

@@ -1263,6 +1268,7 @@ public UserAccount updateUser(Long userId, String firstName, String lastName, St
12631268
}
12641269

12651270
@Override
1271+
@ActionEvent(eventType = EventTypes.EVENT_USER_UPDATE, eventDescription = "updating User")
12661272
public UserAccount updateUser(UpdateUserCmd cmd) {
12671273
Long id = cmd.getId();
12681274
String apiKey = cmd.getApiKey();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,6 +3247,7 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
32473247
_affinityGroupVMMapDao.updateMap(vm.getId(), affinityGroupIdList);
32483248
}
32493249

3250+
CallContext.current().putContextParameter(VirtualMachine.class, vm.getUuid());
32503251
return vm;
32513252
}
32523253

0 commit comments

Comments
 (0)