[NSX] Address SonarCloud Bugs (#8341)

* [NSX] Address SonarCloud Bugs

* Fix NSX API connection issues
This commit is contained in:
Nicolas Vazquez 2023-12-14 09:38:24 -03:00 committed by GitHub
parent 7814829a48
commit 4457c62ad3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 10 deletions

View File

@ -23,7 +23,6 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -32,6 +31,7 @@ import com.cloud.network.VirtualRouterProvider;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
@ -287,7 +287,7 @@ public class CreateNetworkOfferingCmd extends BaseCmd {
}
public Boolean isForNsx() {
return !Objects.isNull(forNsx) && forNsx;
return BooleanUtils.isTrue(forNsx);
}
public String getNsxMode() {

View File

@ -23,7 +23,6 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -33,6 +32,7 @@ import com.cloud.network.VirtualRouterProvider;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
@ -164,7 +164,7 @@ public class CreateVPCOfferingCmd extends BaseAsyncCreateCmd {
}
public Boolean isForNsx() {
return !Objects.isNull(forNsx) && forNsx;
return BooleanUtils.isTrue(forNsx);
}
public String getNsxMode() {

View File

@ -18,8 +18,8 @@ package org.apache.cloudstack.api.command.user.address;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang.BooleanUtils;
import org.apache.log4j.Logger;
import org.apache.cloudstack.acl.RoleType;
@ -182,7 +182,7 @@ public class ListPublicIpAddressesCmd extends BaseListRetrieveOnlyResourceCountC
}
public boolean getForSystemVMs() {
return !Objects.isNull(forSystemVMs) && forSystemVMs;
return BooleanUtils.isTrue(forSystemVMs);
}
/////////////////////////////////////////////////////

View File

@ -371,8 +371,12 @@ public class NsxResource implements ServerResource {
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(),
cmd.getVpcId(), cmd.getNetworkId());
try {
Thread.sleep(30*1000);
Thread.sleep(30 * 1000L);
nsxApiClient.deleteSegment(cmd.getZoneId(), cmd.getDomainId(), cmd.getAccountId(), cmd.getVpcId(), cmd.getNetworkId(), segmentName);
} catch (InterruptedException | ThreadDeath e) {
LOGGER.error("Thread interrupted", e);
Thread.currentThread().interrupt();
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
} catch (Exception e) {
LOGGER.error(String.format("Failed to delete NSX segment: %s", segmentName));
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));

View File

@ -97,6 +97,7 @@ import static org.apache.cloudstack.utils.NsxControllerUtils.getLoadBalancerAlgo
public class NsxApiClient {
protected ApiClient apiClient;
protected Function<Class<? extends Service>, Service> nsxService;
public static final int RESPONSE_TIMEOUT_SECONDS = 60;
@ -187,7 +188,7 @@ public class NsxApiClient {
.register(Configuration.STUB_CONFIG_CFG, stubConfig)
.register(RestProtocol.REST_REQUEST_AUTHENTICATOR_CFG, new BasicAuthenticationAppender());
Configuration config = configBuilder.build();
ApiClient apiClient = ApiClients.newRestClient(controllerUrl, config);
apiClient = ApiClients.newRestClient(controllerUrl, config);
nsxService = apiClient::createStub;
}

View File

@ -104,10 +104,11 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
}
NetworkVO network = (NetworkVO) super.design(offering, plan, userSpecified, name, vpcId, owner);
network.setBroadcastDomainType(Networks.BroadcastDomainType.NSX);
if (network == null) {
return null;
}
network.setBroadcastDomainType(Networks.BroadcastDomainType.NSX);
if (userSpecified != null) {
if ((userSpecified.getIp6Cidr() == null && userSpecified.getIp6Gateway() != null) || (
userSpecified.getIp6Cidr() != null && userSpecified.getIp6Gateway() == null)) {

View File

@ -106,6 +106,7 @@ public class NsxPublicNetworkGuru extends PublicNetworkGuru {
throw new CloudRuntimeException(err);
}
Long vpcId = ipAddress.getVpcId();
boolean isForVpc = vpcId != null;
if (vpcId == null) {
// TODO: Pass network.getId() to support isolated networks
throw new CloudRuntimeException("TODO: Add support for isolated networks public network");
@ -133,7 +134,6 @@ public class NsxPublicNetworkGuru extends PublicNetworkGuru {
long accountId = vpc.getAccountId();
long domainId = vpc.getDomainId();
long dataCenterId = vpc.getZoneId();
boolean isForVpc = true;
long resourceId = isForVpc ? vpc.getId() : network.getId();
Network.Service[] services = { Network.Service.SourceNat };
boolean sourceNatEnabled = vpcOfferingServiceMapDao.areServicesSupportedByVpcOffering(vpc.getVpcOfferingId(), services);