amazon web services - AWS: Price Calculation with AWS Price List API using java -
i want calculate total price each instance types.
step1) @ first getting ec2 reservation data:
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 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); }
step2) after getting ec2data
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>(); (reservation reservation : listreservation) { instances.addall(reservation.getinstances()); } if (formatmanager.isfilled(instances)) { ec2datalist = new arraylist<ec2data>(); (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); } }
step3) after combine both reservation data , ec2 data (with unique combo of instance type, availability zone, windows, , vpc), have calculated reservation count, total number of running instances. (http://awsresco.s3-website-us-east-1.amazonaws.com/)
now need calculate price instances.
i have gone through link https://aws.amazon.com/blogs/aws/new-aws-price-list-api/.
step4) use of https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json need separate product information , pricing information. have used location product attributes find sku needed calculate prices for.
fileinputstream fileinputstream = new fileinputstream("c:/index.json"); string responsestring = formatmanager.frominputstreamtostring(fileinputstream,"utf-8"); org.json.simple.jsonobject jsonobject = getjsonobject(responsestring); org.json.simple.jsonobject products = getjsonobject("products",jsonobject); set<map.entry<?,?>> entryset = products.entryset(); (map.entry<?,?> entry : entryset) { string sku = getjsonkey(entry); org.json.simple.jsonobject productitem = getjsonobject(sku,products); org.json.simple.jsonobject attributes = getjsonobject("attributes",productitem); string instancetype = getstring("instancetype",attributes); string location = getstring("location",attributes); string operatingsystem = getstring("operatingsystem",attributes); string tenancy = getstring("tenancy",attributes); string preinstalledsw = getstring("preinstalledsw",attributes); string licensemodel = getstring("licensemodel",attributes); //system.out.println("operatingsystem '"+operatingsystem+"' - preinstalledsw '"+preinstalledsw+"'"); //system.out.println("location '"+location+"' - instancetype '"+instancetype+"'"+" - "+instancetypeindex); string targetlocation = "us west (oregon)"; if (targetlocation.equals(location)){ if (!skuindexbyinstancetype.containskey(instancetype)){ skuset.add(sku); if (instancetypeindex.contains(instancetype)){ //we interested on instancetype skuindexbyinstancetype.put(instancetype,entry); skuinstancetypemap.put(sku, instancetype); instancetypeskumap.put(instancetype, sku); } else { //we not interested on instance type } } else { map.entry<?,?> entryprevious = skuindexbyinstancetype.get(instancetype); throw new servicefatalexception("instancetype '"+instancetype+"' duplicated location '"+location+"'. here entryprevious '"+entryprevious+"' , entry '"+entry+"'"); } } else { } }
here confused 1 sku has many instance type. instance type need consider when calculating price? how differentiate instance type in step4 of step1 , step2.
Comments
Post a Comment