|
| 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