From 0fc005f20a103b000137bf2fbfffc734d9aaed99 Mon Sep 17 00:00:00 2001 From: Abhinandan Prateek Date: Wed, 27 Apr 2011 11:07:43 +0530 Subject: [PATCH] bug 6451: XenServer username/password should be changable through API --- .../api/commands/UpdateHostPasswordCmd.java | 95 +++++++++++++++++++ .../com/cloud/server/ManagementService.java | 3 + client/tomcatconf/commands.properties.in | 1 + .../cloud/server/ManagementServerImpl.java | 61 +++++++++++- 4 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 api/src/com/cloud/api/commands/UpdateHostPasswordCmd.java diff --git a/api/src/com/cloud/api/commands/UpdateHostPasswordCmd.java b/api/src/com/cloud/api/commands/UpdateHostPasswordCmd.java new file mode 100644 index 00000000000..a9fe743e4fb --- /dev/null +++ b/api/src/com/cloud/api/commands/UpdateHostPasswordCmd.java @@ -0,0 +1,95 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.cloud.api.commands; + +import org.apache.log4j.Logger; + + +import com.cloud.api.ApiConstants; +import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; +import com.cloud.api.Parameter; +import com.cloud.api.ServerApiException; +import com.cloud.api.BaseCmd.CommandType; +import com.cloud.api.response.RegisterResponse; +import com.cloud.api.response.SuccessResponse; +import com.cloud.user.Account; + +@Implementation(description = "Update password of a host/pool on management server.", responseObject = SuccessResponse.class) +public class UpdateHostPasswordCmd extends BaseCmd { + public static final Logger s_logger = Logger.getLogger(UpdateHostPasswordCmd.class.getName()); + + private static final String s_name = "updatehostpasswordresponse"; + + // /////////////////////////////////////////////////// + // ////////////// API parameters ///////////////////// + // /////////////////////////////////////////////////// + + @Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID") + private Long hostId; + + @Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="the cluster ID for the host") + private Long clusterId; + + @Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="the username for the host/cluster") + private String username; + + @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required=true, description="the password for the host/cluster") + private String password; + + // /////////////////////////////////////////////////// + // ///////////////// Accessors /////////////////////// + // /////////////////////////////////////////////////// + + public Long getHostId() { + return hostId; + } + + public Long getClusterId() { + return clusterId; + } + + public String getPassword() { + return password; + } + + public String getUsername() { + return username; + } + + // /////////////////////////////////////////////////// + // ///////////// API Implementation/////////////////// + // /////////////////////////////////////////////////// + + @Override + public String getCommandName() { + return s_name; + } + + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } + + @Override + public void execute() { + _mgr.updateHostPassword(this); + this.setResponseObject(new SuccessResponse()); + } +} \ No newline at end of file diff --git a/api/src/com/cloud/server/ManagementService.java b/api/src/com/cloud/server/ManagementService.java index 894a4ed1935..b8e134392bc 100644 --- a/api/src/com/cloud/server/ManagementService.java +++ b/api/src/com/cloud/server/ManagementService.java @@ -68,6 +68,7 @@ import com.cloud.api.commands.RegisterSSHKeyPairCmd; import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.UpdateDomainCmd; +import com.cloud.api.commands.UpdateHostPasswordCmd; import com.cloud.api.commands.UpdateIsoCmd; import com.cloud.api.commands.UpdateIsoPermissionsCmd; import com.cloud.api.commands.UpdateTemplateCmd; @@ -370,6 +371,8 @@ public interface ManagementService { String[] createApiKeyAndSecretKey(RegisterCmd cmd); + boolean updateHostPassword(UpdateHostPasswordCmd cmd); + InstanceGroup updateVmGroup(UpdateVMGroupCmd cmd); List searchForVmGroups(ListVMGroupsCmd cmd); diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index 85acaa999dd..a4c8ed9ea85 100755 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -180,6 +180,7 @@ prepareHostForMaintenance=com.cloud.api.commands.PrepareForMaintenanceCmd;1 cancelHostMaintenance=com.cloud.api.commands.CancelMaintenanceCmd;1 listHosts=com.cloud.api.commands.ListHostsCmd;3 addSecondaryStorage=com.cloud.api.commands.AddSecondaryStorageCmd;1 +updateHostPassword=com.cloud.api.commands.UpdateHostPasswordCmd;1 #### volume commands attachVolume=com.cloud.api.commands.AttachVolumeCmd;15 diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index adca05cf528..255917c4f93 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -61,6 +61,7 @@ import com.cloud.alert.Alert; import com.cloud.alert.AlertManager; import com.cloud.alert.AlertVO; import com.cloud.alert.dao.AlertDao; +import com.cloud.api.ApiConstants; import com.cloud.api.ApiDBUtils; import com.cloud.api.commands.CreateDomainCmd; import com.cloud.api.commands.CreateSSHKeyPairCmd; @@ -105,6 +106,7 @@ import com.cloud.api.commands.RegisterSSHKeyPairCmd; import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.UpdateDomainCmd; +import com.cloud.api.commands.UpdateHostPasswordCmd; import com.cloud.api.commands.UpdateIsoCmd; import com.cloud.api.commands.UpdateIsoPermissionsCmd; import com.cloud.api.commands.UpdateTemplateCmd; @@ -165,8 +167,10 @@ import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.StorageUnavailableException; import com.cloud.host.Host; import com.cloud.host.Host.Type; +import com.cloud.host.DetailVO; import com.cloud.host.HostVO; import com.cloud.host.Status; +import com.cloud.host.dao.DetailsDao; import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.info.ConsoleProxyInfo; @@ -285,7 +289,8 @@ public class ManagementServerImpl implements ManagementServer { private final VlanDao _vlanDao; private final AccountVlanMapDao _accountVlanMapDao; private final PodVlanMapDao _podVlanMapDao; - private final HostDao _hostDao; + private final HostDao _hostDao; + private final DetailsDao _detailsDao; private final UserDao _userDao; private final UserVmDao _userVmDao; private final ConfigurationDao _configDao; @@ -352,7 +357,8 @@ public class ManagementServerImpl implements ManagementServer { _vlanDao = locator.getDao(VlanDao.class); _accountVlanMapDao = locator.getDao(AccountVlanMapDao.class); _podVlanMapDao = locator.getDao(PodVlanMapDao.class); - _hostDao = locator.getDao(HostDao.class); + _hostDao = locator.getDao(HostDao.class); + _detailsDao = locator.getDao(DetailsDao.class); _hostPodDao = locator.getDao(HostPodDao.class); _jobDao = locator.getDao(AsyncJobDao.class); _clusterDao = locator.getDao(ClusterDao.class); @@ -4756,5 +4762,56 @@ public class ManagementServerImpl implements ManagementServer { return password; } + + @DB + public boolean updateHostPassword(UpdateHostPasswordCmd cmd) { + if (cmd.getClusterId()==null && cmd.getHostId() == null){ + throw new InvalidParameterValueException("You should provide one of cluster id or a host id."); + } + else if (cmd.getClusterId()==null){ + HostVO h = _hostDao.findById(cmd.getHostId()); + if (h.getHypervisorType() == HypervisorType.XenServer){ + throw new InvalidParameterValueException("You should provide cluster id for Xenserver cluster."); + } + DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); + if (nv.getValue().equals(cmd.getUsername())){ + DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); + nvp.setValue(cmd.getPassword()); + _detailsDao.persist(nvp); + } + else { + throw new InvalidParameterValueException("The username is not under use by management server."); + } + } + else { + //get all the hosts in this cluster + List hosts = _hostDao.listByCluster(cmd.getClusterId()); + Transaction txn = Transaction.currentTxn(); + txn.start(); + for (HostVO h : hosts) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Changing password for host name = " + h.getName()); + } + //update password for this host + DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); + if (nv.getValue().equals(cmd.getUsername())){ + DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); + nvp.setValue(cmd.getPassword()); + _detailsDao.persist(nvp); + } + else { //if one host in the cluster has diff username then rollback to maintain consistency + txn.rollback(); + throw new InvalidParameterValueException("The username is not same for all hosts, please modify passwords for individual hosts."); + } + } + txn.commit(); + // if hypervisor is xenserver then we update it in CitrixResourceBase + } + return true; + } + + + + }