Skip to content

Commit

Permalink
Merge pull request #230 from cnovak/drush_create_role
Browse files Browse the repository at this point in the history
Drush command to create Apigee Edge role
  • Loading branch information
cnovak committed Dec 19, 2019
2 parents 2ccf33f + d6e3c4d commit c564d04
Show file tree
Hide file tree
Showing 21 changed files with 1,888 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .travis/composer.json
Expand Up @@ -8,7 +8,9 @@
"composer/installers": "^1.6",
"drupal-composer/drupal-scaffold": "^2.5.4",
"wikimedia/composer-merge-plugin": "dev-capture-input-options",
"zaporylie/composer-drupal-optimizations": "^1.0"
"zaporylie/composer-drupal-optimizations": "^1.0",
"drupal/console": "~1.0",
"drush/drush": "^9.7"
},
"repositories": [
{
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -45,6 +45,7 @@ Please note that Team APIs and Monetization APIs are not currently supported on
2. Click **Extend** in the Drupal administration menu.
3. Select the **Apigee Edge** module.
4. Click **Install**.
5. Configure the [connection to your Apigee org](https://www.drupal.org/docs/8/modules/apigee-edge/configure-the-connection-to-apigee-edge)

## Notes

Expand Down
5 changes: 5 additions & 0 deletions apigee_edge.services.yml
Expand Up @@ -12,6 +12,11 @@ services:

apigee_edge.cli:
class: Drupal\apigee_edge\CliService
arguments: ['@apigee_edge.apigee_edge_mgmt_cli_service']

apigee_edge.apigee_edge_mgmt_cli_service:
class: Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliService
arguments: ['@http_client']

apigee_edge.sdk_connector:
class: Drupal\apigee_edge\SDKConnector
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -5,6 +5,7 @@
"description": "Apigee Edge for Drupal.",
"require": {
"php": ">=7.1",
"ext-json": "*",
"apigee/apigee-client-php": "^2.0.4",
"cweagans/composer-patches": "^1.6.5",
"drupal/core": "~8.7",
Expand Down
5 changes: 5 additions & 0 deletions console.services.yml
Expand Up @@ -4,3 +4,8 @@ services:
arguments: ['@apigee_edge.cli', '@logger.log_message_parser', '@logger.factory']
tags:
- { name: drupal.command }
apigee_edge.edge_role_create:
class: Drupal\apigee_edge\Command\CreateEdgeRoleCommand
arguments: ['@apigee_edge.cli', '@logger.log_message_parser', '@logger.factory']
tags:
- { name: drupal.command }
13 changes: 13 additions & 0 deletions console/translations/en/apigee_edge.role.create.yml
@@ -0,0 +1,13 @@
description: 'Create Apigee role for Drupal'
help: 'Create a custom Apigee role that limits permissions for Drupal connections to the Apigee API. '
arguments:
org: 'The Apigee Edge org to create the role in'
email: 'An Apigee user email address with orgadmin role for this org'
options:
password: 'Password for the Apigee orgadmin user (if not passed in you will be prompted)'
base-url: 'Base URL to use, defaults to public cloud URL https://api.enterprise.apigee.com/v1'
role-name: 'The role to create in the Apigee Edge org, defaults to "drupalportal"'
force: 'Force running of permissions on a role that already exists. Note that permissions are only added, any current permissions not not removed.'
questions:
password: 'Enter password for Apigee orgadmin user'
messages:
2 changes: 1 addition & 1 deletion drush.services.yml
@@ -1,6 +1,6 @@
services:
apigee_edge.commands:
class: \Drupal\apigee_edge\Commands\ApigeeEdgeCommands
arguments: ['@apigee_edge.cli']
arguments: ['@apigee_edge.cli', '@apigee_edge.apigee_edge_mgmt_cli_service']
tags:
- { name: drush.command }
45 changes: 44 additions & 1 deletion src/CliService.php
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2018 Google Inc.
* 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
Expand All @@ -20,13 +20,31 @@
namespace Drupal\apigee_edge;

use Drupal\apigee_edge\Controller\DeveloperSyncController;
use Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface;
use Symfony\Component\Console\Style\StyleInterface;

/**
* A CLI service which defines all the commands logic and delegates the methods.
*/
class CliService implements CliServiceInterface {

/**
* The service that makes calls to the Apigee API.
*
* @var \Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface
*/
private $apigeeEdgeManagementCliService;

/**
* CliService constructor.
*
* @param \Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface $apigeeEdgeManagementCliService
* The ApigeeEdgeManagementCliService to make calls to Apigee Edge.
*/
public function __construct(ApigeeEdgeManagementCliServiceInterface $apigeeEdgeManagementCliService) {
$this->apigeeEdgeManagementCliService = $apigeeEdgeManagementCliService;
}

/**
* {@inheritdoc}
*/
Expand All @@ -52,4 +70,29 @@ public function sync(StyleInterface $io, callable $t) {
}
}

/**
* {@inheritdoc}
*/
public function createEdgeRoleForDrupal(
StyleInterface $io,
callable $t,
string $org,
string $email,
string $password,
?string $base_url,
?string $role_name,
?bool $force
) {
$this->apigeeEdgeManagementCliService->createEdgeRoleForDrupal(
$io,
$t,
$org,
$email,
$password,
$base_url,
$role_name,
$force
);
}

}
31 changes: 31 additions & 0 deletions src/CliServiceInterface.php
Expand Up @@ -36,4 +36,35 @@ interface CliServiceInterface {
*/
public function sync(StyleInterface $io, callable $t);

/**
* Create an Apigee role for Drupal use.
*
* @param \Symfony\Component\Console\Style\StyleInterface $io
* The IO interface of the CLI tool calling the method.
* @param callable $t
* The translation function akin to t().
* @param string $org
* The organization to connect to.
* @param string $email
* The email of an Edge user with org admin role to make Edge API calls.
* @param string $password
* The password of an Edge user with org admin role to make Edge API calls.
* @param string|null $base_url
* The base url of the Edge API.
* @param string|null $role_name
* The role name to add the permissions to.
* @param bool|null $force
* Force permissions to be set even if role exists.
*/
public function createEdgeRoleForDrupal(
StyleInterface $io,
callable $t,
string $org,
string $email,
string $password,
?string $base_url,
?string $role_name,
?bool $force
);

}
4 changes: 2 additions & 2 deletions src/Command/CommandBase.php
Expand Up @@ -24,7 +24,7 @@
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LogMessageParserInterface;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Core\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function setupLogger(OutputInterface $output) {
* @return \Symfony\Component\Console\Style\StyleInterface
* The IO interface.
*/
protected function getIo(): StyleInterface {
public function getIo(): StyleInterface {
return $this->io;
}

Expand Down
110 changes: 110 additions & 0 deletions src/Command/CreateEdgeRoleCommand.php
@@ -0,0 +1,110 @@
<?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\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Developer synchronization command class for Drupal Console.
*
* @Drupal\Console\Annotations\DrupalCommand (
* extension="apigee_edge",
* extensionType="module"
* )
*/
class CreateEdgeRoleCommand extends CommandBase {

/**
* {@inheritdoc}
*/
protected function configure() {
$this
->setName('apigee_edge:role:create')
->setDescription($this->trans('commands.apigee_edge.role.create.description'))
->setHelp('commands.apigee_edge.role.create.help')
->addArgument(
'org',
InputArgument::REQUIRED,
$this->trans('commands.apigee_edge.role.create.arguments.org')
)
->addArgument(
'email',
InputArgument::REQUIRED,
$this->trans('commands.apigee_edge.role.create.arguments.email')
)
->addOption(
'password',
'p',
InputArgument::OPTIONAL,
$this->trans('commands.apigee_edge.role.create.options.password')
)
->addOption(
'base-url',
'b',
InputArgument::OPTIONAL,
$this->trans('commands.apigee_edge.role.create.options.base-url')
)
->addOption(
'role-name',
'r',
InputArgument::OPTIONAL,
$this->trans('commands.apigee_edge.role.create.options.role-name')
)->addOption(
'force',
'f',
InputOption::VALUE_NONE,
$this->trans('commands.apigee_edge.role.create.options.force')
);

}

/**
* {@inheritdoc}
*/
public function interact(InputInterface $input, OutputInterface $output) {
$this->setupIo($input, $output);
$password = $input->getOption('password');
if (!$password) {
$password = $this->getIo()->askHidden(
$this->trans('commands.apigee_edge.role.create.questions.password')
);
$input->setOption('password', $password);
}
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output) {
$this->setupIo($input, $output);
$org = $input->getArgument('org');
$email = $input->getArgument('email');
$password = $input->getOption('password');
$base_url = $input->getOption('base-url');
$role_name = $input->getOption('role-name');
$force = $input->getOption('force');

$this->cliService->createEdgeRoleForDrupal($this->getIo(), 't', $org, $email, $password, $base_url, $role_name, $force);
}

}

0 comments on commit c564d04

Please sign in to comment.