@@ -3739,50 +3739,84 @@ public String getVMPassword(final GetVMPasswordCmd cmd) {
37393739 return password ;
37403740 }
37413741
3742+ private boolean updateHostsInCluster (final UpdateHostPasswordCmd command ) {
3743+ // get all the hosts in this cluster
3744+ final List <HostVO > hosts = _resourceMgr .listAllHostsInCluster (command .getClusterId ());
3745+
3746+ Transaction .execute (new TransactionCallbackNoReturn () {
3747+ @ Override
3748+ public void doInTransactionWithoutResult (final TransactionStatus status ) {
3749+ for (final HostVO h : hosts ) {
3750+ if (s_logger .isDebugEnabled ()) {
3751+ s_logger .debug ("Changing password for host name = " + h .getName ());
3752+ }
3753+ // update password for this host
3754+ final DetailVO nv = _detailsDao .findDetail (h .getId (), ApiConstants .USERNAME );
3755+ if (nv .getValue ().equals (command .getUsername ())) {
3756+ final DetailVO nvp = _detailsDao .findDetail (h .getId (), ApiConstants .PASSWORD );
3757+ nvp .setValue (DBEncryptionUtil .encrypt (command .getPassword ()));
3758+ _detailsDao .persist (nvp );
3759+ } else {
3760+ // if one host in the cluster has diff username then
3761+ // rollback to maintain consistency
3762+ throw new InvalidParameterValueException ("The username is not same for all hosts, please modify passwords for individual hosts." );
3763+ }
3764+ }
3765+ }
3766+ });
3767+ return true ;
3768+ }
3769+
3770+ /**
3771+ * This method updates the password of all hosts in a given cluster.
3772+ */
3773+ @ Override
3774+ @ DB
3775+ public boolean updateClusterPassword (final UpdateHostPasswordCmd command ) {
3776+ if (command .getClusterId () == null ) {
3777+ throw new InvalidParameterValueException ("You should provide a cluster id." );
3778+ }
3779+
3780+ final ClusterVO cluster = ApiDBUtils .findClusterById (command .getClusterId ());
3781+ if (cluster == null || cluster .getHypervisorType () != HypervisorType .XenServer || cluster .getHypervisorType () != HypervisorType .KVM ) {
3782+ throw new InvalidParameterValueException ("This operation is not supported for this hypervisor type" );
3783+ }
3784+ return updateHostsInCluster (command );
3785+ }
3786+
37423787 @ Override
37433788 @ DB
37443789 public boolean updateHostPassword (final UpdateHostPasswordCmd cmd ) {
3745- if (cmd .getClusterId () == null && cmd .getHostId () == null ) {
3746- throw new InvalidParameterValueException ("You should provide one of cluster id or a host id." );
3747- } else if (cmd .getClusterId () == null ) {
3748- final HostVO host = _hostDao .findById (cmd .getHostId ());
3749- if (host != null && host .getHypervisorType () == HypervisorType .XenServer ) {
3750- throw new InvalidParameterValueException ("You should provide cluster id for Xenserver cluster." );
3751- } else {
3752- throw new InvalidParameterValueException ("This operation is not supported for this hypervisor type" );
3753- }
3754- } else {
3790+ if (cmd .getHostId () == null ) {
3791+ throw new InvalidParameterValueException ("You should provide an host id." );
3792+ }
37553793
3756- final ClusterVO cluster = ApiDBUtils .findClusterById (cmd .getClusterId ());
3757- if (cluster == null || cluster .getHypervisorType () != HypervisorType .XenServer ) {
3758- throw new InvalidParameterValueException ("This operation is not supported for this hypervisor type" );
3759- }
3760- // get all the hosts in this cluster
3761- final List <HostVO > hosts = _resourceMgr .listAllHostsInCluster (cmd .getClusterId ());
3762-
3763- Transaction .execute (new TransactionCallbackNoReturn () {
3764- @ Override
3765- public void doInTransactionWithoutResult (final TransactionStatus status ) {
3766- for (final HostVO h : hosts ) {
3767- if (s_logger .isDebugEnabled ()) {
3768- s_logger .debug ("Changing password for host name = " + h .getName ());
3769- }
3770- // update password for this host
3771- final DetailVO nv = _detailsDao .findDetail (h .getId (), ApiConstants .USERNAME );
3772- if (nv .getValue ().equals (cmd .getUsername ())) {
3773- final DetailVO nvp = _detailsDao .findDetail (h .getId (), ApiConstants .PASSWORD );
3774- nvp .setValue (DBEncryptionUtil .encrypt (cmd .getPassword ()));
3775- _detailsDao .persist (nvp );
3776- } else {
3777- // if one host in the cluster has diff username then
3778- // rollback to maintain consistency
3779- throw new InvalidParameterValueException ("The username is not same for all hosts, please modify passwords for individual hosts." );
3780- }
3781- }
3782- }
3783- });
3794+ final HostVO host = _hostDao .findById (cmd .getHostId ());
3795+
3796+ if (host .getHypervisorType () != HypervisorType .XenServer || host .getHypervisorType () != HypervisorType .KVM ) {
3797+ throw new InvalidParameterValueException ("This operation is not supported for this hypervisor type" );
37843798 }
37853799
3800+ Transaction .execute (new TransactionCallbackNoReturn () {
3801+ @ Override
3802+ public void doInTransactionWithoutResult (final TransactionStatus status ) {
3803+ if (s_logger .isDebugEnabled ()) {
3804+ s_logger .debug ("Changing password for host name = " + host .getName ());
3805+ }
3806+ // update password for this host
3807+ final DetailVO nv = _detailsDao .findDetail (host .getId (), ApiConstants .USERNAME );
3808+ if (nv .getValue ().equals (cmd .getUsername ())) {
3809+ final DetailVO nvp = _detailsDao .findDetail (host .getId (), ApiConstants .PASSWORD );
3810+ nvp .setValue (DBEncryptionUtil .encrypt (cmd .getPassword ()));
3811+ _detailsDao .persist (nvp );
3812+ } else {
3813+ // if one host in the cluster has diff username then
3814+ // rollback to maintain consistency
3815+ throw new InvalidParameterValueException ("The username is not same for the hosts.." );
3816+ }
3817+ }
3818+ });
3819+
37863820 return true ;
37873821 }
37883822
0 commit comments