<?php

/*
 * Copyright 2014 Rackspace US, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require dirname(__DIR__) . '/../vendor/autoload.php';
use OpenCloud\Rackspace;
// 1. Instantiate a Rackspace client. You can replace {authUrl} with
// Rackspace::US_IDENTITY_ENDPOINT or similar
$client = new Rackspace('{authUrl}', array('username' => '{username}', 'apiKey' => '{apiKey}'));
// 2. Crete Compute and Volume service objects
$volumeService = $client->volumeService(null, '{region}');
$computeService = $client->computeService(null, '{region}');
// 3. Get your volume
$myVolume = $volumeService->volume('{volumeId}');
// 4. Get your server
$myServer = $computeService->server('{serverId}');
// 5. Detach
$myServer->detachVolume($myVolume);
 *
 * Prior to running this script, you must setup the following environment variables:
 * - RAX_USERNAME: Your Rackspace Cloud Account Username, and
 * - RAX_API_KEY:  Your Rackspace Cloud Account API Key
 *
 * - You have an existing keypair. For this script, it will be called 'my_keypair'
 * but this will change depending on what you called yours.
 */
require __DIR__ . '/../../vendor/autoload.php';
use OpenCloud\Rackspace;
use Guzzle\Http\Exception\BadResponseException;
// 1. Instantiate a Rackspace client.
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array('username' => getenv('RAX_USERNAME'), 'apiKey' => getenv('RAX_API_KEY')));
// 2. Create Compute service
$region = 'ORD';
$service = $client->computeService(null, $region);
// 3. Get empty server
$server = $service->server();
// 4. Select an OS image
$images = $service->imageList();
foreach ($images as $image) {
    if (strpos($image->name, 'Ubuntu') !== false) {
        $ubuntuImage = $image;
        break;
    }
}
// 5. Select a hardware flavor
$flavors = $service->flavorList();
foreach ($flavors as $flavor) {
    if (strpos($flavor->name, '2GB') !== false) {
        $twoGbFlavor = $flavor;
 public function list_nodes($availability_zone_name, $availability_zone_friendly_name)
 {
     $output = new Symfony\Component\Console\Output\ConsoleOutput();
     $success = false;
     $integration = Integration::find($this->db_integration_id);
     $nodes = [];
     // We need to measure how often this actually matters. I wonder if UK_IDENTITY_ENDPOINT is only
     // for UK-based companies, or if it's needed to connect to Hong Kong.
     try {
         $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array('username' => $integration->authorization_field_1, 'apiKey' => $integration->authorization_field_2));
     } catch (Exception $e) {
         $client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array('username' => $integration->authorization_field_1, 'apiKey' => $integration->authorization_field_2));
     }
     $computeService = $client->computeService(null, $availability_zone_name, 'publicURL');
     $serverList = $computeService->serverList();
     foreach ($serverList as $server) {
         $server_ips = [];
         $private_dns = "";
         $public_dns = "";
         $server_status = "";
         if (strtolower($server->status) == "active") {
             foreach ($server->addresses->public as $ip) {
                 array_push($server_ips, $ip->addr);
             }
             foreach ($server->addresses->private as $ip) {
                 array_push($server_ips, $ip->addr);
             }
             $public_dns = null;
             foreach ($server->addresses->public as $pubdns) {
                 if (strlen($pubdns->addr) < 16) {
                     $public_dns = $pubdns->addr;
                 }
             }
             $private_dns = null;
             foreach ($server->addresses->private as $pdns) {
                 if (strlen($pubdns->addr) < 16) {
                     $private_dns = $pdns->addr;
                 }
             }
             $server_status = 'running';
         } else {
             switch (strtolower($server->status)) {
                 case "active":
                     $server_status = 'running';
                     break;
                 case "build":
                     $server_status = 'starting';
                     break;
                 default:
                     continue;
             }
         }
         // Get image info so we can get platform info
         $imageService = $client->imageService("cloudImages", $availability_zone_name);
         $imageInfo = $imageService->getImage($server->image->id);
         $platform = $imageInfo['os_type'];
         array_push($nodes, array('service_provider_status' => $server_status, 'service_provider_base_image_id' => $server->image->id, 'service_provider_id' => $server->id, 'private_dns_name' => $private_dns, 'public_dns_name' => $public_dns, 'network_interfaces' => [], 'service_provider_cluster_id' => null, 'service_provider_ip_addresses' => $server_ips, 'availability_zone_friendly' => $availability_zone_friendly_name, 'availability_zone_name' => $availability_zone_name, 'platform' => ucfirst($platform)));
     }
     // If nodes is empty, make attempt to list them through the legacy api.
     if (empty($nodes)) {
         $output->writeln("robby's account");
     }
     return $nodes;
 }