Advertisement
Danack

SRSLY AMZN?

Jul 14th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. function findTestInstances() {
  2.  
  3. $testInstances = array();
  4.  
  5. $ec2 = new AmazonEC2();
  6.  
  7. //$ec2->set_region(AmazonEC2::REGION_SYDNEY);
  8. $ec2->set_region('ec2.ap-southeast-2.amazonaws.com');
  9.  
  10.  
  11. // Get the hostname from a call to the DescribeImages operation.
  12. $response = $ec2->describe_instances(/*array(
  13. 'Filter' => array(
  14. array('Name' => 'Name',
  15. 'Value' => 'Testing',
  16. ),
  17. )
  18. )*/);
  19.  
  20. if ($response->isOK() == FALSE) {
  21. throw new ExternalAPIFailedException("Failed to list running instances.");
  22. }
  23.  
  24. //$reservationSetArray = $response->body->reservationSet->to_array();
  25.  
  26. $reservationSetArray = $response->body->toSaneArray();
  27.  
  28. //$array = $response->body->GetAttributesResult->to_array();
  29.  
  30. //var_dump($reservationSetArray);
  31.  
  32. foreach ($reservationSetArray['reservationSet'] as $reservationSetList) {
  33.  
  34. if(array_key_exists('instancesSet', $reservationSetList) == TRUE){
  35. //Amazon return 1 item as $item, multiple items as array($item1, $item2)
  36. $reservationSetList = array($reservationSetList);
  37. }
  38.  
  39. foreach($reservationSetList as $instanceInfoArray){
  40.  
  41. foreach($instanceInfoArray['instancesSet'] as $instance){
  42.  
  43. if(isset($instance['tagSet']['item']) == FALSE){
  44. continue;
  45. }
  46.  
  47. if($instance['tagSet']['item'] == NULL){
  48. continue;
  49. }
  50.  
  51. if(is_array($instance['tagSet']) == TRUE){
  52. foreach($instance['tagSet'] as $tagItem){
  53.  
  54. if(array_key_exists('key', $tagItem) == TRUE){
  55. //echo "key ".$tagItem['key']." value ".$tagItem['value']."\r\n";
  56. if ($tagItem['key'] == 'Name' && $tagItem['value'] == 'Testing') {
  57. if ($instance['instanceState']['name'] == 'running') {
  58. $testInstances[] = $instance['instanceId'];
  59. }
  60. }
  61.  
  62. }
  63. else{
  64. foreach($tagItem as $tag){
  65. //echo "key ".$tag['key']." value ".$tag['value']."\r\n";
  66. if ($tag['key'] == 'Name' && $tag['value'] == 'Testing') {
  67. if ($instance['instanceState']['name'] == 'running') {
  68. $testInstances[] = $instance['instanceId'];
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. else{
  76. $tagItem = $instance['tagSet']['item'];
  77. echo "key ".$tagItem['key']." value ".$tagItem['value']."\r\n";
  78. }
  79. //echo "hms";
  80. }
  81. }
  82. }
  83.  
  84.  
  85. /*
  86. $instance = $reservationSet->instancesSet->item;
  87.  
  88. echo "Dump instance tagset******************************\r\n";
  89. var_dump($instance->tagSet);
  90. echo "Dump instance tagset******************************\r\n";
  91.  
  92. $tagInfo = NULL;
  93. if (isset($instance->tagSet->item) == TRUE) {
  94. $tagInfo = $instance->tagSet->item;
  95. }
  96.  
  97. if ($tagInfo == NULL) {
  98. continue;
  99. }
  100.  
  101.  
  102. if(is_array($tagInfo) == FALSE){
  103. echo get_class($tagInfo);
  104. echo "so what is it? ";
  105. var_dump($tagInfo);
  106. }
  107.  
  108. if (is_array($tagInfo) == TRUE) {
  109. foreach ($tagInfo as $tag) {
  110. if ($tag->key == 'Name' && $tag->value == 'Testing') {
  111. $instanceStateName = (string)$instance->instanceState->name;
  112. if ($instanceStateName == 'running') {
  113. $testInstances[] = (string)$instance->instanceId;
  114. }
  115. }
  116. }
  117. }
  118. else {
  119. $tag = $tagInfo->item;
  120.  
  121. if (isset($tag->key) && isset($tag->value)) {
  122. if ($tag->key == 'Name' && $tag->value == 'Testing') {
  123. $instanceStateName = (string)$instance->instanceState->name;
  124. if ($instanceStateName == 'running') {
  125. $testInstances[] = (string)$instance->instanceId;
  126. }
  127. }
  128. }*/
  129. // }
  130.  
  131. //}
  132.  
  133.  
  134. return $testInstances;
  135. //var_dump();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement