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(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); } } /// /// 申请并绑定公网IP /// /// /// /// /// /// 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); } } } }