AliyunCore.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 QueryAccountBalanceResponse QueryAccountBalance()
  23. {
  24. return plus.QueryAccountBalance();
  25. }
  26. public bool EcsCreateNetworkInterface(string region, string instanceId)
  27. {
  28. try
  29. {
  30. //查询实例信息
  31. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  32. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  33. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  34. //申请弹性网卡
  35. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  36. string networkInterfaceId = response.Body.NetworkInterfaceId;
  37. string privateIpAddress = response.Body.PrivateIpAddress;
  38. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  39. //绑定网卡
  40. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  41. //申请并绑定公网IP
  42. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  43. return true;
  44. }
  45. catch (Exception ex)
  46. {
  47. throw new Exception(ex.Message);
  48. }
  49. }
  50. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count)
  51. {
  52. try
  53. {
  54. //查询实例信息
  55. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  56. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  57. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  58. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  59. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  60. {
  61. if ("Secondary".Equals(e.Type))
  62. {
  63. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  64. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  65. //foreach (var privateIp in PrivateIpAddressList)
  66. //{
  67. // if (privateIp.Primary == true) continue;
  68. // var privateIpAddress = privateIp.PrivateIpAddress;
  69. // //申请公网ip
  70. // var eip = plus.AllocateEipAddress(region);
  71. // var allocationId = eip.Body.AllocationId;
  72. // string eipAddress = eip.Body.EipAddress;
  73. // //绑定公网ip
  74. // var AssociateEipAddress =
  75. // plus.AssociateEipAddress(
  76. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  77. //}
  78. //添加辅助私网
  79. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  80. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  81. foreach (var privateIpAddress in PrivateIpAddressList)
  82. {
  83. //申请公网ip
  84. var eip = plus.AllocateEipAddress(region);
  85. var allocationId = eip.Body.AllocationId;
  86. string eipAddress = eip.Body.EipAddress;
  87. //绑定公网ip
  88. var AssociateEipAddress =
  89. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  90. }
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. catch (Exception ex)
  97. {
  98. throw new Exception(ex.Message);
  99. }
  100. }
  101. /// <summary>
  102. /// 申请并绑定公网IP
  103. /// </summary>
  104. /// <param name="region"></param>
  105. /// <param name="instanceId"></param>
  106. /// <param name="privateIpAddress"></param>
  107. /// <returns></returns>
  108. /// <exception cref="Exception"></exception>
  109. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  110. {
  111. try
  112. {
  113. //申请公网ip
  114. var eip = plus.AllocateEipAddress(region);
  115. var allocationId = eip.Body.AllocationId;
  116. string eipAddress = eip.Body.EipAddress;
  117. //绑定公网ip
  118. var AssociateEipAddress =
  119. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  120. return true;
  121. }
  122. catch (Exception ex)
  123. {
  124. throw new Exception(ex.Message);
  125. }
  126. }
  127. public bool ReleaseEipAddress(string region, string allocationId)
  128. {
  129. try
  130. {
  131. plus.ReleaseEipAddress(region, allocationId);
  132. return true;
  133. }
  134. catch (Exception ex)
  135. {
  136. throw new Exception(ex.Message);
  137. }
  138. }
  139. }
  140. }