|
|
@@ -13,6 +13,8 @@ using static AlibabaCloud.SDK.Vpc20160428.Models.DescribeEipAddressesResponseBod
|
|
|
using AlibabaCloud.SDK.ImageSearch20210501.Models;
|
|
|
using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeNetworkInterfacesResponseBody.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets;
|
|
|
using TencentCloud.Cam.V20190116.Models;
|
|
|
+using static AlibabaCloud.SDK.Vpc20160428.Models.DescribeNetworkAclAttributesResponseBody;
|
|
|
+using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeNetworkInterfaceAttributeResponseBody;
|
|
|
|
|
|
namespace molilian.core
|
|
|
{
|
|
|
@@ -184,14 +186,42 @@ namespace molilian.core
|
|
|
var privateIps = networkInterface?.PrivateIpSets;
|
|
|
|
|
|
var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
|
|
|
- if (taraget == null) break;
|
|
|
+ if (taraget == null) continue;
|
|
|
|
|
|
//找到当前公网IP
|
|
|
string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
|
|
|
- if (string.IsNullOrEmpty(currentPublicIp)) throw new Exception("没有找到公网IP");
|
|
|
+ DescribeEipAddressesResponseBodyEipAddressesEipAddress currentEip;
|
|
|
+ if (string.IsNullOrEmpty(currentPublicIp))
|
|
|
+ {
|
|
|
+ var currentEips = DescribeEipsAddresse(region, string.Empty);
|
|
|
+ currentEip = currentEips?.Where(e => privateIp.Equals(e.PrivateIpAddress))?.FirstOrDefault();
|
|
|
+ currentPublicIp = currentEip?.IpAddress;
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(currentPublicIp))
|
|
|
+ {
|
|
|
+ var emptyPrivateEips = currentEips?.Where(e => string.IsNullOrEmpty(e.PrivateIpAddress));
|
|
|
+ foreach (var emptyEip in emptyPrivateEips)
|
|
|
+ {
|
|
|
+ var body = DescribeNetworkInterfaceAttribute(region, emptyEip.InstanceId);
|
|
|
+ if (body != null)
|
|
|
+ {
|
|
|
+ if (body.PrivateIpAddress == privateIp)
|
|
|
+ {
|
|
|
+ currentEip = emptyEip;
|
|
|
+ currentPublicIp = emptyEip?.IpAddress;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
|
|
|
+ }
|
|
|
+ if (currentEip == null) throw new Exception("没有找到 EIP");
|
|
|
|
|
|
//第1步、获取当前的公网EIP
|
|
|
- var currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
|
|
|
var currentAllocationId = currentEip.AllocationId;
|
|
|
|
|
|
//第2步、申请新的公网ip
|
|
|
@@ -368,13 +398,14 @@ namespace molilian.core
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipAddresses(string region, string eipAddress)
|
|
|
+
|
|
|
+ public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var response = plus.DescribeEipAddresses(region, eipAddress);
|
|
|
- var list = response?.Body?.EipAddresses?.EipAddress;
|
|
|
- return list;
|
|
|
+ var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
|
|
|
+ return eip;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -382,13 +413,28 @@ namespace molilian.core
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
|
|
|
+
|
|
|
+ public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipsAddresse(string region, string eipAddress)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var response = plus.DescribeEipAddresses(region, eipAddress);
|
|
|
- var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
|
|
|
- return eip;
|
|
|
+ var eips = response?.Body?.EipAddresses?.EipAddress;
|
|
|
+ return eips;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new Exception(ex.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public DescribeNetworkInterfaceAttributeResponseBody DescribeNetworkInterfaceAttribute(string region, string networkInterfaceId)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var response = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
|
|
|
+ var body = response?.Body;
|
|
|
+ return body;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -396,6 +442,7 @@ namespace molilian.core
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public bool ReleaseEipAddress(string region, string allocationId)
|
|
|
{
|
|
|
try
|