private void NewTestDialog_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.None && DialogResult == DialogResult.OK) { try { IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); incrementalDiscovery.Add(CreatableObjectAdapter.BaseObject); EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); if (ExistingObject == null) { incrementalDiscovery.Commit(connector); // new } else { incrementalDiscovery.Overwrite(connector); // update } } catch (Exception ex) { MessageBox.Show($"Failed to create new test object.\r\nError: {ex.Message}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } } }
private void OnDeleteTest(object sender, CommandEventArgs e) { if (DetailView is TestBrowser tb && tb.Grid.SelectedRows != null && tb.Grid.SelectedRows.Count > 0) { try { TestObjectAdapter firstDataItem = (TestObjectAdapter)tb.Grid.SelectedRows[0].DataBoundItem; bool doDelete = false; if (tb.Grid.SelectedRows.Count == 1) { doDelete = MessageBox.Show($"You're about to delete the {firstDataItem.DisplayName} destination.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } else { doDelete = MessageBox.Show($"You're about to delete multiple destinations.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } if (doDelete) { EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); ManagementPackClass fqdnClass = ManagementGroup.EntityTypes.GetClass(IDs.FullyQualifiedDomainNameClassId); foreach (DataGridViewRow gridRow in tb.Grid.SelectedRows) { TestObjectAdapter dataItem = (TestObjectAdapter)gridRow.DataBoundItem; MonitoringObject currentObject = ManagementGroup.EntityObjects.GetObject <MonitoringObject>(dataItem.Source.Id, ObjectQueryOptions.Default); CreatableEnterpriseManagementObject oldInstance = new CreatableEnterpriseManagementObject(ManagementGroup, currentObject.GetMostDerivedClasses().First()); // host keys oldInstance[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value; oldInstance[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value; // key oldInstance[IDs.TestBaseClassProperties.TestIdPropertyId].Value = currentObject[IDs.TestBaseClassProperties.TestIdPropertyId].Value; incrementalDiscovery.Remove(oldInstance); } incrementalDiscovery.Commit(connector); } // emulate Refresh command for detail view SendRefreshCommandToDetailView(); } catch (Exception ex) { MessageBox.Show($"In {System.Reflection.MethodBase.GetCurrentMethod().Name} {ex.GetType().Name} said {ex.Message} at {ex.StackTrace}"); } } }
private void OnDeleteDestination(object sender, CommandEventArgs e) { if (Grid.SelectedRows != null && Grid.SelectedRows.Count > 0) { try { InstanceState firstDataItem = (InstanceState)(Grid.SelectedRows[0].Cells[0].Tag as GridDataItem).DataItem; bool doDelete = false; if (Grid.SelectedRows.Count == 1) { doDelete = MessageBox.Show($"You're about to delete the {firstDataItem.DisplayName} destination.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } else { doDelete = MessageBox.Show($"You're about to delete multiple destinations.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } if (doDelete) { EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); ManagementPackClass fqdnClass = ManagementGroup.EntityTypes.GetClass(IDs.FullyQualifiedDomainNameClassId); foreach (DataGridViewRow gridRow in Grid.SelectedRows) { InstanceState dataItem = (InstanceState)(gridRow.Cells[0].Tag as GridDataItem).DataItem; MonitoringObject currentObject = dataItem.GetMonitoringObject(ManagementGroup, ObjectQueryOptions.Default, retrieveAllProperties: true); CreatableEnterpriseManagementObject oldInstance = new CreatableEnterpriseManagementObject(ManagementGroup, fqdnClass); oldInstance[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value; oldInstance[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value; incrementalDiscovery.Remove(oldInstance); } incrementalDiscovery.Commit(connector); } // emulate Refresh command OnRefreshCommand(sender, e); } catch (Exception ex) { MessageBox.Show($"In {System.Reflection.MethodBase.GetCurrentMethod().Name} {ex.GetType().Name} said {ex.Message} at {ex.StackTrace}"); } } }
private void NewDestinationDialog_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.None && DialogResult == DialogResult.OK) { string fqdnValue = tbFQDN.Text; int targetIndexValue = 0; if (cbAllowDuplicates.Checked || ExistingObject != null) // for existing object always use existing index value, which was loaded into nudTargetIndex in the constructor { targetIndexValue = Convert.ToInt32(nudTargetIndex.Value); } object selectedMAP = null; if (rbPool.Checked) { selectedMAP = cbResourcePools.SelectedItem; } if (rbAgent.Checked) { selectedMAP = lbAgentList.SelectedItem; } if (selectedMAP is MonitoringObject mo) { tbMAPDetails.Text = $"{mo.DisplayName ?? "N/A"} of {mo.GetType().Name}"; } else { tbMAPDetails.Text = $"{selectedMAP?.ToString() ?? "NULL"} of {selectedMAP?.GetType().Name ?? "N/A"}"; } // basic preliminary checks if (string.IsNullOrWhiteSpace(fqdnValue)) { MessageBox.Show("FQDN value cannot be empty.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true; return; } bool ipResolved; try { IPAddress[] ipList = Dns.GetHostAddresses(fqdnValue); if (ipList == null || ipList.Length == 0) { ipResolved = false; } else { ipResolved = true; } } catch { ipResolved = false; } if (!ipResolved) { if (MessageBox.Show("FQDN is specified, but cannot be resolved into an IP address in the current location, i.e. at the machine where SCOM Console application is running. However, the selected action point might be able to resolve it.\r\nContinue or cancel?", "Warning!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { e.Cancel = true; return; } } if (selectedMAP == null) { MessageBox.Show("Select an agent or a resource pool, which is to handle tests for the destination.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true; return; } // Scenarios: 1) new object 2) edit object 3) move object to a new MAP/HS 4) 2 + 3 #region New Object if (ExistingObjectId == Guid.Empty) { // try to find an existing object, to avoid update instead of insert/add if (IsFQDNClassInstanceExists(fqdnValue, targetIndexValue)) { if (cbAllowDuplicates.Checked) { if (MessageBox.Show("Duplicates are allowed, but a duplicate with the same target index already exists.\r\nFind another index automatically?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int newIndex = 0; while (IsFQDNClassInstanceExists(fqdnValue, newIndex)) { newIndex++; } targetIndexValue = newIndex; nudTargetIndex.Value = newIndex; } else { e.Cancel = true; return; } } else { MessageBox.Show("This destination already exists.\r\nSelect 'Allow duplicates' to create a duplicate destination, or reuse the existing destination to add tests.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true; return; } } // finally, trying to create a new destination try { IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); CreatableEnterpriseManagementObject newInstance = new CreatableEnterpriseManagementObject(ManagementGroup, fqdnClass); CreatableEnterpriseManagementRelationshipObject newSomethingShouldManageInstance = null; newInstance[SystemId.EntityClassProperties.DisplayNamePropertyId].Value = string.IsNullOrEmpty(tbDisplayName.Text) ? fqdnValue : tbDisplayName.Text; newInstance[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value = fqdnValue; newInstance[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value = targetIndexValue; newInstance[IDs.FullyQualifiedDomainNameClassProperties.DescriptionPropertyId].Value = string.IsNullOrEmpty(tbDescription.Text) ? "" : tbDescription.Text; if (rbAgent.Checked) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(ManagementGroup, relHealthServiceShouldManageEntiry); } if (rbPool.Checked) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(ManagementGroup, relResourcePoolShouldManageEntity); } newSomethingShouldManageInstance?.SetTarget(newInstance); newSomethingShouldManageInstance?.SetSource(selectedMAP as MonitoringObject); incrementalDiscovery.Add(newInstance); if (newSomethingShouldManageInstance != null) { incrementalDiscovery.Add(newSomethingShouldManageInstance); } EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); incrementalDiscovery.Commit(connector); } catch (Exception ex) { MessageBox.Show($"Failed to create new destination.\r\nError: {ex.Message}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } // END SCENARIO #1 return; } #endregion #region Update Object details if (ExistingObject != null) { bool objectChanged = false; objectChanged = objectChanged || (ExistingObject[SystemId.EntityClassProperties.DisplayNamePropertyId].Value?.ToString() != tbDisplayName.Text); // immutable: objectChanged = objectChanged || (ExistingObject[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value?.ToString() != fqdnValue); objectChanged = objectChanged || (ExistingObject[IDs.FullyQualifiedDomainNameClassProperties.DescriptionPropertyId].Value?.ToString() != tbDescription.Text); // TargetIndex cannot be changed for existing objects if (objectChanged) { try { IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); CreatableEnterpriseManagementObject modifiedInstance = new CreatableEnterpriseManagementObject(ManagementGroup, fqdnClass); modifiedInstance[SystemId.EntityClassProperties.DisplayNamePropertyId].Value = string.IsNullOrEmpty(tbDisplayName.Text) ? "" : tbDisplayName.Text; modifiedInstance[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value = fqdnValue; modifiedInstance[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value = targetIndexValue; modifiedInstance[IDs.FullyQualifiedDomainNameClassProperties.DescriptionPropertyId].Value = string.IsNullOrEmpty(tbDescription.Text) ? "" : tbDescription.Text; incrementalDiscovery.Add(modifiedInstance); EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); incrementalDiscovery.Overwrite(connector); } catch (Exception ex) { MessageBox.Show($"Failed to update the destination details.\r\nError: {ex.Message}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } } } #endregion #region Move object servicing point if (ExistingObject != null) { IEnumerable <MonitoringObject> currentActionPoints = FindActionPoints(ExistingObject); if (currentActionPoints.Any() && HasMAPChanged(currentActionPoints.First())) { try { MonitoringObject newMAP = selectedMAP as MonitoringObject; // remove all existing SHOULD-relations EnterpriseManagementConnector connector = ManagementGroup.ConnectorFramework.GetMonitoringConnector(); IncrementalDiscoveryData RemovalDiscovery = new IncrementalDiscoveryData(); bool commitOverwrite = false; IEnumerable <EnterpriseManagementRelationshipObject <EnterpriseManagementObject> > allMAPRelations = ManagementGroup.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(ExistingObject.Id, relResourcePoolShouldManageEntity, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); IEnumerable <EnterpriseManagementRelationshipObject <EnterpriseManagementObject> > allHSRelations = ManagementGroup.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(ExistingObject.Id, relHealthServiceShouldManageEntiry, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); foreach (EnterpriseManagementRelationshipObject <EnterpriseManagementObject> rel in allMAPRelations) { if (rel.SourceObject.Id != newMAP.Id) { // remove this relationship RemovalDiscovery.Remove(rel); commitOverwrite = true; } } foreach (EnterpriseManagementRelationshipObject <EnterpriseManagementObject> rel in allHSRelations) { if (rel.SourceObject.Id != newMAP.Id) { // remove this relationship RemovalDiscovery.Remove(rel); commitOverwrite = true; } } if (commitOverwrite) { RemovalDiscovery.Overwrite(connector); } // add the new relationship IncrementalDiscoveryData incrementalDiscovery = new IncrementalDiscoveryData(); CreatableEnterpriseManagementRelationshipObject newSomethingShouldManageInstance = null; if (rbAgent.Checked) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(ManagementGroup, relHealthServiceShouldManageEntiry); } if (rbPool.Checked) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(ManagementGroup, relResourcePoolShouldManageEntity); } newSomethingShouldManageInstance?.SetTarget(ExistingObject); newSomethingShouldManageInstance?.SetSource(newMAP); if (newSomethingShouldManageInstance != null) { incrementalDiscovery.Add(newSomethingShouldManageInstance); } incrementalDiscovery.Commit(connector); } catch (Exception ex) { MessageBox.Show($"Failed to update destination's management action point.\r\nError: {ex.Message}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } } } #endregion } }