Skip to content

Commit c564d04

Browse files
authored
Merge pull request #230 from cnovak/drush_create_role
Drush command to create Apigee Edge role
2 parents 2ccf33f + d6e3c4d commit c564d04

21 files changed

+1888
-11
lines changed

.travis/composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"composer/installers": "^1.6",
99
"drupal-composer/drupal-scaffold": "^2.5.4",
1010
"wikimedia/composer-merge-plugin": "dev-capture-input-options",
11-
"zaporylie/composer-drupal-optimizations": "^1.0"
11+
"zaporylie/composer-drupal-optimizations": "^1.0",
12+
"drupal/console": "~1.0",
13+
"drush/drush": "^9.7"
1214
},
1315
"repositories": [
1416
{

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Please note that Team APIs and Monetization APIs are not currently supported on
4545
2. Click **Extend** in the Drupal administration menu.
4646
3. Select the **Apigee Edge** module.
4747
4. Click **Install**.
48+
5. Configure the [connection to your Apigee org](https://www.drupal.org/docs/8/modules/apigee-edge/configure-the-connection-to-apigee-edge)
4849

4950
## Notes
5051

apigee_edge.services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ services:
1212

1313
apigee_edge.cli:
1414
class: Drupal\apigee_edge\CliService
15+
arguments: ['@apigee_edge.apigee_edge_mgmt_cli_service']
16+
17+
apigee_edge.apigee_edge_mgmt_cli_service:
18+
class: Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliService
19+
arguments: ['@http_client']
1520

1621
apigee_edge.sdk_connector:
1722
class: Drupal\apigee_edge\SDKConnector

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "Apigee Edge for Drupal.",
66
"require": {
77
"php": ">=7.1",
8+
"ext-json": "*",
89
"apigee/apigee-client-php": "^2.0.4",
910
"cweagans/composer-patches": "^1.6.5",
1011
"drupal/core": "~8.7",

console.services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ services:
44
arguments: ['@apigee_edge.cli', '@logger.log_message_parser', '@logger.factory']
55
tags:
66
- { name: drupal.command }
7+
apigee_edge.edge_role_create:
8+
class: Drupal\apigee_edge\Command\CreateEdgeRoleCommand
9+
arguments: ['@apigee_edge.cli', '@logger.log_message_parser', '@logger.factory']
10+
tags:
11+
- { name: drupal.command }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
description: 'Create Apigee role for Drupal'
2+
help: 'Create a custom Apigee role that limits permissions for Drupal connections to the Apigee API. '
3+
arguments:
4+
org: 'The Apigee Edge org to create the role in'
5+
email: 'An Apigee user email address with orgadmin role for this org'
6+
options:
7+
password: 'Password for the Apigee orgadmin user (if not passed in you will be prompted)'
8+
base-url: 'Base URL to use, defaults to public cloud URL https://api.enterprise.apigee.com/v1'
9+
role-name: 'The role to create in the Apigee Edge org, defaults to "drupalportal"'
10+
force: 'Force running of permissions on a role that already exists. Note that permissions are only added, any current permissions not not removed.'
11+
questions:
12+
password: 'Enter password for Apigee orgadmin user'
13+
messages:

drush.services.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
apigee_edge.commands:
33
class: \Drupal\apigee_edge\Commands\ApigeeEdgeCommands
4-
arguments: ['@apigee_edge.cli']
4+
arguments: ['@apigee_edge.cli', '@apigee_edge.apigee_edge_mgmt_cli_service']
55
tags:
66
- { name: drush.command }

src/CliService.php

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Copyright 2018 Google Inc.
4+
* Copyright 2019 Google Inc.
55
*
66
* This program is free software; you can redistribute it and/or modify it under
77
* the terms of the GNU General Public License version 2 as published by the
@@ -20,13 +20,31 @@
2020
namespace Drupal\apigee_edge;
2121

2222
use Drupal\apigee_edge\Controller\DeveloperSyncController;
23+
use Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface;
2324
use Symfony\Component\Console\Style\StyleInterface;
2425

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

31+
/**
32+
* The service that makes calls to the Apigee API.
33+
*
34+
* @var \Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface
35+
*/
36+
private $apigeeEdgeManagementCliService;
37+
38+
/**
39+
* CliService constructor.
40+
*
41+
* @param \Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface $apigeeEdgeManagementCliService
42+
* The ApigeeEdgeManagementCliService to make calls to Apigee Edge.
43+
*/
44+
public function __construct(ApigeeEdgeManagementCliServiceInterface $apigeeEdgeManagementCliService) {
45+
$this->apigeeEdgeManagementCliService = $apigeeEdgeManagementCliService;
46+
}
47+
3048
/**
3149
* {@inheritdoc}
3250
*/
@@ -52,4 +70,29 @@ public function sync(StyleInterface $io, callable $t) {
5270
}
5371
}
5472

73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function createEdgeRoleForDrupal(
77+
StyleInterface $io,
78+
callable $t,
79+
string $org,
80+
string $email,
81+
string $password,
82+
?string $base_url,
83+
?string $role_name,
84+
?bool $force
85+
) {
86+
$this->apigeeEdgeManagementCliService->createEdgeRoleForDrupal(
87+
$io,
88+
$t,
89+
$org,
90+
$email,
91+
$password,
92+
$base_url,
93+
$role_name,
94+
$force
95+
);
96+
}
97+
5598
}

src/CliServiceInterface.php

+31
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,35 @@ interface CliServiceInterface {
3636
*/
3737
public function sync(StyleInterface $io, callable $t);
3838

39+
/**
40+
* Create an Apigee role for Drupal use.
41+
*
42+
* @param \Symfony\Component\Console\Style\StyleInterface $io
43+
* The IO interface of the CLI tool calling the method.
44+
* @param callable $t
45+
* The translation function akin to t().
46+
* @param string $org
47+
* The organization to connect to.
48+
* @param string $email
49+
* The email of an Edge user with org admin role to make Edge API calls.
50+
* @param string $password
51+
* The password of an Edge user with org admin role to make Edge API calls.
52+
* @param string|null $base_url
53+
* The base url of the Edge API.
54+
* @param string|null $role_name
55+
* The role name to add the permissions to.
56+
* @param bool|null $force
57+
* Force permissions to be set even if role exists.
58+
*/
59+
public function createEdgeRoleForDrupal(
60+
StyleInterface $io,
61+
callable $t,
62+
string $org,
63+
string $email,
64+
string $password,
65+
?string $base_url,
66+
?string $role_name,
67+
?bool $force
68+
);
69+
3970
}

src/Command/CommandBase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Drupal\Console\Core\Style\DrupalStyle;
2525
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
2626
use Drupal\Core\Logger\LogMessageParserInterface;
27-
use Symfony\Component\Console\Command\Command;
27+
use Drupal\Console\Core\Command\Command;
2828
use Symfony\Component\Console\Input\InputInterface;
2929
use Symfony\Component\Console\Logger\ConsoleLogger;
3030
use Symfony\Component\Console\Output\OutputInterface;
@@ -115,7 +115,7 @@ protected function setupLogger(OutputInterface $output) {
115115
* @return \Symfony\Component\Console\Style\StyleInterface
116116
* The IO interface.
117117
*/
118-
protected function getIo(): StyleInterface {
118+
public function getIo(): StyleInterface {
119119
return $this->io;
120120
}
121121

src/Command/CreateEdgeRoleCommand.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Command;
21+
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
/**
28+
* Developer synchronization command class for Drupal Console.
29+
*
30+
* @Drupal\Console\Annotations\DrupalCommand (
31+
* extension="apigee_edge",
32+
* extensionType="module"
33+
* )
34+
*/
35+
class CreateEdgeRoleCommand extends CommandBase {
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function configure() {
41+
$this
42+
->setName('apigee_edge:role:create')
43+
->setDescription($this->trans('commands.apigee_edge.role.create.description'))
44+
->setHelp('commands.apigee_edge.role.create.help')
45+
->addArgument(
46+
'org',
47+
InputArgument::REQUIRED,
48+
$this->trans('commands.apigee_edge.role.create.arguments.org')
49+
)
50+
->addArgument(
51+
'email',
52+
InputArgument::REQUIRED,
53+
$this->trans('commands.apigee_edge.role.create.arguments.email')
54+
)
55+
->addOption(
56+
'password',
57+
'p',
58+
InputArgument::OPTIONAL,
59+
$this->trans('commands.apigee_edge.role.create.options.password')
60+
)
61+
->addOption(
62+
'base-url',
63+
'b',
64+
InputArgument::OPTIONAL,
65+
$this->trans('commands.apigee_edge.role.create.options.base-url')
66+
)
67+
->addOption(
68+
'role-name',
69+
'r',
70+
InputArgument::OPTIONAL,
71+
$this->trans('commands.apigee_edge.role.create.options.role-name')
72+
)->addOption(
73+
'force',
74+
'f',
75+
InputOption::VALUE_NONE,
76+
$this->trans('commands.apigee_edge.role.create.options.force')
77+
);
78+
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function interact(InputInterface $input, OutputInterface $output) {
85+
$this->setupIo($input, $output);
86+
$password = $input->getOption('password');
87+
if (!$password) {
88+
$password = $this->getIo()->askHidden(
89+
$this->trans('commands.apigee_edge.role.create.questions.password')
90+
);
91+
$input->setOption('password', $password);
92+
}
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
public function execute(InputInterface $input, OutputInterface $output) {
99+
$this->setupIo($input, $output);
100+
$org = $input->getArgument('org');
101+
$email = $input->getArgument('email');
102+
$password = $input->getOption('password');
103+
$base_url = $input->getOption('base-url');
104+
$role_name = $input->getOption('role-name');
105+
$force = $input->getOption('force');
106+
107+
$this->cliService->createEdgeRoleForDrupal($this->getIo(), 't', $org, $email, $password, $base_url, $role_name, $force);
108+
}
109+
110+
}

0 commit comments

Comments
 (0)