/branches/1.2.0/www/server/grids/aws_ec2_pricehistory_list.php
PHP | 141 lines | 104 code | 28 blank | 9 comment | 18 complexity | dfb1b05483f3833cc10031d462ffba49 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
- <?php
- $response = array();
-
- // AJAX_REQUEST;
- $context = 6;
-
- try
- {
- $enable_json = true;
- include("../../src/prepend.inc.php");
-
- $req_show_all = true;
-
- if (isset($req_show_all))
- {
- if ($req_show_all == 'true')
- $_SESSION['sg_show_all'] = true;
- else
- $_SESSION['sg_show_all'] = false;
- }
-
- $Client = Client::Load($_SESSION['uid']);
-
- $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($_SESSION['aws_region']));
- $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
-
- if($req_query)
- {
-
- //---------------------------- filter for SpotPriceHistory:
- $filter = array();
- $filter = explode(" ", $req_query);
-
- $instanceTypes = explode(" ","m1.small m1.large m1.xlarge c1.medium c1.xlarge m2.2xlarge m2.4xlarge");
- $i = 0;
- $instanceTypeSet = array();
- $requestDesription = array();
-
- foreach($filter as $item) // search instance type conjunctions
- {
-
- for($i = 0; $i<count($instanceTypes);$i++)
- {
- // serach by instance type
- if(strcasecmp($instanceTypes[$i],$item) == 0)
- {
- $instanceTypeSet[] = $instanceTypes[$i];
- continue;
- }
- }
- // serach by description
- $win = stristr($item,"windows");
- $linux = stristr($item,"linux");
- $unix = stristr($item,"unix");
- if($win)
- {
- $requestDesription[] = "Windows";
- continue;
- }
- elseif(($linux) || ($unix))
- {
- $requestDesription[] = "Linux/UNIX";
- continue;
- }
- }
- //---------------------------- end filter ------------------------
- $describeSpotPriceType = new DescribeSpotPriceHistoryType(null,$instanceTypeSet,$requestDesription,null);
- $aws_response = $AmazonEC2Client->DescribeSpotPriceHistory($describeSpotPriceType);
- }
- else
- $aws_response = $AmazonEC2Client->DescribeSpotPriceHistory(); // show all prices
-
-
- // Rows
- $rows = (array)$aws_response->spotPriceHistorySet;
-
- if ($rows["item"] instanceof stdClass)
- $rows["item"] = array($rows["item"]); // convert along subnet record to array
-
- $rowz = array();
-
- foreach ($rows['item'] as $row)
- $rowz[]=(array)$row;
-
- if ($req_sort == 'price' )
- {
- $nrowz = array();
- foreach ($rowz as $row)
- $nrowz[(string)$row['spotPrice']] = $row;
-
- ksort($nrowz);
-
- if ($req_dir == 'ASC')
- $rowz = array_reverse($nrowz);
- else
- $rowz = $nrowz;
- }
-
- if ($req_sort == 'timestamp')
- {
- $nrowz = array();
- foreach ($rowz as $row)
- $nrowz[(string)$row['timestamp']] = $row;
-
- ksort($nrowz);
-
- if ($req_dir == 'ASC')
- $rowz = array_reverse($nrowz);
- else
- $rowz = $nrowz;
- }
-
- // diplay list limits
- $start = $req_start ? (int) $req_start : 0;
- $limit = $req_limit ? (int) $req_limit : 20;
-
- $response['total'] = count($rowz);
- $rowz = (count($rowz) > $limit) ? array_slice($rowz, $start, $limit) : $rowz;
-
- // descending sorting of requested result
- $response["data"] = array();
-
- // Rows. Create final rows array for script
- foreach ($rowz as $row)
- {
- $response["data"][] = array(
- "type" => (string)$row['instanceType'], // have to call only like "id" for correct script work in template
- "description" => (string)$row['productDescription'],
- "price" => (string)$row['spotPrice'],
- "timestamp" => (string)$row['timestamp']
- );
- }
-
- }
- catch(Exception $e)
- {
- $response = array("error" => $e->getMessage(), "data" => array());
- }
-
- print json_encode($response);
- ?>