Skip to content

Commit

Permalink
#167 update EipServiceImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
HoweChen committed May 26, 2020
1 parent 6ef49d9 commit e9394c7
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public List<CoreAllocateEipResponseDto> allocateEipAddress(List<CoreAllocateEipR
AllocateEipAddressRequest allocateEipAddressRequest = requestDto.toSdk();
AllocateEipAddressResponse createEipResponse = this.acsClientStub.request(client, allocateEipAddressRequest);

// wait till EIP as allocated and available
checkIfEipAvailable(client, regionId, createEipResponse.getAllocationId());

// if cbpName is empty, create CBP
String cbpId = StringUtils.EMPTY;
if (!StringUtils.isEmpty(requestDto.getName())) {
Expand Down Expand Up @@ -107,6 +110,11 @@ public List<CoreAllocateEipResponseDto> allocateEipAddress(List<CoreAllocateEipR
return resultList;
}

public void checkIfEipAvailable(IAcsClient client, String regionId, String allocationId) throws PluginException, AliCloudException {
Function<?, Boolean> func = o -> ifEipInStatus(client, regionId, allocationId, EipStatus.Available);
PluginTimer.runTask(new PluginTimerTask(func));
}


@Override
public List<CoreReleaseEipResponseDto> releaseEipAddress(List<CoreReleaseEipRequestDto> requestDtoList) {
Expand Down Expand Up @@ -144,6 +152,9 @@ public List<CoreReleaseEipResponseDto> releaseEipAddress(List<CoreReleaseEipRequ
removeFromCBP(client, requestDto.getAllocationId(), regionId, foundCBPId);
}

// release CBP if possible
releaseCBPIfPossible(client, regionId, foundCBPId);

// release eip
ReleaseEipAddressRequest request = requestDto.toSdk();
ReleaseEipAddressResponse response = this.acsClientStub.request(client, request, regionId);
Expand All @@ -166,6 +177,27 @@ public List<CoreReleaseEipResponseDto> releaseEipAddress(List<CoreReleaseEipRequ
return resultList;
}

private void releaseCBPIfPossible(IAcsClient client, String regionId, String foundCBPId) throws PluginException, AliCloudException {
DescribeCommonBandwidthPackagesRequest request = new DescribeCommonBandwidthPackagesRequest();
request.setBandwidthPackageId(foundCBPId);

final DescribeCommonBandwidthPackagesResponse response = acsClientStub.request(client, request, regionId);

if (response.getCommonBandwidthPackages().isEmpty()) {
throw new PluginException(String.format("Cannot find CBP by given id: [%s]", foundCBPId));
}

final DescribeCommonBandwidthPackagesResponse.CommonBandwidthPackage cbp = response.getCommonBandwidthPackages().get(0);

// if there is no EIP bound, delete CBP
if (cbp.getPublicIpAddresses().isEmpty()) {
DeleteCommonBandwidthPackageRequest deleteRequest = new DeleteCommonBandwidthPackageRequest();
deleteRequest.setBandwidthPackageId(foundCBPId);
acsClientStub.request(client, deleteRequest, regionId);
}

}

private String queryCBPByEip(IAcsClient client, String regionId, String allocationId) throws PluginException, AliCloudException {
DescribeEipAddressesRequest request = new DescribeEipAddressesRequest();
request.setAllocationId(allocationId);
Expand Down Expand Up @@ -437,13 +469,13 @@ private DescribeEipAddressesResponse.EipAddress queryEipByAddress(IAcsClient cli
return response.getEipAddresses().get(0);
}

private boolean ifEipInStatus(IAcsClient client, String regionId, String allocationId, EipStatus status) throws AliCloudException {
private boolean ifEipInStatus(IAcsClient client, String regionId, String allocationId, EipStatus status) throws PluginException, AliCloudException {
final DescribeEipAddressesResponse response = retrieveEipByAllocationId(client, regionId, allocationId, false);

return StringUtils.equals(status.toString(), response.getEipAddresses().get(0).getStatus());
}

private boolean ifEipNotInStatus(IAcsClient client, String regionId, String allocationId, EipStatus status) throws AliCloudException {
private boolean ifEipNotInStatus(IAcsClient client, String regionId, String allocationId, EipStatus status) throws PluginException, AliCloudException {
final DescribeEipAddressesResponse response = retrieveEipByAllocationId(client, regionId, allocationId, false);

return !StringUtils.equals(status.toString(), response.getEipAddresses().get(0).getStatus());
Expand Down

0 comments on commit e9394c7

Please sign in to comment.