forked from googleapis/google-cloud-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes googleapis#8
- Loading branch information
Takashi Matsuo
committed
Nov 10, 2015
1 parent
a0d4e91
commit 34e8570
Showing
3 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2015 Google 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. | ||
*/ | ||
namespace Google\Cloud\Compute\Metadata; | ||
|
||
use Google\Cloud\Compute\Metadata\Readers\StreamReader; | ||
|
||
/** | ||
* A stream wrapper for GCE metadata. | ||
* | ||
* You can get the GCE metadata values very easily like: | ||
* | ||
* use Google\Cloud\Compute\Metadata\GCEMetadataStream; | ||
* | ||
* GCEMetadataStream::register(); | ||
* $val = file_get_contents('gce-metadata://project/mykey') | ||
* or | ||
* $val = file_get_contents('gce-metadata://instance/mykey') | ||
*/ | ||
class GCEMetadataStream | ||
{ | ||
/** | ||
* The base URL for the metadata. | ||
*/ | ||
const METADATA_BASE_URL = 'http://metadata.google.internal/computeMetadata/v1/'; | ||
|
||
/** | ||
* The current seek position. | ||
*/ | ||
private $position; | ||
|
||
/** | ||
* The metadata value. | ||
*/ | ||
private $val; | ||
|
||
/** | ||
* The metadata reader. | ||
*/ | ||
private $reader; | ||
|
||
/** | ||
* A static method to register this stream wrapper. | ||
*/ | ||
public static function register() | ||
{ | ||
stream_wrapper_register( | ||
'gce-metadata', | ||
'\Google\Cloud\Compute\Metadata\GCEMetadataStream') | ||
or die('Failed to register GCEMetadataStream'); | ||
} | ||
|
||
public function __construct() | ||
{ | ||
$this->reader = new StreamReader(); | ||
} | ||
|
||
public function set_reader($reader) | ||
{ | ||
$this->reader = $reader; | ||
} | ||
|
||
public function stream_open($path, $mode, $options, &$opened_path) | ||
{ | ||
$url = parse_url($path); | ||
if ($url['host'] != 'project' and $url['host'] != 'instance') { | ||
return false; | ||
} | ||
$metadata_url = self::METADATA_BASE_URL.$url['host'].'/attributes' | ||
.$url['path']; | ||
$this->val = $this->reader->read($metadata_url); | ||
$this->position = 0; | ||
|
||
return true; | ||
} | ||
|
||
public function stream_stat() | ||
{ | ||
return array(); | ||
} | ||
|
||
public function stream_read($count) | ||
{ | ||
$ret = substr($this->val, $this->position, $count); | ||
$this->position += strlen($ret); | ||
|
||
return $ret; | ||
} | ||
|
||
public function stream_write($data) | ||
{ | ||
trigger_error('stream_write is not implemented.', E_WARNING); | ||
} | ||
|
||
public function stream_tell() | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function stream_eof() | ||
{ | ||
return $this->position >= strlen($this->val); | ||
} | ||
|
||
public function stream_seek($offset, $whence) | ||
{ | ||
switch ($whence) { | ||
case SEEK_SET: | ||
if ($offset < strlen($this->val) && $offset >= 0) { | ||
$this->position = $offset; | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
break; | ||
|
||
case SEEK_CUR: | ||
if ($offset >= 0) { | ||
$this->position += $offset; | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
break; | ||
|
||
case SEEK_END: | ||
if (strlen($this->val) + $offset >= 0) { | ||
$this->position = strlen($this->val) + $offset; | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
public function stream_metadata($path, $option, $var) | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2015 Google 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. | ||
*/ | ||
namespace Google\Cloud\Compute\Metadata\Readers; | ||
|
||
/** | ||
* A class only reading the metadata URL with an appropriate header. | ||
* | ||
* This class makes it easy to test the MetadataStream class. | ||
*/ | ||
class StreamReader | ||
{ | ||
/** | ||
* The header whose presence indicates GCE presence. | ||
*/ | ||
const FLAVOR_HEADER = 'Metadata-Flavor'; | ||
|
||
public function read($url) | ||
{ | ||
$options = array( | ||
'http' => array( | ||
'method' => 'GET', | ||
'header' => self::FLAVOR_HEADER.": Google\r\n", | ||
), | ||
); | ||
$context = stream_context_create($options); | ||
|
||
return file_get_contents($url, false, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2015 Google 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. | ||
*/ | ||
namespace Google\Cloud\Tests; | ||
|
||
use Google\Cloud\Compute\Metadata\GCEMetadataStream; | ||
|
||
class ComputeMetadataStreamTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
protected $mock; | ||
|
||
public function setUp() | ||
{ | ||
$this->stream = new GCEMetadataStream; | ||
$this->mock = $this->getMockBuilder( | ||
'\Google\Cloud\Compute\Metadata\StreamReader') | ||
->setMethods(array('read')) | ||
->getmock(); | ||
$this->stream->set_reader($this->mock); | ||
} | ||
|
||
public function testStreamOpen() | ||
{ | ||
$expected_url = 'http://metadata.google.internal/computeMetadata/v1/' | ||
.'project/attributes/mykey'; | ||
$expected_val = 'myval'; | ||
$this->mock->expects($this->once()) | ||
->method('read') | ||
->with($this->equalTo($expected_url)) | ||
->willReturn($expected_val); | ||
$dummy = ''; | ||
$this->stream->stream_open('gce-metadata://project/mykey', null, null, | ||
$dummy); | ||
$val = $this->stream->stream_read(10); | ||
$this->assertEquals($expected_val, $val); | ||
} | ||
} |