Skip to content

Commit 9de77e1

Browse files
API to list console sessions (#11016)
* create API to list console sessions
1 parent 5c1bf4a commit 9de77e1

File tree

17 files changed

+1218
-9
lines changed

17 files changed

+1218
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ApiConstants {
2323
public static final String ACCOUNT_ID = "accountid";
2424
public static final String ACCOUNT_IDS = "accountids";
2525
public static final String ACCUMULATE = "accumulate";
26+
public static final String ACQUIRED = "acquired";
2627
public static final String ACTIVATION_RULE = "activationrule";
2728
public static final String ACTIVITY = "activity";
2829
public static final String ADAPTER_TYPE = "adaptertype";
@@ -94,9 +95,11 @@ public class ApiConstants {
9495
public static final String CONVERT_INSTANCE_HOST_ID = "convertinstancehostid";
9596
public static final String CONVERT_INSTANCE_STORAGE_POOL_ID = "convertinstancepoolid";
9697
public static final String ENABLED_REVOCATION_CHECK = "enabledrevocationcheck";
98+
public static final String CLIENT_ADDRESS = "clientaddress";
9799
public static final String COMBINED_CAPACITY_ORDERING = "COMBINED";
98100
public static final String CONTROLLER = "controller";
99101
public static final String CONTROLLER_UNIT = "controllerunit";
102+
public static final String CONSOLE_ENDPOINT_CREATOR_ADDRESS = "consoleendpointcreatoraddress";
100103
public static final String COPY_IMAGE_TAGS = "copyimagetags";
101104
public static final String CPU_OVERCOMMIT_RATIO = "cpuOvercommitRatio";
102105
public static final String CSR = "csr";

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
26+
import org.apache.cloudstack.consoleproxy.ConsoleSession;
2527
import org.apache.cloudstack.affinity.AffinityGroup;
2628
import org.apache.cloudstack.affinity.AffinityGroupResponse;
2729
import org.apache.cloudstack.api.ApiConstants.HostDetails;
@@ -579,4 +581,6 @@ List<TemplateResponse> createTemplateResponses(ResponseView view, VirtualMachine
579581
void updateTemplateIsoResponsesForIcons(List<TemplateResponse> responses, ResourceTag.ResourceObjectType type);
580582

581583
GuiThemeResponse createGuiThemeResponse(GuiThemeJoin guiThemeJoin);
584+
585+
ConsoleSessionResponse createConsoleSessionResponse(ConsoleSession consoleSession, ResponseView responseView);
582586
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.import org.apache.cloudstack.context.CallContext;
17+
package org.apache.cloudstack.api.command.user.consoleproxy;
18+
19+
import org.apache.cloudstack.consoleproxy.ConsoleSession;
20+
21+
import com.cloud.user.Account;
22+
import com.cloud.user.AccountService;
23+
import com.cloud.user.UserAccount;
24+
import org.apache.cloudstack.acl.RoleType;
25+
import org.apache.cloudstack.api.ACL;
26+
import org.apache.cloudstack.api.APICommand;
27+
import org.apache.cloudstack.api.ApiConstants;
28+
import org.apache.cloudstack.api.BaseListCmd;
29+
import org.apache.cloudstack.api.Parameter;
30+
import org.apache.cloudstack.api.response.AccountResponse;
31+
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
32+
import org.apache.cloudstack.api.response.DomainResponse;
33+
import org.apache.cloudstack.api.response.HostResponse;
34+
import org.apache.cloudstack.api.response.ListResponse;
35+
import org.apache.cloudstack.api.response.UserResponse;
36+
import org.apache.cloudstack.api.response.UserVmResponse;
37+
import org.apache.cloudstack.consoleproxy.ConsoleAccessManager;
38+
39+
import javax.inject.Inject;
40+
import java.util.Date;
41+
42+
@APICommand(name = "listConsoleSessions", description = "Lists console sessions.", responseObject = ConsoleSessionResponse.class,
43+
entityType = {ConsoleSession.class}, since = "4.21.0",
44+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
45+
authorized = {RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin, RoleType.User})
46+
public class ListConsoleSessionsCmd extends BaseListCmd {
47+
@Inject
48+
private AccountService accountService;
49+
50+
@Inject
51+
private ConsoleAccessManager consoleAccessManager;
52+
53+
@ACL
54+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = ConsoleSessionResponse.class, description = "The ID of the console session.")
55+
private Long id;
56+
57+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "The domain ID of the account that created the console endpoint.")
58+
private Long domainId;
59+
60+
@ACL
61+
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.UUID, entityType = AccountResponse.class, description = "The ID of the account that created the console endpoint.")
62+
private Long accountId;
63+
64+
@ACL
65+
@Parameter(name = ApiConstants.USER_ID, type = CommandType.UUID, entityType = UserResponse.class, description = "The ID of the user that created the console endpoint.")
66+
private Long userId;
67+
68+
@Parameter(name = ApiConstants.HOST_ID, type = CommandType.UUID, entityType = HostResponse.class, authorized = {RoleType.Admin}, description = "Lists console sessions from the specified host.")
69+
private Long hostId;
70+
71+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "Lists console sessions generated from this date onwards. " +
72+
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
73+
private Date startDate;
74+
75+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "Lists console sessions generated up until this date. " +
76+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
77+
private Date endDate;
78+
79+
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.UUID, entityType = UserVmResponse.class, description = "The ID of the virtual machine.")
80+
private Long vmId;
81+
82+
@Parameter(name = ApiConstants.CONSOLE_ENDPOINT_CREATOR_ADDRESS, type = CommandType.STRING, description = "IP address of the creator of the console endpoint.")
83+
private String consoleEndpointCreatorAddress;
84+
85+
@Parameter(name = ApiConstants.CLIENT_ADDRESS, type = CommandType.STRING, description = "IP address of the client that accessed the console session.")
86+
private String clientAddress;
87+
88+
@Parameter(name = ApiConstants.ACTIVE_ONLY, type = CommandType.BOOLEAN,
89+
description = "Lists only active console sessions, defaults to true. Active sessions are the ones that have been acquired and have not been removed.")
90+
private boolean activeOnly = true;
91+
92+
@Parameter(name = ApiConstants.ACQUIRED, type = CommandType.BOOLEAN,
93+
description = "Lists acquired console sessions, defaults to false. Acquired console sessions are the ones that have been accessed. " +
94+
"The 'activeonly' parameter has precedence over the 'acquired' parameter, i.e., when the 'activeonly' parameter is 'true', the 'acquired' parameter value will be ignored.")
95+
private boolean acquired = false;
96+
97+
@Parameter(name = ApiConstants.IS_RECURSIVE, type = CommandType.BOOLEAN,
98+
description = "Lists console sessions recursively per domain. If an account ID is informed, only the account's console sessions will be listed. Defaults to false.")
99+
private boolean recursive = false;
100+
101+
public Long getId() {
102+
return id;
103+
}
104+
105+
public Long getDomainId() {
106+
return domainId;
107+
}
108+
109+
public Long getAccountId() {
110+
return accountId;
111+
}
112+
113+
public Long getUserId() {
114+
return userId;
115+
}
116+
117+
public Long getHostId() {
118+
return hostId;
119+
}
120+
121+
public Date getStartDate() {
122+
return startDate;
123+
}
124+
125+
public Date getEndDate() {
126+
return endDate;
127+
}
128+
129+
public Long getVmId() {
130+
return vmId;
131+
}
132+
133+
public String getConsoleEndpointCreatorAddress() {
134+
return consoleEndpointCreatorAddress;
135+
}
136+
137+
public String getClientAddress() {
138+
return clientAddress;
139+
}
140+
141+
public boolean isActiveOnly() {
142+
return activeOnly;
143+
}
144+
145+
public boolean getAcquired() {
146+
return acquired;
147+
}
148+
149+
public boolean isRecursive() {
150+
return recursive;
151+
}
152+
153+
@Override
154+
public void execute() {
155+
ListResponse<ConsoleSessionResponse> response = consoleAccessManager.listConsoleSessions(this);
156+
response.setResponseName(getCommandName());
157+
setResponseObject(response);
158+
}
159+
160+
@Override
161+
public long getEntityOwnerId() {
162+
if (getId() != null) {
163+
ConsoleSession consoleSession = consoleAccessManager.listConsoleSessionById(getId());
164+
if (consoleSession != null) {
165+
return consoleSession.getAccountId();
166+
}
167+
}
168+
169+
if (getAccountId() != null) {
170+
return getAccountId();
171+
}
172+
173+
if (getUserId() != null) {
174+
UserAccount userAccount = accountService.getUserAccountById(getUserId());
175+
if (userAccount != null) {
176+
return userAccount.getAccountId();
177+
}
178+
}
179+
180+
return Account.ACCOUNT_ID_SYSTEM;
181+
}
182+
}

0 commit comments

Comments
 (0)