@@ -3929,6 +3929,11 @@ private boolean updateHostsInCluster(final UpdateHostPasswordCmd command) {
39293929 // get all the hosts in this cluster
39303930 final List <HostVO > hosts = _resourceMgr .listAllHostsInCluster (command .getClusterId ());
39313931
3932+ String userNameWithoutSpaces = StringUtils .deleteWhitespace (command .getUsername ());
3933+ if (StringUtils .isBlank (userNameWithoutSpaces )) {
3934+ throw new InvalidParameterValueException ("Username should be non empty string" );
3935+ }
3936+
39323937 Transaction .execute (new TransactionCallbackNoReturn () {
39333938 @ Override
39343939 public void doInTransactionWithoutResult (final TransactionStatus status ) {
@@ -3938,7 +3943,12 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
39383943 }
39393944 // update password for this host
39403945 final DetailVO nv = _detailsDao .findDetail (h .getId (), ApiConstants .USERNAME );
3941- if (nv .getValue ().equals (command .getUsername ())) {
3946+ if (nv == null ) {
3947+ final DetailVO nvu = new DetailVO (h .getId (), ApiConstants .USERNAME , userNameWithoutSpaces );
3948+ _detailsDao .persist (nvu );
3949+ final DetailVO nvp = new DetailVO (h .getId (), ApiConstants .PASSWORD , DBEncryptionUtil .encrypt (command .getPassword ()));
3950+ _detailsDao .persist (nvp );
3951+ } else if (nv .getValue ().equals (userNameWithoutSpaces )) {
39423952 final DetailVO nvp = _detailsDao .findDetail (h .getId (), ApiConstants .PASSWORD );
39433953 nvp .setValue (DBEncryptionUtil .encrypt (command .getPassword ()));
39443954 _detailsDao .persist (nvp );
@@ -3986,6 +3996,12 @@ public boolean updateHostPassword(final UpdateHostPasswordCmd cmd) {
39863996 if (!supportedHypervisors .contains (host .getHypervisorType ())) {
39873997 throw new InvalidParameterValueException ("This operation is not supported for this hypervisor type" );
39883998 }
3999+
4000+ String userNameWithoutSpaces = StringUtils .deleteWhitespace (cmd .getUsername ());
4001+ if (StringUtils .isBlank (userNameWithoutSpaces )) {
4002+ throw new InvalidParameterValueException ("Username should be non empty string" );
4003+ }
4004+
39894005 Transaction .execute (new TransactionCallbackNoReturn () {
39904006 @ Override
39914007 public void doInTransactionWithoutResult (final TransactionStatus status ) {
@@ -3994,7 +4010,12 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
39944010 }
39954011 // update password for this host
39964012 final DetailVO nv = _detailsDao .findDetail (host .getId (), ApiConstants .USERNAME );
3997- if (nv .getValue ().equals (cmd .getUsername ())) {
4013+ if (nv == null ) {
4014+ final DetailVO nvu = new DetailVO (host .getId (), ApiConstants .USERNAME , userNameWithoutSpaces );
4015+ _detailsDao .persist (nvu );
4016+ final DetailVO nvp = new DetailVO (host .getId (), ApiConstants .PASSWORD , DBEncryptionUtil .encrypt (cmd .getPassword ()));
4017+ _detailsDao .persist (nvp );
4018+ } else if (nv .getValue ().equals (userNameWithoutSpaces )) {
39984019 final DetailVO nvp = _detailsDao .findDetail (host .getId (), ApiConstants .PASSWORD );
39994020 nvp .setValue (DBEncryptionUtil .encrypt (cmd .getPassword ()));
40004021 _detailsDao .persist (nvp );
0 commit comments