Skip to content

Commit d8c2d89

Browse files
create API to list console sessions
1 parent 2d669db commit d8c2d89

File tree

16 files changed

+1083
-2
lines changed

16 files changed

+1083
-2
lines changed

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

Lines changed: 4 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";
@@ -90,8 +91,10 @@ public class ApiConstants {
9091
public static final String CONVERT_INSTANCE_HOST_ID = "convertinstancehostid";
9192
public static final String CONVERT_INSTANCE_STORAGE_POOL_ID = "convertinstancepoolid";
9293
public static final String ENABLED_REVOCATION_CHECK = "enabledrevocationcheck";
94+
public static final String CLIENT_ADDRESS = "clientaddress";
9395
public static final String CONTROLLER = "controller";
9496
public static final String CONTROLLER_UNIT = "controllerunit";
97+
public static final String CONSOLE_ENDPOINT_CREATOR_ADDRESS = "consoleendpointcreatoraddress";
9598
public static final String COPY_IMAGE_TAGS = "copyimagetags";
9699
public static final String CSR = "csr";
97100
public static final String PRIVATE_KEY = "privatekey";
@@ -247,6 +250,7 @@ public class ApiConstants {
247250
public static final String HYPERVISOR = "hypervisor";
248251
public static final String INLINE = "inline";
249252
public static final String INSTANCE = "instance";
253+
public static final String INSTANCE_ID = "instance_id";
250254
public static final String ICMP_CODE = "icmpcode";
251255
public static final String ICMP_TYPE = "icmptype";
252256
public static final String ID = "id";

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
SharedFSResponse createSharedFSResponse(ResponseView view, SharedFS sharedFS);
580582

581583
void updateTemplateIsoResponsesForIcons(List<TemplateResponse> responses, ResourceTag.ResourceObjectType type);
584+
585+
ConsoleSessionResponse createConsoleSessionResponse(ConsoleSession consoleSession, ResponseView responseView);
582586
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = ConsoleSessionResponse.class, description = "The ID of the console session.")
54+
private Long id;
55+
56+
@ACL
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 acquired 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 acquired up until this date. " +
76+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
77+
private Date endDate;
78+
79+
@Parameter(name = ApiConstants.INSTANCE_ID, type = CommandType.UUID, entityType = UserVmResponse.class, description = "The ID of the instance.")
80+
private Long instanceId;
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.IS_RECURSIVE, type = CommandType.BOOLEAN, since = "4.20.0.1-scclouds",
93+
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.")
94+
private boolean recursive = false;
95+
96+
public Long getId() {
97+
return id;
98+
}
99+
100+
public Long getDomainId() {
101+
return domainId;
102+
}
103+
104+
public Long getAccountId() {
105+
return accountId;
106+
}
107+
108+
public Long getUserId() {
109+
return userId;
110+
}
111+
112+
public Long getHostId() {
113+
return hostId;
114+
}
115+
116+
public Date getStartDate() {
117+
return startDate;
118+
}
119+
120+
public Date getEndDate() {
121+
return endDate;
122+
}
123+
124+
public Long getInstanceId() {
125+
return instanceId;
126+
}
127+
128+
public String getConsoleEndpointCreatorAddress() {
129+
return consoleEndpointCreatorAddress;
130+
}
131+
132+
public String getClientAddress() {
133+
return clientAddress;
134+
}
135+
136+
public boolean isActiveOnly() {
137+
return activeOnly;
138+
}
139+
140+
public boolean isRecursive() {
141+
return recursive;
142+
}
143+
144+
@Override
145+
public void execute() {
146+
ListResponse<ConsoleSessionResponse> response = consoleAccessManager.listConsoleSessions(this);
147+
response.setResponseName(getCommandName());
148+
setResponseObject(response);
149+
}
150+
151+
@Override
152+
public long getEntityOwnerId() {
153+
if (getId() != null) {
154+
ConsoleSession consoleSession = consoleAccessManager.listConsoleSessionById(getId());
155+
if (consoleSession != null) {
156+
return consoleSession.getAccountId();
157+
}
158+
}
159+
160+
if (getAccountId() != null) {
161+
return getAccountId();
162+
}
163+
164+
if (getUserId() != null) {
165+
UserAccount userAccount = accountService.getUserAccountById(getUserId());
166+
if (userAccount != null) {
167+
return userAccount.getAccountId();
168+
}
169+
}
170+
171+
return Account.ACCOUNT_ID_SYSTEM;
172+
}
173+
}

0 commit comments

Comments
 (0)