|
20 | 20 | namespace Drupal\apigee_edge\Plugin\KeyInput;
|
21 | 21 |
|
22 | 22 | use Apigee\Edge\HttpClient\Plugin\Authentication\Oauth;
|
| 23 | +use Drupal\apigee_edge\Connector\GceServiceAccountAuthentication; |
23 | 24 | use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
|
24 | 25 | use Drupal\Component\Serialization\Json;
|
25 | 26 | use Drupal\Core\Form\FormStateInterface;
|
@@ -92,9 +93,11 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
|
92 | 93 | '#type' => 'textfield',
|
93 | 94 | '#title' => $this->t('Organization'),
|
94 | 95 | '#description' => $this->t('Name of the organization on Apigee Edge. Changing this value could make your site stop working.'),
|
95 |
| - '#required' => TRUE, |
96 | 96 | '#default_value' => $values['organization'] ?? '',
|
| 97 | + '#required' => TRUE, |
97 | 98 | '#attributes' => ['autocomplete' => 'off'],
|
| 99 | + '#prefix' => '<div id="edit-organization-field">', |
| 100 | + '#suffix' => '</div>', |
98 | 101 | ];
|
99 | 102 | $form['username'] = [
|
100 | 103 | '#type' => 'textfield',
|
@@ -123,15 +126,33 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
|
123 | 126 | if (empty($values['organization'])) {
|
124 | 127 | $form['password']['#states']['required'] = [$state_for_public, $state_for_private];
|
125 | 128 | }
|
| 129 | + |
| 130 | + $state_for_not_gcp_hosted = []; |
| 131 | + $gceServiceAccountAuth = new GceServiceAccountAuthentication(\Drupal::service('apigee_edge.authentication.oauth_token_storage')); |
| 132 | + if ($gceServiceAccountAuth->isAvailable()) { |
| 133 | + $form['gcp_hosted'] = [ |
| 134 | + '#type' => 'checkbox', |
| 135 | + '#title' => $this->t('Use the default service account if this portal is hosted on GCP'), |
| 136 | + '#description' => $this->t("Please ensure you have added 'Apigee Developer Administrator' role to the default compute engine service account hosting this portal."), |
| 137 | + '#default_value' => $values['gcp_hosted'] ?? TRUE, |
| 138 | + '#states' => [ |
| 139 | + 'visible' => $state_for_hybrid, |
| 140 | + ], |
| 141 | + ]; |
| 142 | + $state_for_not_gcp_hosted = [ |
| 143 | + ':input[name="key_input_settings[gcp_hosted]"]' => ['checked' => FALSE], |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
126 | 147 | $form['account_json_key'] = [
|
127 | 148 | '#type' => 'textarea',
|
128 | 149 | '#title' => $this->t('GCP service account key'),
|
129 | 150 | '#description' => $this->t("Paste the contents of the GCP service account key JSON file."),
|
130 | 151 | '#default_value' => $values['account_json_key'] ?? '',
|
131 | 152 | '#rows' => '8',
|
132 | 153 | '#states' => [
|
133 |
| - 'visible' => $state_for_hybrid, |
134 |
| - 'required' => $state_for_hybrid, |
| 154 | + 'visible' => $state_for_hybrid + $state_for_not_gcp_hosted, |
| 155 | + 'required' => $state_for_hybrid + $state_for_not_gcp_hosted, |
135 | 156 | ],
|
136 | 157 | ];
|
137 | 158 | $form['endpoint'] = [
|
@@ -232,8 +253,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
|
232 | 253 | */
|
233 | 254 | public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
234 | 255 | $input_values = $form_state->getUserInput()['key_input_settings'];
|
235 |
| - |
236 |
| - if ($input_values['instance_type'] == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) { |
| 256 | + if ($input_values['instance_type'] == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID && |
| 257 | + empty($input_values['gcp_hosted'])) { |
237 | 258 | $account_key = $input_values['account_json_key'] ?? '';
|
238 | 259 | $json = json_decode($account_key, TRUE);
|
239 | 260 | if (empty($json['private_key']) || empty($json['client_email'])) {
|
@@ -270,11 +291,16 @@ public function processSubmittedKeyValue(FormStateInterface $form_state) {
|
270 | 291 | $input_values['authorization_server'] = '';
|
271 | 292 | $input_values['client_id'] = '';
|
272 | 293 | $input_values['client_secret'] = '';
|
| 294 | + if (!empty($input_values['gcp_hosted'])) { |
| 295 | + $input_values['account_json_key'] = ''; |
| 296 | + } |
273 | 297 | }
|
274 | 298 | else {
|
275 | 299 | // Remove unneeded values if on a Public or Private instance.
|
276 | 300 | $input_values['account_json_key'] = '';
|
277 |
| - |
| 301 | + if (!empty($input_values['gcp_hosted'])) { |
| 302 | + unset($input_values['gcp_hosted']); |
| 303 | + } |
278 | 304 | // If password field is empty we just skip it and preserve the initial
|
279 | 305 | // password if there is one already.
|
280 | 306 | if (empty($input_values['password']) && !empty($form_state->get('key_value')['current'])) {
|
|
0 commit comments