100100import static org .apache .cloudstack .utils .NsxControllerUtils .getLoadBalancerName ;
101101import static org .apache .cloudstack .utils .NsxControllerUtils .getLoadBalancerAlgorithm ;
102102import static org .apache .cloudstack .utils .NsxControllerUtils .getActiveMonitorProfileName ;
103+ import static org .apache .cloudstack .utils .NsxControllerUtils .getTier1GatewayName ;
103104
104105public class NsxApiClient {
105106
@@ -429,6 +430,10 @@ public void createSegment(String segmentName, String tier1GatewayName, String ga
429430 public void deleteSegment (long zoneId , long domainId , long accountId , Long vpcId , long networkId , String segmentName ) {
430431 try {
431432 removeSegmentDistributedFirewallRules (segmentName );
433+ if (Objects .isNull (vpcId )) {
434+ String t1GatewayName = getTier1GatewayName (domainId , accountId , zoneId , networkId , false );
435+ deleteLoadBalancer (getLoadBalancerName (t1GatewayName ));
436+ }
432437 removeSegment (segmentName );
433438 DhcpRelayConfigs dhcpRelayConfig = (DhcpRelayConfigs ) nsxService .apply (DhcpRelayConfigs .class );
434439 String dhcpRelayConfigId = NsxControllerUtils .getNsxDhcpRelayConfigId (zoneId , domainId , accountId , vpcId , networkId );
@@ -445,9 +450,15 @@ public void deleteSegment(long zoneId, long domainId, long accountId, Long vpcId
445450 protected void removeSegment (String segmentName ) {
446451 LOGGER .debug (String .format ("Removing the segment with ID %s" , segmentName ));
447452 Segments segmentService = (Segments ) nsxService .apply (Segments .class );
448- Segment segment = segmentService .get (segmentName );
449- if (segment == null ) {
450- LOGGER .error (String .format ("The segment with ID %s is not found, skipping removal" , segmentName ));
453+ String errMsg = String .format ("The segment with ID %s is not found, skipping removal" , segmentName );
454+ try {
455+ Segment segment = segmentService .get (segmentName );
456+ if (segment == null ) {
457+ LOGGER .warn (errMsg );
458+ return ;
459+ }
460+ } catch (Exception e ) {
461+ LOGGER .warn (errMsg );
451462 return ;
452463 }
453464 String siteId = getDefaultSiteId ();
0 commit comments