Skip to content

Commit ff4ab2d

Browse files
committed
review changes
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 19814b0 commit ff4ab2d

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class ApiConstants {
8080
public static final String BYTES_WRITE_RATE_MAX = "byteswriteratemax";
8181
public static final String BYTES_WRITE_RATE_MAX_LENGTH = "byteswriteratemaxlength";
8282
public static final String BYPASS_VLAN_OVERLAP_CHECK = "bypassvlanoverlapcheck";
83+
public static final String CALLER = "caller";
8384
public static final String CAPACITY = "capacity";
8485
public static final String CATEGORY = "category";
8586
public static final String CAN_REVERT = "canrevert";

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImpl.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,29 @@ protected void updateAllExtensionHosts(Extension extension, Long clusterId, bool
476476
executorService.shutdown();
477477
}
478478

479+
protected Map<String, String> getCallerDetails() {
480+
Account caller = CallContext.current().getCallingAccount();
481+
if (caller == null) {
482+
return null;
483+
}
484+
Map<String, String> callerDetails = new HashMap<>();
485+
callerDetails.put(ApiConstants.ID, caller.getUuid());
486+
callerDetails.put(ApiConstants.NAME, caller.getAccountName());
487+
if (caller.getType() != null) {
488+
callerDetails.put(ApiConstants.TYPE, caller.getType().name());
489+
}
490+
Role role = roleService.findRole(caller.getRoleId());
491+
if (role == null) {
492+
return callerDetails;
493+
}
494+
callerDetails.put(ApiConstants.ROLE_ID, role.getUuid());
495+
callerDetails.put(ApiConstants.ROLE_NAME, role.getName());
496+
if (role.getRoleType() != null) {
497+
callerDetails.put(ApiConstants.ROLE_TYPE, role.getRoleType().name());
498+
}
499+
return callerDetails;
500+
}
501+
479502
protected Map<String, Map<String, String>> getExternalAccessDetails(Map<String, String> actionDetails, long hostId,
480503
ExtensionResourceMap resourceMap) {
481504
Map<String, Map<String, String>> externalDetails = new HashMap<>();
@@ -497,6 +520,10 @@ protected Map<String, Map<String, String>> getExternalAccessDetails(Map<String,
497520
if (MapUtils.isNotEmpty(extensionDetails)) {
498521
externalDetails.put(ApiConstants.EXTENSION, extensionDetails);
499522
}
523+
Map<String, String> callerDetails = getCallerDetails();
524+
if (MapUtils.isNotEmpty(callerDetails)) {
525+
externalDetails.put(ApiConstants.CALLER, callerDetails);
526+
}
500527
return externalDetails;
501528
}
502529

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/manager/ExtensionsManagerImplTest.java

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import com.cloud.storage.dao.VMTemplateDao;
122122
import com.cloud.user.Account;
123123
import com.cloud.utils.Pair;
124+
import com.cloud.utils.UuidUtils;
124125
import com.cloud.utils.db.EntityManager;
125126
import com.cloud.utils.db.SearchBuilder;
126127
import com.cloud.utils.db.SearchCriteria;
@@ -525,11 +526,15 @@ public void getExternalAccessDetailsReturnsMapWithHostAndExtension() {
525526
when(hostDetailsDao.findDetails(hostId)).thenReturn(null);
526527
when(extensionResourceMapDetailsDao.listDetailsKeyPairs(2L, true)).thenReturn(Collections.emptyMap());
527528
when(extensionDetailsDao.listDetailsKeyPairs(3L, true)).thenReturn(map);
528-
Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(map, hostId, resourceMap);
529-
assertTrue(result.containsKey(ApiConstants.ACTION));
530-
assertFalse(result.containsKey(ApiConstants.HOST));
531-
assertFalse(result.containsKey(ApiConstants.RESOURCE_MAP));
532-
assertTrue(result.containsKey(ApiConstants.EXTENSION));
529+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
530+
mockCallerRole(RoleType.Admin);
531+
Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(map, hostId, resourceMap);
532+
assertTrue(result.containsKey(ApiConstants.ACTION));
533+
assertFalse(result.containsKey(ApiConstants.HOST));
534+
assertFalse(result.containsKey(ApiConstants.RESOURCE_MAP));
535+
assertTrue(result.containsKey(ApiConstants.EXTENSION));
536+
assertTrue(result.containsKey(ApiConstants.CALLER));
537+
}
533538
}
534539

535540
@Test(expected = CloudRuntimeException.class)
@@ -1285,9 +1290,14 @@ private void mockCallerRole(RoleType roleType) {
12851290
CallContext callContextMock = mock(CallContext.class);
12861291
when(CallContext.current()).thenReturn(callContextMock);
12871292
Account accountMock = mock(Account.class);
1293+
when(accountMock.getAccountName()).thenReturn("testAccount");
1294+
when(accountMock.getUuid()).thenReturn(UUID.randomUUID().toString());
1295+
when(accountMock.getType()).thenReturn(RoleType.Admin.equals(roleType) ? Account.Type.ADMIN : Account.Type.NORMAL);
12881296
when(accountMock.getRoleId()).thenReturn(1L);
12891297
Role role = mock(Role.class);
12901298
when(role.getRoleType()).thenReturn(roleType);
1299+
when(role.getUuid()).thenReturn("role-uuid-1");
1300+
when(role.getName()).thenReturn(roleType.name() + "Role");
12911301
when(roleService.findRole(1L)).thenReturn(role);
12921302
when(callContextMock.getCallingAccount()).thenReturn(accountMock);
12931303
}
@@ -1952,4 +1962,78 @@ public void getInstanceConsole_whenAgentManagerFails() {
19521962
Answer result = extensionsManager.getInstanceConsole(vm, host);
19531963
assertNull(result);
19541964
}
1965+
1966+
@Test
1967+
public void getExternalAccessDetailsReturnsExpectedDetails() {
1968+
Host host = mock(Host.class);
1969+
when(host.getId()).thenReturn(100L);
1970+
when(host.getClusterId()).thenReturn(1L);
1971+
Map<String, String> vmDetails = Map.of("key1", "value1", "key2", "value2");
1972+
ExtensionResourceMapVO resourceMapVO = mock(ExtensionResourceMapVO.class);
1973+
when(extensionResourceMapDao.findByResourceIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster))
1974+
.thenReturn(resourceMapVO);
1975+
doReturn(new HashMap<>()).when(extensionsManager).getExternalAccessDetails(null, 100L, resourceMapVO);
1976+
Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(host, vmDetails);
1977+
assertNotNull(result);
1978+
assertNotNull(result.get(ApiConstants.VIRTUAL_MACHINE));
1979+
assertEquals(vmDetails, result.get(ApiConstants.VIRTUAL_MACHINE));
1980+
}
1981+
1982+
@Test
1983+
public void getExternalAccessDetailsReturnsExpectedNullDetails() {
1984+
Host host = mock(Host.class);
1985+
when(host.getId()).thenReturn(101L);
1986+
when(host.getClusterId()).thenReturn(1L);
1987+
Map<String, String> vmDetails = null;
1988+
ExtensionResourceMapVO resourceMapVO = mock(ExtensionResourceMapVO.class);
1989+
when(extensionResourceMapDao.findByResourceIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster))
1990+
.thenReturn(resourceMapVO);
1991+
doReturn(new HashMap<>()).when(extensionsManager).getExternalAccessDetails(null, 101L, resourceMapVO);
1992+
Map<String, Map<String, String>> result = extensionsManager.getExternalAccessDetails(host, vmDetails);
1993+
assertNotNull(result);
1994+
assertNull(result.get(ApiConstants.VIRTUAL_MACHINE));
1995+
}
1996+
1997+
@Test
1998+
public void getCallerDetailsReturnsExpectedDetailsForValidCaller() {
1999+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
2000+
mockCallerRole(RoleType.Admin);
2001+
Map<String, String> result = extensionsManager.getCallerDetails();
2002+
assertNotNull(result);
2003+
assertTrue(UuidUtils.isUuid(result.get(ApiConstants.ID)));
2004+
assertEquals("testAccount", result.get(ApiConstants.NAME));
2005+
assertEquals("ADMIN", result.get(ApiConstants.TYPE));
2006+
assertEquals("role-uuid-1", result.get(ApiConstants.ROLE_ID));
2007+
assertEquals("AdminRole", result.get(ApiConstants.ROLE_NAME));
2008+
assertEquals("Admin", result.get(ApiConstants.ROLE_TYPE));
2009+
}
2010+
}
2011+
2012+
@Test
2013+
public void getCallerDetailsReturnsNullWhenCallerIsNull() {
2014+
CallContext callContext = mock(CallContext.class);
2015+
when(callContext.getCallingAccount()).thenReturn(null);
2016+
try (MockedStatic<CallContext> mockedCallContext = mockStatic(CallContext.class)) {
2017+
mockedCallContext.when(CallContext::current).thenReturn(callContext);
2018+
Map<String, String> result = extensionsManager.getCallerDetails();
2019+
assertNull(result);
2020+
}
2021+
}
2022+
2023+
@Test
2024+
public void getCallerDetailsReturnsDetailsWithoutRoleWhenRoleIsNull() {
2025+
try (MockedStatic<CallContext> ignored = mockStatic(CallContext.class)) {
2026+
mockCallerRole(RoleType.User);
2027+
when(roleService.findRole(1L)).thenReturn(null);
2028+
Map<String, String> result = extensionsManager.getCallerDetails();
2029+
assertNotNull(result);
2030+
assertTrue(UuidUtils.isUuid(result.get(ApiConstants.ID)));
2031+
assertEquals("testAccount", result.get(ApiConstants.NAME));
2032+
assertEquals("NORMAL", result.get(ApiConstants.TYPE));
2033+
assertNull(result.get(ApiConstants.ROLE_ID));
2034+
assertNull(result.get(ApiConstants.ROLE_NAME));
2035+
assertNull(result.get(ApiConstants.ROLE_TYPE));
2036+
}
2037+
}
2038+
19552039
}

server/src/main/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public void setModeFromExternalProtocol(String protocol) {
781781
if (StringUtils.isBlank(protocol)) {
782782
return;
783783
}
784-
if (List.of("link", "url", "direct").contains(protocol.toLowerCase())) {
784+
if (Mode.Direct.name().toLowerCase().equalsIgnoreCase(protocol)) {
785785
this.mode = Mode.Direct;
786786
}
787787
}

server/src/test/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManagerImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,15 +770,15 @@ public void composeConsoleAccessEndpointReturnsWithoutPersistWhenConsoleConnecti
770770
String addr = "addr";
771771
ConsoleConnectionDetails details = new ConsoleConnectionDetails("password", "en", "tag", null);
772772
details.setDirectUrl(url);
773-
details.setModeFromExternalProtocol("url");
773+
details.setModeFromExternalProtocol("direct");
774774
VirtualMachine vm = Mockito.mock(VirtualMachine.class);
775775
Mockito.when(vm.getId()).thenReturn(vmId);
776776
HostVO host = Mockito.mock(HostVO.class);
777777
Mockito.when(host.getId()).thenReturn(hostId);
778778
Mockito.doReturn(details).when(consoleAccessManager).getConsoleConnectionDetails(vm, host);
779779
Mockito.doNothing().when(consoleAccessManager).persistConsoleSession(sessionUuid, vmId, hostId, addr);
780780

781-
ConsoleEndpoint endpoint = consoleAccessManager.composeConsoleAccessEndpoint("url", vm, host, addr, sessionUuid, "");
781+
ConsoleEndpoint endpoint = consoleAccessManager.composeConsoleAccessEndpoint("rootUrl", vm, host, addr, sessionUuid, "");
782782

783783
Mockito.verify(consoleAccessManager).persistConsoleSession(sessionUuid, vmId, hostId, addr);
784784
Mockito.verify(managementServer, Mockito.never()).setConsoleAccessForVm(Mockito.anyLong(), Mockito.anyString());

0 commit comments

Comments
 (0)