|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * version 2 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Drupal\apigee_edge\Service; |
| 22 | + |
| 23 | +use Apigee\Edge\Api\Management\Controller\OrganizationController; |
| 24 | +use Apigee\Edge\ClientInterface; |
| 25 | +use Drupal\apigee_edge\SDKConnectorInterface; |
| 26 | +use Drupal\Core\State\StateInterface; |
| 27 | +use Drupal\key\KeyInterface; |
| 28 | +use Drupal\Core\Messenger\MessengerInterface; |
| 29 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 30 | +use Drupal\Core\StringTranslation\TranslationInterface; |
| 31 | + |
| 32 | +/** |
| 33 | + * Discovers the data residency endpoint for a given key. |
| 34 | + */ |
| 35 | +class DataResidencyEndpoint implements DataResidencyEndpointInterface { |
| 36 | + use StringTranslationTrait; |
| 37 | + |
| 38 | + /** |
| 39 | + * The SDK connector service. |
| 40 | + * |
| 41 | + * @var \Drupal\apigee_edge\SDKConnectorInterface |
| 42 | + */ |
| 43 | + protected $sdkConnector; |
| 44 | + |
| 45 | + /** |
| 46 | + * The state service. |
| 47 | + * |
| 48 | + * @var \Drupal\Core\State\StateInterface |
| 49 | + */ |
| 50 | + protected $state; |
| 51 | + |
| 52 | + /** |
| 53 | + * The messenger service. |
| 54 | + * |
| 55 | + * @var \Drupal\Core\Messenger\MessengerInterface |
| 56 | + */ |
| 57 | + protected $messenger; |
| 58 | + |
| 59 | + /** |
| 60 | + * The string translation service. |
| 61 | + * |
| 62 | + * @var \Drupal\Core\StringTranslation\TranslationInterface |
| 63 | + */ |
| 64 | + protected $stringTranslation; |
| 65 | + |
| 66 | + /** |
| 67 | + * Constructs a new DataResidencyEndpoint object. |
| 68 | + * |
| 69 | + * @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector |
| 70 | + * The SDK connector service. |
| 71 | + * @param \Drupal\Core\State\StateInterface $state |
| 72 | + * The state service. |
| 73 | + * @param \Drupal\Core\Messenger\MessengerInterface $messenger |
| 74 | + * The messenger service. |
| 75 | + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation |
| 76 | + * String translation. |
| 77 | + */ |
| 78 | + public function __construct(SDKConnectorInterface $sdk_connector, StateInterface $state, MessengerInterface $messenger, TranslationInterface $string_translation) { |
| 79 | + $this->sdkConnector = $sdk_connector; |
| 80 | + $this->state = $state; |
| 81 | + $this->messenger = $messenger; |
| 82 | + $this->stringTranslation = $string_translation; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + public function getEndpoint(KeyInterface $key): void { |
| 89 | + /** @var \Drupal\apigee_edge\Plugin\KeyType\ApigeeAuthKeyType $key_type */ |
| 90 | + $key_type = $key->getKeyType(); |
| 91 | + |
| 92 | + try { |
| 93 | + $base_endpoint = ClientInterface::APIGEE_ON_GCP_ENDPOINT; |
| 94 | + |
| 95 | + $client = $this->sdkConnector->buildClient($key_type->getAuthenticationMethod($key), $base_endpoint); |
| 96 | + $orgController = new OrganizationController($client); |
| 97 | + $dataResidencyData = $orgController->getProjectMapping($key_type->getOrganization($key)); |
| 98 | + |
| 99 | + if (isset($dataResidencyData['location']) && $dataResidencyData['location']) { |
| 100 | + $dataResidencyEndpoint = str_replace("https://", "https://{$dataResidencyData['location']}-", $base_endpoint); |
| 101 | + |
| 102 | + $this->state->set(self::DRZ_ENDPOINT, $dataResidencyEndpoint); |
| 103 | + $this->messenger->addStatus($this->stringTranslation->translate('Data residency is enabled for this organization. Service endpoint being used is @serviceEndpoint', ['@serviceEndpoint' => $dataResidencyEndpoint])); |
| 104 | + } |
| 105 | + else { |
| 106 | + $this->state->delete(self::DRZ_ENDPOINT); |
| 107 | + } |
| 108 | + } |
| 109 | + catch (\Exception $e) { |
| 110 | + // We can ignore this exception, as it means that the data residency |
| 111 | + // endpoint could not be determined. |
| 112 | + $this->state->delete(self::DRZ_ENDPOINT); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments