Skip to content

Commit

Permalink
Merge pull request #303 from arlina-espinoza/merge-hybrid-work
Browse files Browse the repository at this point in the history
Merge hybrid work
  • Loading branch information
arlina-espinoza authored Dec 5, 2019
2 parents b9cf9aa + e234133 commit a5c28c4
Show file tree
Hide file tree
Showing 20 changed files with 590 additions and 102 deletions.
19 changes: 18 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ vendor/bin/phpcs --standard=web/modules/contrib/apigee_edge/phpcs.xml.dist web/m
git push -u origin patch-2:patch-2
```

## Running tests
## Set up environment variables

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

### Notes for testing using a Hybrid organization

If testing with a Hybrid organization, only the following three environment variables are required:

* `APIGEE_EDGE_INSTANCE_TYPE`: should be `hybrid`.
* `APIGEE_EDGE_ORGANIZATION`
* `APIGEE_EDGE_ACCOUNT_JSON_KEY`: the JSON encoded GCP service account key.

If you wish to run tests both against a Public and a Hybrid instance:

1. First configure the credentials to the public org as described above.
2. Add the `APIGEE_EDGE_ACCOUNT_JSON_KEY` environment variable.
3. Add a`APIGEE_EDGE_HYBRID_ORGANIZATION` environment variable, which specifies the Hybrid organization to use for tests.


## Running tests

After you have these environment variables set you can execute tests of this
module with the following command (note the location of the `phpunit` executable
may vary):
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ some submodules are marked as "Experimental". They are provided for evaluation a
considered to be in development. Experimental modules are included in the "Apigee (Experimental)" package
on the "Extend" page of a Drupal site (/admin/modules).

## Support for Apigee Hybrid Cloud: Alpha Release

Support for [Apigee hybrid API](https://docs.apigee.com/hybrid/reference-overview) has been added but is considered to
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).
Please note that Team APIs and Monetization APIs are not currently supported on Apigee hybrid.

## Requirements

* The Apigee Edge module requires **Drupal 8.7.x** or higher and PHP 7.1 or higher.
Expand Down
32 changes: 18 additions & 14 deletions apigee_edge.install
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
* Install, update and uninstall functions for Apigee Edge.
*/

use Drupal\apigee_edge\Plugin\KeyType\ApigeeAuthKeyType;
use Apigee\Edge\Utility\OrganizationFeatures;
use Drupal\apigee_edge\OauthTokenFileStorage;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
use Drupal\user\RoleInterface;

/**
* Implements hook_requirements().
*/
function apigee_edge_requirements($phase) {
$requirements = [];
$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.');

if ($phase === 'install') {
// This should be checked only if Drupal is installed.
Expand All @@ -51,12 +50,26 @@ function apigee_edge_requirements($phase) {
];
}
}

\Drupal::messenger()->addWarning($hybrid_support_message);
}
elseif ($phase === 'runtime') {
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
try {
$sdk_connector->testConnection();

// Hybrid support warning.
$org_controller = \Drupal::service('apigee_edge.controller.organization');
/* @var \Apigee\Edge\Api\Management\Entity\Organization $organization */
$organization = $org_controller->load($sdk_connector->getOrganization());
if ($organization && OrganizationFeatures::isHybridEnabled($organization)) {
$requirements['apigee_edge_hybrid_support'] = [
'title' => t('Apigee Edge'),
'description' => $hybrid_support_message,
'severity' => REQUIREMENT_WARNING,
];
}
}
catch (\Exception $exception) {
$requirements['apigee_edge_connection_error'] = [
Expand Down Expand Up @@ -166,15 +179,6 @@ function apigee_edge_update_8001() {
* Update defaults on Apigee Auth Keys.
*/
function apigee_edge_update_8002() {
$keys = \Drupal::service('key.repository')->getKeys();
foreach ($keys as $key) {
/* @var \Drupal\key\Entity\Key $key */
if ($key->getKeyType() instanceof ApigeeAuthKeyType && $key->getKeyProvider() instanceof KeyProviderSettableValueInterface) {
$values = $key->getKeyValues();
$values['endpoint_type'] = $values['endpoint'] ? 'custom' : 'default';
$values['authorization_server_type'] = $values['authorization_server'] ? 'custom' : 'default';
$key->setKeyValue(Json::encode($values));
$key->save();
}
}
// Empty.
// Removed because Hybrid support makes this update hook unneeded.
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Apigee Edge for Drupal.",
"require": {
"php": ">=7.1",
"apigee/apigee-client-php": "^2.0.1",
"apigee/apigee-client-php": "^2.0.4",
"cweagans/composer-patches": "^1.6.5",
"drupal/core": "~8.7.0",
"drupal/entity": "^1.0",
Expand Down
61 changes: 61 additions & 0 deletions modules/apigee_edge_teams/apigee_edge_teams.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @file
* Copyright 2019 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

use Apigee\Edge\Utility\OrganizationFeatures;

/**
* @file
* Install, update and uninstall functions for Apigee Edge Teams.
*/

/**
* Implements hook_requirements().
*/
function apigee_edge_teams_requirements($phase) {
$requirements = [];

if ($phase == 'install' || $phase == 'runtime') {
try {
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
$org_controller = \Drupal::service('apigee_edge.controller.organization');
/* @var \Apigee\Edge\Api\Management\Entity\Organization $organization */
$organization = $org_controller->load($sdk_connector->getOrganization());
if ($organization && !OrganizationFeatures::isCompaniesFeatureAvailable($organization)) {
$url = [
':url' => 'https://docs.apigee.com/hybrid/compare-hybrid-edge#unsupported-apis',
];
$message = ($phase == 'runtime') ?
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) :
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);
$requirements['apigee_edge_teams_not_supported'] = [
'title' => t('Apigee Edge Teams'),
'description' => $message,
'severity' => REQUIREMENT_ERROR,
];
}
}
catch (\Exception $exception) {
// Do nothing if connection to Edge is not available.
}
}

return $requirements;
}
41 changes: 41 additions & 0 deletions src/Connector/HybridAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Copyright 2019 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

namespace Drupal\apigee_edge\Connector;

use Apigee\Edge\ClientInterface;
use Apigee\Edge\HttpClient\Plugin\Authentication\HybridOauth2;
use Apigee\Edge\HttpClient\Plugin\Authentication\NullAuthentication;

/**
* Decorator for Hybrid authentication plugin.
*/
class HybridAuthentication extends HybridOauth2 {

/**
* {@inheritdoc}
*/
protected function authClient(): ClientInterface {
/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
return $sdk_connector->buildClient(new NullAuthentication(), $this->getAuthServer());
}

}
54 changes: 54 additions & 0 deletions src/Connector/HybridCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Copyright 2019 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Drupal\apigee_edge\Connector;

use Drupal\apigee_edge\Credentials;
use Drupal\apigee_edge\Exception\InvalidArgumentException;
use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
use Drupal\key\KeyInterface;

/**
* The API credentials for HybridCredentials.
*/
class HybridCredentials extends Credentials {

/**
* HybridCredentials constructor.
*
* @param \Drupal\key\KeyInterface $key
* The key entity which stores the API credentials.
*
* @throws \InvalidArgumentException
* An InvalidArgumentException is thrown if the key type
* does not implement EdgeKeyTypeInterface.
*/
public function __construct(KeyInterface $key) {
if ($key->getKeyType() instanceof EdgeKeyTypeInterface
&& ($auth_type = $key->getKeyType()->getAuthenticationType($key))
&& $auth_type === EdgeKeyTypeInterface::EDGE_AUTH_TYPE_JWT
) {
parent::__construct($key);
}
else {
throw new InvalidArgumentException("The `{$key->id()}` key is not configured for Hybrid Authentication.");
}
}

}
2 changes: 2 additions & 0 deletions src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

/**
* The API credentials.
*
* @todo: move to \Drupal\apigee_edge\Connector namespace.
*/
class Credentials implements CredentialsInterface {

Expand Down
2 changes: 2 additions & 0 deletions src/CredentialsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

/**
* Defines an interface for credentials classes.
*
* @todo: move to \Drupal\apigee_edge\Connector namespace.
*/
interface CredentialsInterface {

Expand Down
Loading

0 comments on commit a5c28c4

Please sign in to comment.