private void PerformRelationshipCleanup(ManagementPackClass seedClass, CreatableEnterpriseManagementObject seedInstance, MonitoringObject monitoringObject) { // it's so complex, because the new instance may not exist EnterpriseManagementObject realSeedInstance; string criteriaString = string.Empty; foreach (var property in seedInstance.GetProperties()) { if (property.Key) { if (criteriaString == string.Empty) { criteriaString = property.Name + "='" + seedInstance[property] + "'"; } else { criteriaString += " AND " + property.Name + "='" + seedInstance[property] + "'"; } } } var searchQuery = new EnterpriseManagementObjectCriteria(criteriaString, seedClass); var realSeedInstanceList = _managementGroup.EntityObjects.GetObjectReader <EnterpriseManagementObject>(searchQuery, ObjectQueryOptions.Default); realSeedInstance = realSeedInstanceList.FirstOrDefault(); if (realSeedInstance == null) { return; } var removalDiscovery = new IncrementalDiscoveryData(); // Management Point bool commitOverwrite = false; var allMAPRelations = _managementGroup.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(realSeedInstance.Id, _relToClearMap, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); var allHSRelations = _managementGroup.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(realSeedInstance.Id, _relToClearHs, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); foreach (var rel in allMAPRelations) { if (rel.SourceObject.Id != monitoringObject.Id) { // remove this relationship removalDiscovery.Remove(rel); commitOverwrite = true; } } foreach (var rel in allHSRelations) { if (rel.SourceObject.Id != monitoringObject.Id) { // remove this relationship removalDiscovery.Remove(rel); commitOverwrite = true; } } if (commitOverwrite) { removalDiscovery.Overwrite(_managementGroup); } }
private void PerformRelationshipCleanup(ManagementPackClass mySeedClass, CreatableEnterpriseManagementObject newSeedInstance, MonitoringObject monitoringObject) { // it's so complex, because the new instance may not exist EnterpriseManagementObject realSeedInstance; string criteriaString = ""; foreach (ManagementPackProperty property in newSeedInstance.GetProperties()) { if (property.Key) { if (criteriaString == "") { criteriaString = property.Name + "='" + newSeedInstance[property] + "'"; } else { criteriaString += " AND " + property.Name + "='" + newSeedInstance[property] + "'"; } } } EnterpriseManagementObjectCriteria searchQuery = new EnterpriseManagementObjectCriteria(criteriaString, mySeedClass); IObjectReader <EnterpriseManagementObject> realSeedInstanceList = myMG.EntityObjects.GetObjectReader <EnterpriseManagementObject>(searchQuery, ObjectQueryOptions.Default); if (realSeedInstanceList.Count == 0) { return; } else { realSeedInstance = realSeedInstanceList.First(); } IncrementalDiscoveryData RemovalDiscovery = new IncrementalDiscoveryData(); // Management Point bool commitOverwrite = false; IEnumerable <EnterpriseManagementRelationshipObject <EnterpriseManagementObject> > allMAPRelations = myMG.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(realSeedInstance.Id, relToClearMAP, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); IEnumerable <EnterpriseManagementRelationshipObject <EnterpriseManagementObject> > allHSRelations = myMG.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(realSeedInstance.Id, relToClearHS, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default).Where(x => !x.IsDeleted); foreach (EnterpriseManagementRelationshipObject <EnterpriseManagementObject> rel in allMAPRelations) { if (rel.SourceObject.Id != monitoringObject.Id) { // remove this relationship RemovalDiscovery.Remove(rel); commitOverwrite = true; } } foreach (EnterpriseManagementRelationshipObject <EnterpriseManagementObject> rel in allHSRelations) { if (rel.SourceObject.Id != monitoringObject.Id) { // remove this relationship RemovalDiscovery.Remove(rel); commitOverwrite = true; } } if (commitOverwrite) { RemovalDiscovery.Overwrite(myConnector); } }