Skip to content

Commit a5c28c4

Browse files
Merge pull request #303 from arlina-espinoza/merge-hybrid-work
Merge hybrid work
2 parents b9cf9aa + e234133 commit a5c28c4

20 files changed

+590
-102
lines changed

CONTRIBUTING.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ vendor/bin/phpcs --standard=web/modules/contrib/apigee_edge/phpcs.xml.dist web/m
104104
git push -u origin patch-2:patch-2
105105
```
106106

107-
## Running tests
107+
## Set up environment variables
108108

109109
Before you could start testing this module some environment variables
110110
needs to be set on your system. These variables are:
@@ -124,6 +124,23 @@ You can set these environment variables multiple ways, either by defining them
124124
with `export` or `set` in the terminal or creating a copy of the `core/phpunit.xml.dist`
125125
file as `core/phpunit.xml` and specifying them in that file.
126126

127+
### Notes for testing using a Hybrid organization
128+
129+
If testing with a Hybrid organization, only the following three environment variables are required:
130+
131+
* `APIGEE_EDGE_INSTANCE_TYPE`: should be `hybrid`.
132+
* `APIGEE_EDGE_ORGANIZATION`
133+
* `APIGEE_EDGE_ACCOUNT_JSON_KEY`: the JSON encoded GCP service account key.
134+
135+
If you wish to run tests both against a Public and a Hybrid instance:
136+
137+
1. First configure the credentials to the public org as described above.
138+
2. Add the `APIGEE_EDGE_ACCOUNT_JSON_KEY` environment variable.
139+
3. Add a`APIGEE_EDGE_HYBRID_ORGANIZATION` environment variable, which specifies the Hybrid organization to use for tests.
140+
141+
142+
## Running tests
143+
127144
After you have these environment variables set you can execute tests of this
128145
module with the following command (note the location of the `phpunit` executable
129146
may vary):

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ some submodules are marked as "Experimental". They are provided for evaluation a
1717
considered to be in development. Experimental modules are included in the "Apigee (Experimental)" package
1818
on the "Extend" page of a Drupal site (/admin/modules).
1919

20+
## Support for Apigee Hybrid Cloud: Alpha Release
21+
22+
Support for [Apigee hybrid API](https://docs.apigee.com/hybrid/reference-overview) has been added but is considered to
23+
be an alpha. If you run into any problems, add an issue to our [GitHub issue queue](https://github.com/apigee/apigee-edge-drupal/issues).
24+
Please note that Team APIs and Monetization APIs are not currently supported on Apigee hybrid.
25+
2026
## Requirements
2127

2228
* The Apigee Edge module requires **Drupal 8.7.x** or higher and PHP 7.1 or higher.

apigee_edge.install

+18-14
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
* Install, update and uninstall functions for Apigee Edge.
2424
*/
2525

26-
use Drupal\apigee_edge\Plugin\KeyType\ApigeeAuthKeyType;
26+
use Apigee\Edge\Utility\OrganizationFeatures;
2727
use Drupal\apigee_edge\OauthTokenFileStorage;
28-
use Drupal\Component\Serialization\Json;
2928
use Drupal\Core\Url;
30-
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
3129
use Drupal\user\RoleInterface;
3230

3331
/**
3432
* Implements hook_requirements().
3533
*/
3634
function apigee_edge_requirements($phase) {
3735
$requirements = [];
36+
$hybrid_support_message = t('Support for Apigee hybrid in the Apigee modules is in Alpha. Connecting to a hybrid organization is appropriate for evaluation and testing purposes during this pre-production stage.');
3837

3938
if ($phase === 'install') {
4039
// This should be checked only if Drupal is installed.
@@ -51,12 +50,26 @@ function apigee_edge_requirements($phase) {
5150
];
5251
}
5352
}
53+
54+
\Drupal::messenger()->addWarning($hybrid_support_message);
5455
}
5556
elseif ($phase === 'runtime') {
5657
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
5758
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
5859
try {
5960
$sdk_connector->testConnection();
61+
62+
// Hybrid support warning.
63+
$org_controller = \Drupal::service('apigee_edge.controller.organization');
64+
/* @var \Apigee\Edge\Api\Management\Entity\Organization $organization */
65+
$organization = $org_controller->load($sdk_connector->getOrganization());
66+
if ($organization && OrganizationFeatures::isHybridEnabled($organization)) {
67+
$requirements['apigee_edge_hybrid_support'] = [
68+
'title' => t('Apigee Edge'),
69+
'description' => $hybrid_support_message,
70+
'severity' => REQUIREMENT_WARNING,
71+
];
72+
}
6073
}
6174
catch (\Exception $exception) {
6275
$requirements['apigee_edge_connection_error'] = [
@@ -166,15 +179,6 @@ function apigee_edge_update_8001() {
166179
* Update defaults on Apigee Auth Keys.
167180
*/
168181
function apigee_edge_update_8002() {
169-
$keys = \Drupal::service('key.repository')->getKeys();
170-
foreach ($keys as $key) {
171-
/* @var \Drupal\key\Entity\Key $key */
172-
if ($key->getKeyType() instanceof ApigeeAuthKeyType && $key->getKeyProvider() instanceof KeyProviderSettableValueInterface) {
173-
$values = $key->getKeyValues();
174-
$values['endpoint_type'] = $values['endpoint'] ? 'custom' : 'default';
175-
$values['authorization_server_type'] = $values['authorization_server'] ? 'custom' : 'default';
176-
$key->setKeyValue(Json::encode($values));
177-
$key->save();
178-
}
179-
}
182+
// Empty.
183+
// Removed because Hybrid support makes this update hook unneeded.
180184
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Apigee Edge for Drupal.",
66
"require": {
77
"php": ">=7.1",
8-
"apigee/apigee-client-php": "^2.0.1",
8+
"apigee/apigee-client-php": "^2.0.4",
99
"cweagans/composer-patches": "^1.6.5",
1010
"drupal/core": "~8.7.0",
1111
"drupal/entity": "^1.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Copyright 2019 Google Inc.
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License version 2 as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14+
* License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc., 51
18+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
use Apigee\Edge\Utility\OrganizationFeatures;
22+
23+
/**
24+
* @file
25+
* Install, update and uninstall functions for Apigee Edge Teams.
26+
*/
27+
28+
/**
29+
* Implements hook_requirements().
30+
*/
31+
function apigee_edge_teams_requirements($phase) {
32+
$requirements = [];
33+
34+
if ($phase == 'install' || $phase == 'runtime') {
35+
try {
36+
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
37+
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
38+
$org_controller = \Drupal::service('apigee_edge.controller.organization');
39+
/* @var \Apigee\Edge\Api\Management\Entity\Organization $organization */
40+
$organization = $org_controller->load($sdk_connector->getOrganization());
41+
if ($organization && !OrganizationFeatures::isCompaniesFeatureAvailable($organization)) {
42+
$url = [
43+
':url' => 'https://docs.apigee.com/hybrid/compare-hybrid-edge#unsupported-apis',
44+
];
45+
$message = ($phase == 'runtime') ?
46+
t("The Apigee Edge Teams module functionality is not available for your org and should be uninstalled, because <a href=':url' target='_blank'>Edge company APIs are not supported in Apigee hybrid orgs</a>.", $url) :
47+
t("The Apigee Edge Teams module functionality is not available for your org because <a href=':url' target='_blank'>Edge company APIs are not supported in Apigee hybrid orgs</a>.", $url);
48+
$requirements['apigee_edge_teams_not_supported'] = [
49+
'title' => t('Apigee Edge Teams'),
50+
'description' => $message,
51+
'severity' => REQUIREMENT_ERROR,
52+
];
53+
}
54+
}
55+
catch (\Exception $exception) {
56+
// Do nothing if connection to Edge is not available.
57+
}
58+
}
59+
60+
return $requirements;
61+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 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\Connector;
22+
23+
use Apigee\Edge\ClientInterface;
24+
use Apigee\Edge\HttpClient\Plugin\Authentication\HybridOauth2;
25+
use Apigee\Edge\HttpClient\Plugin\Authentication\NullAuthentication;
26+
27+
/**
28+
* Decorator for Hybrid authentication plugin.
29+
*/
30+
class HybridAuthentication extends HybridOauth2 {
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function authClient(): ClientInterface {
36+
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
37+
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
38+
return $sdk_connector->buildClient(new NullAuthentication(), $this->getAuthServer());
39+
}
40+
41+
}

src/Connector/HybridCredentials.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or modify it under
7+
* the terms of the GNU General Public License version 2 as published by the
8+
* Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc., 51
17+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*/
19+
20+
namespace Drupal\apigee_edge\Connector;
21+
22+
use Drupal\apigee_edge\Credentials;
23+
use Drupal\apigee_edge\Exception\InvalidArgumentException;
24+
use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
25+
use Drupal\key\KeyInterface;
26+
27+
/**
28+
* The API credentials for HybridCredentials.
29+
*/
30+
class HybridCredentials extends Credentials {
31+
32+
/**
33+
* HybridCredentials constructor.
34+
*
35+
* @param \Drupal\key\KeyInterface $key
36+
* The key entity which stores the API credentials.
37+
*
38+
* @throws \InvalidArgumentException
39+
* An InvalidArgumentException is thrown if the key type
40+
* does not implement EdgeKeyTypeInterface.
41+
*/
42+
public function __construct(KeyInterface $key) {
43+
if ($key->getKeyType() instanceof EdgeKeyTypeInterface
44+
&& ($auth_type = $key->getKeyType()->getAuthenticationType($key))
45+
&& $auth_type === EdgeKeyTypeInterface::EDGE_AUTH_TYPE_JWT
46+
) {
47+
parent::__construct($key);
48+
}
49+
else {
50+
throw new InvalidArgumentException("The `{$key->id()}` key is not configured for Hybrid Authentication.");
51+
}
52+
}
53+
54+
}

src/Credentials.php

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
/**
2727
* The API credentials.
28+
*
29+
* @todo: move to \Drupal\apigee_edge\Connector namespace.
2830
*/
2931
class Credentials implements CredentialsInterface {
3032

src/CredentialsInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
/**
2727
* Defines an interface for credentials classes.
28+
*
29+
* @todo: move to \Drupal\apigee_edge\Connector namespace.
2830
*/
2931
interface CredentialsInterface {
3032

0 commit comments

Comments
 (0)