@@ -493,10 +493,13 @@ private synchronized Answer execute(LoadBalancerConfigCommand cmd, int numRetrie
493493 throw new ExecutionException ("Failed to delete the binding between the virtual server: " + nsVirtualServerName + " and service:" + nsServiceName + " due to" + apiCallResult .message );
494494 }
495495
496- // delete the service
497- apiCallResult = com .citrix .netscaler .nitro .resource .config .basic .service .delete (_netscalerService , nsServiceName );
498- if (apiCallResult .errorcode != 0 ) {
499- throw new ExecutionException ("Failed to delete service: " + nsServiceName + " due to " + apiCallResult .message );
496+ // check if service is bound to any other virtual server
497+ if (!isServiceBoundToVirtualServer (nsServiceName )) {
498+ // no lb virtual servers are bound to this service so delete it
499+ apiCallResult = com .citrix .netscaler .nitro .resource .config .basic .service .delete (_netscalerService , nsServiceName );
500+ if (apiCallResult .errorcode != 0 ) {
501+ throw new ExecutionException ("Failed to delete service: " + nsServiceName + " due to " + apiCallResult .message );
502+ }
500503 }
501504
502505 // delete the server if there is no associated services
@@ -531,21 +534,12 @@ private synchronized Answer execute(LoadBalancerConfigCommand cmd, int numRetrie
531534 String nsServerName = svc .get_servername ();
532535
533536 // check if service is bound to any other virtual server
534- boolean deleteService = true ;
535- lbvserver [] lbservers = lbvserver .get (_netscalerService );
536- for (lbvserver vserver : lbservers ) {
537- filtervalue [] filter = new filtervalue [1 ];
538- filter [0 ] = new filtervalue ("servicename" , serviceName );
539- lbvserver_service_binding [] result = (lbvserver_service_binding []) lbvserver_service_binding .get_filtered (_netscalerService , vserver .get_name (), filter );
540- if (result != null && result .length > 0 ) {
541- deleteService = false ;
542- break ;
543- }
544- }
545-
546- if (deleteService ) {
537+ if (!isServiceBoundToVirtualServer (serviceName )) {
547538 // no lb virtual servers are bound to this service so delete it
548- com .citrix .netscaler .nitro .resource .config .basic .service .delete (_netscalerService , serviceName );
539+ apiCallResult = com .citrix .netscaler .nitro .resource .config .basic .service .delete (_netscalerService , serviceName );
540+ if (apiCallResult .errorcode != 0 ) {
541+ throw new ExecutionException ("Failed to delete service: " + serviceName + " due to " + apiCallResult .message );
542+ }
549543 }
550544
551545 //delete the server if no more services attached
@@ -1080,6 +1074,23 @@ private lbvserver getVirtualServerIfExisits(String lbVServerName ) throws Execut
10801074 }
10811075 }
10821076
1077+ private boolean isServiceBoundToVirtualServer (String serviceName ) throws ExecutionException {
1078+ try {
1079+ lbvserver [] lbservers = lbvserver .get (_netscalerService );
1080+ for (lbvserver vserver : lbservers ) {
1081+ filtervalue [] filter = new filtervalue [1 ];
1082+ filter [0 ] = new filtervalue ("servicename" , serviceName );
1083+ lbvserver_service_binding [] result = (lbvserver_service_binding []) lbvserver_service_binding .get_filtered (_netscalerService , vserver .get_name (), filter );
1084+ if (result != null && result .length > 0 ) {
1085+ return true ;
1086+ }
1087+ }
1088+ return false ;
1089+ } catch (Exception e ) {
1090+ throw new ExecutionException ("Failed to verify service " + serviceName + " is bound to any virtual server due to " + e .getMessage ());
1091+ }
1092+ }
1093+
10831094 private boolean nsServiceExists (String serviceName ) throws ExecutionException {
10841095 try {
10851096 if (com .citrix .netscaler .nitro .resource .config .basic .service .get (_netscalerService , serviceName ) != null ) {
0 commit comments