示例#1
0
文件: EC2.cs 项目: Trov/Document.AWS
        public static List<SecurityGroup> GetAllSecurityGroups(AWSEnvironment environment, RegionEndpoint region)
        {
            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionSecurityGroups = ec2Client.DescribeSecurityGroups();
                return regionSecurityGroups.SecurityGroups;
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllSecurityGroups() : {aex.Message}");
            }

            return null;
        }
示例#2
0
文件: RDS.cs 项目: Trov/Document.AWS
        public static List<AWSInstance> GetAllRDSInstances(AWSEnvironment environment, RegionEndpoint region)
        {
            var dbInstances = new List<AWSInstance>();

            try
            {
                var rdsClient = AWSClientFactory.CreateAmazonRDSClient(environment.AccessKeyID, environment.SecretAccessKey, region);
                var rdsInstances = rdsClient.DescribeDBInstances();
                foreach (var dbInstance in rdsInstances.DBInstances)
                {
                    var rdsInstance = new AWSInstance
                    {
                        Role = dbInstance.Engine.ToUpper(),
                        Name = dbInstance.DBInstanceIdentifier,
                        InstanceType = dbInstance.DBInstanceClass,
                        PubliclyAccessible = dbInstance.PubliclyAccessible,
                        Encrypted = dbInstance.StorageEncrypted,
                        Platform = Platforms.Linux,
                        State = States.Running,
                        IsRDS = true
                    };

                    if (rdsInstance.Role == "MSSQL")
                    {
                        rdsInstance.Platform = Platforms.Windows;
                    }

                    foreach (var subnet in dbInstance.DBSubnetGroup.Subnets)
                    {
                        rdsInstance.SubnetIDs.Add(subnet.SubnetIdentifier);
                    }

                    foreach (var vpcSecurityGroup in dbInstance.VpcSecurityGroups)
                    {
                        rdsInstance.SecurityGroupIDs.Add(vpcSecurityGroup.VpcSecurityGroupId);
                    }

                    dbInstances.Add(rdsInstance);
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllRDSInstances() : {aex.Message}");
            }

            return dbInstances;
        }
示例#3
0
文件: EC2.cs 项目: Trov/Document.AWS
        public static List<AWSSubnet> GetAllAWSSubnets(AWSEnvironment environment, RegionEndpoint region)
        {
            var subnets = new List<AWSSubnet>();
            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionSubnets = ec2Client.DescribeSubnets();
                foreach (var subnet in regionSubnets.Subnets)
                {
                    subnets.Add(new AWSSubnet(subnet) { Public = IsSubnetPublic(ec2Client, subnet.SubnetId) });
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllAWSSubnets() : {aex.Message}");
            }

            return subnets;
        }
示例#4
0
文件: EC2.cs 项目: Trov/Document.AWS
        public static List<AWSInstance> GetAllEC2Instances(AWSEnvironment environment, RegionEndpoint region)
        {
            var servers = new List<AWSInstance>();

            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionInstances = ec2Client.DescribeInstances();
                foreach (var reservations in regionInstances.Reservations)
                {
                    foreach (var instance in reservations.Instances)
                    {
                        var server = new AWSInstance
                        {
                            InstanceID = instance.InstanceId,
                            InstanceType = instance.InstanceType
                        };

                        foreach (var blockDevice in instance.BlockDeviceMappings)
                        {
                            server.Volumes.Add(GetInstanceVolumes(environment, region, blockDevice));
                        }

                        server.Platform = string.IsNullOrEmpty(instance.Platform) ? Platforms.Linux : Platforms.Windows;
                        foreach (var securityGroup in instance.SecurityGroups)
                        {
                            server.SecurityGroupIDs.Add(securityGroup.GroupId);
                        }

                        server.State = GetInstanceState(instance.State);
                        foreach (var tag in instance.Tags)
                        {
                            if (tag.Key == "Name")
                            {
                                server.Name = tag.Value;
                            }

                            if (tag.Key == "Description")
                            {
                                server.Description = tag.Value;
                            }

                            if (tag.Key == "Owner")
                            {
                                server.Owner = tag.Value;
                            }

                            if (tag.Key == "Role")
                            {
                                server.Role = tag.Value;
                            }
                        }

                        foreach (var networkInterface in instance.NetworkInterfaces)
                        {
                            foreach (var privateIP in networkInterface.PrivateIpAddresses)
                            {
                                if (!string.IsNullOrEmpty(privateIP.Association?.PublicIp))
                                {
                                    server.PublicIPs.Add(privateIP.Association.PublicIp);
                                }

                                server.PrivateIPs.Add(privateIP.PrivateIpAddress);
                            }
                        }

                        server.VPCID = instance.VpcId;
                        server.SubnetIDs.Add(instance.SubnetId);
                        servers.Add(server);
                    }
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetAllEC2Instances() : {aex.Message}");
            }

            return servers;
        }
示例#5
0
文件: EC2.cs 项目: Trov/Document.AWS
        private static Volume GetInstanceVolumes(AWSEnvironment environment, RegionEndpoint region, InstanceBlockDeviceMapping blockDevice)
        {
            try
            {
                var ec2Client = AWSClientFactory.CreateAmazonEC2Client(environment.AccessKeyID, environment.SecretAccessKey, region);
                var regionVolumes = ec2Client.DescribeVolumes();
                foreach (var ec2Volume in regionVolumes.Volumes)
                {
                    if (ec2Volume.State == VolumeState.InUse && ec2Volume.VolumeId == blockDevice.Ebs.VolumeId)
                    {
                        return ec2Volume;
                    }
                }
            }
            catch (AmazonEC2Exception aex)
            {
                Logger.Log(LogLevel.Error, aex, $"AmazonEC2Exception in GetInstanceVolumes() : {aex.Message}");
            }

            return null;
        }