Skip to content

Commit

Permalink
Merge pull request #710 from pantheon-systems/absolute_url_requests
Browse files Browse the repository at this point in the history
Improved handling of non-API requests, added test to site wake
  • Loading branch information
TeslaDethray committed Nov 30, 2015
2 parents c6aac3d + 1d7ae80 commit 90c28d6
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu
### Fixed
- `site backups get` no longer errs when there are no backups. (#690)
- Interactive commands' environment menus now consistently include multidev environments, where applicable. (#701)
- `site wake` (#710)

### Changed
- Extricated the request logic from TerminusCommand class and moved it to the Request class. (#704)
Expand Down
4 changes: 2 additions & 2 deletions php/Terminus/Commands/SiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1754,10 +1754,10 @@ public function wake($args, $assoc_args) {
if (!$data['styx']) {
$this->failure('Pantheon headers missing, which is not quite right.');
}

$context = array('target' => $data['target'], 'time' => $data['time']);
$this->log()->info(
'OK >> {target} responded in {time}',
$data
$context
);
}

Expand Down
8 changes: 4 additions & 4 deletions php/Terminus/Models/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,14 @@ public function wake() {
$on_stats = function (TransferStats $stats) {
$this->transfertime = $stats->getTransferTime();
};
$hostnames = $this->getHostnames();
$target = key($hostnames);
$hostnames = array_keys((array)$this->getHostnames());
$target = array_pop($hostnames);
$healthc = "http://$target/pantheon_healthcheck";
$response = $this->request->simpleRequest($healthc, compact('on_stats'));
$return_data = array(
'success' => $response->getStatusCode() === 200,
'success' => ($response['status_code'] === 200),
'time' => $this->transfertime,
'styx' => $response->getHeader('X-Pantheon-Styx-Hostname'),
'styx' => $response['headers']['X-Pantheon-Styx-Hostname'],
'response' => $response,
'target' => $target,
);
Expand Down
29 changes: 15 additions & 14 deletions php/Terminus/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,24 @@ public function request(
/**
* Simplified request method for Pantheon API
*
* @param [string] $path API path (URL)
* @param [array] $options Options for the request
* [string] method GET is default
* [mixed] data Native PHP data structure (e.g. int, string array, or
* simple object) to be sent along with the request. Will be encoded as
* JSON for you.
* @param [string] $path API path (URL)
* @param [array] $arg_options Options for the request
* [string] method GET is default
* [mixed] data Native PHP data structure (e.g. int, string
* array, or simple object) to be sent along with the request. Will
* be encoded as JSON for you.
* [boolean] absolute_url True if URL passed is to be treated as absolute
* @return [array] $data
*/
public function simpleRequest($path, $options = array()) {
$method = 'get';
if (isset($options['method'])) {
$method = $options['method'];
unset($options['method']);
}
public function simpleRequest($path, $arg_options = array()) {
$default_options = array(
'method' => 'get',
'absolute_url' => false,
);
$options = array_merge($default_options, $arg_options);

$url = $path;
if (!isset($options['absolute_url']) || !$options['absolute_url']) {
if ((strpos($path, 'http') !== 0) && !$options['absolute_url']) {
$url = sprintf(
'%s://%s:%s/api/%s',
TERMINUS_PROTOCOL,
Expand All @@ -200,7 +201,7 @@ public function simpleRequest($path, $options = array()) {

try {
Terminus::getLogger()->debug('URL: {url}', compact('url'));
$response = $this->send($url, $method, $options);
$response = $this->send($url, $options['method'], $options);
} catch (GuzzleHttp\Exception\BadResponseException $e) {
throw new TerminusException(
'API Request Error: {msg}',
Expand Down
13 changes: 13 additions & 0 deletions tests/features/site_wake.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Waking a site

Background: I am authenticated and have a site named [[test_site_name]]
Given I am authenticated
And a site named "[[test_site_name]]"

@vcr site_wake
Scenario: Waking a site
When I run "terminus site wake --site=[[test_site_name]] --env=dev"
Then I should get:
"""
OK >> dev-[[test_site_name]].pantheon.io responded in
"""
Loading

0 comments on commit 90c28d6

Please sign in to comment.