Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findTestInstances() {
- $testInstances = array();
- $ec2 = new AmazonEC2();
- //$ec2->set_region(AmazonEC2::REGION_SYDNEY);
- $ec2->set_region('ec2.ap-southeast-2.amazonaws.com');
- // Get the hostname from a call to the DescribeImages operation.
- $response = $ec2->describe_instances(/*array(
- 'Filter' => array(
- array('Name' => 'Name',
- 'Value' => 'Testing',
- ),
- )
- )*/);
- if ($response->isOK() == FALSE) {
- throw new ExternalAPIFailedException("Failed to list running instances.");
- }
- //$reservationSetArray = $response->body->reservationSet->to_array();
- $reservationSetArray = $response->body->toSaneArray();
- //$array = $response->body->GetAttributesResult->to_array();
- //var_dump($reservationSetArray);
- foreach ($reservationSetArray['reservationSet'] as $reservationSetList) {
- if(array_key_exists('instancesSet', $reservationSetList) == TRUE){
- //Amazon return 1 item as $item, multiple items as array($item1, $item2)
- $reservationSetList = array($reservationSetList);
- }
- foreach($reservationSetList as $instanceInfoArray){
- foreach($instanceInfoArray['instancesSet'] as $instance){
- if(isset($instance['tagSet']['item']) == FALSE){
- continue;
- }
- if($instance['tagSet']['item'] == NULL){
- continue;
- }
- if(is_array($instance['tagSet']) == TRUE){
- foreach($instance['tagSet'] as $tagItem){
- if(array_key_exists('key', $tagItem) == TRUE){
- //echo "key ".$tagItem['key']." value ".$tagItem['value']."\r\n";
- if ($tagItem['key'] == 'Name' && $tagItem['value'] == 'Testing') {
- if ($instance['instanceState']['name'] == 'running') {
- $testInstances[] = $instance['instanceId'];
- }
- }
- }
- else{
- foreach($tagItem as $tag){
- //echo "key ".$tag['key']." value ".$tag['value']."\r\n";
- if ($tag['key'] == 'Name' && $tag['value'] == 'Testing') {
- if ($instance['instanceState']['name'] == 'running') {
- $testInstances[] = $instance['instanceId'];
- }
- }
- }
- }
- }
- }
- else{
- $tagItem = $instance['tagSet']['item'];
- echo "key ".$tagItem['key']." value ".$tagItem['value']."\r\n";
- }
- //echo "hms";
- }
- }
- }
- /*
- $instance = $reservationSet->instancesSet->item;
- echo "Dump instance tagset******************************\r\n";
- var_dump($instance->tagSet);
- echo "Dump instance tagset******************************\r\n";
- $tagInfo = NULL;
- if (isset($instance->tagSet->item) == TRUE) {
- $tagInfo = $instance->tagSet->item;
- }
- if ($tagInfo == NULL) {
- continue;
- }
- if(is_array($tagInfo) == FALSE){
- echo get_class($tagInfo);
- echo "so what is it? ";
- var_dump($tagInfo);
- }
- if (is_array($tagInfo) == TRUE) {
- foreach ($tagInfo as $tag) {
- if ($tag->key == 'Name' && $tag->value == 'Testing') {
- $instanceStateName = (string)$instance->instanceState->name;
- if ($instanceStateName == 'running') {
- $testInstances[] = (string)$instance->instanceId;
- }
- }
- }
- }
- else {
- $tag = $tagInfo->item;
- if (isset($tag->key) && isset($tag->value)) {
- if ($tag->key == 'Name' && $tag->value == 'Testing') {
- $instanceStateName = (string)$instance->instanceState->name;
- if ($instanceStateName == 'running') {
- $testInstances[] = (string)$instance->instanceId;
- }
- }
- }*/
- // }
- //}
- return $testInstances;
- //var_dump();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement