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

Issue #1911202 by baldwinlouie: Switched to using db_select

parent 27edd7f4
No related branches found
No related tags found
No related merge requests found
......@@ -302,54 +302,6 @@ function _cloud_metering_user_list_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/cloud_metering/group_settings';
}
/**
* Helper function to validate User and Group Budget changes
*/
function _cloud_metering_check_budget_changes($group_id, $instances_budget = '', $user_id, $budget) {
$invalid = 0;
$current_budget = _cloud_metering_get_user_budget($user_id);
$group_budget = db_query('SELECT group_budget FROM {' . CLOUD_METERING_BUDGET_GROUP_TABLE . '} WHERE gid = :gid ', array(':gid' => $group_id))->fetchField();
$total_user_budget = 0;
$arg = array(
'column' => 'gid',
'value' => $group_id,
);
$group_user_details = _cloud_metering_get_group_users_details($arg);
foreach ($group_user_details as $key => $value) {
if ($value['gid'] == $group_id) {
$get_budget = _cloud_metering_get_user_budget($key);
}
$total_user_budget += $get_budget;
}
if (!empty($instances_budget)) {
$check_budget = ($instances_budget + $budget);
$can_use_budget = ($current_budget - $check_budget);
$usable_budget = ($group_budget - $total_user_budget);
}
else {
$check_budget = ($total_user_budget - $current_budget);
$balance_budget = abs($group_budget - $total_user_budget);
$can_use_budget = ($group_budget - $check_budget) - $budget;
$usable_budget = ($group_budget - $total_user_budget);
}
if ($budget > $group_budget) {
$invalid = 1;
}
elseif ($can_use_budget < 0) {
$invalid = 2;
}
return $invalid;
}
/**
* Group Budget Configuration Screen
*/
......@@ -1036,8 +988,8 @@ function _cloud_metering_check_launch($template_id, $cloud_context) {
$template_budget = _cloud_metering_get_template_budget($template_id);
// Check if budget set for template
if (!is_null($template_budget['template_id']) && $template_budget['budget'] !== 0) {
$template_budget = $template_budget['budget'];
if (!is_null($template_budget['template_id']) && $template_budget['initial_budget'] !== 0) {
$initial_budget = $template_budget['initial_budget'];
if ($template_data = _cloud_metering_get_template_data($template_id)) {
$key_name = $template_data['key_name'];
......@@ -1051,7 +1003,7 @@ function _cloud_metering_check_launch($template_id, $cloud_context) {
$user_budget = _cloud_metering_get_user_budget($account->uid);
if ($user_budget > 0) {
$total_allocated_budget = _cloud_metering_get_user_instances_budget($key_owner);
$new_instances_total_budget = $template_budget * $instance_count;
$new_instances_total_budget = $initial_budget * $instance_count;
$total = $total_allocated_budget + $new_instances_total_budget;
if ($total > $user_budget) {
$total = number_format($total, 2, '.', ',');
......
......@@ -110,7 +110,7 @@ function cloud_metering_form_aws_cloud_server_templates_new_alter(&$form, &$form
if (user_access('configure instance budget')) {
$template_id = isset($form['templateid']['#value']) ? $form['templateid']['#value'] : NULL;
if (isset($template_id)) {
$budget_data = _cloud_metering_get_template_budget($template_id);
$budget_data = _cloud_metering_get_template_budget($template_id);
$form['initial_budget_info'] = array(
'#type' => 'fieldset',
'#title' => t('Budget'),
......@@ -120,14 +120,14 @@ function cloud_metering_form_aws_cloud_server_templates_new_alter(&$form, &$form
$form['initial_budget_info']['initial_budget'] = array(
'#type' => 'textfield',
'#title' => t('Initial Budget'),
'#default_value' => $budget_data['budget'],
'#default_value' => isset($budget_data['initial_budget']) ? $budget_data['initial_budget'] : '0',
'#size' => 20,
);
$form['initial_budget_info']['cost_threshold'] = array(
'#title' => t('Threshold (%)'),
'#description' => t('The cost threshold percentage. Thershold value will be percent of the Initial Budget.'),
'#type' => 'textfield',
'#default_value' => $budget_data['threshold'],
'#default_value' => isset($budget_data['cost_threshold']) ? $budget_data['cost_threshold']: '0',
'#size' => 20,
);
$form['initial_budget_info']['update_budget'] = array(
......@@ -206,8 +206,8 @@ function cloud_metering_form_aws_cloud_server_templates_view_alter(&$form, &$for
);
// format the data in rows
$rows = array();
$rows[] = array(t('Budget'), number_format($budget_data['budget'], 2, '.', ','));
$rows[] = array(t('Threshold (%)'), $budget_data['threshold']);
$rows[] = array(t('Budget'), number_format($budget_data['initial_budget'], 2, '.', ','));
$rows[] = array(t('Threshold (%)'), isset($budget_data) && isset($budget_data['cost_threshold']) ? $budget_data['cost_threshold'] : '0');
$table = theme('table', array('header' => NULL, 'rows' => $rows, 'attributes' => array('class' => 'template-budget-fields')));
$form['cloud_metering_budget']['#children'] = $table;
......@@ -302,7 +302,7 @@ function cloud_metering_cloud_action_notify($op, $params) {
$template_budget = _cloud_metering_get_template_budget($template_id);
if (!empty($template_budget['template_id'])) {
_cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $template_budget['budget'], 'threshold' => $template_budget['threshold']));
_cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $template_budget['initial_budget'], 'threshold' => $template_budget['cost_threshold']));
}
}
break;
......
......@@ -192,27 +192,13 @@ function _cloud_metering_get_instance_budget($cloud_context, $instance_id) {
* Get template budget data
* @param int template_id
* @return array
* array containing template_id, budget and threshold
* array containing template_id, budget and threshold
*/
function _cloud_metering_get_template_budget($template_id) {
$return = array(
'template_id' => NULL,
'budget' => 0,
'threshold' => 0,
);
$query = 'SELECT template_id, initial_budget, cost_threshold FROM {' . CLOUD_METERING_TEMPLATES_BUDGET_TABLE . '} WHERE template_id = :template_id ';
$result = db_query($query, array(':template_id' => $template_id) );
$row = $result->fetchAssoc();
if ($row) {
$return['template_id'] = $row['template_id'];
$return['budget'] = $row['initial_budget'];
$return['threshold'] = $row['cost_threshold'];
}
return $return;
return db_select(CLOUD_METERING_TEMPLATES_BUDGET_TABLE, 'c')
->fields('c')
->condition('template_id', $template_id)
->execute()->fetchAssoc();
}
/**
......@@ -242,7 +228,7 @@ function _cloud_metering_template_budget_save($template_id, $data) {
$existing_data = _cloud_metering_get_template_budget($template_id);
if (isset($existing_data['template_id'])) {
if ($existing_data) {
// update an existing template
$query_update = db_update(CLOUD_METERING_TEMPLATES_BUDGET_TABLE);
$fields = array();
......@@ -254,9 +240,9 @@ function _cloud_metering_template_budget_save($template_id, $data) {
}
$query_update->fields($fields);
$result = $query_update
->condition('template_id', $template_id, '=')
->execute();
$query_update
->condition('template_id', $template_id, '=')
->execute();
}
else {
$budget = is_null($budget) ? 0 : $budget;
......@@ -273,47 +259,15 @@ function _cloud_metering_template_budget_save($template_id, $data) {
drupal_set_message(t('The template budget settings have been saved.'));
}
/**
* Get ssh key owner from db
*/
function _cloud_metering_get_key_owner($cloud_context, $key) {
$ec2_lib_ssh_userkeys_table = aws_cloud_get_table_info( AWS_CLOUD_USER_KEYS_TABLE );
$key_owner = db_query('SELECT owner FROM {' . $ec2_lib_ssh_userkeys_table . '} WHERE key_name = :key_name AND cloud_type = :cloud_type',
array(
':key_name' => $key,
':cloud_type' => $cloud_context,
)
)->fetchField();
return $key_owner;
}
/**
* Get server template data from db
*/
function _cloud_metering_get_template_data($template_id) {
$result = db_query('SELECT * FROM {' . CLOUD_SERVER_TEMPLATES_TABLE . '} WHERE template_id = :template_id ',
array(':template_id' => $template_id) );
$row = $result->fetchAssoc();
return $row;
}
/**
* Get users whose budgets have been configured
*/
function _cloud_metering_get_budget_configured_users() {
$rows = array();
$query = 'SELECT * FROM {' . CLOUD_METERING_USER_BUDGET_TABLE . '} WHERE budget != 0';
$result = db_query($query);
while ($row = $result->fetchAssoc()) {
$rows[] = $row;
}
return $rows;
return db_select(CLOUD_SERVER_TEMPLATES_TABLE, 'c')
->fields('c')
->condition('template_id', $template_id)
->execute()
->fetchAssoc();
}
/**
......@@ -401,9 +355,11 @@ function _cloud_metering_get_groups($gids = array()) {
* @return Array The group admin(s)
*/
function _cloud_metering_get_group_admins($gid) {
$query = "SELECT user_data FROM {" . CLOUD_METERING_BUDGET_GROUP_TABLE . "} WHERE gid = " . $gid;
$result = db_query($query)->fetchField();
$result = db_select(CLOUD_METERING_BUDGET_GROUP_TABLE, 'c')
->fields('c', array('user_data'))
->condition('gid', $gid)
->execute()
->fetchField();
if ($result !== FALSE) {
$result = unserialize($result);
......@@ -417,9 +373,7 @@ function _cloud_metering_get_group_admins($gid) {
return $return;
}
}
return array(1); // Return the super-user in case there is no group admin(s)
}
/**
......@@ -474,41 +428,6 @@ function _cloud_metering_get_user_budget($uid) {
else {
return 0;
}
}
/**
* Get Group Users Details
*/
function _cloud_metering_get_group_users_details($arg = '') {
if (!empty($arg['value'])) {
$where_query = "WHERE " . $arg['column'] . " = " . $arg['value'];
}
else {
$where_query = '';
}
$group_users_query = db_query("SELECT * FROM {" . CLOUD_METERING_USER_BUDGET_TABLE . "} " . $where_query);
$group_users = array();
foreach ($group_users_query as $u) {
$row = array();
$row['uid'] = $u->uid;
$row['gid'] = $u->gid;
$row['budget'] = $u->budget;
if (!empty($arg)) {
$group_users[$u->uid] = $row;
}
else {
$group_users[$u->gid] = $row;
}
}
return $group_users;
}
/**
......@@ -524,27 +443,3 @@ function _cloud_metering_get_users_list_by_role($rid) {
$query->innerJoin('users_roles', 'ur', 'u.uid=ur.uid');
return $query->execute()->fetchAllAssoc('uid', PDO::FETCH_ASSOC);
}
/**
* Get all Drupal Roles expect the Default Drupal roles
*/
function _cloud_metering_get_roles_list($arg = '') {
$roles = array();
$roles[0] = '--Select Role--';
if (!empty($arg)) {
$where_query = "WHERE rid =" . $arg;
}
else {
$where_query = 'WHERE rid > 2';
}
$roles_query = db_query('SELECT * FROM {role} ' . $where_query);
foreach ($roles_query as $u) {
$roles[$u->rid] = $u->name;
}
return $roles;
}
\ No newline at end of file
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