private function loadProducts() { $key = join("-", array($this->type, $this->category, $this->framework)); $response = \Terminus_Command::request("products", "public", false, "GET"); $products = array(); $keys_to_show = array('longname', 'framework', 'type', 'category'); // we'll use this to sort the list later $sort = array(); foreach ((array) $response['data'] as $id => $details) { if (!empty($this->type) and $details->attributes->type != $this->type) { continue; } if (!empty($this->category) and $details->attributes->category != $this->category) { continue; } if (!empty($this->framework) and $details->attributes->framework != $this->framework) { continue; } $sort[] = $details->attributes->shortname; $row = array(); $row['id'] = $id; foreach ($keys_to_show as $key) { $row[$key] = @$details->attributes->{$key}; } array_push($products, $row); } array_multisort($sort, SORT_ASC, SORT_REGULAR, $products); $this->products = $products; return $products; }
/** * Make a request to the Dashbord's internal API. * * @param $realm * Permissions realm for data request: currently "user" or "site" but in the * future this could also be "organization" or another high-level business * object (e.g. "product" for managing your app). Can also be "public" to * simply pull read-only data that is not privileged. * * @param $uuid * The UUID of the item in the realm you want to access. * * @param $method * HTTP method (verb) to use. * * @param $data * A native PHP data structure (int, string, arary or simple object) to be * sent along with the request. Will be encoded as JSON for you. */ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $options = NULL) { if (!in_array($realm, array('login', 'user', 'public'))) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Logger::debug('Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); \Terminus::error("%s", $response->getBody(true)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); //die(Terminus_Command::stripSensitiveData()); $sanitized_request = Terminus_Command::stripSensitiveData((string) $request, Terminus_Command::$_blacklist); \Terminus::error("Request %s had failed: %s", array($sanitized_request, $e->getMessage())); } catch (Exception $e) { \Terminus::error("Unrecognised request failure: %s", $e->getMessage()); } }
public function refresh() { $response = \Terminus_Command::request($this->realm, $this->object->getId(), "workflows/" . $this->id, 'GET'); $this->status = $response['data']; $this->id = $response['data']->id; $this->result = $this->status->result; }
public function getSites() { $path = 'memberships/sites'; $method = 'GET'; $response = \Terminus_Command::request('organizations', $this->id, $path, $method); return $response['data']; }
public function __construct() { # Load commonly used data from cache. $this->cache = Terminus::get_cache(); $this->session = Session::instance(); $this->sites = $this->cache->get_data('sites'); self::$instance = $this; }
public function run() { $params = array('type' => 'deploy', 'params' => array('annotation' => $this->annotation, 'clear_cache' => $this->cc, 'updatedb' => $this->updatedb)); $options = array('body' => json_encode($params), 'headers' => array('Content-type' => 'application/json')); $path = sprintf("environments/%s/workflows/deploy", $this->env->name); $response = \Terminus_Command::request('sites', $this->env->site->getId(), $path, 'POST', $options); return $response['data']; }
public function refresh() { $response = \Terminus_Command::request('sites', $this->site->getId(), "workflows/" . $this->id, 'GET'); // we have to do this because the api sometimes returns an object and sometimes a collections if (!\Terminus\utils\result_is_multiobj($response['data'])) { $response['data'] = array($response['data']); } $this->status = $response['data'][0]; $this->id = $response['data'][0]->id; $this->result = $response['data'][0]->result; }
public function sites($organization = null) { if ($organization) { $path = sprintf("organizations/%s/memberships/sites", $organization); } else { $path = "sites"; } $method = 'GET'; $response = \Terminus_Command::request('users', $this->id, $path, $method); return $response['data']; }
/** * Send the workflow to the api */ public function start() { $data = array(); $path = sprintf('environments/%s/workflows', $this->object->name); if ('POST' == $this->getMethod()) { $data['body'] = json_encode(array('type' => $this->type, 'params' => $this->params)); $data['headers'] = array('Content-type' => 'application/json'); } else { $path = "{$path}?type=" . $this->type; } $response = \Terminus_Command::request($this->realm, $this->object->site->getId(), $path, $this->getMethod(), $data); if (is_object($response['data'])) { $this->status = $response['data']; $this->id = $this->status->id; $this->result = $this->status->result; } return $this; }
/** * creates a new environment * */ public function create($env_name) { $path = sprintf('environments/%s', $env_name); $OPTIONS = array('headers' => array('Content-type' => 'application/json')); $response = \Terminus_Command::request('sites', $site_id, $path, 'POST', $OPTIONS); return $response['data']; }
/** * Get memberships for a site */ function memberships($type = 'organizations') { $path = sprintf('memberships/%s', $type); $method = 'GET'; $response = \Terminus_Command::request('sites', $this->getId(), $path, $method); return $response['data']; }
public function __construct() { parent::__construct(); }
/** * Show a list of your sites on Pantheon * @package Terminus * @version 2.0 */ public function __construct() { parent::__construct(); Auth::loggedIn(); $this->sitesCache = new SitesCache(); }
/** * Execute the login based on an existing session token * * @param $session_token string (required) * @return array */ private function doLoginFromSessionToken($session_token) { $options = array('headers' => array('Content-type' => 'application/json'), 'cookies' => array('X-Pantheon-Session' => $session_token)); # Temporarily disable the cache for this GET call Terminus::set_config('nocache', TRUE); $response = Terminus_Command::request('user', '', '', 'GET', $options); Terminus::set_config('nocache', FALSE); if (!$response or '200' != @$response['info']['http_code']) { \Terminus::error("[auth_error]: session token not valid"); } // Prepare credentials for storage. $data = array('user_uuid' => $response['data']->id, 'session' => $session_token, 'session_expire_time' => 0, 'email' => $response['data']->email); // creates a session instance Session::instance()->setData($data); return $data; }
/** * Execute the login based on email,password * * @param $email string (required) * @param $password string (required) * @package Terminus * @version 0.04-alpha * @return string */ private function doLogin($email, $password) { if (Terminus::is_test()) { $data = array('user_uuid' => '77629472-3050-457c-8c3d-32b2cabf992b', 'session' => '77629472-3050-457c-8c3d-32b2cabf992b:7dc42f40-65f8-11e4-b314-bc764e100eb1:ZHR0TgtQYsKcOOwMOd0tk', 'session_expire_time' => '1417727066', 'email' => '*****@*****.**'); return $data; } $options = array('body' => json_encode(array('email' => $email, 'password' => $password)), 'headers' => array('Content-type' => 'application/json')); $response = Terminus_Command::request('login', '', '', 'POST', $options); if (!$response or '200' != @$response['info']['http_code']) { \Terminus::error("[auth_error]: unsuccessful login"); } // Prepare credentials for storage. $data = array('user_uuid' => $response['data']->user_id, 'session' => $response['data']->session, 'session_expire_time' => $response['data']->expires_at, 'email' => $email); // creates a session instance Session::instance()->setData($data); return $data; }
/** * Make a request to the Dashbord's internal API. * * @param $realm * Permissions realm for data request: currently "user" or "site" but in the * future this could also be "organization" or another high-level business * object (e.g. "product" for managing your app). Can also be "public" to * simply pull read-only data that is not privileged. * * @param $uuid * The UUID of the item in the realm you want to access. * * @param $method * HTTP method (verb) to use. * * @param $data * A native PHP data structure (int, string, arary or simple object) to be * sent along with the request. Will be encoded as JSON for you. */ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $options = NULL) { if (!in_array($realm, array('login', 'user', 'public')) and !Terminus::is_test()) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); // combine session realm uuid and path to get a unique key // @todo need cache "groups" $cachekey = md5(Session::getValue('user_uuid') . $uuid . $realm . $path); $data = $cache->get_data($cachekey); // check the request cache if ("GET" == $method and !Terminus::get_config('nocache') and !getenv('CLI_TEST_MODE') and !empty($data)) { if (Terminus::get_config('debug')) { Logger::debug('CacheKey: ' . $cachekey); } return (array) $data; } // for some methods we'll assume the cache should be invalidated if (in_array($method, array("POST", "PUT", "DELETE"))) { $cache->flush(null, 'session'); } if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Logger::debug('Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(TRUE); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); $cache->put_data($cachekey, $data); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); \Terminus::error("%s", $response->getBody(TRUE)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); //die(Terminus_Command::stripSensitiveData()); $sanitized_request = Terminus_Command::stripSensitiveData((string) $request, Terminus_Command::$_blacklist); \Terminus::error("Request %s had failed: %s", array($sanitized_request, $e->getMessage())); } catch (Exception $e) { \Terminus::error("Unrecognised request failure: %s", $e->getMessage()); } }
private function envExists($site_id, $env) { $response = \Terminus_Command::request('sites', $site_id, 'code-tips', 'GET'); $envs = (array) $response['data']; return array_key_exists($env, $envs); }
/** * Delete a site from pantheon * * ## OPTIONS * --site=<site> * : Id of the site you want to delete * * [--all] * : Just kidding ... we won't let you do that. * * [--force] * : to skip the confirmations * */ function delete($args, $assoc_args) { $site_to_delete = SiteFactory::instance(@$assoc_args['site']); if (!$site_to_delete) { foreach (SiteFactory::instance() as $id => $site) { $site->id = $id; $sites[] = $site; $menu[] = $site->information->name; } $index = Terminus::menu($menu, null, "Select a site to delete"); $site_to_delete = $sites[$index]; } if (!isset($assoc_args['force']) and !Terminus::get_config('yes')) { // if the force option isn't used we'll ask you some annoying questions Terminus::confirm(sprintf("Are you sure you want to delete %s?", $site_to_delete->information->name)); Terminus::confirm("Are you really sure?"); } Terminus::line(sprintf("Deleting %s ...", $site_to_delete->information->name)); $response = \Terminus_Command::request('sites', $site_to_delete->id, '', 'DELETE'); Terminus::success("Deleted %s!", $site_to_delete->information->name); }
/** * Delete a site from pantheon * * ## OPTIONS * [--site=<site>] * : ID of the site you want to delete * * [--force] * : to skip the confirmations */ function delete($args, $assoc_args) { $sitename = Input::sitename($assoc_args); $site_id = $this->sitesCache->findID($sitename); $site_to_delete = new Site($site_id); if (!isset($assoc_args['force']) and !Terminus::get_config('yes')) { // if the force option isn't used we'll ask you some annoying questions Terminus::confirm(sprintf("Are you sure you want to delete %s?", $site_to_delete->information->name)); Terminus::confirm("Are you really sure?"); } Terminus::line(sprintf("Deleting %s ...", $site_to_delete->information->name)); $response = \Terminus_Command::request('sites', $site_to_delete->id, '', 'DELETE'); $this->sitesCache->remove($sitename); Terminus::success("Deleted %s!", $sitename); }