Skip to content

Commit

Permalink
Some refactoring.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>
  • Loading branch information
kc284 committed Nov 29, 2023
1 parent effc7fe commit 68a9b41
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 94 deletions.
2 changes: 2 additions & 0 deletions XenAdmin/Dialogs/PropertiesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ private List<AsyncAction> SaveSettings()

foreach (IEditPage editPage in verticalTabs.Items)
{
editPage.HideLocalValidationMessages();

if (!editPage.HasChanged)
continue;

Expand Down
68 changes: 34 additions & 34 deletions XenAdmin/SettingsPanels/SnmpEditPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@ public partial class SnmpEditPage : UserControl, IEditPage
IsBalloon = true,
ToolTipIcon = ToolTipIcon.Warning
};
private string _invalidParamToolTipText = "";

private static readonly Regex RegexCommon = new Regex(@"^[a-zA-Z0-9-.\#@=:_]{6,32}$");
private static readonly Regex RegexEncryptTextBox = new Regex(@"^([a-zA-Z0-9-.\#@=:_]{8,32}|[*]{8})$");
private bool _encryptTextBoxFlag;
private IXenObject _clone;
private SnmpConfiguration _snmpConfiguration = new SnmpConfiguration();
private SnmpConfiguration _snmpConfiguration;
private readonly SnmpConfiguration _snmpCurrentConfiguration = new SnmpConfiguration();
private (Control Control, string Title, string Text) _tuple;

public SnmpEditPage()
{
InitializeComponent();
}

public bool HasChanged
{
Expand All @@ -70,85 +76,79 @@ public bool ValidToSave
{
get
{
_invalidParamToolTipText = " ";
_tuple = (null, null, null);

if (!EnableSnmpCheckBox.Checked)
{
return true;
}

if (!SupportV2cCheckBox.Checked && !SupportV3CheckBox.Checked)
{
_invalidParamToolTip.Tag = SupportV2cCheckBox;
_invalidParamToolTip.ToolTipTitle = Messages.SNMP_ALLOW_CHOOSE_TITLE;
_tuple = (SupportV2cCheckBox, Messages.SNMP_ALLOW_PROTOCOL_TITLE, Messages.SNMP_ALLOW_PROTOCOL_TEXT);
return false;
}

if (SupportV2cCheckBox.Checked)
{
var communityStr = CommunityTextBox.Text;
if (string.IsNullOrEmpty(communityStr) || !RegexCommon.Match(communityStr.Trim()).Success)
{
_invalidParamToolTip.Tag = CommunityTextBox;
_invalidParamToolTip.ToolTipTitle = Messages.SNMP_ALLOW_COMMUNITY_TITLE;
_invalidParamToolTipText = Messages.SNMP_ALLOW_COMMUNITY_TEXT;
_tuple = (CommunityTextBox, Messages.SNMP_ALLOW_COMMUNITY_TITLE, Messages.SNMP_ALLOW_COMMUNITY_TEXT);
return false;
}
}

if (SupportV3CheckBox.Checked)
{
var usernameStr = UserNameTextBox.Text;
var authPassStr = AuthenticationPasswordLabelTextBox.Text;
var privacyPassStr = PrivacyPasswordTextBox.Text;

if (string.IsNullOrEmpty(usernameStr) || !RegexCommon.Match(usernameStr.Trim()).Success)
{
_invalidParamToolTip.Tag = UserNameTextBox;
_invalidParamToolTip.ToolTipTitle = Messages.SNMP_ALLOW_USER_TITLE;
_invalidParamToolTipText = Messages.SNMP_ALLOW_COMMUNITY_TEXT;
_tuple = (UserNameTextBox, Messages.SNMP_ALLOW_USER_TITLE, Messages.SNMP_ALLOW_COMMUNITY_TEXT);
return false;
}

if (string.IsNullOrEmpty(authPassStr) || !RegexEncryptTextBox.Match(authPassStr.Trim()).Success)
{
_invalidParamToolTip.Tag = AuthenticationPasswordLabelTextBox;
_invalidParamToolTip.ToolTipTitle = Messages.SNMP_ALLOW_AUTH_TITLE;
_invalidParamToolTipText = Messages.SNMP_ALLOW_AUTH_TEXT;
_tuple = (AuthenticationPasswordLabelTextBox, Messages.SNMP_ALLOW_AUTH_TITLE, Messages.SNMP_ALLOW_AUTH_TEXT);
return false;
}

if (string.IsNullOrEmpty(privacyPassStr) || !RegexEncryptTextBox.Match(privacyPassStr.Trim()).Success)
{
_invalidParamToolTip.Tag = PrivacyPasswordTextBox;
_invalidParamToolTip.ToolTipTitle = Messages.SNMP_ALLOW_PRIVACY_TITLE;
_invalidParamToolTipText = Messages.SNMP_ALLOW_AUTH_TEXT;
_tuple = (PrivacyPasswordTextBox, Messages.SNMP_ALLOW_PRIVACY_TITLE, Messages.SNMP_ALLOW_AUTH_TEXT);
return false;
}
}

return true;
}
}

public SnmpEditPage()
{
InitializeComponent();
}

public void Cleanup()
{
_invalidParamToolTip.Dispose();
}

public void ShowLocalValidationMessages()
{
if (_invalidParamToolTip.Tag is Control ctrl)
if (_tuple.Control != null)
{
_invalidParamToolTip.Hide(ctrl);
if (!string.IsNullOrEmpty(_invalidParamToolTip.ToolTipTitle))
{
HelpersGUI.ShowBalloonMessage(ctrl, _invalidParamToolTip, _invalidParamToolTipText);
}
_invalidParamToolTip.ToolTipTitle = _tuple.Title;
HelpersGUI.ShowBalloonMessage(_tuple.Control, _invalidParamToolTip, _tuple.Text);
}
}

public void HideLocalValidationMessages()
{
if (_tuple.Control != null)
_invalidParamToolTip.Hide(_tuple.Control);

_invalidParamToolTip.RemoveAll();
_invalidParamToolTip.ToolTipTitle = null;
}

public AsyncAction SaveSettings()
Expand All @@ -167,16 +167,17 @@ public void SetXenObjects(IXenObject orig, IXenObject clone)

private void ActionCompleted(ActionBase sender)
{
if (sender is SnmpRetrieveAction a && a.Succeeded)
{
_snmpConfiguration = null;

if (sender.Succeeded && sender is SnmpRetrieveAction a && a.SnmpConfiguration != null)
_snmpConfiguration = a.SnmpConfiguration;
Program.Invoke(this.Parent, UpdateRetrieveStatus);
}

Program.Invoke(Parent, UpdateRetrieveStatus);
}

private void UpdateRetrieveStatus()
{
if (_snmpConfiguration.IsSuccessful)
if (_snmpConfiguration != null)
{
_encryptTextBoxFlag = true;
EnableSnmpCheckBox.Enabled = true;
Expand Down Expand Up @@ -205,7 +206,6 @@ private void UpdateRetrieveStatus()
RetrieveSnmpLabel.Text = Messages.SNMP_RETRIEVE_FAILED;
RetrieveSnmpPicture.Image = Images.StaticImages._000_error_h32bit_16;
}

}

private void UpdateCurrentSnmpConfiguration()
Expand Down
2 changes: 0 additions & 2 deletions XenModel/Actions/SNMP/SnmpConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace XenAdmin.Actions.SNMP
{
public class SnmpConfiguration : IEquatable<SnmpConfiguration>
{
public bool IsSuccessful { get; set; }
public bool ServiceStatus { get; set; }
public bool IsSnmpEnabled { get; set; }
public bool IsLogEnabled { get; set; }
Expand All @@ -46,7 +45,6 @@ public class SnmpConfiguration : IEquatable<SnmpConfiguration>
public string AuthProtocol { get; set; }
public string PrivacyPass { get; set; }
public string PrivacyProtocol { get; set; }
public SnmpConfiguration() { }

public bool Equals(SnmpConfiguration other)
{
Expand Down
81 changes: 37 additions & 44 deletions XenModel/Actions/SNMP/SnmpRetrieveAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace XenAdmin.Actions.SNMP
public class SnmpRetrieveAction : AsyncAction
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public SnmpConfiguration SnmpConfiguration { get; private set; } = new SnmpConfiguration();

public SnmpConfiguration SnmpConfiguration { get; private set; }

public SnmpRetrieveAction(IXenObject xenObject, bool suppressHistory)
: base(xenObject.Connection, Messages.SNMP_ACTION_RETRIEVE, Messages.SNMP_ACTION_RETRIEVE, suppressHistory)
Expand All @@ -60,56 +61,48 @@ public SnmpRetrieveAction(IXenObject xenObject, bool suppressHistory)

protected override void Run()
{
SnmpConfiguration.IsSuccessful = true;
try
{
InitSnmpGeneralConfiguration(Host);
InitSnmpServiceStatus(Host);
var resultJson1 = Host.call_plugin(Connection.Session, Host.opaque_ref, SnmpXapiConfig.XAPI_SNMP_PLUGIN_NAME,
SnmpXapiConfig.XAPI_SNMP_GET_CONFIG, null);

Log.InfoFormat("Run SNMP {0}, return: {1}", SnmpXapiConfig.XAPI_SNMP_GET_CONFIG, resultJson1);

var resultObj1 = JsonConvert.DeserializeObject<SnmpConfigurationRes<GetConfigData>>(resultJson1);
if (resultObj1 == null || resultObj1.code != 0)
return;

var resultJson2 = Host.call_plugin(Connection.Session, Host.opaque_ref, SnmpXapiConfig.XAPI_SNMP_PLUGIN_NAME,
SnmpXapiConfig.XAPI_SNMP_GET_STATUS, null);

Log.InfoFormat("Run SNMP {0}, return: {1}", SnmpXapiConfig.XAPI_SNMP_GET_STATUS, resultJson2);

var resultObj2 = JsonConvert.DeserializeObject<SnmpConfigurationRes<GetServiceStatusData>>(resultJson2);
if (resultObj2 == null || resultObj2.code != 0)
return;

SnmpConfiguration = new SnmpConfiguration();

var resultData1 = resultObj1.result;
SnmpConfiguration.IsSnmpEnabled = resultData1.common.enabled == "yes";
SnmpConfiguration.IsLogEnabled = resultData1.common.debug_log == "yes";
SnmpConfiguration.IsV2CEnabled = resultData1.snmpd.v2c == "yes";
SnmpConfiguration.IsV3Enabled = resultData1.snmpd.v3 == "yes";
SnmpConfiguration.Community = resultData1.snmpd_v2c.community;
SnmpConfiguration.UserName = resultData1.snmpd_v3.user_name;
SnmpConfiguration.AuthPass = resultData1.snmpd_v3.authentication_password;
SnmpConfiguration.AuthProtocol = resultData1.snmpd_v3.authentication_protocol;
SnmpConfiguration.PrivacyPass = resultData1.snmpd_v3.privacy_password;
SnmpConfiguration.PrivacyProtocol = resultData1.snmpd_v3.privacy_protocol;

var resultData2 = resultObj2.result;
SnmpConfiguration.ServiceStatus = resultData2.enabled == "enabled" && resultData2.active == "active";
}
catch (Exception e)
{
SnmpConfiguration = null;
Log.ErrorFormat("Run SNMP plugin failed, failed reason: {0}", e.Message);
SnmpConfiguration.IsSuccessful = false;
}
}

private void InitSnmpGeneralConfiguration(IXenObject o)
{
var resultJson = Host.call_plugin(o.Connection.Session, o.opaque_ref, SnmpXapiConfig.XAPI_SNMP_PLUGIN_NAME,
SnmpXapiConfig.XAPI_SNMP_GET_CONFIG, null);
Log.InfoFormat("Run SNMP {0}, return: {1}", SnmpXapiConfig.XAPI_SNMP_GET_CONFIG, resultJson);
var resultObj = JsonConvert.DeserializeObject<SnmpConfigurationRes<GetConfigData>>(resultJson);
if (resultObj == null || resultObj.code != 0)
{
SnmpConfiguration.IsSuccessful = false;
return;
}
var resultData = resultObj.result;
SnmpConfiguration.IsSnmpEnabled = resultData.common.enabled == "yes";
SnmpConfiguration.IsLogEnabled = resultData.common.debug_log == "yes";
SnmpConfiguration.IsV2CEnabled = resultData.snmpd.v2c == "yes";
SnmpConfiguration.IsV3Enabled = resultData.snmpd.v3 == "yes";
SnmpConfiguration.Community = resultData.snmpd_v2c.community;
SnmpConfiguration.UserName = resultData.snmpd_v3.user_name;
SnmpConfiguration.AuthPass = resultData.snmpd_v3.authentication_password;
SnmpConfiguration.AuthProtocol = resultData.snmpd_v3.authentication_protocol;
SnmpConfiguration.PrivacyPass = resultData.snmpd_v3.privacy_password;
SnmpConfiguration.PrivacyProtocol = resultData.snmpd_v3.privacy_protocol;
}

private void InitSnmpServiceStatus(IXenObject o)
{
var resultJson = Host.call_plugin(o.Connection.Session, o.opaque_ref, SnmpXapiConfig.XAPI_SNMP_PLUGIN_NAME,
SnmpXapiConfig.XAPI_SNMP_GET_STATUS, null);
Log.InfoFormat("Run SNMP {0}, return: {1}", SnmpXapiConfig.XAPI_SNMP_GET_STATUS, resultJson);
var resultObj = JsonConvert.DeserializeObject<SnmpConfigurationRes<GetServiceStatusData>>(resultJson);
if (resultObj == null || resultObj.code != 0)
{
SnmpConfiguration.IsSuccessful = false;
return;
}
var resultData = resultObj.result;
SnmpConfiguration.ServiceStatus = resultData.enabled == "enabled" && resultData.active == "active";
}
}
}
29 changes: 19 additions & 10 deletions XenModel/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions XenModel/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -12220,9 +12220,6 @@ Reverting to this snapshot will revert the VM back to the point in time that the
<data name="SNMP_ALLOW_AUTH_TITLE" xml:space="preserve">
<value>Authentication password is invalid</value>
</data>
<data name="SNMP_ALLOW_CHOOSE_TITLE" xml:space="preserve">
<value>You need to select at least one of v2c and v3</value>
</data>
<data name="SNMP_ALLOW_COMMUNITY_TEXT" xml:space="preserve">
<value>The length is between 6 and 32, and it is a combination of: letters, numbers, -.#@=:_</value>
</data>
Expand All @@ -12232,6 +12229,12 @@ Reverting to this snapshot will revert the VM back to the point in time that the
<data name="SNMP_ALLOW_PRIVACY_TITLE" xml:space="preserve">
<value>Privacy password is invalid</value>
</data>
<data name="SNMP_ALLOW_PROTOCOL_TEXT" xml:space="preserve">
<value>You need to select at least one of v2c and v3</value>
</data>
<data name="SNMP_ALLOW_PROTOCOL_TITLE" xml:space="preserve">
<value>A protocol is not selected</value>
</data>
<data name="SNMP_ALLOW_USER_TITLE" xml:space="preserve">
<value>User name is invalid</value>
</data>
Expand All @@ -12242,7 +12245,7 @@ Reverting to this snapshot will revert the VM back to the point in time that the
<value>Failed to retrieve SNMP configuration, please check application logs.</value>
</data>
<data name="SNMP_UPDATE_ERROR1" xml:space="preserve">
<value>An error occurs, please check application logs for details.</value>
<value>An error occurred, please check application logs for details.</value>
</data>
<data name="SNMP_UPDATE_ERROR2" xml:space="preserve">
<value>Parameter missing error, please check application logs for details.</value>
Expand Down

0 comments on commit 68a9b41

Please sign in to comment.