public ASRRecoveryPlanGroup(RecoveryPlanGroup recoveryPlanGroup, IList<ReplicationProtectedItem> replicationProtectedItems = null)
        {
            if(recoveryPlanGroup != null)
            {
                this.GroupType = recoveryPlanGroup.GroupType;
                this.StartGroupActions = recoveryPlanGroup.StartGroupActions;
                this.EndGroupActions = recoveryPlanGroup.EndGroupActions;

                if (replicationProtectedItems != null)
                {
                    var replicationProtectedItemList = recoveryPlanGroup.ReplicationProtectedItems.Select(item => item.Id.ToLower());
                    this.ReplicationProtectedItems = replicationProtectedItems.Where(rpi => replicationProtectedItemList.Contains(rpi.Id.ToLower())).ToList();
                }
                else
                {
                    this.ReplicationProtectedItems = new List<ReplicationProtectedItem>();
                }
            }
        }
 public ASRRecoveryPlanGroup(string groupName, RecoveryPlanGroup recoveryPlanGroup, IList<ReplicationProtectedItem> replicationProtectedItems = null)
     : this(recoveryPlanGroup, replicationProtectedItems)
 {
     this.Name = groupName;
 }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            ASRRecoveryPlanGroup tempGroup;

            switch (this.ParameterSetName)
            {
                case ASRParameterSets.AppendGroup:
                    RecoveryPlanGroup recoveryPlanGroup = new RecoveryPlanGroup()
                    {
                        GroupType = Constants.Boot,
                        ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>(),
                        StartGroupActions = new List<RecoveryPlanAction>(),
                        EndGroupActions = new List<RecoveryPlanAction>()
                    };

                    this.RecoveryPlan.Groups.Add(new ASRRecoveryPlanGroup("Group " + (RecoveryPlan.Groups.Count - 1).ToString(), recoveryPlanGroup));
                    break;
                case ASRParameterSets.RemoveGroup:
                    tempGroup = this.RecoveryPlan.Groups.FirstOrDefault(g => String.Compare(g.Name, RemoveGroup.Name, StringComparison.OrdinalIgnoreCase) == 0);

                    if (tempGroup != null)
                    {
                        this.RecoveryPlan.Groups.Remove(tempGroup);
                        this.RecoveryPlan = this.RecoveryPlan.RefreshASRRecoveryPlanGroupNames();
                    }
                    else
                    {
                        throw new PSArgumentException(string.Format(Properties.Resources.GroupNotFoundInRecoveryPlan, this.RemoveGroup.Name, this.RecoveryPlan.FriendlyName));
                    }

                    break;
                case ASRParameterSets.AddProtectedEntities:
                    foreach (ASRProtectionEntity pe in AddProtectedEntities)
                    {
                        string fabricName = Utilities.GetValueFromArmId(pe.ID, ARMResourceTypeConstants.ReplicationFabrics);
                        // fetch the latest PE object
                        ProtectableItemResponse protectableItemResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(fabricName,
                        pe.ProtectionContainerId, pe.Name);

                        ReplicationProtectedItemResponse replicationProtectedItemResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(fabricName,
                        pe.ProtectionContainerId, Utilities.GetValueFromArmId(protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId,
                        ARMResourceTypeConstants.ReplicationProtectedItems));

                        tempGroup = this.RecoveryPlan.Groups.FirstOrDefault(g => String.Compare(g.Name, Group.Name, StringComparison.OrdinalIgnoreCase) == 0);

                        if (tempGroup != null)
                        {
                            foreach (ASRRecoveryPlanGroup gp in this.RecoveryPlan.Groups)
                            {
                                if (gp.ReplicationProtectedItems == null)
                                    continue;

                                if (gp.ReplicationProtectedItems.Any(pi => String.Compare(pi.Id, replicationProtectedItemResponse.ReplicationProtectedItem.Id, StringComparison.OrdinalIgnoreCase) == 0))
                                {
                                    throw new PSArgumentException(string.Format(Properties.Resources.VMAlreadyPartOfGroup, pe.FriendlyName, gp.Name, this.RecoveryPlan.FriendlyName));
                                }
                            }

                            this.RecoveryPlan.Groups[RecoveryPlan.Groups.IndexOf(tempGroup)].ReplicationProtectedItems.Add(replicationProtectedItemResponse.ReplicationProtectedItem);
                        }
                        else
                        {
                            throw new PSArgumentException(string.Format(Properties.Resources.GroupNotFoundInRecoveryPlan, this.Group.Name, this.RecoveryPlan.FriendlyName));
                        }
                    }
                    break;
                case ASRParameterSets.RemoveProtectedEntities:
                    foreach (ASRProtectionEntity pe in RemoveProtectedEntities)
                    {
                        string fabricName = Utilities.GetValueFromArmId(pe.ID, ARMResourceTypeConstants.ReplicationFabrics);
                        // fetch the latest PE object
                        ProtectableItemResponse protectableItemResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(fabricName,
                        pe.ProtectionContainerId, pe.Name);

                        tempGroup = this.RecoveryPlan.Groups.FirstOrDefault(g => String.Compare(g.Name, Group.Name, StringComparison.OrdinalIgnoreCase) == 0);

                        if (tempGroup != null)
                        {
                            var ReplicationProtectedItem =
                                this.RecoveryPlan.Groups[RecoveryPlan.Groups.IndexOf(tempGroup)].
                                ReplicationProtectedItems.
                                FirstOrDefault(pi => String.Compare(pi.Id,
                                protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId,
                                StringComparison.OrdinalIgnoreCase) == 0);

                            if (ReplicationProtectedItem != null)
                            {
                                this.RecoveryPlan.Groups[RecoveryPlan.Groups.IndexOf(tempGroup)].ReplicationProtectedItems.Remove(ReplicationProtectedItem);
                            }
                            else
                            {
                                throw new PSArgumentException(string.Format(Properties.Resources.VMNotFoundInGroup, pe.FriendlyName, this.Group.Name, this.RecoveryPlan.FriendlyName));
                            }
                        }
                        else
                        {
                            throw new PSArgumentException(string.Format(Properties.Resources.GroupNotFoundInRecoveryPlan, this.Group.Name, this.RecoveryPlan.FriendlyName));
                        }
                    }
                    break;
            };

            this.WriteObject(this.RecoveryPlan);
        }
        /// <summary>
        /// Update Recovery Plan: By powerShell Recovery Plan object
        /// </summary>
        private void UpdateRecoveryPlan(ASRRecoveryPlan asrRecoveryPlan)
        {
            UpdateRecoveryPlanInputProperties updateRecoveryPlanInputProperties = new UpdateRecoveryPlanInputProperties()
            {
                Groups = new List<RecoveryPlanGroup>(),
            };

            foreach (ASRRecoveryPlanGroup asrRecoveryPlanGroup in asrRecoveryPlan.Groups)
            {
                RecoveryPlanGroup recoveryPlanGroup = new RecoveryPlanGroup()
                {
                    GroupType = asrRecoveryPlanGroup.GroupType,

                    // Initialize ReplicationProtectedItems with empty List if asrRecoveryPlanGroup.ReplicationProtectedItems is null
                    // otherwise assign respective values
                    ReplicationProtectedItems = asrRecoveryPlanGroup.ReplicationProtectedItems == null ? new List<RecoveryPlanProtectedItem>() :
                    asrRecoveryPlanGroup.ReplicationProtectedItems.Select(item =>
                        {
                            var newItem = new RecoveryPlanProtectedItem(item.Id);

                            string VmId = null;

                            if (item.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplicaAzureReplicationDetails))
                            {
                                VmId = ((HyperVReplicaAzureReplicationDetails)item.Properties.ProviderSpecificDetails).VmId;
                            }
                            else if (item.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplica2012ReplicationDetails))
                            {
                                VmId = ((HyperVReplica2012ReplicationDetails)item.Properties.ProviderSpecificDetails).VmId;
                            }

                            newItem.VirtualMachineId = VmId;

                            return newItem;

                        }).ToList(),
                    StartGroupActions = asrRecoveryPlanGroup.StartGroupActions,
                    EndGroupActions = asrRecoveryPlanGroup.EndGroupActions
                };

                updateRecoveryPlanInputProperties.Groups.Add(recoveryPlanGroup);
            }

            UpdateRecoveryPlanInput updateRecoveryPlanInput = new UpdateRecoveryPlanInput()
            {
                Properties = updateRecoveryPlanInputProperties
            };

            UpdateRecoveryPlan(asrRecoveryPlan.Name, updateRecoveryPlanInput);
        }
        /// <summary>
        /// Creates Replication Plan
        /// </summary>
        private void CreateRecoveryPlan()
        {
            CreateRecoveryPlanInputProperties createRecoveryPlanInputProperties = new CreateRecoveryPlanInputProperties()
            {
                FailoverDeploymentModel = failoverDeploymentModel,
                Groups = new List<RecoveryPlanGroup>(),
                PrimaryFabricId = this.primaryserver,
                RecoveryFabricId = this.recoveryserver
            };

            RecoveryPlanGroup recoveryPlanGroup = new RecoveryPlanGroup()
            {
                GroupType = Constants.Boot,
                ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>(),
                StartGroupActions = new List<RecoveryPlanAction>(),
                EndGroupActions = new List<RecoveryPlanAction>()
            };

            foreach (ASRReplicationProtectedItem rpi in ReplicationProtectedItem)
            {
                string fabricName = Utilities.GetValueFromArmId(rpi.ID, ARMResourceTypeConstants.ReplicationFabrics);

                ReplicationProtectedItemResponse replicationProtectedItemResponse =
                            RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(fabricName,
                            Utilities.GetValueFromArmId(rpi.ID, ARMResourceTypeConstants.ReplicationProtectionContainers), 
                            rpi.Name);

                string VmId = null;

                if (replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplicaAzureReplicationDetails))
                {
                    VmId = ((HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails).VmId;
                }
                else if (replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplica2012ReplicationDetails))
                {
                    VmId = ((HyperVReplica2012ReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails).VmId;
                }

                RecoveryPlanProtectedItem recoveryPlanProtectedItem = new RecoveryPlanProtectedItem();
                recoveryPlanProtectedItem.Id = replicationProtectedItemResponse.ReplicationProtectedItem.Id;
                recoveryPlanProtectedItem.VirtualMachineId = VmId;
                recoveryPlanGroup.ReplicationProtectedItems.Add(recoveryPlanProtectedItem);
            }

            createRecoveryPlanInputProperties.Groups.Add(recoveryPlanGroup);

            CreateRecoveryPlanInput createRecoveryPlanInput = new CreateRecoveryPlanInput()
            {
                Properties = createRecoveryPlanInputProperties
            };

            CreateRecoveryPlan(this.Name, createRecoveryPlanInput);
        }