-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPlatformClient.php
More file actions
722 lines (661 loc) · 24.4 KB
/
PlatformClient.php
File metadata and controls
722 lines (661 loc) · 24.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
<?php
declare(strict_types=1);
namespace Platformsh\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Utils as Psr7Utils;
use GuzzleHttp\Utils;
use Platformsh\Client\Connection\Connector;
use Platformsh\Client\Connection\ConnectorInterface;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Exception\ProjectReferenceException;
use Platformsh\Client\Model\BasicProjectInfo;
use Platformsh\Client\Model\Billing\PlanRecord;
use Platformsh\Client\Model\Billing\PlanRecordQuery;
use Platformsh\Client\Model\Catalog;
use Platformsh\Client\Model\CentralizedPermissions\UserExtendedAccess;
use Platformsh\Client\Model\Filter\Filter;
use Platformsh\Client\Model\Organization\Organization;
use Platformsh\Client\Model\Plan;
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\ProjectStub;
use Platformsh\Client\Model\Region;
use Platformsh\Client\Model\Result;
use Platformsh\Client\Model\SetupOptions;
use Platformsh\Client\Model\SshKey;
use Platformsh\Client\Model\Subscription;
use Platformsh\Client\Model\Subscription\SubscriptionOptions;
use Platformsh\Client\Model\Team\Team;
use Platformsh\Client\Model\User;
class PlatformClient
{
protected ConnectorInterface $connector;
/**
* @var array|null A per-client cache for account info
*/
protected ?array $accountInfo = null;
/**
* @var string|false|null A per-client cache for the user ID
*/
protected false|null|string $userId;
public function __construct(ConnectorInterface $connector = null)
{
$this->connector = $connector ?: new Connector();
}
public function getConnector(): ConnectorInterface
{
return $this->connector;
}
/**
* Get a single project by its ID.
*/
public function getProject(string $id, string $hostname = null, bool $https = true): Project|false
{
// Look for a project directly if the hostname is known.
if ($hostname !== null) {
return $this->getProjectDirect($id, $hostname, $https);
}
// Use the API gateway.
$apiUrl = $this->connector->getApiUrl();
if ($apiUrl) {
$project = Project::get($id, $apiUrl . '/projects', $this->connector->getClient());
if ($project) {
$project->setApiUrl($apiUrl);
}
return $project;
}
// Use the project locator.
if ($url = $this->locateProject($id)) {
$project = Project::get($url, null, $this->connector->getClient());
if ($project && ($apiUrl = $this->connector->getApiUrl())) {
$project->setApiUrl($apiUrl);
}
return $project;
}
return false;
}
/**
* Get the logged-in user's projects.
*
* @return Project[]
*@deprecated replaced by getMyProjects()
*/
public function getProjects(bool $reset = false): array
{
$data = $this->getAccountInfo($reset);
$client = $this->connector->getClient();
$apiUrl = $this->connector->getApiUrl();
$projects = [];
foreach ($data['projects'] as $data) {
// Each project has its own endpoint on a Platform.sh region.
$project = new Project($data, $data['endpoint'], $client);
if ($apiUrl) {
$project->setApiUrl($apiUrl);
}
$projects[] = $project;
}
return $projects;
}
/**
* Returns the logged-in user's project stubs.
*
* @return ProjectStub[]
*@deprecated replaced by getMyProjects()
*/
public function getProjectStubs(bool $reset = false): array
{
return ProjectStub::wrapCollection($this->getAccountInfo($reset), $this->apiUrl(), $this->connector->getClient());
}
/**
* Returns all the projects that the current user can access.
*
* @return BasicProjectInfo[]
* A list of basic project information.
*/
public function getMyProjects(string $vendor = null): array
{
$projects = [];
if (! empty($this->connector->getConfig()['centralized_permissions_enabled'])) {
$userId = $this->getMyUserId();
if ($userId === false) {
throw new \InvalidArgumentException('No user ID specified');
}
$strict = ! empty($this->connector->getConfig()['strict_project_references']);
$extendedAccesses = UserExtendedAccess::byUser($userId, [
'query' => [
'filter[resource_type]' => 'project',
],
], $this->connector->getClient());
foreach ($extendedAccesses as $extendedAccess) {
try {
$project = BasicProjectInfo::fromExtendedAccess($extendedAccess);
if ($vendor === null || $vendor === $project->vendor) {
$projects[] = $project;
}
} catch (ProjectReferenceException $e) {
// This exception may be thrown on non-production
// environments where grants and project reference
// information are not correctly synchronized.
if ($strict) {
throw $e;
}
trigger_error($e->getMessage(), E_USER_WARNING);
}
}
} else {
foreach ($this->getProjectStubs() as $stub) {
$projects[] = BasicProjectInfo::fromStub($stub);
}
}
return $projects;
}
/**
* Get account information for the logged-in user.
*
* This information includes various integrated details such as the
* projects the user can access, their registered SSH keys, and legacy
* information.
*
* For projects, getMyProjects() is recommended.
* For purely user profile related information, getUser() is recommended.
*
*@see PlatformClient::getUser()
*/
public function getAccountInfo(bool $reset = false): ?array
{
if (! isset($this->accountInfo) || $reset) {
$url = $this->apiUrl() . '/me';
try {
$this->accountInfo = $this->simpleGet($url);
} catch (GuzzleException $e) {
throw ApiResponseException::wrapGuzzleException($e);
}
}
return $this->accountInfo;
}
/**
* Get a single project at a known location.
*
* @param string $id The project ID.
* @param string $hostname The hostname of the Platform.sh regional API,
* e.g. 'eu.platform.sh' or 'us.platform.sh'.
* @param bool $https Whether to use HTTPS (default: true).
*
*@internal It's now better to use getProject(). This method will be made
* private in a future release.
*/
public function getProjectDirect(string $id, string $hostname, bool $https = true): Project|false
{
$scheme = $https ? 'https' : 'http';
$collection = "{$scheme}://{$hostname}/api/projects";
$project = Project::get($id, $collection, $this->connector->getClient());
if ($project && ($apiUrl = $this->connector->getApiUrl())) {
$project->setApiUrl($apiUrl);
}
return $project;
}
/**
* Get the logged-in user's SSH keys.
*
* @return SshKey[]
*/
public function getSshKeys(bool $reset = false): array
{
$data = $this->getAccountInfo($reset);
return SshKey::wrapCollection($data['ssh_keys'], $this->apiUrl() . '/ssh_keys', $this->connector->getClient());
}
/**
* Get a single SSH key by its ID.
*/
public function getSshKey(int|string $id): false|SshKey
{
$url = $this->apiUrl() . '/ssh_keys';
return SshKey::get($id, $url, $this->connector->getClient());
}
/**
* Add an SSH public key to the logged-in user's account.
*
* @param string $value The SSH key value.
* @param string|null $title A title for the key (optional).
*/
public function addSshKey(string $value, string $title = null): Result
{
$values = $this->cleanRequest([
'value' => $value,
'title' => $title,
]);
$url = $this->apiUrl() . '/ssh_keys';
return SshKey::create($values, $url, $this->connector->getClient());
}
/**
* Create a new Platform.sh subscription.
*
* @param string|SubscriptionOptions $options
* Subscription request options, which override the other arguments.
* If a string is passed, it will be used as the region ID (deprecated). See getRegions().
* @param string|null $plan The plan. See getPlans(). @deprecated
* @param string|null $title The project title. @deprecated
* @param int|null $storage The storage of each environment, in MiB. @deprecated
* @param int|null $environments The number of available environments. @deprecated
* @param array|null $activation_callback An activation callback for the subscription. @deprecated
* @param string|null $options_url The catalog options URL. See getCatalog(). @deprecated
*
* @return Subscription
* A subscription, representing a project. Use Subscription::wait() or
* similar code to wait for the subscription's project to be provisioned
* and activated.
*
* @see PlatformClient::getCatalog()
* @see PlatformClient::getRegions()
* @see Subscription::wait()
*/
public function createSubscription(SubscriptionOptions|string $options, string $plan = null, string $title = null, int $storage = null, int $environments = null, array $activation_callback = null, string $options_url = null): Subscription
{
if ($options instanceof SubscriptionOptions) {
$values = $options->toArray();
} else {
\trigger_error('The previous arguments list has been replaced by a single SubscriptionOptions argument', E_USER_DEPRECATED);
if ($plan === null) {
// Backwards-compatible default.
$plan = 'development';
}
$values = $this->cleanRequest([
'project_region' => $options,
'plan' => $plan,
'project_title' => $title,
'storage' => $storage,
'environments' => $environments,
'activation_callback' => $activation_callback,
'options_url' => $options_url,
]);
}
if ($id = $options->organizationId()) {
$url = $this->apiUrl() . '/organizations/' . \rawurlencode($id) . '/subscriptions';
} else {
$url = $this->apiUrl() . '/subscriptions';
}
return Subscription::create($values, $url, $this->connector->getClient());
}
/**
* Get a list of your Platform.sh subscriptions.
*
* @return Subscription[]
*/
public function getSubscriptions(string $organizationId = null): array
{
if (isset($organizationId)) {
$url = $this->apiUrl() . '/organizations/' . $organizationId . '/subscriptions';
} else {
$url = $this->apiUrl() . '/subscriptions';
}
return Subscription::getCollection($url, 0, [], $this->connector->getClient());
}
/**
* Get a subscription by its ID.
*/
public function getSubscription(int|string $id): Subscription|false
{
$url = $this->apiUrl() . '/subscriptions';
return Subscription::get($id, $url, $this->connector->getClient());
}
/**
* Estimate the cost of a subscription.
*
* @param string $plan The plan machine name.
* @param int $storage The allowed storage per environment (MiB).
* @param int $environments The number of environments.
* @param int $users The number of users.
* @param string|null $countryCode A two-letter country code.
* @param string|null $organizationId An organization ID.
*
* @return array An array containing at least 'total' (a formatted price).
*/
public function getSubscriptionEstimate(string $plan, int $storage, int $environments, int $users, string $countryCode = null, string $organizationId = null): array
{
$options = [];
$options['query'] = [
'plan' => $plan,
'storage' => $storage,
'environments' => $environments,
'user_licenses' => $users,
];
if ($countryCode !== null) {
$options['query']['country_code'] = $countryCode;
}
if ($organizationId) {
$url = $this->apiUrl() . '/organizations/' . \rawurlencode($organizationId) . '/subscriptions/estimate';
} else {
$url = $this->apiUrl() . '/subscriptions/estimate';
}
return $this->simpleGet($url, $options);
}
/**
* Get a list of available plans.
*
* @return Plan[]
*/
public function getPlans(): array
{
return Plan::getCollection($this->apiUrl() . '/plans', 0, [], $this->getConnector()->getClient());
}
/**
* Get a list of available regions.
*
* @return Region[]
*/
public function getRegions(): array
{
return Region::getCollection($this->apiUrl() . '/regions', 0, [], $this->getConnector()->getClient());
}
/**
* Get plan records.
*
* @param PlanRecordQuery|null $query A query to restrict the returned plans.
*
* @return PlanRecord[]
*/
public function getPlanRecords(PlanRecordQuery $query = null): array
{
$url = $this->apiUrl() . '/records/plan';
$options = [];
if ($query) {
$options['query'] = $query->getParams();
}
return PlanRecord::getCollection($url, 0, $options, $this->connector->getClient());
}
/**
* Request an SSH certificate.
*
* @param string $publicKey
* The contents of an SSH public key. Do not reuse a key that had other
* purposes: generate a dedicated key pair for the current user.
*
* @return string
* An SSH certificate, which should be saved alongside the SSH key pair,
* e.g. as "id_rsa-cert.pub", alongside "id_rsa" and "id_rsa.pub".
*/
public function getSshCertificate(string $publicKey): string
{
$response = $this->connector->getClient()->post(
Psr7Utils::uriFor($this->connector->getConfig()['certifier_url'])->withPath('/ssh'),
[
'json' => [
'key' => $publicKey,
],
]
);
return Utils::jsonDecode((string) $response->getBody(), true)['certificate'];
}
/**
* Get the project options catalog.
*
* @return \Platformsh\Client\Model\CatalogItem[]
*/
public function getCatalog(): array
{
return Catalog::create([], $this->apiUrl() . '/setup/catalog', $this->getConnector()->getClient());
}
/**
* Get the setup options file for a user.
*
* @param string|null $vendor The query string containing the vendor machine name.
* @param string|null $plan The machine name of the plan which has been selected during the project setup process.
* @param string|null $options_url The URL of a project options file which has been selected as a setup template.
* @param string|null $username The name of the account for which the project is to be created.
* @param string|null $organization The name of the organization for which the project is to be created.
*/
public function getSetupOptions(string $vendor = null, string $plan = null, string $options_url = null, string $username = null, string $organization = null): SetupOptions
{
$url = $this->apiUrl() . '/setup/options';
$options = $this->cleanRequest([
'vendor' => $vendor,
'plan' => $plan,
'options_url' => $options_url,
'username' => $username,
'organization' => $organization,
]);
return SetupOptions::create($options, $url, $this->connector->getClient());
}
/**
* Get a user account.
*
* @param string|null $id
* The user ID. Defaults to the current user.
*/
public function getUser(string $id = null): false|User
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
if ($id === null) {
$id = 'me';
}
return User::get($id, $this->connector->getApiUrl() . '/users', $this->connector->getClient());
}
/**
* Returns the current user's ID, if any.
*
* @param bool $reset Reset the per-client cache.
*
* @return string|false
* The user ID, or false if the access token is not associated with a user.
*/
public function getMyUserId(bool $reset = false): string|false
{
if (isset($this->userId) && ! $reset) {
return $this->userId;
}
$accessToken = $this->connector->getAccessToken();
if ($accessToken && ($claims = $this->unsafeGetJwtClaims($accessToken))) {
if (! empty($claims['sub']) && preg_match('/^[a-zA-Z0-9-]+$/', $claims['sub']) === 1) {
return $this->userId = $claims['sub'];
}
return $this->userId = false;
}
try {
return $this->userId = $this->getUser('me')->id;
} catch (BadResponseException $e) {
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 403) {
return $this->userId = false;
}
throw $e;
}
}
/**
* Lists all available organizations.
*
* @param \Platformsh\Client\Model\Filter\FilterInterface[] $filters
*
* @return Organization[]
*/
public function listOrganizations(array $filters = []): array
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
$path = '/organizations';
$options = [];
if (! empty($filters)) {
$options['query'] = [];
foreach ($filters as $filter) {
$options['query'] += $filter->params();
}
}
return Organization::getCollection($this->connector->getApiUrl() . $path, 0, $options, $this->connector->getClient());
}
/**
* Lists organizations of which the given user is a member.
*
* @return Organization[]
*/
public function listOrganizationsWithMember(string $userId): array
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
$path = '/users/' . \rawurlencode($userId) . '/organizations';
return Organization::getCollection($path, 0, [], $this->connector->getClient());
}
/**
* Lists organizations owned by the given user ID.
*
* @return Organization[]
*/
public function listOrganizationsByOwner(string $ownerId): array
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
return $this->listOrganizations([new Filter('owner_id', $ownerId)]);
}
/**
* Gets a single organization by name.
*/
public function getOrganizationByName(string $name): Organization|false
{
return $this->getOrganizationById('name=' . $name);
}
/**
* Gets a single organization.
*/
public function getOrganizationById(string $id): Organization|false
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
return Organization::get($id, '/organizations', $this->connector->getClient());
}
/**
* Creates a new organization.
*
* Warning: owning more than 1 organization will cause certain deprecated
* APIs to stop working. The /subscriptions API now must be accessed under
* /organizations/{id}/subscriptions, and the same applies to similar APIs
* that are concerned with subscriptions or billing. The old API path will
* only continue to work for users who own just 1 organization (or 0).
*
* @param string $name The organization name (unique, "machine-readable", used in the URL path).
* @param string $label The organization label ("human-readable").
* @param string $country An ISO 2-letter country code.
* @param string $owner The organization owner ID. Leave empty to use the current user.
* @param string $type The organization type. Leave blank to use the default.
*/
public function createOrganization(string $name, string $label = '', string $country = '', string $owner = '', string $type = ''): Organization
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
$url = '/organizations';
$values = [
'name' => $name,
'label' => $label,
'country' => $country,
];
if ($owner !== '') {
$values['owner_id'] = $owner;
}
if ($type !== '') {
$values['type'] = $type;
}
return Organization::create($values, $url, $this->connector->getClient());
}
/**
* Fetches a team by ID.
*
*@throws \RuntimeException if the given organization and team IDs conflict
*/
public function getTeam(string $id, Organization $organization = null): false|Team
{
if (! $this->connector->getApiUrl()) {
throw new \RuntimeException('No API URL configured');
}
$team = Team::get($id, '/teams', $this->connector->getClient());
if ($organization && $team && $team->organization_id !== $organization->id) {
throw new \RuntimeException(sprintf('Found team %s, but it is not part of the specified organization %s', $team->id, $organization->id));
}
return $team;
}
/**
* Locate a project by ID.
*
* @param string $id
* The project ID.
*
* @return string|false
* The project's API endpoint.
*/
protected function locateProject(string $id): false|string
{
if (! $this->connector instanceof Connector) {
return false;
}
$url = rtrim($this->connector->getAccountsEndpoint(), '/') . '/projects/' . rawurlencode($id);
try {
$result = $this->simpleGet($url);
} catch (BadResponseException $e) {
$ignoredErrorCodes = [403, 404];
if (in_array($e->getResponse()->getStatusCode(), $ignoredErrorCodes, true)) {
return false;
}
throw ApiResponseException::wrapGuzzleException($e);
}
if (isset($result['endpoint'])) {
return $result['endpoint'];
}
if (isset($result['_links']['self']['href'])) {
return $result['_links']['self']['href'];
}
return false;
}
/**
* Filter a request array to remove null values.
*/
protected function cleanRequest(array $request): array
{
return array_filter($request, function ($element) {
return $element !== null;
});
}
/**
* Returns the base URL of the API, without trailing slash.
*/
private function apiUrl(): string
{
$url = $this->connector->getApiUrl();
if ($url === '' && $this->connector instanceof Connector) {
$url = rtrim($this->connector->getAccountsEndpoint(), '/');
}
return $url;
}
/**
* Get a URL and return the JSON-decoded response.
*
* @throws GuzzleException
*/
private function simpleGet(string $url, array $options = []): array
{
return (array) Utils::jsonDecode(
$this->getConnector()
->getClient()
->request('get', $url, $options)
->getBody()
->getContents(),
true
);
}
/**
* Returns the payload of a JWT without verification.
*/
private function unsafeGetJwtClaims(string $jwt): false|array
{
$split = explode('.', $jwt, 3);
if (! isset($split[1])) {
return false;
}
$json = base64_decode($split[1], true);
if (! $json) {
return false;
}
return json_decode($json, true) ?: false;
}
}