AliyunCore.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using dodohold.core;
  2. using Dataoke;
  3. using Tea;
  4. using AlibabaCloud.SDK.BssOpenApi20171214.Models;
  5. using AlibabaCloud.SDK.Ecs20140526.Models;
  6. using AlibabaCloud.SDK.Vpc20160428.Models;
  7. using TencentCloud.Dbbrain.V20210527.Models;
  8. using Org.BouncyCastle.Bcpg.OpenPgp;
  9. using TencentCloud.Tke.V20180525.Models;
  10. namespace molilian.core
  11. {
  12. public partial class AliyunCore
  13. {
  14. private AliyunPlus plus;
  15. private AliyunAccountDTO _account;
  16. public AliyunCore(int id)
  17. {
  18. _account = new DBContext.Table("aliyun_pool").Get<AliyunAccountDTO>(id);
  19. if (_account == null) throw new Exception("无效账号");
  20. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  21. }
  22. public bool EcsCreateNetworkInterface(string region, string instanceId)
  23. {
  24. try
  25. {
  26. //查询实例信息
  27. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  28. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  29. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  30. //申请弹性网卡
  31. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  32. string networkInterfaceId = response.Body.NetworkInterfaceId;
  33. string privateIpAddress = response.Body.PrivateIpAddress;
  34. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  35. //绑定网卡
  36. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  37. //申请并绑定公网IP
  38. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  39. return true;
  40. }
  41. catch (Exception ex)
  42. {
  43. throw new Exception(ex.Message);
  44. }
  45. }
  46. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count)
  47. {
  48. try
  49. {
  50. //查询实例信息
  51. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  52. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  53. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  54. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  55. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  56. {
  57. if ("Secondary".Equals(e.Type))
  58. {
  59. #if DEBUG
  60. var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  61. var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  62. foreach (var privateIp in PrivateIpAddressList)
  63. {
  64. if (privateIp.Primary == true) continue;
  65. var privateIpAddress = privateIp.PrivateIpAddress;
  66. //申请公网ip
  67. var eip = plus.AllocateEipAddress(region);
  68. var allocationId = eip.Body.AllocationId;
  69. string eipAddress = eip.Body.EipAddress;
  70. //绑定公网ip
  71. var AssociateEipAddress =
  72. plus.AssociateEipAddress(
  73. region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  74. }
  75. #else
  76. //添加辅助私网
  77. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  78. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  79. foreach (var privateIpAddress in PrivateIpAddressList)
  80. {
  81. //申请公网ip
  82. var eip = plus.AllocateEipAddress(region);
  83. var allocationId = eip.Body.AllocationId;
  84. string eipAddress = eip.Body.EipAddress;
  85. //绑定公网ip
  86. var AssociateEipAddress =
  87. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  88. }
  89. #endif
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. catch (Exception ex)
  96. {
  97. throw new Exception(ex.Message);
  98. }
  99. }
  100. /// <summary>
  101. /// 申请并绑定公网IP
  102. /// </summary>
  103. /// <param name="region"></param>
  104. /// <param name="instanceId"></param>
  105. /// <param name="privateIpAddress"></param>
  106. /// <returns></returns>
  107. /// <exception cref="Exception"></exception>
  108. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  109. {
  110. try
  111. {
  112. //申请公网ip
  113. var eip = plus.AllocateEipAddress(region);
  114. var allocationId = eip.Body.AllocationId;
  115. string eipAddress = eip.Body.EipAddress;
  116. //绑定公网ip
  117. var AssociateEipAddress =
  118. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  119. return true;
  120. }
  121. catch (Exception ex)
  122. {
  123. throw new Exception(ex.Message);
  124. }
  125. }
  126. public bool ReleaseEipAddress(string region, string allocationId)
  127. {
  128. try
  129. {
  130. plus.ReleaseEipAddress(region, allocationId);
  131. return true;
  132. }
  133. catch (Exception ex)
  134. {
  135. throw new Exception(ex.Message);
  136. }
  137. }
  138. }
  139. }