Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Common/Service/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ private function stockHttpClient(array &$options, string $serviceName)
$stack = $this->getStack($options['authHandler'], $token);
}

$microVersion = $options['microVersion'] ?? null;

$this->addDebugMiddleware($options, $stack);

$options['httpClient'] = $this->httpClient($baseUrl, $stack);
$options['httpClient'] = $this->httpClient($baseUrl, $stack, $options['catalogType'], $microVersion);
}
}

Expand Down Expand Up @@ -137,17 +139,20 @@ private function getStack(callable $authHandler, Token $token = null): HandlerSt
return $stack;
}

private function httpClient(string $baseUrl, HandlerStack $stack): ClientInterface
private function httpClient(string $baseUrl, HandlerStack $stack, string $serviceType = null, string $microVersion = null): ClientInterface
{
$clientOptions = [
'base_uri' => Utils::normalizeUrl($baseUrl),
'handler' => $stack,
];

if ($microVersion && $serviceType) {
$clientOptions['headers']['OpenStack-API-Version'] = sprintf('%s %s', $serviceType, $microVersion);
}

if (isset($this->globalOptions['requestOptions'])) {
$clientOptions = array_merge($this->globalOptions['requestOptions'], $clientOptions);
}

return new Client($clientOptions);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Compute/v2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ public function getKeypair(): array
'method' => 'GET',
'path' => 'os-keypairs/{name}',
'params' => [
'name' => $this->isRequired($this->params->keypairName())
'name' => $this->isRequired($this->params->keypairName()),
'userId' => $this->params->userId()
],
];
}
Expand All @@ -610,7 +611,9 @@ public function getKeypairs(): array
return [
'method' => 'GET',
'path' => 'os-keypairs',
'params' => [],
'params' => [
'userId' => $this->params->userId()
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Compute/v2/Models/Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function getAliases(): array
*/
public function retrieve()
{
$response = $this->execute($this->api->getKeypair(), ['name' => (string) $this->name]);
$response = $this->execute($this->api->getKeypair(), $this->getAttrs(['name', 'userId']));
$this->populateFromResponse($response);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Compute/v2/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ public function keypairName(): array
];
}

public function userId(): array
{
return [
'type' => self::STRING_TYPE,
'sentAs' => 'user_id',
'location' => self::QUERY,
'description' => 'This allows administrative users to operate key-pairs of specified user ID. Requires micro version 2.10.'
];
}

public function flavorRam(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/OpenStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function getDefaultIdentityService(array $options): Service

$clientOptions = [
'base_uri' => Utils::normalizeUrl($options['authUrl']),
'handler' => $stack
'handler' => $stack,
];

if (isset($options['requestOptions'])) {
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/Common/Service/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenStack\Test\Common\Service;

use GuzzleHttp\Client;
use OpenStack\Common\Auth\IdentityService;
use OpenStack\Common\Auth\Token;
use OpenStack\Common\Service\Builder;
Expand Down Expand Up @@ -116,6 +117,30 @@ public function test_it_does_not_authenticate_for_identity_services()

$this->assertInstanceOf(Fixtures\Identity\Service::class, $s);
}

public function test_it_create_service_with_micro_version()
{
$is = $this->prophesize(TestIdentity::class);
$is->authenticate(Argument::any())->willReturn([new FakeToken(), '']);

$s = $this->builder->createService('Test\\Common\\Service\\Fixtures', $this->opts + [
'identityService' => $is->reveal(),
'microVersion' => '1.2.3'
]);

$this->assertInstanceOf(Fixtures\Service::class, $s);

$refClass = new \ReflectionClass($s);
$refProperty = $refClass->getProperty('client');
$refProperty->setAccessible(true);

/** @var Client $client */
$client = $refProperty->getValue($s);

$headers = $client->getConfig()['headers'];
$this->assertArrayHasKey('OpenStack-API-Version', $headers);
$this->assertEquals('7 1.2.3', $headers['OpenStack-API-Version']);
}
}

class FakeToken implements Token
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Compute/v2/Fixtures/keypair-get.resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Content-Type: application/json
"keypair": {
"fingerprint": "44:fe:29:6e:23:14:b9:53:5b:65:82:58:1c:fe:5a:c3",
"name": "keypair-test",
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1HTrHCbb9NawNLSV8N6tSa8i637+EC2dA+lsdHHfQlT54t+N0nHhJPlKWDLhc579j87vp6RDFriFJ/smsTnDnf64O12z0kBaJpJPH2zXrBkZFK6q2rmxydURzX/z0yLSCP77SFJ0fdXWH2hMsAusflGyryHGX20n+mZK6mDrxVzGxEz228dwQ5G7Az5OoZDWygH2pqPvKjkifRw0jwUKf3BbkP0QvANACOk26cv16mNFpFJfI1N3OC5lUsZQtKGR01ptJoWijYKccqhkAKuo902tg/qup58J5kflNm7I61sy1mJon6SGqNUSfoQagqtBH6vd/tU1jnlwZ03uUroAL",
"public_key": "ssh-rsa AAAAAAABBBBBBBBBCCCCCCCCCCC foo@bar.com",
"user_id": "fake",
"deleted": false,
"created_at": "2014-05-07T12:06:13.681238",
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/Compute/v2/Models/KeypairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,29 @@ public function test_it_retrieves()
$this->assertEquals('44:fe:29:6e:23:14:b9:53:5b:65:82:58:1c:fe:5a:c3', $this->keypair->fingerprint);
$this->assertEquals(self::KEYPAIR_NAME, $this->keypair->name);
$this->assertEquals(
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1HTrHCbb9NawNLSV8N6tSa8i637+EC2dA+lsdHHfQlT54t+N0nHhJPlKWDLhc579j87vp6RDFriFJ/smsTnDnf64O12z0kBaJpJPH2zXrBkZFK6q2rmxydURzX/z0yLSCP77SFJ0fdXWH2hMsAusflGyryHGX20n+mZK6mDrxVzGxEz228dwQ5G7Az5OoZDWygH2pqPvKjkifRw0jwUKf3BbkP0QvANACOk26cv16mNFpFJfI1N3OC5lUsZQtKGR01ptJoWijYKccqhkAKuo902tg/qup58J5kflNm7I61sy1mJon6SGqNUSfoQagqtBH6vd/tU1jnlwZ03uUroAL',
'ssh-rsa AAAAAAABBBBBBBBBCCCCCCCCCCC foo@bar.com',
$this->keypair->publicKey
);
$this->assertFalse($this->keypair->deleted);
}

public function test_it_retrieves_by_user_id()
{
$this->client
->request('GET', 'os-keypairs/' . self::KEYPAIR_NAME, ['headers' => [], 'query' => ['user_id' => 'fake']])
->shouldBeCalled()
->willReturn($this->getFixture('keypair-get'));


$this->keypair->userId = 'fake';
$this->keypair->retrieve();

$this->assertEquals('1', $this->keypair->id);
$this->assertEquals('fake', $this->keypair->userId);
$this->assertEquals('44:fe:29:6e:23:14:b9:53:5b:65:82:58:1c:fe:5a:c3', $this->keypair->fingerprint);
$this->assertEquals(self::KEYPAIR_NAME, $this->keypair->name);
$this->assertEquals(
'ssh-rsa AAAAAAABBBBBBBBBCCCCCCCCCCC foo@bar.com',
$this->keypair->publicKey
);
$this->assertFalse($this->keypair->deleted);
Expand Down