AliyunCore.cs 7.0 KB

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