示例#1
0
        public AWSAvailabilityZone[] GetAvailabilityZones(IAmazonEC2 ec2Client = null)
        {
            var ret = new List<AWSAvailabilityZone>();

            var ec2 = ec2Client ?? GetEC2Client(); // might be passed in by instances calls...
            var request = new DescribeAvailabilityZonesRequest();
            var response = ec2.DescribeAvailabilityZones(request);

            foreach (var zone in response.AvailabilityZones)
            {
                var awsZone = new AWSAvailabilityZone() { Region = zone.RegionName, Name = zone.ZoneName, };
                ret.Add(awsZone);
            }

            return ret.ToArray();
        }
示例#2
0
        private AWSInstance GetAWSInstanceFromInstance(Instance instance, AWSVolume[] volumes, AWSAvailabilityZone[] zones, Pricing pricing)
        {
            var tags = new List<AWSTag>();
            string name = null;
            if (instance.Tags != null)
            {
                foreach (var tag in instance.Tags)
                {
                    if (tag.Key == "Name" && tag.Value != null)
                    {
                        name = tag.Value;
                    }
                    else
                    {
                        tags.Add(new AWSTag { Key = tag.Key, Value = tag.Value });
                    }
                }
            }

            var awsInstanceVolumes = new List<AWSInstanceVolume>();
            foreach (var mapping in instance.BlockDeviceMappings)
            {
                var volume = volumes.SingleOrDefault(_ => _.VolumeId == mapping.Ebs.VolumeId);
                var awsInstanceVolume = new AWSInstanceVolume()
                                    {
                                        DeviceName = mapping.DeviceName,
                                        EbsVolumeId = mapping.Ebs.VolumeId,
                                        EbsStatus = mapping.Ebs.Status.Value,
                                        VolumeSizeInGB = volume != null ? volume.Size : double.NaN,
                                        VolumeType = volume != null ? volume.VolumeType : null,
                                    };
                awsInstanceVolumes.Add(awsInstanceVolume);
            }

            var instanceType = instance.InstanceType.Value;
            var availabilityZone = instance.Placement.AvailabilityZone;
            var region = zones.Single(_ => _.Name.Equals(availabilityZone)).Region;
            var osTypeTag = tags.SingleOrDefault(_ => _.Key == Constants.OSTypes.TAG_NAME);
            var osType = osTypeTag != null ? osTypeTag.Value : Constants.OSTypes.WINDOWS_BASE; // defaulting to windows base b/c that's the common case

            var pricePerHour = pricing != null ? pricing.EC2Pricing.GetPrice(instanceType, osType, region) : double.NaN;
            var pricePerMonth = pricing != null ? pricing.EBSPricing.GetPrice(region, awsInstanceVolumes) : double.NaN;

            var ret = new AWSInstance()
                          {
                              InstanceId = instance.InstanceId,
                              ImageId = instance.ImageId,
                              AvailabilityZone = availabilityZone,
                              Region = region,
                              Platform = AWSInstance.GetPlatformFromString(instance.Platform == null ? null : instance.Platform.Value),
                              OSType = osTypeTag != null ? osTypeTag.Value : "Unknown - costed as Windows Base",
                              Volumes = awsInstanceVolumes.ToArray(),
                              Name = name,
                              IpAddress = instance.PublicIpAddress,
                              PrivateIpAddress = instance.PrivateIpAddress,
                              InstanceState = AWSInstance.GetStateFromString(instance.State.Name),
                              InstanceType = instanceType,
                              PricePerHourRunning = pricePerHour,
                              PricePerMonthStorage = pricePerMonth,
                              Tags = tags.ToArray(),
                          };

            return ret;
        }