You are here

public function ApigeeEdgeManagementCliService::createEdgeRoleForDrupal in Apigee Edge 8

Create role in Apigee Edge for Drupal to use for Edge connection.

Parameters

\Symfony\Component\Console\Style\StyleInterface $io: The IO interface of the CLI tool calling the method.

callable $t: The translation function akin to t().

string $org: The organization to connect to.

string $email: The email of an Edge user with org admin role to make Edge API calls.

string $password: The password of an Edge user with org admin role to make Edge API calls.

null|string $base_url: The base url of the Edge API.

null|string $role_name: The role name to add the permissions to.

null|bool $force: Force running of permissions on a role that already exists.

Overrides ApigeeEdgeManagementCliServiceInterface::createEdgeRoleForDrupal

File

src/Command/Util/ApigeeEdgeManagementCliService.php, line 54

Class

ApigeeEdgeManagementCliService
Defines an interface for Edge connection classes.

Namespace

Drupal\apigee_edge\Command\Util

Code

public function createEdgeRoleForDrupal(StyleInterface $io, callable $t, string $org, string $email, string $password, ?string $base_url, ?string $role_name, bool $force) {

  // Set default base URL if var is null or empty string.
  if (empty($base_url)) {
    $base_url = ApigeeClientInterface::EDGE_ENDPOINT;
  }
  else {

    // Validate it is a valid URL.
    if (!UrlHelper::isValid($base_url, TRUE)) {
      $io
        ->error($t('Base URL is not valid.'));
      return;
    }
  }

  // Set default if null or empty string.
  $role_name = $role_name ?: self::DEFAULT_ROLE_NAME;
  if (!$this
    ->isValidEdgeCredentials($io, $t, $org, $email, $password, $base_url)) {
    return;
  }
  $does_role_exist = $this
    ->doesRoleExist($org, $email, $password, $base_url, $role_name);

  // If role does not exist and force flag is not used, throw error.
  if ($does_role_exist && !$force) {
    $io
      ->error('Role ' . $role_name . ' already exists.');
    $io
      ->note('Run with --force option to set default permissions on this role.');
    return;
  }

  // Create the role if it does not exist.
  if (!$does_role_exist) {
    $io
      ->text($t('Role :role does not exist. Creating role.', [
      ':role' => $role_name,
    ]));
    $url = "{$base_url}/o/{$org}/userroles";
    try {
      $this->httpClient
        ->post($url, [
        'body' => json_encode([
          'role' => [
            $role_name,
          ],
        ]),
        'auth' => [
          $email,
          $password,
        ],
        'headers' => [
          'Accept' => 'application/json',
          'Content-Type' => 'application/json',
        ],
      ]);
    } catch (TransferException $exception) {
      $this
        ->handleHttpClientExceptions($exception, $io, $t, $url, $org, $email);
      return;
    }
  }
  $this
    ->setDefaultPermissions($io, $t, $org, $email, $password, $base_url, $role_name);
  $io
    ->success($t('Role :role is configured. Log into apigee.com to assign a user to this role.', [
    ':role' => $role_name,
  ]));
}