Skip to content

Commit 0002299

Browse files
committed
Allows building service with specified micro version
1 parent 949ac57 commit 0002299

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/Common/Service/Builder.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ public function createService(string $namespace, array $serviceOptions = []): Se
8383

8484
list($apiClass, $serviceClass) = $this->getClasses($namespace);
8585

86-
/** @var Client $client */
87-
$client = $options['httpClient'];
88-
8986
return new $serviceClass($options['httpClient'], new $apiClass());
9087
}
9188

@@ -100,15 +97,11 @@ private function stockHttpClient(array &$options, string $serviceName)
10097
$stack = $this->getStack($options['authHandler'], $token);
10198
}
10299

103-
$microversion = null;
104-
if (isset($options['microversion'])) {
105-
$microversion = $options['microversion'];
106-
}
107-
dump($microversion);
100+
$microVersion = $options['microVersion'] ?? null;
108101

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

111-
$options['httpClient'] = $this->httpClient($baseUrl, $stack, $microversion);
104+
$options['httpClient'] = $this->httpClient($baseUrl, $stack, $options['catalogType'], $microVersion);
112105
}
113106
}
114107

@@ -146,15 +139,15 @@ private function getStack(callable $authHandler, Token $token = null): HandlerSt
146139
return $stack;
147140
}
148141

149-
private function httpClient(string $baseUrl, HandlerStack $stack, string $microversion = null): ClientInterface
142+
private function httpClient(string $baseUrl, HandlerStack $stack, string $serviceType, string $microVersion = null): ClientInterface
150143
{
151144
$clientOptions = [
152145
'base_uri' => Utils::normalizeUrl($baseUrl),
153146
'handler' => $stack,
154147
];
155148

156-
if ($microversion) {
157-
$clientOptions['headers']['X-OpenStack-Nova-API-Version'] = $microversion;
149+
if ($microVersion) {
150+
$clientOptions['headers']['OpenStack-API-Version'] = sprintf('%s %s', $serviceType, $microVersion);
158151
}
159152

160153
if (isset($this->globalOptions['requestOptions'])) {

tests/unit/Common/Service/BuilderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenStack\Test\Common\Service;
44

5+
use GuzzleHttp\Client;
56
use OpenStack\Common\Auth\IdentityService;
67
use OpenStack\Common\Auth\Token;
78
use OpenStack\Common\Service\Builder;
@@ -116,6 +117,30 @@ public function test_it_does_not_authenticate_for_identity_services()
116117

117118
$this->assertInstanceOf(Fixtures\Identity\Service::class, $s);
118119
}
120+
121+
public function test_it_create_service_with_micro_version()
122+
{
123+
$is = $this->prophesize(TestIdentity::class);
124+
$is->authenticate(Argument::any())->willReturn([new FakeToken(), '']);
125+
126+
$s = $this->builder->createService('Test\\Common\\Service\\Fixtures', $this->opts + [
127+
'identityService' => $is->reveal(),
128+
'microVersion' => '1.2.3'
129+
]);
130+
131+
$this->assertInstanceOf(Fixtures\Service::class, $s);
132+
133+
$refClass = new \ReflectionClass($s);
134+
$refProperty = $refClass->getProperty('client');
135+
$refProperty->setAccessible(true);
136+
137+
/** @var Client $client */
138+
$client = $refProperty->getValue($s);
139+
140+
$headers = $client->getConfig()['headers'];
141+
$this->assertArrayHasKey('OpenStack-API-Version', $headers);
142+
$this->assertEquals('7 1.2.3', $headers['OpenStack-API-Version']);
143+
}
119144
}
120145

121146
class FakeToken implements Token

0 commit comments

Comments
 (0)