AliyunCore.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  60. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  61. //foreach (var privateIp in PrivateIpAddressList)
  62. //{
  63. // if (privateIp.Primary == true) continue;
  64. // var privateIpAddress = privateIp.PrivateIpAddress;
  65. // //申请公网ip
  66. // var eip = plus.AllocateEipAddress(region);
  67. // var allocationId = eip.Body.AllocationId;
  68. // string eipAddress = eip.Body.EipAddress;
  69. // //绑定公网ip
  70. // var AssociateEipAddress =
  71. // plus.AssociateEipAddress(
  72. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  73. //}
  74. //添加辅助私网
  75. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  76. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  77. foreach (var privateIpAddress in PrivateIpAddressList)
  78. {
  79. //申请公网ip
  80. var eip = plus.AllocateEipAddress(region);
  81. var allocationId = eip.Body.AllocationId;
  82. string eipAddress = eip.Body.EipAddress;
  83. //绑定公网ip
  84. var AssociateEipAddress =
  85. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  86. }
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. catch (Exception ex)
  93. {
  94. throw new Exception(ex.Message);
  95. }
  96. }
  97. /// <summary>
  98. /// 申请并绑定公网IP
  99. /// </summary>
  100. /// <param name="region"></param>
  101. /// <param name="instanceId"></param>
  102. /// <param name="privateIpAddress"></param>
  103. /// <returns></returns>
  104. /// <exception cref="Exception"></exception>
  105. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  106. {
  107. try
  108. {
  109. //申请公网ip
  110. var eip = plus.AllocateEipAddress(region);
  111. var allocationId = eip.Body.AllocationId;
  112. string eipAddress = eip.Body.EipAddress;
  113. //绑定公网ip
  114. var AssociateEipAddress =
  115. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  116. return true;
  117. }
  118. catch (Exception ex)
  119. {
  120. throw new Exception(ex.Message);
  121. }
  122. }
  123. public bool ReleaseEipAddress(string region, string allocationId)
  124. {
  125. try
  126. {
  127. plus.ReleaseEipAddress(region, allocationId);
  128. return true;
  129. }
  130. catch (Exception ex)
  131. {
  132. throw new Exception(ex.Message);
  133. }
  134. }
  135. }
  136. }