AWS:使用Java使用AWS Price List API进行价格计算

时间:2016-01-27 08:45:59

标签: java amazon-web-services amazon-ec2 aws-sdk

background: url(3.png), url(2.png), url(1.png);/*left to right: top, middle, bottom*/
background-size: 600px 10px, 100px 100px, 50px 50px;

Step1)首先我得到了EC2预订数据:

I want to calculate the total price for each instance types.

Step2)之后我得到了Ec2Data

    DescribeReservedInstancesRequest describeReservedInstancesRequest= new DescribeReservedInstancesRequest().withFilters(new LinkedList<Filter>());
    describeReservedInstancesRequest.getFilters().add(new Filter().withName("state").withValues("active"));
    DescribeReservedInstancesResult describeReservedInstancesResult = ec2.describeReservedInstances(describeReservedInstancesRequest);

    describeReservedInstancesResult.getReservedInstances() gives me the reserve instance.
for(ReservedInstances reservedInstances : describeReservedInstancesResult.getReservedInstances()){
                EC2ResData ec2ResData = new EC2ResData();
                ec2ResData.setAvailabilityZone(reservedInstances.getAvailabilityZone());
                ec2ResData.setInstanceCount(reservedInstances.getInstanceCount());
                ec2ResData.setInstanceTenancy(reservedInstances.getInstanceTenancy());
                ec2ResData.setInstanceType(reservedInstances.getInstanceType());
                ec2ResData.setProductDescription(reservedInstances.getProductDescription());
                ec2ResData.setResInstanceId(reservedInstances.getReservedInstancesId());
                ec2ResData.setWindows(reservedInstances.getProductDescription().toLowerCase().indexOf("windows") != -1);
                ec2ResData.setInstanceName(getName(reservedInstances.getTags()));
                instanceTypeIndex.add(reservedInstances.getInstanceType());
                ec2ResData.setVPC(reservedInstances.getProductDescription().toLowerCase().indexOf("vpc") != -1);
                ec2ResDataList.add(ec2ResData);
            }

步骤3)之后我将预留数据和ec2数据(具有实例类型,可用区,窗口和vpc的唯一组合)结合起来,其中我计算了预留计数,正在运行的实例总数。 (http://awsresco.s3-website-us-east-1.amazonaws.com/

现在我需要计算这些实例的价格。

我已浏览了https://aws.amazon.com/blogs/aws/new-aws-price-list-api/链接。

Step4)使用https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json我需要分离产品信息和定价信息。 我使用了位置产品属性来查找计算价格所需的SKU。

DescribeInstancesRequest describeInstancesRequest = new 

    DescribeInstancesRequest().withFilters(new LinkedList<Filter>());
    describeInstancesRequest.getFilters().add(new Filter().withName("instance-state-name").withValues("running"));
    List<Reservation> listReservation = ec2.describeInstances(describeInstancesRequest).getReservations();
    Set<Instance> instances = new HashSet<Instance>();
            for (Reservation reservation : listReservation) {
                instances.addAll(reservation.getInstances());
            }
            if (FormatManager.isFilled(instances)) {
                ec2DataList = new ArrayList<EC2Data>();
                for (Instance instance : instances) {
                    EC2Data ec2Data = new EC2Data();
                    ec2Data.setInstanceId(instance.getInstanceId());
                    ec2Data.setInstanceType(instance.getInstanceType());
                    ec2Data.setCurrentState(instance.getState().getName());
                    ec2Data.setAvailabilityZone(instance.getPlacement().getAvailabilityZone());
                    List<Tag> listTag = instance.getTags();
                    ec2Data.setInstanceName(getName(listTag));
                    if(FormatManager.isFilled(instance.getPlatform())){
                        ec2Data.setWindows(instance.getPlatform().toLowerCase().equals("windows"));
                    }
                    else{
                        ec2Data.setWindows(false);
                    }
                    instanceTypeIndex.add(instance.getInstanceType());
                    ec2Data.setVPC(FormatManager.isFilled(instance.getVpcId()));
                    ec2DataList.add(ec2Data);
                }
            }

我很困惑,因为一个SKU有很多实例类型。在计算价格时我需要考虑哪种实例类型?如何区分Step4中的实例类型与Step1和Step2的实例类型。

0 个答案:

没有答案