/// <summary> /// Deletes the specified appliance. /// </summary> /// <param name="appliance">The appliance.</param> /// <returns>Task<Result>.</returns> public async Task <Result> Delete(FdAppliance appliance) { return(await Task.Run(async() => { try { var obj = await this.FindByHost(appliance.HostIP); var exsitObj = obj.Data; if (exsitObj == null) { return Result.Failed(104, $"{appliance.HostIP} does not exists, delete failed."); } var client = new FdClient(appliance); var res = await client.DeleteGivenSubscriptions(appliance.SubscribeId); LogHelper.Info($"Delete Fd:DeleteGivenSubscriptions:{res.Code} {res.Message}"); var incrementalDiscoveryData = new IncrementalDiscoveryData(); incrementalDiscoveryData.Remove(exsitObj); incrementalDiscoveryData.Commit(MGroup.Instance); return Result.Done(); } catch (Exception e) { return Result.Failed(100, $"Internal error caused by {e.Message}", e); } })); }
/// <summary> /// Updates the specified appliance. /// </summary> /// <param name="appliance">The appliance.</param> /// <param name="isUpdateCredential">是否修改了密码</param> /// <returns>Result.</returns> public async Task <Result> Update(FdAppliance appliance, bool isUpdateCredential) { return(await Task.Run(async() => { try { var obj = await this.FindByHost(appliance.HostIP); var exsitObj = obj.Data; if (exsitObj == null) { return Result.Failed(104, $"Fd {appliance.HostIP} can not find."); } var incrementalDiscoveryData = new IncrementalDiscoveryData(); var props = this.FdApplianceClass.PropertyCollection; exsitObj[props["AliasName"]].Value = appliance.AliasName; exsitObj[props["Port"]].Value = appliance.Port; exsitObj[props["LastModifyTime"]].Value = appliance.LastModifyTime; if (isUpdateCredential) { try { var eventAccount = exsitObj[props["EventUserName"]].Value as string; var eventPd = exsitObj[props["EventPd"]].Value as string; if (eventAccount != appliance.EventUserName || eventPd != appliance.EventPd) { using (var client = new FdClient(appliance)) { var res = await client.DeleteGivenSubscriptions(appliance.SubscribeId); LogHelper.Info($"Update Fd:DeleteGivenSubscriptions:{res.Code} {res.Message}"); // 取消订阅后重置订阅状态 exsitObj[props["SubscribeId"]].Value = string.Empty; exsitObj[props["SubscribeStatus"]].Value = string.Empty; exsitObj[props["LatestSubscribeInfo"]].Value = string.Empty; } } } catch (Exception ex) { LogHelper.Error("DeleteSubscriptions Faild", ex); } exsitObj[props["LoginAccount"]].Value = appliance.LoginAccount; exsitObj[props["LoginPd"]].Value = RijndaelManagedCrypto.Instance.EncryptForCs(appliance.LoginPd); exsitObj[props["EventUserName"]].Value = appliance.EventUserName; exsitObj[props["EventPd"]].Value = RijndaelManagedCrypto.Instance.EncryptForCs(appliance.EventPd); } incrementalDiscoveryData.Add(exsitObj); incrementalDiscoveryData.Overwrite(MGroup.Instance); return Result.Done(); } catch (Exception e) { return Result.Failed(100, $"Internal error caused by {e.Message}", e); } })); }
public async Task <Result <List <FdAppliance> > > LoadAll() { try { var getListResult = await FdApplianceConnector.Instance.All(); if (!getListResult.Success) { LogHelper.Error(getListResult.Cause, getListResult.Message); return(Result <List <FdAppliance> > .Failed(getListResult.Code, getListResult.Message, getListResult.Cause)); } this.AllItems = getListResult.Data.Select(x => GetModelFromMpObject(x)).ToList(); OnFilteredItemsChanged(this.AllItems); foreach (var appliance in this.AllItems) { if (PingFd(appliance.HostIP)) { appliance.LatestConnectInfo = "success"; appliance.LatestStatus = Constants.FdConnectionStatus.ONLINE; } else { appliance.DirectorVersion = string.Empty; appliance.LatestConnectInfo = "Can not connect the remote server."; appliance.LatestStatus = Constants.FdConnectionStatus.FAILED; LogHelper.Info("Can not connect the remote server.", $"PingFd Error:"); } try { using (var client = new FdClient(appliance)) { var result = await client.GetApplianceVersion(); appliance.DirectorVersion = result.CurrentVersion; } } catch (Exception e) { appliance.DirectorVersion = string.Empty; LogHelper.Error(e, $"GetApplianceVersion Error:"); } } OnFilteredItemsChanged(this.AllItems); return(Result <List <FdAppliance> > .Done(this.AllItems)); } catch (Exception ex) { LogHelper.Error(ex, "LoadAll"); return(Result <List <FdAppliance> > .Failed("LoadAll", ex)); } }
/// <summary> /// Finds the by host. /// </summary> /// <param name="host">The host.</param> /// <returns>Task<Result<EnterpriseManagementObject>>.</returns> public async Task <Result <EnterpriseManagementObject> > FindByHost(string host) { return(await Task.Run(() => { try { var objects = MGroup.Instance.Query(this.FdApplianceClass, $"HostIP='{host}'"); return Result <EnterpriseManagementObject> .Done(objects.FirstOrDefault()); } catch (Exception e) { return Result <EnterpriseManagementObject> .Failed("All Error", e); } })); }
/// <summary> /// Alls this instance. /// </summary> /// <returns>Task<Result<List<EnterpriseManagementObject>>>.</returns> public async Task <Result <List <EnterpriseManagementObject> > > All() { return(await Task.Run(() => { try { var objects = MGroup.Instance.All(this.FdApplianceClass); return Result <List <EnterpriseManagementObject> > .Done(objects.ToList()); } catch (Exception e) { return Result <List <EnterpriseManagementObject> > .Failed("Get List Error", e); } })); }
/// <summary> /// Adds the specified appliance. /// </summary> /// <param name="appliance">The appliance.</param> /// <returns>Result.</returns> public async Task <Result> Add(FdAppliance appliance) { return(await Task.Run(() => { try { if (MGroup.Instance.Exsits(this.FdApplianceClass, $"HostIP='{appliance.HostIP}'")) { return Result.Failed(101, $"FusionDirector {appliance.HostIP} already exsits."); } var incrementalDiscoveryData = new IncrementalDiscoveryData(); // add appliance record var emoAppliance = new CreatableEnterpriseManagementObject(MGroup.Instance, this.FdApplianceClass); var props = this.FdApplianceClass.PropertyCollection; emoAppliance[props["UniqueId"]].Value = FusionDirectUniqueIdPrefix + Guid.NewGuid().ToString("D"); emoAppliance[props["HostIP"]].Value = appliance.HostIP; emoAppliance[props["AliasName"]].Value = appliance.AliasName; emoAppliance[props["LoginAccount"]].Value = appliance.LoginAccount; emoAppliance[props["LoginPd"]].Value = RijndaelManagedCrypto.Instance.EncryptForCs(appliance.LoginPd); emoAppliance[props["Port"]].Value = appliance.Port; emoAppliance[props["EventUserName"]].Value = appliance.EventUserName; emoAppliance[props["EventPd"]].Value = RijndaelManagedCrypto.Instance.EncryptForCs(appliance.EventPd); emoAppliance[props["SubscribeId"]].Value = appliance.SubscribeId; emoAppliance[props["SubscribeStatus"]].Value = appliance.SubscribeStatus; emoAppliance[props["LatestSubscribeInfo"]].Value = appliance.LatestSubscribeInfo; emoAppliance[props["LastModifyTime"]].Value = appliance.LastModifyTime; emoAppliance[props["CreateTime"]].Value = appliance.CreateTime; emoAppliance[this.DisplayNameField].Value = appliance.HostIP; incrementalDiscoveryData.Add(emoAppliance); incrementalDiscoveryData.Commit(MGroup.Instance); return Result.Done(); } catch (Exception e) { return Result.Failed(100, $"Internal error caused by {e.Message}", e); } })); }
/// <summary> /// 验证 /// </summary> /// <param name="appliance">appliance</param> /// <param name="isUpdate">是否是更新操作</param> /// <param name="isUpdateCredential">是否修改了密码</param> /// <returns></returns> public Result Validate(FdAppliance appliance, bool isUpdate, bool isUpdateCredential) { #region HostIP var hostIp = appliance.HostIP; if (string.IsNullOrEmpty(hostIp)) { return(Result.Failed(1000, "The FQDN or IP Address is required")); } IPAddress Address = null; var isValidIPAddr = IPAddress.TryParse(hostIp, out Address); if (!isValidIPAddr) //如果不是IP { if (Regex.IsMatch(hostIp.Replace(".", ""), @"^[0-9]*$")) //如果是纯数字,说明不是域名 { return(Result.Failed(1000, "Invalid FQDN or IP Address.")); } else { if (Uri.CheckHostName(hostIp) != UriHostNameType.Dns || !hostIp.Contains(".") || hostIp.EndsWith(".") || hostIp.StartsWith(".")) { return(Result.Failed(1000, "Invalid FQDN or IP Address.")); } } } #endregion #region Port if (string.IsNullOrEmpty(appliance.Port)) { return(Result.Failed(1001, "The port is required")); } int PortAsInt; var isNumeric = int.TryParse(appliance.Port, out PortAsInt); if (isNumeric) { if (PortAsInt < 1 || PortAsInt > 65535) { return(Result.Failed(1001, "The port must between 0 and 65535")); } } else { return(Result.Failed(1001, "The port must between 0 and 65535")); } #endregion if (isUpdateCredential || !isUpdate) { // Login Account if (string.IsNullOrEmpty(appliance.LoginAccount)) { return(Result.Failed(1001, "The LoginAccount is required")); } if (!Regex.IsMatch(appliance.LoginAccount, @"^[a-zA-Z0-9_\-\.]{1,100}$")) { return(Result.Failed(1001, "The loginAccount contains 1 to 100 characters, which can include letters, digits, hyphens (-), underscores (_), and periods (.).")); } // Login Password if (string.IsNullOrEmpty(appliance.LoginPd)) { return(Result.Failed(1001, "The password is required")); } // Event Password var regexEventPwd = new Regex(@" (?=.*[0-9]) #必须包含数字 (?=.*[a-z]) #必须包含小写字母 (?=.*[A-Z]) #必须包含大写字母 (?=([\x21-\x7e]+)[^a-zA-Z0-9]) #必须包含特殊符号 .{8,32} #至少8个字符,最多32个字符 ", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); if (string.IsNullOrEmpty(appliance.EventPd)) { return(Result.Failed(1001, "The event password is required")); } if (appliance.EventPd.Length > 32) { return(Result.Failed(1001, @"The event password must contain 8 to 32 characters.")); } if (!regexEventPwd.IsMatch(appliance.EventPd) || appliance.EventPd.Contains("#")) { return(Result.Failed(1001, @"The event password must contain 8 to 32 characters, and include: uppercase letters, lowercase letters, digits, and special characters including `~!@$%\^&*()_+-={}[]|;:"";'<,>.?")); } } return(Result.Done()); }