| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using dodohold.core;
- using Dataoke;
- using Tea;
- using AlibabaCloud.SDK.BssOpenApi20171214.Models;
- using AlibabaCloud.SDK.Ecs20140526.Models;
- using AlibabaCloud.SDK.Vpc20160428.Models;
- using TencentCloud.Dbbrain.V20210527.Models;
- using Org.BouncyCastle.Bcpg.OpenPgp;
- using TencentCloud.Tke.V20180525.Models;
- namespace molilian.core
- {
- public partial class AliyunCore
- {
- private AliyunPlus plus;
- private AliyunAccountDTO _account;
- public AliyunCore(int id)
- {
- _account = new DBContext.Table("aliyun_pool").Get<AliyunAccountDTO>(id);
- if (_account == null) throw new Exception("无效账号");
- plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
- }
- public QueryAccountBalanceResponse QueryAccountBalance()
- {
- return plus.QueryAccountBalance();
- }
- //public bool ChangePublicIp(string region, string instanceId, string privateIp)
- //{
- // try
- // {
- // //查询实例信息
- // var instance = plus.DescribeInstanceAttribute(region, instanceId);
- // var vswitchId = instance.Body.VpcAttributes.VSwitchId;
- // var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
- // //释放弹性网卡
- // ReleaseEipAddress("cn-beijing", allocationId);
- // //申请并绑定公网IP
- // EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
- // return true;
- // }
- // catch (Exception ex)
- // {
- // throw new Exception(ex.Message);
- // }
- //}
- public bool EcsCreateNetworkInterface(string region, string instanceId)
- {
- try
- {
- //查询实例信息
- var instance = plus.DescribeInstanceAttribute(region, instanceId);
- var vswitchId = instance.Body.VpcAttributes.VSwitchId;
- var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
- //申请弹性网卡
- var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
- string networkInterfaceId = response.Body.NetworkInterfaceId;
- string privateIpAddress = response.Body.PrivateIpAddress;
- if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
- //绑定网卡
- plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
- //申请并绑定公网IP
- EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
- return true;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count)
- {
- try
- {
- //查询实例信息
- var instance = plus.DescribeInstanceAttribute(region, instanceId);
- var vswitchId = instance.Body.VpcAttributes.VSwitchId;
- var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
- var response = plus.DescribeNetworkInterfaces(region, instanceId);
- foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
- {
- if ("Secondary".Equals(e.Type))
- {
- //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
- //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
- //foreach (var privateIp in PrivateIpAddressList)
- //{
- // if (privateIp.Primary == true) continue;
- // var privateIpAddress = privateIp.PrivateIpAddress;
- // //申请公网ip
- // var eip = plus.AllocateEipAddress(region);
- // var allocationId = eip.Body.AllocationId;
- // string eipAddress = eip.Body.EipAddress;
- // //绑定公网ip
- // var AssociateEipAddress =
- // plus.AssociateEipAddress(
- // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
- //}
- //添加辅助私网
- var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
- var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
- foreach (var privateIpAddress in PrivateIpAddressList)
- {
- //申请公网ip
- var eip = plus.AllocateEipAddress(region);
- var allocationId = eip.Body.AllocationId;
- string eipAddress = eip.Body.EipAddress;
- //绑定公网ip
- var AssociateEipAddress =
- plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
- }
- return true;
- }
- }
- return false;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- /// <summary>
- /// 申请并绑定公网IP
- /// </summary>
- /// <param name="region"></param>
- /// <param name="instanceId"></param>
- /// <param name="privateIpAddress"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
- {
- try
- {
- //申请公网ip
- var eip = plus.AllocateEipAddress(region);
- var allocationId = eip.Body.AllocationId;
- string eipAddress = eip.Body.EipAddress;
- //绑定公网ip
- var AssociateEipAddress =
- plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
- return true;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- public bool ReleaseEipAddress(string region, string allocationId)
- {
- try
- {
- plus.ReleaseEipAddress(region, allocationId);
- return true;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }
- }
|