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

Refactored retrieval of values from Messages.resx. #3212

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 11 additions & 19 deletions XenAdmin/Controls/Wlb/WlbOptModeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,35 +432,27 @@ public static uint Bitcount(int n)
}


private static string GetTaskDayOfWeek(WlbScheduledTask.WlbTaskDaysOfWeek taskDaysOfWeek)
{
return WlbScheduledTask.DaysOfWeekL10N(taskDaysOfWeek);
}

private static string GetTaskDayOfWeek(WlbScheduledTask.WlbTaskDaysOfWeek taskDaysOfWeek,
WlbScheduledTask.WlbTaskDaysOfWeek taskDaysofWeekSortedList)
{
string returnStr = "";
returnStr += WlbScheduledTask.DaysOfWeekL10N(taskDaysOfWeek);
string day = WlbScheduledTask.DaysOfWeekL10N(taskDaysOfWeek);

//count the bits set in days of week.
//this workaround had to be made to determine whether the original task was set for
//weekends/weekdays/alldays
uint bitCount = Bitcount((int)taskDaysofWeekSortedList);
if (bitCount == 2)
{
returnStr += " (" + Messages.ResourceManager.GetString("WLB_DAY_WEEKENDS") + ")";
}
else if (bitCount == 5)
{
returnStr += " (" + Messages.ResourceManager.GetString("WLB_DAY_WEEKDAYS") + ")";
}
else if (bitCount == 7)

switch (bitCount)
{
returnStr += " (" + Messages.ResourceManager.GetString("WLB_DAY_ALL") + ")";
case 2:
return $"{day} ({Messages.WLB_DAY_WEEKENDS})";
case 5:
return $"{day} ({Messages.WLB_DAY_WEEKDAYS})";
case 7:
return $"{day} ({Messages.WLB_DAY_ALL})";
default:
return day;
}

return returnStr;
}

public static string GetTaskRunTime(DateTime time)
Expand Down
52 changes: 24 additions & 28 deletions XenModel/Actions/Host/HostPowerOnAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ namespace XenAdmin.Actions.HostActions
{
public class HostPowerOnAction : AsyncAction
{

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public HostPowerOnAction(XenAPI.Host host)

public HostPowerOnAction(Host host)
: base(host.Connection, Messages.HOST_POWER_ON)
{
Host = host;
Expand All @@ -55,47 +55,43 @@ protected override void Run()
{
bool succeeded = false;
string name = Helpers.GetName(Host);
XenAPI.Host coordinator = Helpers.GetCoordinator(Connection);
Host coordinator = Helpers.GetCoordinator(Connection);
AppliesTo.Add(coordinator.opaque_ref);
Title = string.Format(Messages.ACTION_HOST_START_TITLE, name);
Description = Messages.ACTION_HOST_STARTING;

try
{
XenAPI.Host.power_on(Session, Host.opaque_ref);
Host.power_on(Session, Host.opaque_ref);
Description = Messages.ACTION_HOST_STARTED;
succeeded = true;

/* WLB: Below code doesn't work, becasue RelatedTask is not set.
* Need to explore other option when enabling set poweron task value for wlb reporting
if (Helpers.IsWLBEnabled(this.Connection)
&& Host.other_config.ContainsKey(WlbOptimizePoolAction.OPTIMIZINGPOOL))
{
// set host poweroff task key values for wlb reporting purpose
Task.add_to_other_config(this.Session, this.RelatedTask.opaque_ref, "wlb_advised", Host.other_config[WlbOptimizePoolAction.OPTIMIZINGPOOL]);
Task.add_to_other_config(this.Session, this.RelatedTask.opaque_ref, "wlb_action", "host_poweron");
Task.add_to_other_config(this.Session, this.RelatedTask.opaque_ref, "wlb_action_obj_ref", Host.opaque_ref);
Task.add_to_other_config(this.Session, this.RelatedTask.opaque_ref, "wlb_action_obj_type", "host");
}
*/
}
catch (Exception e)
{
Failure f = e as Failure;
if (f != null)
if (e is Failure f)
{
string msg = f.ErrorDescription.Count > 2 ? Messages.ResourceManager.GetString(f.ErrorDescription[2]) : null;
if (msg != null)
throw new Exception(msg);
else
if (f.ErrorDescription.Count > 2)
{
throw new Exception(string.Format(Messages.POWER_ON_REQUEST_FAILED, this.Host));
switch (f.ErrorDescription[2])
{
case "DRAC_NO_SUPP_PACK":
throw new Exception(Messages.DRAC_NO_SUPP_PACK);
case "DRAC_POWERON_FAILED":
throw new Exception(Messages.DRAC_POWERON_FAILED);
case "ILO_CONNECTION_ERROR":
throw new Exception(Messages.ILO_CONNECTION_ERROR);
case "ILO_POWERON_FAILED":
throw new Exception(Messages.ILO_POWERON_FAILED);
}
}

throw new Exception(string.Format(Messages.POWER_ON_REQUEST_FAILED, Host));
}
throw;
}
finally
{
if (Helpers.WlbConfigured(this.Connection) && Helpers.WlbEnabledAndConfigured(this.Connection))
if (Helpers.WlbConfigured(Connection) && Helpers.WlbEnabledAndConfigured(Connection))
{
UpdateHostLastPowerOnSucceeded(succeeded, Host);
}
Expand All @@ -105,18 +101,18 @@ protected override void Run()
/// <summary>
/// Attempts to set the LastPowerOnSucceeded flag in the WLB Host configuration
/// </summary>
private void UpdateHostLastPowerOnSucceeded(bool succeeded, XenAPI.Host host)
private void UpdateHostLastPowerOnSucceeded(bool succeeded, Host host)
{
try
{
//Helpers.SetOtherConfig(this.Host.Connection.Session, this.Host, "LastPowerOnsucceeded", successful.ToString());
WlbHostConfiguration hostConfig = new WlbHostConfiguration(host.uuid);
hostConfig.LastPowerOnSucceeded = succeeded;
if (!succeeded)
{
hostConfig.ParticipatesInPowerManagement = false;
}
XenAPI.Pool pool = Helpers.GetPoolOfOne(host.Connection);

Pool pool = Helpers.GetPoolOfOne(host.Connection);
if (null != pool)
{
SendWlbConfigurationAction action = new SendWlbConfigurationAction(pool, hostConfig.ToDictionary(), SendWlbConfigurationKind.SetHostConfiguration);
Expand Down
29 changes: 15 additions & 14 deletions XenModel/Actions/WLB/InitializeWLBAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

using XenAdmin.Core;
using XenAdmin.Wlb;
using XenAPI;


Expand All @@ -42,21 +43,21 @@ public class InitializeWLBAction : AsyncAction
private readonly string _wlbPassword;
private readonly string _xenServerUserName;
private readonly string _xenServerPassword;
private static string OPTIMIZINGPOOL = "wlb_optimizing_pool";
private const string OPTIMIZING_POOL = "wlb_optimizing_pool";

public InitializeWLBAction(Pool pool, string wlbUrl, string wlbUserName, string wlbPassword, string xenServerUserName, string xenServerPassword)
: base(pool.Connection, string.Format(Messages.INITIALIZING_WLB_ON, Helpers.GetName(pool).Ellipsise(50)), Messages.INITIALIZING_WLB, false)
{
this.Pool = pool;
Pool = pool;
_wlbUrl = wlbUrl;
_wlbUserName = wlbUserName;
_wlbPassword = wlbPassword;
_xenServerUserName = xenServerUserName;
_xenServerPassword = xenServerPassword;

#region RBAC Dependencies
ApiMethodsToRoleCheck.Add("vm.assert_agile");
ApiMethodsToRoleCheck.Add("pool.initialize_wlb");

ApiMethodsToRoleCheck.AddRange("vm.assert_agile", "pool.initialize_wlb");
ApiMethodsToRoleCheck.AddRange(Role.CommonTaskApiList);
ApiMethodsToRoleCheck.AddRange(Role.CommonSessionApiList);
#endregion
Expand All @@ -68,29 +69,29 @@ protected override void Run()
try
{
log.Debug("Initializing Workload Balancing for pool " + Pool.Name());
RelatedTask = XenAPI.Pool.async_initialize_wlb(this.Session, _wlbUrl, _wlbUserName, _wlbPassword, _xenServerUserName, _xenServerPassword);
RelatedTask = Pool.async_initialize_wlb(Session, _wlbUrl, _wlbUserName, _wlbPassword, _xenServerUserName, _xenServerPassword);
PollToCompletion();
log.Debug("Success initializing WLB on pool " + Pool.Name());
this.Description = Messages.COMPLETED;
Description = Messages.COMPLETED;

//Clear the Optimizing Pool flag in case it was left behind
Helpers.SetOtherConfig(this.Session, this.Pool, OPTIMIZINGPOOL, string.Empty);
Helpers.SetOtherConfig(Session, Pool, OPTIMIZING_POOL, string.Empty);
}
catch (Failure e)
{
if (e.Message == FriendlyErrorNames.WLB_INTERNAL_ERROR)
{
Failure f = new Failure(new string[] { Messages.ResourceManager.GetString("WLB_ERROR_" + e.ErrorDescription[1]) });
throw (f);
var wlbError = WlbServerState.ConvertWlbError(e.ErrorDescription[1]);

if (wlbError != null)
throw new Failure(wlbError);
}
else if (e.ErrorDescription[0] == FriendlyErrorNames.INTERNAL_ERROR)
{
Failure f = new Failure(new string[] { Messages.ResourceManager.GetString("WLB_ERROR_SERVER_NOT_FOUND") });
}
else
{
throw (e);
throw new Failure(Messages.WLB_ERROR_SERVER_NOT_FOUND);
}

throw;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ protected override void Run()
WlbServerState.SetState(Pool, WlbServerState.ServerState.ConnectionError, ex);

if (ex.Message == FriendlyErrorNames.WLB_INTERNAL_ERROR)
throw new Failure(Messages.ResourceManager.GetString("WLB_ERROR_" + ex.ErrorDescription[1]));

{
var wlbError = WlbServerState.ConvertWlbError(ex.ErrorDescription[1]);

if (wlbError != null)
throw new Failure(wlbError);
}

throw;
}
}
Expand Down
74 changes: 37 additions & 37 deletions XenModel/Actions/WLB/SendWlbConfigurationAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
namespace XenAdmin.Actions.Wlb
{
[Flags]
public enum SendWlbConfigurationKind : int
public enum SendWlbConfigurationKind
{
None = 0,
SetPoolConfiguration = 1,
Expand All @@ -47,26 +47,26 @@ public enum SendWlbConfigurationKind : int
SetReportSubscription = 8,
DeleteReportSubscription = 16,
SetHostConfiguration = 32
};
}

public class SendWlbConfigurationAction : AsyncAction
{
private static string SET_HOST_CONFIGURATION = "set_host_configuration";
private static string SET_SCHEDULED_TASK = "set_scheduled_task";
private static string DELETE_SCHEDULED_TASK = "delete_scheduled_task";
private static string SET_REPORT_SUBSCRIPTIONS = "set_report_subscription";
private static string DELETE_REPORT_SUBSCRIPTIONS = "delete_report_subscription";
private const string SET_HOST_CONFIGURATION = "set_host_configuration";
private const string SET_SCHEDULED_TASK = "set_scheduled_task";
private const string DELETE_SCHEDULED_TASK = "delete_scheduled_task";
private const string SET_REPORT_SUBSCRIPTIONS = "set_report_subscription";
private const string DELETE_REPORT_SUBSCRIPTIONS = "delete_report_subscription";

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly SendWlbConfigurationKind _kind;
private Dictionary<string, string> WlbConfiguration = new Dictionary<string, string>();
private readonly Dictionary<string, string> _wlbConfiguration;

public SendWlbConfigurationAction(Pool pool, Dictionary<string, string> configuration, SendWlbConfigurationKind kind)
: base(pool.Connection, string.Format(Messages.SAVING_WLB_CONFIGURATION_FOR, Helpers.GetName(pool).Ellipsise(50)), Messages.SAVING_WLB_CONFIGURATION, false)
{
this.Pool = pool;
this.WlbConfiguration = configuration;
this._kind = kind;
Pool = pool;
_wlbConfiguration = configuration;
_kind = kind;

#region RBAC Dependencies
ApiMethodsToRoleCheck.Add("pool.send_wlb_configuration");
Expand All @@ -81,59 +81,59 @@ protected override void Run()

ClearKeys();

if ((_kind & SendWlbConfigurationKind.SetHostConfiguration) == SendWlbConfigurationKind.SetHostConfiguration)
if (_kind.HasFlag(SendWlbConfigurationKind.SetHostConfiguration))
{
this.WlbConfiguration.Add(SET_HOST_CONFIGURATION, "true");
_wlbConfiguration.Add(SET_HOST_CONFIGURATION, "true");
}
if ((_kind & SendWlbConfigurationKind.SetScheduledTask) == SendWlbConfigurationKind.SetScheduledTask)
if (_kind.HasFlag(SendWlbConfigurationKind.SetScheduledTask))
{
this.WlbConfiguration.Add(SET_SCHEDULED_TASK, "true");
_wlbConfiguration.Add(SET_SCHEDULED_TASK, "true");
}
if ((_kind & SendWlbConfigurationKind.DeleteScheduledTask) == SendWlbConfigurationKind.DeleteScheduledTask)
if (_kind.HasFlag(SendWlbConfigurationKind.DeleteScheduledTask))
{
this.WlbConfiguration.Add(DELETE_SCHEDULED_TASK, "true");
_wlbConfiguration.Add(DELETE_SCHEDULED_TASK, "true");
}
if ((_kind & SendWlbConfigurationKind.SetReportSubscription) == SendWlbConfigurationKind.SetReportSubscription)
if (_kind.HasFlag(SendWlbConfigurationKind.SetReportSubscription))
{
this.WlbConfiguration.Add(SET_REPORT_SUBSCRIPTIONS, "true");
_wlbConfiguration.Add(SET_REPORT_SUBSCRIPTIONS, "true");
}
if ((_kind & SendWlbConfigurationKind.DeleteReportSubscription) == SendWlbConfigurationKind.DeleteReportSubscription)
if (_kind.HasFlag(SendWlbConfigurationKind.DeleteReportSubscription))
{
this.WlbConfiguration.Add(DELETE_REPORT_SUBSCRIPTIONS, "true");
_wlbConfiguration.Add(DELETE_REPORT_SUBSCRIPTIONS, "true");
}

try
{
XenAPI.Pool.send_wlb_configuration(this.Session, this.WlbConfiguration);
Pool.send_wlb_configuration(Session, _wlbConfiguration);
log.Debug("Successfully sent Workload Balancing configuration on pool " + Pool.Name());
this.Description = Messages.COMPLETED;
Description = Messages.COMPLETED;
}
catch (Exception ex)
{
if (ex is Failure)
if (ex is Failure f)
{
WlbServerState.SetState(Pool, WlbServerState.ServerState.ConnectionError, (Failure)ex);
WlbServerState.SetState(Pool, WlbServerState.ServerState.ConnectionError, f);

if (((Failure)ex).Message == FriendlyErrorNames.WLB_INTERNAL_ERROR)
{
Failure f = new Failure(new string[] { Messages.ResourceManager.GetString("WLB_ERROR_" + ((Failure)ex).ErrorDescription[1]) });
throw (f);
}
else
if (f.Message == FriendlyErrorNames.WLB_INTERNAL_ERROR)
{
throw (ex);
var wlbError = WlbServerState.ConvertWlbError(f.ErrorDescription[1]);

if (wlbError != null)
throw new Failure(wlbError);
}
}

throw;
}
}

private void ClearKeys()
{
this.WlbConfiguration.Remove(SET_HOST_CONFIGURATION);
this.WlbConfiguration.Remove(SET_SCHEDULED_TASK);
this.WlbConfiguration.Remove(DELETE_SCHEDULED_TASK);
this.WlbConfiguration.Remove(SET_REPORT_SUBSCRIPTIONS);
this.WlbConfiguration.Remove(DELETE_REPORT_SUBSCRIPTIONS);
_wlbConfiguration.Remove(SET_HOST_CONFIGURATION);
_wlbConfiguration.Remove(SET_SCHEDULED_TASK);
_wlbConfiguration.Remove(DELETE_SCHEDULED_TASK);
_wlbConfiguration.Remove(SET_REPORT_SUBSCRIPTIONS);
_wlbConfiguration.Remove(DELETE_REPORT_SUBSCRIPTIONS);
}
}
}
Loading
Loading