Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDS-72] Add sensor for Tide Jira module #406

Merged
merged 8 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drupal\tide_jira\Plugin\monitoring\SensorPlugin;

use Drupal\monitoring\Entity\SensorConfig;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\jira_rest\JiraRestWrapperService;

/**
* Monitors the Section API connection.
*
* @SensorPlugin(
* id = "tide_jira",
* label = @Translation("Tide Jira Sensor"),
* description = @Translation("Monitors connectivity to the Jira Service Desk API"),
* )
*/
class TideJiraSensorPlugin extends SensorPluginBase implements SensorPluginInterface {

/**
* The config.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $config;

/**
* The connector.
*
* @var \Drupal\jira_rest\JiraRestWrapperService
*/
protected $connector;

/**
* {@inheritdoc}
*/
public function __construct(SensorConfig $sensor_config, $plugin_id, $plugin_definition, ConfigFactory $config_factory, JiraRestWrapperService $jira_rest) {
parent::__construct($sensor_config, $plugin_id, $plugin_definition);
$this->config = $config_factory->get('tide_jira.settings');
$this->connector = $jira_rest;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, SensorConfig $sensor_config, $plugin_id, $plugin_definition) {
return new static(
$sensor_config,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('jira_rest_wrapper_service'),
);
}

/**
* {@inheritdoc}
*/
public function getDefaultConfiguration() {
return [
'caching_time' => 60 * 5,
'value_type' => 'bool',
'category' => 'Tide',
'settings' => [],
];
}

/**
* {@inheritdoc}
*/
public function runSensor(SensorResultInterface $sensor_result) {
$default_email = $this->config->get('no_account_email');
$account_id = '';
try {
$us = $this->connector->getUserService();
$account_id = $us->findUserByEmail($default_email);
}
catch (\Exception $e) {
$sensor_result->setStatus(SensorResultInterface::STATUS_CRITICAL);
$sensor_result->setMessage('Tide Jira endpoint is not correctly configured...');
}

if (empty($account_id)) {
$sensor_result->setStatus(SensorResultInterface::STATUS_CRITICAL);
$sensor_result->setMessage(sprintf('Failed to lookup default account %s', $default_email));
}
else {
$sensor_result->setStatus(SensorResultInterface::STATUS_OK);
$sensor_result->setMessage('Tide Jira OK!');
}

}

}
34 changes: 34 additions & 0 deletions modules/tide_jira/tide_jira.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @file
* Install file for Tide Jira.
*/

use Drupal\Core\Entity\EntityStorageException;
use Drupal\monitoring\Entity\SensorConfig;

/**
* Configure sensor if monitoring module is present.
*/
function tide_jira_update_8002() {
if (\Drupal::service('module_handler')->moduleExists('monitoring')) {
$sensor = SensorConfig::create([
'id' => 'tide_jira',
'label' => 'Tide Jira',
'description' => 'Connectivity Status of the Jira Service Desk API',
'plugin_id' => 'tide_jira',
'value_label' => 'Tide Jira',
'category' => 'Baywatch',
'status' => TRUE,
'caching_time' => 300,
]);
try {
$sensor->save();
}
catch (EntityStorageException $e) {
\Drupal::messenger()->addMessage("tide_jira sensor already configured.");
}

}
}
Loading