Commit f2f7b46c authored by Thomas Gelf's avatar Thomas Gelf
Browse files

AWS module: initial import

parents
AWS module for Icinga Web 2
===========================
This is a simple AWS module. Currently it is nothing but a import source
provider for Icinga Director. You need to extract the AWS PHP SDK v2 to
`library/vendor/aws` and at least one AWS access key in `keys.ini`. Create
a file `/etc/icingaweb2/modules/aws/keys.ini` as follows:
```ini
[My readonly AWS key]
access_key_id = RANDOMANFASDFNASDOFA
secret_access_key = WhatASDmn0asdnfASNDInafsdofdasJ980hansdf
```
<?php
namespace Icinga\Module\Aws;
use Aws\AutoScaling\AutoScalingClient;
use Aws\Common\Aws;
class AwsClient
{
protected $key;
protected $client;
protected $region;
public function __construct(AwsKey $key, $region)
{
$this->region = $region;
$this->key = $key;
$this->prepareAwsLibs();
}
public function getAutoscalingConfig()
{
$groups = array();
$lbClient = $this->client()->get('AutoScaling');
$res = $lbClient->describeAutoScalingGroups();
foreach ($res->get('AutoScalingGroups') as $grp) {
$group = (object) array(
'name' => $grp['AutoScalingGroupName'],
'launch_config' => $grp['LaunchConfigurationName'],
'ctime' => strtotime($grp['CreatedTime']),
'zones' => $grp['AvailabilityZones'],
// 'current_size' => count($grp['Instances']),
'desired_size' => (int) $grp['DesiredCapacity'],
'min_size' => (int) $grp['MinSize'],
'max_size' => (int) $grp['MaxSize'],
// 'instances' => $grp['Instances'],
'lb_names' => $grp['LoadBalancerNames'],
'health_check_type' => $grp['HealthCheckType'],
'tags' => (object) array(),
);
foreach ($grp['Tags'] as $t) {
$group->tags->{$t['Key']} = $t['Value'];
}
$groups[] = $group;
}
return $groups;
}
public static function enumRegions()
{
return array(
'us-east-1' => 'US East (N. Virginia)',
'us-west-2' => 'US West (Oregon)',
'us-west-1' => 'US West (N. California)',
'eu-west-1' => 'EU (Ireland)',
'eu-central-1' => 'EU (Frankfurt)',
'ap-southeast-1' => 'Asia Pacific (Singapore)',
'ap-northeast-1' => 'Asia Pacific (Tokyo)',
'ap-southeast-2' => 'Asia Pacific (Sydney)',
'ap-northeast-2' => 'Asia Pacific (Seoul)',
'sa-east-1' => 'South America (São Paulo)',
);
}
protected function client()
{
if ($this->client === null) {
$this->initializeClient();
}
return $this->client;
}
protected function initializeClient()
{
$this->client = Aws::factory([
'region' => $this->region,
'credentials' => $this->key->getCredentials(),
]);
}
protected function prepareAwsLibs()
{
require_once dirname(__DIR__) . '/vendor/aws/aws-autoloader.php';
}
}
<?php
namespace Icinga\Module\Aws;
use Aws\Common\Credentials\Credentials;
use Icinga\Application\Config;
class AwsKey
{
protected $credentials;
protected $id;
protected $key;
public function __construct($id, $key)
{
$this->id = $id;
$this->key = $key;
}
public function getCredentials()
{
if ($this->credentials === null) {
$this->credentials = new Credentials($this->id, $this->key);
}
return $this->credentials;
}
public static function load($name = null)
{
if ($name === null) {
return self::loadDefault();
} else {
return self::loadByName($name);
}
}
public static function loadDefault()
{
return static::loadByName(current(self::listNames()));
}
public static function loadByName($name)
{
$config = static::config();
return new static(
$config->get($name, 'access_key_id'),
$config->get($name, 'secret_access_key')
);
}
public static function listNames()
{
return static::config()->keys();
}
public static function enumKeyNames()
{
$names = static::listNames();
return array_combine($names, $names);
}
protected static function config()
{
return Config::module('aws', 'keys');
}
public function getId()
{
return $this->id;
}
public function getKey()
{
return $this->key;
}
}
<?php
namespace Icinga\Module\Aws\ProvidedHook\Director;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Aws\AwsClient;
use Icinga\Module\Aws\AwsKey;
use Icinga\Application\Benchmark;
class ImportSource extends ImportSourceHook
{
protected $db;
public function fetchData()
{
$client = new AwsClient(
AwsKey::loadByName($this->getSetting('aws_access_key')),
$this->getSetting('aws_region')
);
return $client->getAutoscalingConfig();
}
public function listColumns()
{
return array(
'name',
'launch_config',
'ctime',
'zones',
'desired_size',
'min_size',
'max_size',
'lb_names',
'health_check_type',
'tags',
'tags.Name',
'tags.aws:cloudformation:logical-id',
'tags.aws:cloudformation:stack-id',
'tags.aws:cloudformation:stack-name',
);
}
public static function getDefaultKeyColumnName()
{
return 'name';
}
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('select', 'aws_region', array(
'label' => 'AWS region',
'required' => true,
'multiOptions' => $form->optionalEnum(AwsClient::enumRegions()),
));
$form->addElement('select', 'aws_access_key', array(
'label' => 'AWS access key',
'required' => true,
'multiOptions' => $form->optionalEnum(AwsKey::enumKeyNames()),
'class' => 'autosubmit',
));
}
}
<?php
$this->provideHook('director/ImportSource');
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment