Skip to content
Snippets Groups Projects
Commit d1f7f7c9 authored by baldwinlouie's avatar baldwinlouie
Browse files

Issue #1927238, #1915880 by baldwinlouie: Reworked the mail templates to use...

Issue #1927238, #1915880 by baldwinlouie: Reworked the mail templates to use Tokens.  Refactored the instance_listing and removed a bunch of unused functions
parent f700afe4
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
......@@ -36,20 +36,12 @@ function cloud_metering_menu() {
$items['admin/config/cloud_metering'] = array(
'title' => 'Cloud Metering',
'description' => 'Set Cloud Metering settings',
'page callback' => 'drupal_get_form',
'page callback' => 'cloud_metering_instances_list',
'page arguments' => array('cloud_metering_instances_list'),
'access arguments' => array('configure instance budget'),
'file' => 'cloud_metering.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/cloud_metering/all'] = array(
'title' => 'Cloud Metering',
'description' => 'Set Cloud Metering settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('cloud_metering_instances_list'),
'access arguments' => array('configure instance budget'),
'weight' => -11,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/cloud_metering/group_settings'] = array(
'title' => 'User Group Settings',
'description' => 'Set Group details and Budget',
......@@ -78,11 +70,11 @@ function cloud_metering_menu() {
'weight' => 1,
);
$items['admin/config/cloud_metering/threshold'] = array(
$items['admin/config/cloud_metering/threshold/%/%'] = array(
'title' => 'Budget Setting',
'description' => 'Set threshold for a specific instance',
'page callback' => 'drupal_get_form',
'page arguments' => array('cloud_metering_set_threshold'),
'page arguments' => array('cloud_metering_set_threshold', 4, 5),
'access arguments' => array('configure instance budget'),
'type' => MENU_CALLBACK,
);
......@@ -248,18 +240,10 @@ function cloud_metering_cron() {
*/
function cloud_metering_theme() {
return array(
'cloud_metering_instances_list' => array(
'render element' => 'form',
'file' => 'cloud_metering.inc',
),
'cloud_metering_user_list' => array(
'render element' => 'form',
'file' => 'cloud_metering.inc',
),
'cloud_metering_instances_list' => array(
'render element' => 'form',
'file' => 'cloud_metering.inc',
),
);
}
......@@ -267,20 +251,26 @@ function cloud_metering_theme() {
* Implementation of hook_mail()
*/
function cloud_metering_mail($key, &$message, $params) {
// call token_replace and set the subject and body
$language = $message['language'];
//$tokens = cloud_metering_mail_tokens(TRUE, $params['cloud_context'], $params['instance_id'], $params['instance_name'], $params['user']);
// pass params as an array and let the final function take care of interpreting it
$tokens = cloud_metering_mail_tokens(TRUE, $params);
foreach ($tokens as $token_key => $token) {
$tokens[$token_key] = $token['value'];
switch($key) {
case 'threshold_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
case 'terminate_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
case 'user_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_USER_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_USER_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
case 'group_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_GROUP_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
}
$message['subject'] .= cloud_metering_mail_replacements($key . '_subject', $language, $tokens);
$message['body'][] = cloud_metering_mail_replacements($key . '_template', $language, $tokens);
}
/**
......@@ -316,13 +306,10 @@ function cloud_metering_cloud_action_notify($op, $params) {
* will perform some usage calculations before launching an instance
*/
function cloud_metering_menu_alter(&$items) {
// Alter the launch from server template menus, to add budget checks
// post launch hook instead of something else!
$clouds = cloud_get_all_clouds();
foreach ($clouds as $cloud_context) {
$items['design/server_templates/' . $cloud_context . '/%/launch'] = array(
'title' => t('Launch'),
'page callback' => '_cloud_metering_check_launch',
......@@ -331,9 +318,7 @@ function cloud_metering_menu_alter(&$items) {
'file' => '',
'type' => MENU_CALLBACK,
);
}
}
/**
......@@ -350,3 +335,49 @@ function cloud_metering_aws_cloud_terminate_instance($cloud_context, $instance_i
_cloud_metering_instance_budget_delete($cloud_context, $instance_id);
}
/**
* Helper function to send email
*/
function cloud_metering_send_mail($uid, $template, $params = array()) {
if ($uid) {
$account = user_load($uid);
// add loaded account into the params
$params['user'] = $account;
$mail_result = drupal_mail(CLOUD_METERING_MODULE_NAME, $template, $account->mail, user_preferred_language($account), $params, variable_get('site_mail', ini_get('sendmail_from')), TRUE);
return $mail_result;
}
}
/**
* Add a task to the job queue
*/
function cloud_metering_add_checker_tasks() {
$in_progress_tasks = _cloud_metering_get_in_progress_tasks();
if (is_array($in_progress_tasks)) {
foreach ($in_progress_tasks as $in_progress_task) {
$data = unserialize($in_progress_task['data']);
$cloud_context = $data['cloud_context'];
$instance_id = $data['instance_id'];
$task = $in_progress_task['task'];
if ($task == CLOUD_METERING_BACKUP) {
$task = CLOUD_METERING_BACKUP_CHECK;
}
elseif ($task == CLOUD_METERING_DETACH) {
$task = CLOUD_METERING_DETACH_CHECK;
}
elseif ($task == CLOUD_METERING_TERMINATE) {
$task = CLOUD_METERING_TERMINATE_CHECK;
}
if (_cloud_metering_get_task_type($task) == 'base') {
continue;
}
$data['qid'] = $in_progress_task['qid'];
// Add a checker task
_cloud_metering_add_task($cloud_context, $instance_id, $task, $data);
}
}
}
<?php
/**
* @file
* Supports cloud_meterings tokens
*/
/**
* Implements hook_token_info().
*/
function cloud_metering_token_info() {
$info['type']['cloud_metering_instance'] = array(
'name' => t('Cloud Metering Instance Tokens'),
'description' => t('Tokens related to a specific instance'),
'needs-data' => 'instance',
);
$info['type']['cloud_metering_group'] = array(
'name' => t('Cloud Metering Group Tokens'),
'description' => t('Tokens for group threshold'),
'needs-data' => 'group',
);
$info['type']['cloud_metering_user'] = array(
'name' => t('Cloud Metering User Tokens'),
'description' => t('Tokens for user threshold'),
'needs-data' => 'cloud_user',
);
// instance tokens
$info['tokens']['cloud_metering_instance']['username'] = array(
'name' => t('Username'),
'description' => t('Username'),
);
$info['tokens']['cloud_metering_instance']['instance-id'] = array(
'name' => t('Instance Id'),
'description' => t('The instance id'),
);
$info['tokens']['cloud_metering_instance']['instance-name'] = array(
'name' => t('Instance Name'),
'description' => t('The name of the instance'),
);
$info['tokens']['cloud_metering_instance']['terminate-url'] = array(
'name' => t('Terminate Url'),
'description' => t('Terminate Url'),
);
// group budget, cost, and group_threshold
$info['tokens']['cloud_metering_group']['username'] = array(
'name' => t('Username'),
'description' => t('Username'),
);
$info['tokens']['cloud_metering_group']['group_budget'] = array(
'name' => t('Group Budget'),
'description' => t('Group Budget'),
);
$info['tokens']['cloud_metering_group']['group_cost'] = array(
'name' => t('Group Cost'),
'description' => t('Group Cost'),
);
$info['tokens']['cloud_metering_group']['group_threshold'] = array(
'name' => t('Group Threshold'),
'description' => t('Group Threshold'),
);
// user threshold
$info['tokens']['cloud_metering_user']['username'] = array(
'name' => t('Username'),
'description' => t('Username'),
);
$info['tokens']['cloud_metering_user']['user_cost'] = array(
'name' => t('User Cost'),
'description' => t('User Cost'),
);
$info['tokens']['cloud_metering_user']['user_budget'] = array(
'name' => t('User Budget'),
'description' => t('User Budget'),
);
// cost, user_budget
return $info;
}
/**
* Implements hook_tokens().
*/
function cloud_metering_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
switch ($type) {
case 'cloud_metering_instance':
foreach ($tokens as $name => $original) {
switch ($name) {
case 'username':
$replacements[$original] = $sanitize ? check_plain($data['user']->name) : $data['user']->name;
break;
case 'instance-id':
$replacements[$original] = $sanitize ? check_plain($data['instance_id']) : $data['instance_id'];
break;
case 'instance-name':
$replacements[$original] = $sanitize ? check_plain($data['instance_name']) : $data['instance_name'];
break;
case 'terminate-url':
$replacements[$original] = url("clouds/${date['cloud_context']}/instances/terminate", array('query' => array('instance_id' => $data['instance_id'])));
break;
}
}
break;
case 'cloud_metering_group':
foreach ($tokens as $name => $original) {
switch ($name) {
case 'username':
$replacements[$original] = $sanitize ? check_plain($data['user']->name) : $data['user']->name;
break;
case 'group_budget':
$replacements[$original] = $sanitize ? check_plain($data['group_budget']) : $data['group_budget'];
break;
case 'group_cost':
$replacements[$original] = $sanitize ? check_plain($data['group_cost']) : $data['group_cost'];
break;
case 'group_threshold':
$replacements[$original] = $sanitize ? check_plain($data['group_threshold']) : $data['group_threshold'];
break;
}
}
break;
case 'cloud_metering_user':
foreach ($tokens as $name => $original) {
switch ($name) {
case 'username':
$replacements[$original] = $sanitize ? check_plain($data['user']->name) : $data['user']->name;
break;
case 'user_cost':
$replacements[$original] = $sanitize ? check_plain($data['user_cost']) : $data['user_cost'];
break;
case 'user_budget':
$replacements[$original] = $sanitize ? check_plain($data['user_budget']) : $data['user_budget'];
break;
}
}
break;
}
return $replacements;
}
......@@ -23,15 +23,22 @@ define('CLOUD_METERING_INSTANCES_BUDGET_TABLE' , CLOUD_METERING_PREFIX
define('CLOUD_METERING_TEMPLATES_BUDGET_TABLE' , CLOUD_METERING_PREFIX . 'metering_templates_budget');
define('CLOUD_METERING_QUEUE_TABLE' , CLOUD_METERING_PREFIX . 'metering_queue');
// Mail related constants
define('CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_SUBJECT' , 'The instance !instance-name has crossed the cost threshold');
define('CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_BODY' , "Dear !username,\n\nThe cost for your cloud instance named !instance-name has crossed the cost threshold set by you. You should either increase the budget for the instance or terminate it. Please login to your account and take the necessary steps.\n\n-- !site");
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_SUBJECT' , 'The instance !instance-name has crossed the allocated budget');
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY' , "Dear !username,\n\nYour cloud instance named !instance-name has crossed the budget allocated to it. The instance will be automatically terminated after a backup and detaching of it's volumes, if any.\n\n-- !site");
define('CLOUD_METERING_DEFAULT_USER_MAIL_SUBJECT' , 'The total cost of your cloud instances has gone past your budget');
define('CLOUD_METERING_DEFAULT_USER_MAIL_BODY' , "Dear !username,\n\nThe total cost of your cloud instances has gone past your budget. Please consider terminating some instances or increasing your budget.\n\n-- !site");
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_SUBJECT' , "The total cost of your group's cloud instances has gone past the budget");
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY' , "Dear !username,\n\nThe total cost of your group's cloud instances has gone past the allocated budget. Please consider terminating some instances or increasing the group's budget.\n\n-- !site");
// threshold mail template
define('CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_SUBJECT' , variable_get('cloud_metering_mail_subject', 'The instance [cloud_metering_instance:instance-name] has crossed the cost threshold!'));
define('CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_BODY' , variable_get('cloud_metering_mail_template', "Dear [cloud_metering_instance:username],\n\nThe cost for your cloud instance named [cloud_metering_instance:instance-name] has crossed the cost threshold set by you. You should either increase the budget for the instance or terminate it. Please login to your account and take the necessary steps.\n\n-- [site:name]"));
// terminate mail
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_SUBJECT' , variable_get('cloud_metering_terminate_mail_subject', 'The instance [cloud_metering_instance:instance-name] has crossed the allocated budget!'));
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY' , variable_get('cloud_metering_terminate_mail_template', "Dear [cloud_metering_instance:username],\n\nYour cloud instance named [cloud_metering_instance:instance-name] has crossed the budget allocated to it. The instance will be automatically terminated after a backup and detaching any attached volumes.\n\n-- [site:name]"));
// user default mail templates
define('CLOUD_METERING_DEFAULT_USER_MAIL_SUBJECT' , variable_get('cloud_metering_user_mail_subject', 'The total cost of your cloud instances has gone past your budget!'));
define('CLOUD_METERING_DEFAULT_USER_MAIL_BODY' , variable_get('cloud_metering_user_mail_template', "Dear [cloud_metering_user:username],\n\nThe total cost of your cloud instances has gone past your budget of [cloud_metering_user:user_budget]. It is currently costing [cloud_metering_user:user_cost]. Please consider terminating some instances or have the adminstrator increase your budget.\n\n-- [site:name]"));
// group default mail templates
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_SUBJECT' , variable_get('cloud_metering_group_mail_subject', "Cloud instances has gone past the group budget!"));
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY' , variable_get('cloud_metering_group_mail_template', "Dear [cloud_metering_group:username],\n\nThe total cost of your group's cloud instances has gone past the allocated budget of [cloud_metering_group:group_budget]. It is currently [cloud_metering_group:group_cost]. Please consider terminating some instances or increasing the group's budget.\n\n-- [site:name]"));
define('CLOUD_METERING_MAIL_INTERVAL' , 7); // 7 days
// Queue related constants
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment