2323import javax .inject .Inject ;
2424import javax .naming .ConfigurationException ;
2525
26+ import org .apache .commons .lang3 .StringUtils ;
2627import org .apache .cloudstack .affinity .AffinityGroup ;
2728import org .apache .cloudstack .affinity .AffinityGroupService ;
2829import org .apache .cloudstack .affinity .dao .AffinityGroupDao ;
@@ -236,7 +237,7 @@ public List<DedicatedResourceVO> dedicateZone(final Long zoneId, final Long doma
236237 @ Override
237238 public List <DedicatedResourceVO > doInTransaction (TransactionStatus status ) {
238239 // find or create the affinity group by name under this account/domain
239- AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal );
240+ AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal , DedicatedResources . Type . Zone );
240241 if (group == null ) {
241242 logger .error ("Unable to dedicate zone due to, failed to create dedication affinity group" );
242243 throw new CloudRuntimeException ("Failed to dedicate zone. Please contact Cloud Support." );
@@ -372,10 +373,10 @@ public List<DedicatedResourceVO> dedicatePod(final Long podId, final Long domain
372373 @ Override
373374 public List <DedicatedResourceVO > doInTransaction (TransactionStatus status ) {
374375 // find or create the affinity group by name under this account/domain
375- AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal );
376+ AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal , DedicatedResources . Type . Pod );
376377 if (group == null ) {
377- logger .error ("Unable to dedicate zone due to, failed to create dedication affinity group" );
378- throw new CloudRuntimeException ("Failed to dedicate zone . Please contact Cloud Support." );
378+ logger .error ("Unable to dedicate pod due to, failed to create dedication affinity group" );
379+ throw new CloudRuntimeException ("Failed to dedicate pod . Please contact Cloud Support." );
379380 }
380381 DedicatedResourceVO dedicatedResource = new DedicatedResourceVO (null , podId , null , null , null , null , group .getId ());
381382 try {
@@ -485,10 +486,10 @@ public List<DedicatedResourceVO> dedicateCluster(final Long clusterId, final Lon
485486 @ Override
486487 public List <DedicatedResourceVO > doInTransaction (TransactionStatus status ) {
487488 // find or create the affinity group by name under this account/domain
488- AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal );
489+ AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal , DedicatedResources . Type . Cluster );
489490 if (group == null ) {
490- logger .error ("Unable to dedicate zone due to, failed to create dedication affinity group" );
491- throw new CloudRuntimeException ("Failed to dedicate zone . Please contact Cloud Support." );
491+ logger .error ("Unable to dedicate cluster due to, failed to create dedication affinity group" );
492+ throw new CloudRuntimeException ("Failed to dedicate cluster . Please contact Cloud Support." );
492493 }
493494 DedicatedResourceVO dedicatedResource = new DedicatedResourceVO (null , null , clusterId , null , null , null , group .getId ());
494495 try {
@@ -582,10 +583,10 @@ public List<DedicatedResourceVO> dedicateHost(final Long hostId, final Long doma
582583 @ Override
583584 public List <DedicatedResourceVO > doInTransaction (TransactionStatus status ) {
584585 // find or create the affinity group by name under this account/domain
585- AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal );
586+ AffinityGroup group = findOrCreateDedicatedAffinityGroup (domainId , accountIdFinal , DedicatedResources . Type . Host );
586587 if (group == null ) {
587- logger .error ("Unable to dedicate zone due to, failed to create dedication affinity group" );
588- throw new CloudRuntimeException ("Failed to dedicate zone . Please contact Cloud Support." );
588+ logger .error ("Unable to dedicate host due to, failed to create dedication affinity group" );
589+ throw new CloudRuntimeException ("Failed to dedicate host . Please contact Cloud Support." );
589590 }
590591 DedicatedResourceVO dedicatedResource = new DedicatedResourceVO (null , null , null , hostId , null , null , group .getId ());
591592 try {
@@ -607,7 +608,7 @@ public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
607608
608609 }
609610
610- private AffinityGroup findOrCreateDedicatedAffinityGroup (Long domainId , Long accountId ) {
611+ private AffinityGroup findOrCreateDedicatedAffinityGroup (Long domainId , Long accountId , DedicatedResources . Type dedicatedResource ) {
611612 if (domainId == null ) {
612613 return null ;
613614 }
@@ -624,24 +625,25 @@ private AffinityGroup findOrCreateDedicatedAffinityGroup(Long domainId, Long acc
624625 if (group != null ) {
625626 return group ;
626627 }
627- // default to a groupname with account/domain information
628- affinityGroupName = "DedicatedGrp-" + accountName ;
629628
629+ // defaults to a groupName with resourceType and account/domain information
630+ affinityGroupName = String .format ("Dedicated%sGrp-%s" , dedicatedResource , accountName );
630631 } else {
631632 // domain level group
632633 group = _affinityGroupDao .findDomainLevelGroupByType (domainId , "ExplicitDedication" );
633634 if (group != null ) {
634635 return group ;
635636 }
636- // default to a groupname with account/domain information
637+
638+ // defaults to a groupName with resourceType and account/domain information
637639 String domainName = _domainDao .findById (domainId ).getName ();
638- affinityGroupName = "DedicatedGrp -domain-" + domainName ;
640+ affinityGroupName = String . format ( "Dedicated%sGrp -domain-%s" , dedicatedResource , domainName ) ;
639641 }
640642
641- group = _affinityGroupService .createAffinityGroup (accountName , null , domainId , affinityGroupName , "ExplicitDedication" , "dedicated resources group" );
643+ String description = String .format ("Dedicated %s group" , StringUtils .lowerCase (dedicatedResource .toString ()));
644+ group = _affinityGroupService .createAffinityGroup (accountName , null , domainId , affinityGroupName , "ExplicitDedication" , description );
642645
643646 return group ;
644-
645647 }
646648
647649 private List <UserVmVO > getVmsOnHost (long hostId ) {
0 commit comments