-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GcpMetadata.php
120 lines (97 loc) · 3.09 KB
/
GcpMetadata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Kreait;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Kreait\GcpMetadata\Error;
use Psr\Http\Message\ResponseInterface;
class GcpMetadata
{
public const baseUrl = 'http://169.254.169.254/computeMetadata/v1/';
public const flavorHeaderName = 'Metadata-Flavor';
public const flavorHeaderValue = 'Google';
/**
* @var ClientInterface|null
*/
private $client;
/**
* @var null|bool
*/
private $isAvailable;
public function __construct(ClientInterface $client = null)
{
$this->client = $client ?? $this->createClient();
}
private function createClient(): Client
{
return new Client([
'connect_timeout' => 1.0, // Default is 0 = indefinitely
'timeout' => 1.0 // Default is 0 = indefinitely
]);
}
public function isAvailable(): bool
{
if ($this->isAvailable !== null) {
return $this->isAvailable;
}
try {
$this->instance();
return $this->isAvailable = true;
} catch (\Throwable $e) {
return $this->isAvailable = false;
}
}
public function instance(string $property = '', array $params = [])
{
return $this->request('instance', $property, $params);
}
public function project(string $property = '', array $params = [])
{
return $this->request('project', $property, $params);
}
private function request(string $type, string $property = '', array $params = [])
{
$property = ltrim($property, '/');
$url = self::baseUrl.$type.'/'.$property;
$options = [
'headers' => [self::flavorHeaderName => self::flavorHeaderValue],
'query' => $params,
'http_errors' => false,
];
try {
$response = $this->client->request('GET', $url, $options);
} catch (ConnectException $e) {
throw new Error('Unable to connect: '.$e->getMessage());
}
$this->verifyHttpStatus($response);
$this->verifyHeaders($response);
return $this->parseResponse($response);
}
private function verifyHttpStatus(ResponseInterface $response): void
{
if (($statusCode = $response->getStatusCode()) !== 200) {
throw new Error('Unsuccessful response status code: '.$statusCode);
}
}
private function verifyHeaders(ResponseInterface $response): void
{
if ($response->getHeaderLine(self::flavorHeaderName) !== self::flavorHeaderValue) {
throw new Error('"'.self::flavorHeaderName.'" header is missing or incorrect.');
}
}
/**
* @param ResponseInterface $response
* @return string|string[]
*/
private function parseResponse(ResponseInterface $response)
{
$body = trim((string) $response->getBody());
$lines = explode("\n", $body);
if (\count($lines) === 1) {
return $body;
}
return $lines;
}
}