Skip to content

Commit

Permalink
AppGroup-App implementation for list, view, edit, delete apps (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-intelli authored and giteshk committed Mar 24, 2023
1 parent 98a80ed commit ccacbcb
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 61 deletions.
1 change: 0 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
fail-fast: false
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
drupal-core:
Expand Down
2 changes: 1 addition & 1 deletion apigee_edge.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ dependencies:

configure: apigee_edge.settings

php: "7.1"
php: "8.0"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "drupal-module",
"description": "Apigee Edge for Drupal.",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"ext-json": "*",
"apigee/apigee-client-php": "dev-appgroup",
"drupal/core": "^9.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function testEvent() {
]);
$config_entity->save();

// Add matched organization response so it returns the org whenever called.
$this->addOrganizationMatchedResponse();

// Insert and update entity.
/** @var \Drupal\apigee_edge\Entity\DeveloperAppInterface $entity */
$entity = $this->createDeveloperApp();
Expand Down
40 changes: 0 additions & 40 deletions modules/apigee_edge_teams/apigee_edge_teams.install
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,10 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

use Apigee\Edge\Utility\OrganizationFeatures;
use Drupal\Core\Config\FileStorage;
use Drupal\user\RoleInterface;
use Drupal\views\Views;

/**
* 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://cloud.google.com/apigee/docs/api-platform/get-started/compare-apigee-products#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 X and 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 X and 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;
}

/**
* Implements hook_install().
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* Copyright 2023 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_teams\Entity\Controller;

use Apigee\Edge\Api\Management\Entity\AppInterface;
use Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface;
use Apigee\Edge\Entity\EntityInterface;
use Drupal\apigee_edge\Entity\Controller\AppControllerInterface;
use Drupal\apigee_edge\Entity\Controller\EdgeEntityControllerInterface;
use Drupal\apigee_edge\Exception\RuntimeException;

/**
* Team app specific entity controller implementation.
*
* It ensures that the right SDK controllers (and with that the right API
* endpoints) gets used for CRUDL operations.
*/
final class TeamAppApigeeXEntityControllerProxy implements EdgeEntityControllerInterface {

/**
* The team app controller factory service.
*
* @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppControllerFactoryInterface
*/
private $teamAppControllerFactory;

/**
* The app controller service.
*
* @var \Drupal\apigee_edge\Entity\Controller\AppControllerInterface
*/
private $appController;

/**
* TeamAppApigeeXEntityControllerProxy constructor.
*
* @param \Drupal\apigee_edge_teams\Entity\Controller\TeamAppControllerFactoryInterface $team_app_controller_factory
* The team app controller factory service.
* @param \Drupal\apigee_edge\Entity\Controller\AppControllerInterface $app_controller
* The app controller service.
*/
public function __construct(TeamAppControllerFactoryInterface $team_app_controller_factory, AppControllerInterface $app_controller) {
$this->teamAppControllerFactory = $team_app_controller_factory;
$this->appController = $app_controller;
}

/**
* {@inheritdoc}
*/
public function create(EntityInterface $entity): void {
/** @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface $entity */
if (empty($entity->getAppGroup())) {
// Sanity check.
throw new RuntimeException('AppGroup name has to set on the app.');
}
$controller = $this->teamAppControllerFactory->teamAppController($entity->getAppGroup());
$controller->create($entity);
}

/**
* {@inheritdoc}
*/
public function load(string $id): EntityInterface {
return $this->appController->loadAppGroup($id);
}

/**
* {@inheritdoc}
*/
public function update(EntityInterface $entity): void {
/** @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface $entity */
$controller = $this->teamAppControllerFactory->teamAppController($entity->getAppGroup());
$controller->update($entity);
}

/**
* {@inheritdoc}
*/
public function delete(string $id): void {
// Try to be smart here and load the app from the appgroup app
// entity cache (app controller's cache is probably empty unless there were
// a load() or getEntities() call before).
$entity = \Drupal::entityTypeManager()->getStorage('team_app')->load($id);
if (!$entity) {
// Entity has not found in the entity cache, we have it from Apigee Edge.
$entity = $this->load($id);
}
/** @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface $entity */
$controller = $this->teamAppControllerFactory->teamAppController($entity->getAppGroup());
// The id that we got is a UUID, what we need is an app name.
$controller->delete($entity->getName());
}

/**
* {@inheritdoc}
*/
public function loadAll(): array {
return array_filter($this->appController->listApps(TRUE), function (AppInterface $app) {
return $app instanceof AppGroupAppInterface;
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Drupal\apigee_edge_teams\Entity\Controller;

use Apigee\Edge\Api\ApigeeX\Controller\AppGroupAppController as EdgeAppGroupAppController;
use Apigee\Edge\Api\Management\Controller\AppByOwnerControllerInterface as EdgeAppByOwnerControllerInterface;
use Apigee\Edge\Api\Management\Controller\CompanyAppController as EdgeCompanyAppController;
use Drupal\apigee_edge\Entity\Controller\AppByOwnerController;
Expand All @@ -36,7 +37,14 @@ final class TeamAppController extends AppByOwnerController implements TeamAppCon
*/
protected function decorated(): EdgeAppByOwnerControllerInterface {
if (!isset($this->instances[$this->owner])) {
$this->instances[$this->owner] = new EdgeCompanyAppController($this->connector->getOrganization(), $this->owner, $this->connector->getClient(), NULL, $this->organizationController);

// Checking if the Organisation is Hybrid/ApigeeX.
if ($this->isOrgApigeeX()) {
$this->instances[$this->owner] = new EdgeAppGroupAppController($this->connector->getOrganization(), $this->owner, $this->connector->getClient(), NULL, $this->organizationController);
}
else {
$this->instances[$this->owner] = new EdgeCompanyAppController($this->connector->getOrganization(), $this->owner, $this->connector->getClient(), NULL, $this->organizationController);
}
}
return $this->instances[$this->owner];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Drupal\apigee_edge\Entity\Storage\AppStorage;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppControllerFactoryInterface;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppEdgeEntityControllerProxy;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppApigeeXEntityControllerProxy;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
Expand Down Expand Up @@ -83,7 +84,13 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
* {@inheritdoc}
*/
protected function entityController(): EdgeEntityControllerInterface {
return new TeamAppEdgeEntityControllerProxy($this->teamAppControllerFactory, $this->appController);
if ($this->appController->isOrgApigeeX()) {
$teamAppEntityControllerProxy = new TeamAppApigeeXEntityControllerProxy($this->teamAppControllerFactory, $this->appController);
}
else {
$teamAppEntityControllerProxy = new TeamAppEdgeEntityControllerProxy($this->teamAppControllerFactory, $this->appController);
}
return $teamAppEntityControllerProxy;
}

/**
Expand Down
60 changes: 52 additions & 8 deletions modules/apigee_edge_teams/src/Entity/TeamApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Drupal\apigee_edge_teams\Entity;

use Apigee\Edge\Api\ApigeeX\Entity\AppGroupApp;
use Apigee\Edge\Api\Management\Entity\CompanyApp;
use Apigee\Edge\Entity\EntityInterface as EdgeEntityInterface;
use Drupal\apigee_edge\Entity\App;
Expand Down Expand Up @@ -87,7 +88,7 @@ class TeamApp extends App implements TeamAppInterface {
/**
* The decorated company app entity from the SDK.
*
* @var \Apigee\Edge\Api\Management\Entity\CompanyApp
* @var \Apigee\Edge\Api\Management\Entity\CompanyApp|\Apigee\Edge\Api\ApigeeX\Entity\AppGroupApp
*/
protected $decorated;

Expand All @@ -103,7 +104,7 @@ class TeamApp extends App implements TeamAppInterface {
* The SDK entity that this Drupal entity decorates.
*/
public function __construct(array $values, ?string $entity_type = NULL, ?EdgeEntityInterface $decorated = NULL) {
/** @var \Apigee\Edge\Api\Management\Entity\CompanyAppInterface $decorated */
/** @var \Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface|\Apigee\Edge\Api\Management\Entity\CompanyAppInterface $decorated */
$entity_type = $entity_type ?? 'team_app';
parent::__construct($values, $entity_type, $decorated);
}
Expand All @@ -122,14 +123,26 @@ public function id(): ?string {
* {@inheritdoc}
*/
public function getAppOwner(): ?string {
return $this->decorated->getCompanyName();
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
return $this->decorated->getAppGroup();
}
else {
return $this->decorated->getCompanyName();
}
}

/**
* {@inheritdoc}
*/
public function setAppOwner(string $owner): void {
$this->decorated->setCompanyName($owner);
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
$this->decorated->setAppGroup($owner);
}
else {
$this->decorated->setCompanyName($owner);
}
}

/**
Expand All @@ -139,18 +152,37 @@ public function getCompanyName(): ?string {
return $this->decorated->getCompanyName();
}

/**
* {@inheritdoc}
*/
public function getAppGroup(): ?string {
return $this->decorated->getAppGroup();
}

/**
* {@inheritdoc}
*/
protected static function decoratedClass(): string {
return CompanyApp::class;
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
return AppGroupApp::class;
}
else {
return CompanyApp::class;
}
}

/**
* {@inheritdoc}
*/
public static function idProperty(): string {
return CompanyApp::idProperty();
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
return AppGroupApp::idProperty();
}
else {
return CompanyApp::idProperty();
}
}

/**
Expand All @@ -174,7 +206,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
}

// Hide readonly properties from Manage form display list.
$definitions['companyName']->setDisplayConfigurable('form', FALSE);
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
$definitions['appGroup']->setDisplayConfigurable('form', FALSE);
}
else {
$definitions['companyName']->setDisplayConfigurable('form', FALSE);
}

return $definitions;
}
Expand All @@ -187,7 +225,13 @@ protected function urlRouteParameters($rel) {
$link_templates = $this->linkTemplates();
if (isset($link_templates[$rel])) {
if (strpos($link_templates[$rel], '{team}') !== FALSE) {
$params['team'] = $this->getCompanyName();
$appController = \Drupal::service('apigee_edge.controller.app');
if ($appController->isOrgApigeeX()) {
$params['team'] = $this->getAppGroup();
}
else {
$params['team'] = $this->getCompanyName();
}
}
if (strpos($link_templates[$rel], '{app}') !== FALSE) {
$params['app'] = $this->getName();
Expand Down
4 changes: 2 additions & 2 deletions modules/apigee_edge_teams/src/Entity/TeamAppInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

namespace Drupal\apigee_edge_teams\Entity;

use Apigee\Edge\Api\Management\Entity\CompanyAppInterface;
use Apigee\Edge\Api\ApigeeX\Entity\AppGroupAppInterface;
use Drupal\apigee_edge\Entity\AppInterface;

/**
* Defines an interface for team (company app entity objects.
*/
interface TeamAppInterface extends CompanyAppInterface, AppInterface {
interface TeamAppInterface extends AppGroupAppInterface, AppInterface {

}
Loading

0 comments on commit ccacbcb

Please sign in to comment.