3030import javax .inject .Inject ;
3131import javax .naming .ConfigurationException ;
3232
33- import org .apache .cloudstack .api .AddUcsManagerCmd ;
34- import org .apache .cloudstack .api .AssociateUcsProfileToBladeCmd ;
35- import org .apache .cloudstack .api .ListUcsBladeCmd ;
36- import org .apache .cloudstack .api .ListUcsManagerCmd ;
37- import org .apache .cloudstack .api .ListUcsProfileCmd ;
33+ import org .apache .cloudstack .api .*;
3834import org .apache .cloudstack .api .response .ListResponse ;
3935import org .apache .cloudstack .api .response .UcsBladeResponse ;
4036import org .apache .cloudstack .api .response .UcsManagerResponse ;
4137import org .apache .cloudstack .api .response .UcsProfileResponse ;
42- import org .apache .log4j .Logger ;
43- import org .apache .cloudstack .api .DeleteUcsManagerCmd ;
38+ import org .apache .log4j .Logger ;
4439
4540import com .cloud .configuration .Config ;
4641import com .cloud .configuration .dao .ConfigurationDao ;
@@ -296,7 +291,14 @@ private List<UcsProfile> getUcsProfiles(Long ucsMgrId) {
296291 UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
297292 String res = client .call (cmd );
298293 List <UcsProfile > profiles = UcsProfile .fromXmlString (res );
299- return profiles ;
294+ List <UcsProfile > unassociated = new ArrayList <UcsProfile >();
295+ for (UcsProfile p : profiles ) {
296+ if (isProfileAssociated (mgrvo .getId (), p .getDn ())) {
297+ continue ;
298+ }
299+ unassociated .add (p );
300+ }
301+ return unassociated ;
300302 }
301303
302304 @ Override
@@ -324,13 +326,25 @@ private String cloneProfile(Long ucsMgrId, String srcDn, String newProfileName)
324326 return xo .get ("outConfig.lsServer.dn" );
325327 }
326328
329+
327330 private boolean isProfileAssociated (Long ucsMgrId , String dn ) {
328331 UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
329332 UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
330333 String cookie = getCookie (ucsMgrId );
331334 String cmd = UcsCommands .configResolveDn (cookie , dn );
332335 String res = client .call (cmd );
333336 XmlObject xo = XmlObjectParser .parseFromString (res );
337+
338+ return xo .get ("outConfig.lsServer.assocState" ).equals ("associated" );
339+ }
340+
341+ private boolean isBladeAssociated (Long ucsMgrId , String dn ) {
342+ UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
343+ UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
344+ String cookie = getCookie (ucsMgrId );
345+ String cmd = UcsCommands .configResolveDn (cookie , dn );
346+ String res = client .call (cmd );
347+ XmlObject xo = XmlObjectParser .parseFromString (res );
334348 s_logger .debug (String .format ("association response is %s" , res ));
335349
336350 if (xo .get ("outConfig.computeBlade.association" ).equals ("none" )) {
@@ -363,7 +377,7 @@ public UcsBladeResponse associateProfileToBlade(AssociateUcsProfileToBladeCmd cm
363377 int count = 0 ;
364378 int timeout = 600 ;
365379 while (count < timeout ) {
366- if (isProfileAssociated (mgrvo .getId (), bvo .getDn ())) {
380+ if (isBladeAssociated (mgrvo .getId (), bvo .getDn ())) {
367381 break ;
368382 }
369383
@@ -504,6 +518,7 @@ public List<Class<?>> getCommands() {
504518 cmds .add (AddUcsManagerCmd .class );
505519 cmds .add (AssociateUcsProfileToBladeCmd .class );
506520 cmds .add (DeleteUcsManagerCmd .class );
521+ cmds .add (DisassociateUcsProfileCmd .class );
507522 return cmds ;
508523 }
509524
@@ -517,4 +532,21 @@ public void deleteUcsManager(Long id) {
517532 }
518533 ucsDao .remove (id );
519534 }
535+
536+ @ Override
537+ public UcsBladeResponse disassociateProfile (Long bladeId ) {
538+ UcsBladeVO blade = bladeDao .findById (bladeId );
539+ UcsManagerVO mgrvo = ucsDao .findById (blade .getUcsManagerId ());
540+ UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
541+ String cookie = getCookie (mgrvo .getId ());
542+ String cmd = UcsCommands .disassociateProfileFromBlade (cookie , blade .getProfileDn ());
543+ client .call (cmd );
544+ cmd = UcsCommands .deleteProfile (cookie , blade .getProfileDn ());
545+ client = new UcsHttpClient (mgrvo .getUrl ());
546+ client .call (cmd );
547+ blade .setProfileDn (null );
548+ bladeDao .update (blade .getId (), blade );
549+ UcsBladeResponse rsp = bladeVOToResponse (blade );
550+ return rsp ;
551+ }
520552}
0 commit comments