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

Issue #1915738 by baldwinlouie: Fixed the mail routines to use the correct templates

parent 4be4adfc
No related branches found
No related tags found
No related merge requests found
...@@ -331,7 +331,6 @@ function theme_cloud_metering_user_list($form) { ...@@ -331,7 +331,6 @@ function theme_cloud_metering_user_list($form) {
return $output; return $output;
} }
/** /**
* Validate the group details form * Validate the group details form
*/ */
...@@ -663,7 +662,6 @@ function cloud_metering_mail_replacements($key, $language = NULL, $variables = a ...@@ -663,7 +662,6 @@ function cloud_metering_mail_replacements($key, $language = NULL, $variables = a
return t(CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY, $variables); return t(CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY, $variables);
} }
} }
} }
/** /**
...@@ -676,7 +674,7 @@ function cloud_metering_mail_settings($form, $form_state) { ...@@ -676,7 +674,7 @@ function cloud_metering_mail_settings($form, $form_state) {
$description = t('Customize the format of the mail that will be sent whenever an instance crosses the cost threshold. The following tokens are available -'); $description = t('Customize the format of the mail that will be sent whenever an instance crosses the cost threshold. The following tokens are available -');
$description .= '<br />'; $description .= '<br />';
$tokens = cloud_metering_mail_tokens(); $tokens = cloud_metering_mail_tokens(FALSE, array());
foreach ($tokens as $token => $token_detail) { foreach ($tokens as $token => $token_detail) {
$description .= $token . ' - ' . $token_detail['description'] . '<br />'; $description .= $token . ' - ' . $token_detail['description'] . '<br />';
} }
...@@ -782,63 +780,45 @@ function cloud_metering_mail_settings($form, $form_state) { ...@@ -782,63 +780,45 @@ function cloud_metering_mail_settings($form, $form_state) {
/** /**
* Returns a number of tokens that can be used in the mail template * Returns a number of tokens that can be used in the mail template
* Optionally returns the replacement values for the tokens * Optionally returns the replacement values for the tokens
* @param boolean $values Whether to return the token values * @param array of parameters
* @param array $tokens The tokens
* @param string $cloud_context The cloud context
* @param string $instance_id The instance id
* @param string $instance_name The instance name
* @param object $user An user object
* @return array The array of tokens * @return array The array of tokens
*/ */
function cloud_metering_mail_tokens($values = FALSE, $cloud_context = '', $instance_id = '', $instance_name = '', $user = NULL) { function cloud_metering_mail_tokens($values = FALSE, $params) {
global $base_url;
$tokens = array(); $tokens = array();
$cloud_context = isset($params['cloud_context']) ? $params['cloud_context'] : '';
if ($values) { $instance_id = isset($params['instance_id']) ? $params['instance_id'] : '';
if (empty($cloud_context) || empty($instance_id)) { $instance_name = isset($params['instance_name']) ? $params['instance_name'] : '';
drupal_set_message(t('Token values can\'t be returned without a cloud context and an instance id.'), 'warning');
$values = FALSE; $tokens['!username'] = array(
}
elseif (empty($user)) {
$user = user_load(array('uid' => 1));
}
}
$tokens = array();
$value = $values ? $user->name : '';
$tokens['!username'] = array(
'description' => t('Username'), 'description' => t('Username'),
'value' => $value, 'value' => isset($params['user']) ? $params['user']->name : '',
); );
$tokens['!site'] = array( $tokens['!site'] = array(
'description' => t('The site-name'), 'description' => t('The Site Name'),
'value' => variable_get('site_name', 'Drupal'), 'value' => variable_get('site_name', 'Drupal'),
); );
$value = $values ? $instance_id : ''; $tokens['!instance-id'] = array(
$tokens['!instance-id'] = array( 'description' => t('The Instance Id'),
'description' => t('The instance Id'), 'value' => $instance_id,
'value' => $value,
); );
$value = $values ? $instance_name : '';
$tokens['!instance-name'] = array( $tokens['!instance-name'] = array(
'description' => t('The Instance name'), 'description' => t('The Instance Name'),
'value' => $value, 'value' => $instance_name,
); );
$value = $values ? url('clouds/' . $cloud_context . '/instances/terminate', array('query' => array('instance_id' => $instance_id))) : ''; // set the terminate_url
$terminate_url = '';
if (!empty($cloud_context) && !empty($instance_id)) {
$terminate_url = url('clouds/' . $cloud_context . '/instances/terminate', array('query' => array('instance_id' => $instance_id)));
}
$tokens['!terminate-url'] = array( $tokens['!terminate-url'] = array(
'description' => t('The URL for terminating an instance'), 'description' => t('The URL for terminating an instance'),
'value' => $value, 'value' => $terminate_url,
); );
return $tokens; return $tokens;
} }
/** /**
...@@ -925,38 +905,23 @@ function cloud_metering_maxed_instances($cloud_context = NULL) { ...@@ -925,38 +905,23 @@ function cloud_metering_maxed_instances($cloud_context = NULL) {
} }
} }
} }
return $maxed_instances; return $maxed_instances;
} }
/** /**
* Send notifications * Notify a user that their threshold has hit
*/ */
function cloud_metering_notify_instance_users($instances) { function cloud_metering_notify_instance_users($instances) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
$cloud_context = $instance['cloud_context']; $cloud_context = $instance['cloud_context'];
$instance_id = $instance['instance_id']; $instance_id = $instance['instance_id'];
$instance_name = $instance['name']; $instance_name = $instance['name'];
// Check if a notification has already been sent for this instance // Check if a notification has already been sent for this instance
$notification_sent = cloud_metering_notification_sent('threshold', $cloud_context, $instance_id); if (!cloud_metering_notification_sent('threshold', $cloud_context, $instance_id)) {
if (!$notification_sent) {
// Send a notification // Send a notification
$user = _cloud_metering_get_instance_user($instance); $user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'threshold_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance_name));
$mail_result = drupal_mail(
CLOUD_METERING_MODULE_NAME,
'threshold_mail',
$user->mail,
user_preferred_language($user),
array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance_name),
variable_get('site_mail', ini_get('sendmail_from')),
TRUE
);
if ($mail_result['result']) { if ($mail_result['result']) {
cloud_metering_save_notification_info('threshold', $cloud_context, $instance_id); cloud_metering_save_notification_info('threshold', $cloud_context, $instance_id);
} }
...@@ -974,11 +939,8 @@ function cloud_metering_notify_instance_users($instances) { ...@@ -974,11 +939,8 @@ function cloud_metering_notify_instance_users($instances) {
function cloud_metering_terminate_budget_crossed_instances($maxed_instances) { function cloud_metering_terminate_budget_crossed_instances($maxed_instances) {
foreach ($maxed_instances as $instance) { foreach ($maxed_instances as $instance) {
if ($instance['state'] == 'running') { if ($instance['state'] == 'running') {
// Backup->Detach->Terminate the instance // Backup->Detach->Terminate the instance
$cloud_context = $instance['cloud_context']; $cloud_context = $instance['cloud_context'];
$instance_id = $instance['instance_id']; $instance_id = $instance['instance_id'];
...@@ -986,42 +948,23 @@ function cloud_metering_terminate_budget_crossed_instances($maxed_instances) { ...@@ -986,42 +948,23 @@ function cloud_metering_terminate_budget_crossed_instances($maxed_instances) {
$volumes = array(); $volumes = array();
if (is_array($instance_volumes) && sizeof($instance_volumes) > 0) { if (is_array($instance_volumes) && sizeof($instance_volumes) > 0) {
foreach ($instance_volumes as $volume_id => $volume) { foreach ($instance_volumes as $volume_id => $volume) {
$volumes[$volume_id] = array(); $volumes[$volume_id] = array();
} }
$data = array('volumes' => $volumes); $data = array('volumes' => $volumes);
// Initiate a backup task // Initiate a backup task
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_BACKUP, $data); _cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_BACKUP, $data);
} }
else { else {
// Initiate terminate // Initiate terminate
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_TERMINATE, array()); _cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_TERMINATE, array());
} }
// Send notification if it hasn't been sent
// Send a notification if (!cloud_metering_notification_sent('terminate', $cloud_context, $instance_id)) {
$notification_sent = cloud_metering_notification_sent('terminate', $cloud_context, $instance_id);
if (!$notification_sent) {
$instance_name = $instance['name'];
// The Drupal associated with the instance // The Drupal associated with the instance
$user = _cloud_metering_get_instance_user($instance); $user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'terminate_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance['name']));
$mail_result = drupal_mail(
CLOUD_METERING_MODULE_NAME,
'terminate_mail',
$user->mail,
user_preferred_language($user),
array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance_name),
variable_get('site_mail', ini_get('sendmail_from')),
TRUE
);
if ($mail_result['result']) { if ($mail_result['result']) {
cloud_metering_save_notification_info('terminate', $cloud_context, $instance_id); cloud_metering_save_notification_info('terminate', $cloud_context, $instance_id);
...@@ -1029,13 +972,9 @@ function cloud_metering_terminate_budget_crossed_instances($maxed_instances) { ...@@ -1029,13 +972,9 @@ function cloud_metering_terminate_budget_crossed_instances($maxed_instances) {
else { else {
_cloud_metering_activity_audit(t('Failed to send notification mail for instance %instance_id.', array('%instance_id' => $instance_id)), 'user_activity', '', WATCHDOG_ERROR); _cloud_metering_activity_audit(t('Failed to send notification mail for instance %instance_id.', array('%instance_id' => $instance_id)), 'user_activity', '', WATCHDOG_ERROR);
} }
} }
} }
} }
} }
/** /**
...@@ -1134,110 +1073,88 @@ function cloud_metering_get_grouped_instances($cloud_context = NULL) { ...@@ -1134,110 +1073,88 @@ function cloud_metering_get_grouped_instances($cloud_context = NULL) {
} }
} }
} }
} }
} }
} }
return $return; return $return;
} }
/**
* Notify the group admins that their users have
* surpassed their budget.
*/
function cloud_metering_notify_group_admins($group_instances) { function cloud_metering_notify_group_admins($group_instances) {
foreach ($group_instances as $rid => $data) { foreach ($group_instances as $rid => $data) {
$gid = $data['gid']; $gid = $data['gid'];
$instances = $data['instances']; $instances = $data['instances'];
// Get group budget and compare with the total cost // Get group budget and compare with the total cost
$group_budget = _cloud_metering_get_group_budget($gid); $group_budget = _cloud_metering_get_group_budget($gid);
if (!empty($group_budget)) {
if (empty($group_budget)) { $group_budget = $group_budget[$gid];
continue; $group_threshold = $group_budget['threshold'];
} $group_budget = $group_budget['group_budget'];
$group_budget = $group_budget[$gid]; if ($group_budget > 0 && $group_threshold > 0) {
$group_threshold = $group_budget['threshold']; $cost = 0;
$group_budget = $group_budget['group_budget']; foreach ($instances as $instance) {
$cost += $instance['cost'];
if ($group_budget > 0 && $group_threshold > 0) { }
$cost = 0; if ($cost >= ($group_budget * $group_threshold / 100)) {
foreach ($instances as $instance) { // Send notification to group admins
$cost += $instance['cost']; $group_admins = _cloud_metering_get_group_admins($gid);
} foreach ($group_admins as $uid) {
// add in group budget, cost, and group_threshold
if ($cost >= ($group_budget * $group_threshold / 100)) { cloud_metering_send_mail($uid, 'group_mail', array('cost' => $cost, 'user_budget' => $group_budget, 'group_threshold' => $group_threshold));
// Send notification to group admins }
$group_admins = _cloud_metering_get_group_admins($gid);
foreach ($group_admins as $group_admin) {
$account = user_load(array('uid' => $group_admin));
$mail = $account->mail;
$mail_result = drupal_mail(
CLOUD_METERING_MODULE_NAME,
'group_mail',
$mail,
user_preferred_language($account),
array('user' => $account, 'cloud_context' => '', 'instance_id' => '', 'instance_name' => ''),
variable_get('site_mail', ini_get('sendmail_from')),
TRUE
);
} }
} }
} }
} }
} }
/**
* Notify users if they crossed their budget
*/
function cloud_metering_notify_users($user_instances) { function cloud_metering_notify_users($user_instances) {
foreach ($user_instances as $uid => $instances) { foreach ($user_instances as $uid => $instances) {
// Get user budget and compare with the total cost // Get user budget and compare with the total cost
$user_budget = _cloud_metering_get_user_budget($uid); $user_budget = _cloud_metering_get_user_budget($uid);
if ($user_budget > 0) { if ($user_budget > 0) {
$cost = 0; $cost = 0;
foreach ($instances as $instance) { foreach ($instances as $instance) {
$cost += $instance['cost']; $cost += $instance['cost'];
} }
if ($cost >= $user_budget) { if ($cost >= $user_budget) {
// Send notification to user // Send notification to user
$account = user_load(array('uid' => $uid)); cloud_metering_send_mail($uid, 'user_mail', array('cost' => $cost, 'user_budget' => $user_budget));
$mail = $account->mail;
$mail_result = drupal_mail(
CLOUD_METERING_MODULE_NAME,
'group_mail',
$mail,
user_preferred_language($account),
array('user' => $account, 'cloud_context' => '', 'instance_id' => '', 'instance_name' => ''),
variable_get('site_mail', ini_get('sendmail_from')),
TRUE
);
} }
} }
} }
}
/**
* 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() { function cloud_metering_add_checker_tasks() {
$in_progress_tasks = _cloud_metering_get_in_progress_tasks(); $in_progress_tasks = _cloud_metering_get_in_progress_tasks();
if (is_array($in_progress_tasks)) { if (is_array($in_progress_tasks)) {
foreach ($in_progress_tasks as $in_progress_task) { foreach ($in_progress_tasks as $in_progress_task) {
$data = unserialize($in_progress_task['data']); $data = unserialize($in_progress_task['data']);
$cloud_context = $data['cloud_context']; $cloud_context = $data['cloud_context'];
$instance_id = $data['instance_id']; $instance_id = $data['instance_id'];
...@@ -1259,11 +1176,8 @@ function cloud_metering_add_checker_tasks() { ...@@ -1259,11 +1176,8 @@ function cloud_metering_add_checker_tasks() {
$data['qid'] = $in_progress_task['qid']; $data['qid'] = $in_progress_task['qid'];
// Add a checker task // Add a checker task
_cloud_metering_add_task($cloud_context, $instance_id, $task, $data); _cloud_metering_add_task($cloud_context, $instance_id, $task, $data);
} }
} }
} }
/** /**
...@@ -1397,7 +1311,7 @@ function _cloud_metering_add_users_to_group_validate($form, &$form_state) { ...@@ -1397,7 +1311,7 @@ function _cloud_metering_add_users_to_group_validate($form, &$form_state) {
form_set_error('user_group', t('Please select a Group Name from the select list.')); form_set_error('user_group', t('Please select a Group Name from the select list.'));
} }
//Validate user budget value // Validate user budget value
$get_budget_check = _cloud_metering_check_budget_changes($group_id, NULL, $user_id, $budget); $get_budget_check = _cloud_metering_check_budget_changes($group_id, NULL, $user_id, $budget);
if (!is_numeric($budget)) { if (!is_numeric($budget)) {
form_set_error('user_budget', t('Please enter only numeric values only.')); form_set_error('user_budget', t('Please enter only numeric values only.'));
...@@ -1469,7 +1383,6 @@ function _cloud_metering_add_users_to_group_submit($form, &$form_state) { ...@@ -1469,7 +1383,6 @@ function _cloud_metering_add_users_to_group_submit($form, &$form_state) {
} }
} }
} }
} }
/** /**
...@@ -1498,7 +1411,6 @@ function _cloud_metering_validate_template_budget($form, &$form_state) { ...@@ -1498,7 +1411,6 @@ function _cloud_metering_validate_template_budget($form, &$form_state) {
if (is_numeric($threshold) && $threshold < 0) { if (is_numeric($threshold) && $threshold < 0) {
form_set_error('cost_threshold', t('The threshold should not be less than 0.')); form_set_error('cost_threshold', t('The threshold should not be less than 0.'));
} }
} }
/** /**
...@@ -1508,7 +1420,6 @@ function _cloud_metering_validate_template_budget($form, &$form_state) { ...@@ -1508,7 +1420,6 @@ function _cloud_metering_validate_template_budget($form, &$form_state) {
* *
*/ */
function _cloud_metering_update_instance_budget($form, &$form_state) { function _cloud_metering_update_instance_budget($form, &$form_state) {
global $user;
$error = 0; $error = 0;
$form_values = $form_state['values']; $form_values = $form_state['values'];
...@@ -1547,25 +1458,18 @@ function _cloud_metering_update_instance_budget($form, &$form_state) { ...@@ -1547,25 +1458,18 @@ function _cloud_metering_update_instance_budget($form, &$form_state) {
$user_budget = number_format($user_budget, 2, '.', ','); $user_budget = number_format($user_budget, 2, '.', ',');
$edit_link = ''; $edit_link = '';
$rid = ''; $rid = '';
if (!empty($account->roles) && user_access('configure user budget')) { if (user_access('configure user budget')) {
foreach ($account->roles as $rid => $role) {
if ($role != 'authenticated user') {
break;
}
}
if (!empty($rid)) { if (!empty($rid)) {
$edit_link = l(t('Edit user budget'), 'admin/config/cloud_metering/group_settings/' . $rid . '/edit'); $edit_link = l(t('Edit user budget'), 'admin/config/cloud_metering/group_settings/' . $rid . '/edit');
} }
} }
$message = filter_xss( t("Instance budget settings not saved due to user's budget constraint. Setting this budget will make the budget allocated to all instances of the user @total; but the user's budget is @user-budget. !edit-link", array('@total' => $total_budget, '@user-budget' => $user_budget, '!edit-link' => $edit_link)) ); $message = t("Instance budget settings not saved due to user's budget constraint. Setting this budget will make the budget allocated to all instances of the user @total; but the user's budget is @user-budget. !edit-link", array('@total' => $total_budget, '@user-budget' => $user_budget, '!edit-link' => $edit_link));
drupal_set_message($message, 'warning'); drupal_set_message($message, 'warning');
} }
} }
} }
if ($error == 0) { if ($error == 0) {
// Update records // Update records
_cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $initial_budget, 'threshold' => $threshold)); _cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $initial_budget, 'threshold' => $threshold));
...@@ -1582,9 +1486,7 @@ function _cloud_metering_update_instance_budget($form, &$form_state) { ...@@ -1582,9 +1486,7 @@ function _cloud_metering_update_instance_budget($form, &$form_state) {
function _cloud_metering_activity_audit($message, $type = 'user_activity', $link = '', $severity = WATCHDOG_NOTICE) { function _cloud_metering_activity_audit($message, $type = 'user_activity', $link = '', $severity = WATCHDOG_NOTICE) {
watchdog(CLOUD_METERING_MODULE_DISPLAY_NAME, $message, NULL, $severity); watchdog(CLOUD_METERING_MODULE_DISPLAY_NAME, $message, NULL, $severity);
if (module_exists('cloud_activity_audit')) { if (module_exists('cloud_activity_audit')) {
cloud_activity_audit_log( cloud_activity_audit_log(
array( array(
'type' => $type, 'type' => $type,
...@@ -1592,11 +1494,8 @@ function _cloud_metering_activity_audit($message, $type = 'user_activity', $link ...@@ -1592,11 +1494,8 @@ function _cloud_metering_activity_audit($message, $type = 'user_activity', $link
'link' => $link, 'link' => $link,
) )
); );
} }
return TRUE; return TRUE;
} }
/** /**
......
...@@ -267,7 +267,9 @@ function cloud_metering_mail($key, &$message, $params) { ...@@ -267,7 +267,9 @@ function cloud_metering_mail($key, &$message, $params) {
$language = $message['language']; $language = $message['language'];
$tokens = cloud_metering_mail_tokens(TRUE, $params['cloud_context'], $params['instance_id'], $params['instance_name'], $params['user']); //$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) { foreach ($tokens as $token_key => $token) {
$tokens[$token_key] = $token['value']; $tokens[$token_key] = $token['value'];
......
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