Skip to content

Commit

Permalink
Add option to fetch default project id as numeric from metadata server (
Browse files Browse the repository at this point in the history
#858)

* Add option to fetch default project id as numeric from metadata server

* Rename useNumericProjectId -> preferNumericProjectId

* Fix memoization variable for projectId in metadata
  • Loading branch information
chingor13 authored and dwsupplee committed Jan 24, 2018
1 parent 54e7a44 commit e620fd0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Core/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ private function detectProjectId(array $config)
'httpHandler' => null,
'projectId' => null,
'projectIdRequired' => false,
'hasEmulator' => false
'hasEmulator' => false,
'preferNumericProjectId' => false
];

if ($config['projectId']) {
Expand All @@ -197,7 +198,9 @@ private function detectProjectId(array $config)

if ($this->onGce($config['httpHandler'])) {
$metadata = $this->getMetaData();
$projectId = $metadata->getProjectId();
$projectId = $config['preferNumericProjectId']
? $metadata->getNumericProjectId()
: $metadata->getProjectId();
if ($projectId) {
return $projectId;
}
Expand Down
28 changes: 26 additions & 2 deletions src/Core/Compute/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@
class Metadata
{
/**
* The metadata reader.
* @var StreamReader The metadata reader.
*/
private $reader;

/**
* The project id.
* @var string The project id.
*/
private $projectId;

/**
* @var int The numeric project id.
*/
private $numericProjectId;

/**
* We use StreamReader for the default implementation for fetching the URL.
*/
Expand Down Expand Up @@ -106,6 +111,25 @@ public function getProjectId()
return $this->projectId;
}

/**
* Detect and return the numeric project ID
*
* Example:
* ```
* $projectId = $metadata->getNumericProjectId();
* ```
*
* @return string
*/
public function getNumericProjectId()
{
if (!isset($this->numericProjectId)) {
$this->numericProjectId = $this->get('project/numeric-project-id');
}

return $this->numericProjectId;
}

/**
* Fetch an item from the project metadata
*
Expand Down
4 changes: 3 additions & 1 deletion src/Debugger/DebuggerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function __construct(array $config = [])
$config['scopes'] = [self::FULL_CONTROL_SCOPE];
}

$this->connection = new Rest($this->configureAuthentication($config));
$this->connection = new Rest($this->configureAuthentication($config + [
'preferNumericProjectId' => true
]));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/snippets/Core/Compute/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public function testGetProjectId()
$this->assertEquals(self::PROJECT, $res->returnVal());
}

public function testGetNumericProjectId()
{
$this->reader->read('project/numeric-project-id')
->shouldBeCalled()
->willReturn(self::PROJECT);

$this->metadata->setReader($this->reader->reveal());

$snippet = $this->snippetFromMethod(Metadata::class, 'getNumericProjectId');
$snippet->addLocal('metadata', $this->metadata);
$res = $snippet->invoke('projectId');

$this->assertEquals(self::PROJECT, $res->returnVal());
}

public function testGetProjectMetadata()
{
$val = 'hello world';
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Core/ClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@ public function testDetectProjectIdOnGce()
$this->assertEquals($res, $projectId);
}

public function testDetectNumericProjectIdOnGce()
{
$projectId = '1234567';

$m = $this->prophesize(Metadata::class);
$m->getNumericProjectId()->willReturn($projectId)->shouldBeCalled();

$trait = \Google\Cloud\Dev\impl(ClientTraitStubOnGce::class, ['metadata']);
$trait->___setProperty('metadata', $m);

$res = $trait->call('detectProjectId', [['preferNumericProjectId' => true]]);

$this->assertEquals($res, $projectId);
}

/**
* @expectedException Google\Cloud\Core\Exception\GoogleException
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/Core/Compute/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ public function testGetProjectId()
// Ensure this value is cached thus we `read` only once.
$this->metadata->getProjectId();
}

public function testGetNumericProjectId()
{
$expected_path = 'project/numeric-project-id';
$expected_val = '1234567';
$this->mock->expects($this->once())
->method('read')
->with($this->equalTo($expected_path))
->willReturn($expected_val);
$project_id = $this->metadata->getNumericProjectId();
$this->assertEquals($expected_val, $project_id);
// Ensure this value is cached thus we `read` only once.
$this->metadata->getNumericProjectId();
}
}

0 comments on commit e620fd0

Please sign in to comment.