private void PerformSCOMDiscovery(SCOMDiscoveryType direction, IList <T> objects) { IncrementalDiscoveryData discoveryDataIncremental = PrepareSCOMDiscoveryData(direction, objects); switch (direction) { case SCOMDiscoveryType.Insert: discoveryDataIncremental.Commit(myConnector); break; case SCOMDiscoveryType.Update: discoveryDataIncremental.Overwrite(myConnector); break; case SCOMDiscoveryType.Delete: discoveryDataIncremental.Commit(myConnector); break; } // after successful operation set all items to committed state foreach (var newObject in objects) { newObject.CommitStatus = InstanceCommitStatus.Committed; } }
protected virtual IncrementalDiscoveryData PrepareSCOMDiscoveryData(SCOMDiscoveryType direction, IList <T> objects) { IncrementalDiscoveryData result = new IncrementalDiscoveryData(); // create object for both incremental and full discovery bool hasData = false; foreach (T newObject in objects) { if (!HasAllRequiredProperties(newObject)) { continue; } hasData = true; CreatableEnterpriseManagementObject newSeedInstance = new CreatableEnterpriseManagementObject(myMG, mySeedClass); CreatableEnterpriseManagementRelationshipObject newSomethingShouldManageInstance = null; foreach (var classProperty in mySeedClass.PropertyCollection) { try { newSeedInstance[classProperty].Value = newObject[classProperty.Id]; } catch (KeyNotFoundException) // ignore situations, when non-key or non-required fields are not available { if (classProperty.Required || classProperty.Key) { throw; } } } // also try to set DisplayName if available try { newSeedInstance[SystemId.EntityClassProperties.DisplayNamePropertyId].Value = newObject.GetClassInstanceProperty(SystemId.EntityClassProperties.DisplayNamePropertyId); } catch (KeyNotFoundException) { } // ignore // Unhosted, with specific action point if (!mySeedClass.Hosted && myActionPointClass != null && newObject.ActionPoint != null) { if (relMAPShouldManageEntity != null) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(myMG, relMAPShouldManageEntity); newSomethingShouldManageInstance.SetTarget(newSeedInstance); newSomethingShouldManageInstance.SetSource(newObject.ActionPoint); } // sometimes when specific Health Service is deleted, relationship may revert to All Management Server Pool if (relHSShouldManageEntiry != null && !newObject.ActionPoint.IsInstanceOf(myMG.EntityTypes.GetClass(SystemCenterId.ManagementServicePoolClassId))) { newSomethingShouldManageInstance = new CreatableEnterpriseManagementRelationshipObject(myMG, relHSShouldManageEntiry); newSomethingShouldManageInstance.SetTarget(newSeedInstance); newSomethingShouldManageInstance.SetSource(newObject.ActionPoint); } if (relMAPShouldManageEntity == null && relHSShouldManageEntiry == null) { throw new NotSupportedException("Scenario not supported."); } } // Hosted, in this case myActionPointClass is the hosing class if (mySeedClass.Hosted && myActionPointClass != null && newObject.ActionPoint != null) { ManagementPackClass hostClass = myActionPointClass; while (hostClass != null) { foreach (ManagementPackProperty hostProperty in hostClass.PropertyCollection) { if (hostProperty.Key) { newSeedInstance[hostProperty].Value = newObject.ActionPoint[hostProperty].Value; } } hostClass = hostClass.FindHostClass(); } } if (hasData) { switch (direction) { case SCOMDiscoveryType.Insert: case SCOMDiscoveryType.Update: result.Add(newSeedInstance); if (newSomethingShouldManageInstance != null) { result.Add(newSomethingShouldManageInstance); } // don't need Hosted==True check, newSomethingShouldManageInstance will be null for hosted classes if (newSomethingShouldManageInstance != null && direction == SCOMDiscoveryType.Update) { PerformRelationshipCleanup(mySeedClass, newSeedInstance, newObject.ActionPoint); } if (mySeedClass.Hosted) { } break; case SCOMDiscoveryType.Delete: result.Remove(newSeedInstance); if (newSomethingShouldManageInstance != null) { result.Remove(newSomethingShouldManageInstance); } break; } } } return(result); }