Пример #1
0
        public void UpdateProtection()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetSiteRecoveryClient(CustomHttpHandler);

                string replicationProtectedItemName = "PE1447651278";
                string fabricName    = "Vmm;b6d8b350-2ee5-40c0-b777-2158a87c2aee";
                string containerName = "cloud_b6d8b350-2ee5-40c0-b777-2158a87c2aee";

                // Network vmNic = client.Network.List(fabricName, RequestHeaders).NetworksList[0];

                var protectedItem = client.ReplicationProtectedItem.Get(fabricName, containerName, replicationProtectedItemName, RequestHeaders);

                UpdateReplicationProtectedItemInputProperties inputProps = new UpdateReplicationProtectedItemInputProperties()
                {
                    RecoveryAzureVMSize = "Basic_A0",
                    RecoveryAzureVMName = "B2AVM4NewName",
                    //SelectedPrimaryNicId = (protectedItem.ReplicationProtectedItem.Properties.ProviderSpecificDetails as HyperVReplicaAzureReplicationDetails).VMNics[0].NicId,
                    SelectedRecoveryAzureNetworkId = "/subscriptions/19b823e2-d1f3-4805-93d7-401c5d8230d5/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/ramjsingNetwork1"
                };

                UpdateReplicationProtectedItemInput input = new UpdateReplicationProtectedItemInput()
                {
                    Properties = inputProps
                };

                var resp = client.ReplicationProtectedItem.UpdateProtection(fabricName, containerName, replicationProtectedItemName, input, RequestHeaders);
            }
        }
Пример #2
0
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            ProtectableItemResponse protectableItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectableItem(Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                           this.VirtualMachine.ProtectionContainerId, this.VirtualMachine.Name);

            if (protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId == null)
            {
                this.WriteWarning(Properties.Resources.ProtectionIsNotEnabledForVM.ToString());
                return;
            }

            ReplicationProtectedItemResponse replicationProtectedItemResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                                                                                    this.VirtualMachine.ProtectionContainerId, Utilities.GetValueFromArmId(protectableItemResponse.ProtectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems));

            // Check for Replication Provider type HyperVReplicaAzure
            if (0 != string.Compare(
                    replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.InstanceType,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning(Properties.Resources.UnsupportedReplicationProvidedForUpdateVmProperties.ToString());
                return;
            }

            // Check for at least one option
            if (string.IsNullOrEmpty(this.Name) &&
                string.IsNullOrEmpty(this.Size) &&
                string.IsNullOrEmpty(this.PrimaryNic) &&
                string.IsNullOrEmpty(this.RecoveryNetworkId))
            {
                this.WriteWarning(Properties.Resources.ArgumentsMissingForUpdateVmProperties.ToString());
                return;
            }

            // Both primary & recovery inputs should be present
            if (string.IsNullOrEmpty(this.PrimaryNic) ^
                string.IsNullOrEmpty(this.RecoveryNetworkId))
            {
                this.WriteWarning(Properties.Resources.NetworkArgumentsMissingForUpdateVmProperties.ToString());
                return;
            }

            List <VMNicInputDetails> vMNicInputDetailsList = new List <VMNicInputDetails>();
            VMNicDetails             vMNicDetailsToBeUpdated;

            if (!string.IsNullOrEmpty(this.PrimaryNic))
            {
                HyperVReplicaAzureReplicationDetails providerSpecificDetails =
                    (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails;

                if (providerSpecificDetails.VMNics != null)
                {
                    vMNicDetailsToBeUpdated = providerSpecificDetails.VMNics.SingleOrDefault(n => string.Compare(n.NicId, this.PrimaryNic, StringComparison.OrdinalIgnoreCase) == 0);
                    if (vMNicDetailsToBeUpdated != null)
                    {
                        VMNicInputDetails vMNicInputDetails = new VMNicInputDetails();

                        vMNicInputDetails.NicId = this.PrimaryNic;
                        vMNicInputDetails.RecoveryVMSubnetName      = this.RecoveryNicSubnetName;
                        vMNicInputDetails.ReplicaNicStaticIPAddress = this.RecoveryNicStaticIPAddress;
                        vMNicInputDetails.SelectionType             = string.IsNullOrEmpty(this.NicSelectionType) ? Constants.SelectedByUser : this.NicSelectionType;
                        vMNicInputDetailsList.Add(vMNicInputDetails);

                        IEnumerable <VMNicDetails> vMNicDetailsListRemaining = providerSpecificDetails.VMNics.Where(n => string.Compare(n.NicId, this.PrimaryNic, StringComparison.OrdinalIgnoreCase) != 0);
                        foreach (VMNicDetails nDetails in vMNicDetailsListRemaining)
                        {
                            vMNicInputDetails = new VMNicInputDetails();

                            vMNicInputDetails.NicId = nDetails.NicId;
                            vMNicInputDetails.RecoveryVMSubnetName      = nDetails.RecoveryVMSubnetName;
                            vMNicInputDetails.ReplicaNicStaticIPAddress = nDetails.ReplicaNicStaticIPAddress;
                            vMNicInputDetails.SelectionType             = nDetails.SelectionType;
                            vMNicInputDetailsList.Add(vMNicInputDetails);
                        }
                    }
                    else
                    {
                        throw new PSInvalidOperationException(Properties.Resources.NicNotFoundInVMForUpdateVmProperties);
                    }
                }
            }

            UpdateReplicationProtectedItemInputProperties updateReplicationProtectedItemInputProperties = new UpdateReplicationProtectedItemInputProperties()
            {
                RecoveryAzureVMName            = this.Name,
                RecoveryAzureVMSize            = this.Size,
                SelectedRecoveryAzureNetworkId = this.RecoveryNetworkId,
                VmNics = vMNicInputDetailsList
            };

            UpdateReplicationProtectedItemInput input = new UpdateReplicationProtectedItemInput()
            {
                Properties = updateReplicationProtectedItemInputProperties
            };

            LongRunningOperationResponse response = RecoveryServicesClient.UpdateVmProperties(
                Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationFabrics),
                Utilities.GetValueFromArmId(this.VirtualMachine.ID, ARMResourceTypeConstants.ReplicationProtectionContainers),
                replicationProtectedItemResponse.ReplicationProtectedItem.Name,
                input);

            JobResponse jobResponse =
                RecoveryServicesClient
                .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

            WriteObject(new ASRJob(jobResponse.Job));
        }
Пример #3
0
        /// <summary>
        ///     ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            if (this.ShouldProcess(
                    this.InputObject.FriendlyName,
                    VerbsCommon.Set))
            {
                var replicationProtectedItemResponse = this.RecoveryServicesClient
                                                       .GetAzureSiteRecoveryReplicationProtectedItem(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name);

                var provider = replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                // Check for Replication Provider type HyperVReplicaAzure/InMageAzureV2
                if (!(provider is HyperVReplicaAzureReplicationDetails) &&
                    !(provider is InMageAzureV2ReplicationDetails) &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(
                        Resources.UnsupportedReplicationProvidedForUpdateVmProperties);
                    return;
                }

                // Check for at least one option
                if (string.IsNullOrEmpty(this.Name) &&
                    string.IsNullOrEmpty(this.Size) &&
                    string.IsNullOrEmpty(this.PrimaryNic) &&
                    string.IsNullOrEmpty(this.RecoveryNetworkId) &&
                    this.UseManagedDisk == null &&
                    string.IsNullOrEmpty(this.RecoveryAvailabilitySet) &&
                    string.IsNullOrEmpty(this.RecoveryCloudServiceId) &&
                    string.IsNullOrEmpty(this.RecoveryResourceGroupId) &&
                    string.IsNullOrEmpty(this.LicenseType))
                {
                    this.WriteWarning(Resources.ArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // Both primary & recovery inputs should be present
                if (string.IsNullOrEmpty(this.PrimaryNic) ^
                    string.IsNullOrEmpty(this.RecoveryNetworkId))
                {
                    this.WriteWarning(Resources.NetworkArgumentsMissingForUpdateVmProperties);
                    return;
                }

                var vmName = this.Name;
                var vmSize = this.Size;
                var vmRecoveryNetworkId     = this.RecoveryNetworkId;
                var licenseType             = this.LicenseType;
                var recoveryResourceGroupId = this.RecoveryResourceGroupId;
                var recoveryCloudServiceId  = this.RecoveryCloudServiceId;
                var useManagedDisk          = this.UseManagedDisk;
                var availabilitySetId       = this.RecoveryAvailabilitySet;
                var vMNicInputDetailsList   = new List <VMNicInputDetails>();
                var providerSpecificInput   = new UpdateReplicationProtectedItemProviderInput();

                if (provider is HyperVReplicaAzureReplicationDetails)
                {
                    var providerSpecificDetails =
                        (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse
                        .Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryAvailabilitySet))
                    {
                        availabilitySetId = providerSpecificDetails.RecoveryAvailabilitySetId;
                    }

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks = useManagedDisk
                        };
                    }

                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is InMageAzureV2ReplicationDetails)
                {
                    var providerSpecificDetails =
                        (InMageAzureV2ReplicationDetails)replicationProtectedItemResponse.Properties
                        .ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryAvailabilitySet))
                    {
                        availabilitySetId = providerSpecificDetails.RecoveryAvailabilitySetId;
                    }

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks = useManagedDisk
                        };
                    }
                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is A2AReplicationDetails)
                {
                    A2AReplicationDetails providerSpecificDetails = (A2AReplicationDetails)replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryResourceGroupId)))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryCloudServiceId)))
                    {
                        recoveryCloudServiceId =
                            providerSpecificDetails.RecoveryCloudService;
                    }

                    providerSpecificInput = new A2AUpdateReplicationProtectedItemInput()
                    {
                        RecoveryCloudServiceId  = this.RecoveryCloudServiceId,
                        RecoveryResourceGroupId = this.RecoveryResourceGroupId
                    };
                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails.SelectedRecoveryAzureNetworkId;
                    }
                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }

                var updateReplicationProtectedItemInputProperties =
                    new UpdateReplicationProtectedItemInputProperties
                {
                    RecoveryAzureVMName            = vmName,
                    RecoveryAzureVMSize            = vmSize,
                    SelectedRecoveryAzureNetworkId = vmRecoveryNetworkId,
                    VmNics      = vMNicInputDetailsList,
                    LicenseType =
                        licenseType ==
                        Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType.ToString()
                                ? Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType
                                : Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .WindowsServer,
                    RecoveryAvailabilitySetId = availabilitySetId,
                    ProviderSpecificDetails   = providerSpecificInput
                };

                var input = new UpdateReplicationProtectedItemInput
                {
                    Properties = updateReplicationProtectedItemInputProperties
                };

                var response = this.RecoveryServicesClient.UpdateVmProperties(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name,
                    input);

                var jobResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryJobDetails(
                    PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                this.WriteObject(new ASRJob(jobResponse));
            }
        }
Пример #4
0
        /// <summary>
        ///     ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            if (this.ShouldProcess(
                    this.InputObject.FriendlyName,
                    VerbsCommon.Set))
            {
                var replicationProtectedItemResponse = this.RecoveryServicesClient
                                                       .GetAzureSiteRecoveryReplicationProtectedItem(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name);

                var provider = replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                // Check for Replication Provider type HyperVReplicaAzure/InMageAzureV2/A2A
                if (!(provider is HyperVReplicaAzureReplicationDetails) &&
                    !(provider is InMageAzureV2ReplicationDetails) &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(
                        Resources.UnsupportedReplicationProvidedForUpdateVmProperties);
                    return;
                }

                // Check for at least one option
                if (string.IsNullOrEmpty(this.Name) &&
                    string.IsNullOrEmpty(this.Size) &&
                    string.IsNullOrEmpty(this.UpdateNic) &&
                    string.IsNullOrEmpty(this.RecoveryNetworkId) &&
                    string.IsNullOrEmpty(this.PrimaryNic) &&
                    this.UseManagedDisk == null &&
                    this.IsParameterBound(c => c.RecoveryAvailabilitySet) &&
                    this.IsParameterBound(c => c.RecoveryAvailabilityZone) &&
                    this.IsParameterBound(c => c.RecoveryProximityPlacementGroupId) &&
                    string.IsNullOrEmpty(this.RecoveryCloudServiceId) &&
                    string.IsNullOrEmpty(this.RecoveryResourceGroupId) &&
                    string.IsNullOrEmpty(this.LicenseType) &&
                    string.IsNullOrEmpty(this.RecoveryBootDiagStorageAccountId) &&
                    this.AzureToAzureUpdateReplicationConfiguration == null)
                {
                    this.WriteWarning(Resources.ArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // Both primary & recovery inputs should be present
                if (this.ASRVMNicConfiguration == null &&
                    (string.IsNullOrEmpty(this.UpdateNic) ^
                     string.IsNullOrEmpty(this.RecoveryNetworkId)))
                {
                    this.WriteWarning(Resources.NetworkArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // NSG, LB and PIP only for A2A provider.
                if ((!string.IsNullOrEmpty(RecoveryNetworkSecurityGroupId) ||
                     !string.IsNullOrEmpty(RecoveryPublicIPAddressId) ||
                     RecoveryLBBackendAddressPoolId != null &&
                     RecoveryLBBackendAddressPoolId.Length > 0) &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(
                        Resources.UnsupportedReplicationProvidedForNetworkingResources);
                    return;
                }

                if (this.ASRVMNicConfiguration != null &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(Resources.UnsupportedReplicationProvidedForASRVMNicConfig);
                    return;
                }

                if (this.ASRVMNicConfiguration != null &&
                    !string.IsNullOrEmpty(this.UpdateNic))
                {
                    this.WriteWarning(Resources.ASRVMNicsAndUpdateNicNotAllowed);
                    return;
                }

                var vmName = this.Name;
                var vmSize = this.Size;
                var vmRecoveryNetworkId       = this.RecoveryNetworkId;
                var licenseType               = this.LicenseType;
                var recoveryResourceGroupId   = this.RecoveryResourceGroupId;
                var recoveryCloudServiceId    = this.RecoveryCloudServiceId;
                var useManagedDisk            = this.UseManagedDisk;
                var availabilitySetId         = this.RecoveryAvailabilitySet;
                var proximityPlacementGroupId = this.RecoveryProximityPlacementGroupId;
                var availabilityZone          = this.RecoveryAvailabilityZone;
                var primaryNic = this.PrimaryNic;
                var diskIdToDiskEncryptionMap = this.DiskIdToDiskEncryptionSetMap;
                var tfoNetworkId          = string.Empty;
                var vMNicInputDetailsList = new List <VMNicInputDetails>();
                var providerSpecificInput = new UpdateReplicationProtectedItemProviderInput();

                if (provider is HyperVReplicaAzureReplicationDetails)
                {
                    var providerSpecificDetails =
                        (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse
                        .Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVmName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet
                        : providerSpecificDetails.RecoveryAvailabilitySetId;

                    availabilityZone = this.IsParameterBound(c => c.RecoveryAvailabilityZone)
                        ? this.RecoveryAvailabilityZone
                        : providerSpecificDetails.TargetAvailabilityZone;

                    proximityPlacementGroupId = this.IsParameterBound(c => c.RecoveryProximityPlacementGroupId)
                        ? this.RecoveryProximityPlacementGroupId
                        : providerSpecificDetails.TargetProximityPlacementGroupId;

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.PrimaryNic)))
                    {
                        primaryNic = providerSpecificDetails.SelectedSourceNicId;
                    }

                    if (this.DiskIdToDiskEncryptionSetMap == null ||
                        this.DiskIdToDiskEncryptionSetMap.Count == 0)
                    {
                        diskIdToDiskEncryptionMap = providerSpecificDetails.AzureVmDiskDetails.
                                                    ToDictionary(x => x.DiskId, x => x.DiskEncryptionSetId);
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks                 = useManagedDisk,
                            DiskIdToDiskEncryptionMap       = this.DiskIdToDiskEncryptionSetMap,
                            TargetAvailabilityZone          = availabilityZone,
                            TargetProximityPlacementGroupId = proximityPlacementGroupId
                        };
                    }

                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is InMageAzureV2ReplicationDetails)
                {
                    var providerSpecificDetails =
                        (InMageAzureV2ReplicationDetails)replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails.SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet : providerSpecificDetails.RecoveryAvailabilitySetId;

                    availabilityZone = this.IsParameterBound(c => c.RecoveryAvailabilityZone)
                        ? this.RecoveryAvailabilityZone : providerSpecificDetails.TargetAvailabilityZone;

                    proximityPlacementGroupId = this.IsParameterBound(c => c.RecoveryProximityPlacementGroupId)
                        ? this.RecoveryProximityPlacementGroupId : providerSpecificDetails.TargetProximityPlacementGroupId;

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId = providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.PrimaryNic)))
                    {
                        primaryNic = providerSpecificDetails.SelectedSourceNicId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.TargetVmId,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.ClassicCompute.ToLower()))
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks                 = useManagedDisk,
                            TargetAvailabilityZone          = availabilityZone,
                            TargetProximityPlacementGroupId = proximityPlacementGroupId
                        };
                    }
                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is A2AReplicationDetails)
                {
                    A2AReplicationDetails providerSpecificDetails = (A2AReplicationDetails)replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryResourceGroupId)))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet
                        : providerSpecificDetails.RecoveryAvailabilitySet;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryNetworkId)))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails.SelectedRecoveryAzureNetworkId;
                    }

                    proximityPlacementGroupId = this.IsParameterBound(c => c.RecoveryProximityPlacementGroupId)
                       ? this.RecoveryProximityPlacementGroupId
                       : providerSpecificDetails.RecoveryProximityPlacementGroupId;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryCloudServiceId)))
                    {
                        recoveryCloudServiceId =
                            providerSpecificDetails.RecoveryCloudService;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryBootDiagStorageAccountId)))
                    {
                        this.RecoveryBootDiagStorageAccountId = providerSpecificDetails.RecoveryBootDiagStorageAccountId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.TfoAzureVMName)))
                    {
                        this.TfoAzureVMName = providerSpecificDetails.TfoAzureVMName;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.Name)))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    List <A2AVmManagedDiskUpdateDetails> managedDiskUpdateDetails = null;

                    // ManagedDisk case
                    if (this.AzureToAzureUpdateReplicationConfiguration == null && providerSpecificDetails.ProtectedManagedDisks != null)
                    {
                        managedDiskUpdateDetails = new List <A2AVmManagedDiskUpdateDetails>();
                        foreach (var managedDisk in providerSpecificDetails.ProtectedManagedDisks)
                        {
                            managedDiskUpdateDetails.Add(
                                new A2AVmManagedDiskUpdateDetails(
                                    managedDisk.DiskId,
                                    managedDisk.RecoveryTargetDiskAccountType,
                                    managedDisk.RecoveryReplicaDiskAccountType,
                                    failoverDiskName: managedDisk.FailoverDiskName,
                                    tfoDiskName: managedDisk.TfoDiskName));
                        }
                    }
                    else if (this.AzureToAzureUpdateReplicationConfiguration != null && this.AzureToAzureUpdateReplicationConfiguration[0].IsManagedDisk)
                    {
                        managedDiskUpdateDetails = new List <A2AVmManagedDiskUpdateDetails>();
                        foreach (var managedDisk in this.AzureToAzureUpdateReplicationConfiguration)
                        {
                            managedDiskUpdateDetails.Add(
                                new A2AVmManagedDiskUpdateDetails(
                                    managedDisk.DiskId,
                                    managedDisk.RecoveryTargetDiskAccountType,
                                    managedDisk.RecoveryReplicaDiskAccountType,
                                    failoverDiskName: managedDisk.FailoverDiskName,
                                    tfoDiskName: managedDisk.TfoDiskName,
                                    diskEncryptionInfo: Utilities.A2AEncryptionDetails(
                                        managedDisk.DiskEncryptionSecretUrl,
                                        managedDisk.DiskEncryptionVaultId,
                                        managedDisk.KeyEncryptionKeyUrl,
                                        managedDisk.KeyEncryptionVaultId)));
                        }
                    }

                    providerSpecificInput = new A2AUpdateReplicationProtectedItemInput()
                    {
                        RecoveryCloudServiceId            = this.RecoveryCloudServiceId,
                        RecoveryResourceGroupId           = this.RecoveryResourceGroupId,
                        RecoveryProximityPlacementGroupId = this.RecoveryProximityPlacementGroupId,
                        RecoveryBootDiagStorageAccountId  = this.RecoveryBootDiagStorageAccountId,
                        ManagedDiskUpdateDetails          = managedDiskUpdateDetails,
                        TfoAzureVMName     = this.TfoAzureVMName,
                        DiskEncryptionInfo = Utilities.A2AEncryptionDetails(
                            this.DiskEncryptionSecretUrl,
                            this.DiskEncryptionVaultId,
                            this.KeyEncryptionKeyUrl,
                            this.KeyEncryptionVaultId)
                    };

                    if (this.ASRVMNicConfiguration != null &&
                        this.ASRVMNicConfiguration.Count() > 0)
                    {
                        var recoveryNetworkIds = new HashSet <string>();
                        var tfoNetworkIds      = new HashSet <string>();

                        this.ASRVMNicConfiguration.ForEach(
                            nic =>
                        {
                            if (!string.IsNullOrEmpty(nic.RecoveryVMNetworkId))
                            {
                                recoveryNetworkIds.Add(nic.RecoveryVMNetworkId);
                            }
                        });
                        this.ASRVMNicConfiguration.ForEach(
                            nic =>
                        {
                            if (!string.IsNullOrEmpty(nic.TfoVMNetworkId))
                            {
                                tfoNetworkIds.Add(nic.TfoVMNetworkId);
                            }
                        });

                        if (recoveryNetworkIds.Count() > 1)
                        {
                            this.WriteWarning(Resources.RecoveryNetworkIdConflictInASRVMNics);
                            return;
                        }

                        if (tfoNetworkIds.Count() > 1)
                        {
                            this.WriteWarning(Resources.TfoNetworkIdConflictInASRVMNics);
                            return;
                        }

                        if (!string.IsNullOrEmpty(this.RecoveryNetworkId) &&
                            !string.IsNullOrEmpty(recoveryNetworkIds.FirstOrDefault()) &&
                            !this.RecoveryNetworkId.Equals(
                                recoveryNetworkIds.First(), StringComparison.OrdinalIgnoreCase))
                        {
                            this.WriteWarning(Resources.RecoveryNetworkInformationMismatch);
                            return;
                        }

                        if (!string.IsNullOrEmpty(recoveryNetworkIds.FirstOrDefault()))
                        {
                            vmRecoveryNetworkId = recoveryNetworkIds.First();
                        }

                        tfoNetworkId = tfoNetworkIds.FirstOrDefault();
                    }

                    if (string.IsNullOrEmpty(tfoNetworkId))
                    {
                        tfoNetworkId = providerSpecificDetails.SelectedTfoAzureNetworkId;
                    }

                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }

                var updateReplicationProtectedItemInputProperties =
                    new UpdateReplicationProtectedItemInputProperties
                {
                    RecoveryAzureVMName            = vmName,
                    RecoveryAzureVMSize            = vmSize,
                    SelectedRecoveryAzureNetworkId = vmRecoveryNetworkId,
                    SelectedTfoAzureNetworkId      = tfoNetworkId,
                    SelectedSourceNicId            = primaryNic,
                    VmNics      = vMNicInputDetailsList,
                    LicenseType =
                        licenseType ==
                        Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType.ToString()
                                ? Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType
                                : Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .WindowsServer,
                    RecoveryAvailabilitySetId = availabilitySetId,
                    ProviderSpecificDetails   = providerSpecificInput
                };

                if (provider is HyperVReplicaAzureReplicationDetails || provider is InMageAzureV2ReplicationDetails)
                {
                    updateReplicationProtectedItemInputProperties.SelectedSourceNicId = primaryNic;
                }
                var input = new UpdateReplicationProtectedItemInput
                {
                    Properties = updateReplicationProtectedItemInputProperties
                };

                var response = this.RecoveryServicesClient.UpdateVmProperties(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name,
                    input);

                var jobResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryJobDetails(
                    PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                this.WriteObject(new ASRJob(jobResponse));
            }
        }
Пример #5
0
        public void UpdateProtectionOfInMageAzureV2ProtectedItem()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetSiteRecoveryClient(CustomHttpHandler);

                string vmId            = "7192c867-b38e-11e5-af2b-0050569e66ab";
                var    responseServers = client.Fabrics.List(RequestHeaders);

                Assert.True(
                    responseServers.Fabrics.Count > 0,
                    "Servers count can't be less than 1");

                var vmWareFabric = responseServers.Fabrics.First(
                    fabric => fabric.Properties.CustomDetails.InstanceType == "VMware");
                Assert.NotNull(vmWareFabric);

                var containersResponse = client.ProtectionContainer.List(
                    vmWareFabric.Name,
                    RequestHeaders);
                Assert.NotNull(containersResponse);
                Assert.True(
                    containersResponse.ProtectionContainers.Count > 0,
                    "Containers count can't be less than 1.");

                var protectedItemResponse = client.ReplicationProtectedItem.Get(
                    vmWareFabric.Name,
                    containersResponse.ProtectionContainers[0].Name,
                    vmId + "-Protected",
                    RequestHeaders);

                var replicationProtectedItem = protectedItemResponse.ReplicationProtectedItem;
                Assert.NotNull(replicationProtectedItem);

                var nics = new List <VMNicInputDetails>();
                nics.Add(
                    new VMNicInputDetails
                {
                    NicId = "00:50:56:9E:3E:F2",
                    RecoveryVMSubnetName = "TenantSubnet",
                    SelectionType        = "SelectedByUser",
                });
                UpdateReplicationProtectedItemInputProperties inputProps = new UpdateReplicationProtectedItemInputProperties()
                {
                    RecoveryAzureVMName = replicationProtectedItem.Properties.FriendlyName,
                    VmNics = nics,
                    SelectedRecoveryAzureNetworkId = "/subscriptions/c183865e-6077-46f2-a3b1-deb0f4f4650a/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/ExpressRouteVNet-WUS-1"
                };

                UpdateReplicationProtectedItemInput input = new UpdateReplicationProtectedItemInput()
                {
                    Properties = inputProps
                };

                var updateResponse =
                    client.ReplicationProtectedItem.UpdateProtection(
                        vmWareFabric.Name,
                        containersResponse.ProtectionContainers[0].Name,
                        replicationProtectedItem.Name,
                        input,
                        RequestHeaders);

                Assert.NotNull(updateResponse);
                Assert.Equal(OperationStatus.Succeeded, updateResponse.Status);
            }
        }
Пример #6
0
        /// <summary>
        ///     ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            if (this.ShouldProcess(
                    this.InputObject.FriendlyName,
                    VerbsCommon.Set))
            {
                var replicationProtectedItemResponse = this.RecoveryServicesClient
                                                       .GetAzureSiteRecoveryReplicationProtectedItem(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name);

                var provider = replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                // Check for Replication Provider type HyperVReplicaAzure/InMageAzureV2/A2A
                if (!(provider is HyperVReplicaAzureReplicationDetails) &&
                    !(provider is InMageAzureV2ReplicationDetails) &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(
                        Resources.UnsupportedReplicationProvidedForUpdateVmProperties);
                    return;
                }

                // Check for at least one option
                if (string.IsNullOrEmpty(this.Name) &&
                    string.IsNullOrEmpty(this.Size) &&
                    string.IsNullOrEmpty(this.UpdateNic) &&
                    string.IsNullOrEmpty(this.RecoveryNetworkId) &&
                    string.IsNullOrEmpty(this.PrimaryNic) &&
                    this.UseManagedDisk == null &&
                    this.IsParameterBound(c => c.RecoveryAvailabilitySet) &&
                    string.IsNullOrEmpty(this.RecoveryCloudServiceId) &&
                    string.IsNullOrEmpty(this.RecoveryResourceGroupId) &&
                    string.IsNullOrEmpty(this.LicenseType) &&
                    string.IsNullOrEmpty(this.RecoveryBootDiagStorageAccountId) &&
                    this.AzureToAzureUpdateReplicationConfiguration == null)
                {
                    this.WriteWarning(Resources.ArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // Both primary & recovery inputs should be present
                if (string.IsNullOrEmpty(this.UpdateNic) ^
                    string.IsNullOrEmpty(this.RecoveryNetworkId))
                {
                    this.WriteWarning(Resources.NetworkArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // NSG, LB and PIP only for A2A provider.
                if ((!string.IsNullOrEmpty(RecoveryNetworkSecurityGroupId) ||
                     !string.IsNullOrEmpty(RecoveryPublicIPAddressId) ||
                     RecoveryLBBackendAddressPoolId != null &&
                     RecoveryLBBackendAddressPoolId.Length > 0) &&
                    !(provider is A2AReplicationDetails))
                {
                    this.WriteWarning(Resources.NetworkingResourcesInDRNotSupportedForClassicVms);
                    return;
                }

                var vmName = this.Name;
                var vmSize = this.Size;
                var vmRecoveryNetworkId     = this.RecoveryNetworkId;
                var licenseType             = this.LicenseType;
                var recoveryResourceGroupId = this.RecoveryResourceGroupId;
                var recoveryCloudServiceId  = this.RecoveryCloudServiceId;
                var useManagedDisk          = this.UseManagedDisk;
                var availabilitySetId       = this.RecoveryAvailabilitySet;
                var primaryNic            = this.PrimaryNic;
                var vMNicInputDetailsList = new List <VMNicInputDetails>();
                var providerSpecificInput = new UpdateReplicationProtectedItemProviderInput();

                if (provider is HyperVReplicaAzureReplicationDetails)
                {
                    var providerSpecificDetails =
                        (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse
                        .Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVmName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet
                        : providerSpecificDetails.RecoveryAvailabilitySetId;

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.PrimaryNic)))
                    {
                        primaryNic = providerSpecificDetails.SelectedSourceNicId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks = useManagedDisk
                        };
                    }

                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is InMageAzureV2ReplicationDetails)
                {
                    var providerSpecificDetails =
                        (InMageAzureV2ReplicationDetails)replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails.SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        licenseType = providerSpecificDetails.LicenseType;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet : providerSpecificDetails.RecoveryAvailabilitySetId;

                    if (string.IsNullOrEmpty(this.UseManagedDisk))
                    {
                        useManagedDisk = providerSpecificDetails.UseManagedDisks;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId = providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.PrimaryNic)))
                    {
                        primaryNic = providerSpecificDetails.SelectedSourceNicId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.TargetVmId,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.ClassicCompute.ToLower()))
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId,
                            UseManagedDisks = useManagedDisk
                        };
                    }
                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }
                else if (provider is A2AReplicationDetails)
                {
                    A2AReplicationDetails providerSpecificDetails = (A2AReplicationDetails)replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryResourceGroupId)))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    availabilitySetId = this.IsParameterBound(c => c.RecoveryAvailabilitySet)
                        ? this.RecoveryAvailabilitySet
                        : providerSpecificDetails.RecoveryAvailabilitySet;

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryNetworkId)))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails.SelectedRecoveryAzureNetworkId;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryCloudServiceId)))
                    {
                        recoveryCloudServiceId =
                            providerSpecificDetails.RecoveryCloudService;
                    }

                    if (!this.MyInvocation.BoundParameters.ContainsKey(
                            Utilities.GetMemberName(() => this.RecoveryBootDiagStorageAccountId)))
                    {
                        this.RecoveryBootDiagStorageAccountId = providerSpecificDetails.RecoveryBootDiagStorageAccountId;
                    }

                    List <A2AVmManagedDiskUpdateDetails> managedDiskUpdateDetails = null;

                    // ManagedDisk case
                    if (this.AzureToAzureUpdateReplicationConfiguration == null && providerSpecificDetails.ProtectedManagedDisks != null)
                    {
                        managedDiskUpdateDetails = new List <A2AVmManagedDiskUpdateDetails>();
                        foreach (var managedDisk in providerSpecificDetails.ProtectedManagedDisks)
                        {
                            managedDiskUpdateDetails.Add(
                                new A2AVmManagedDiskUpdateDetails(
                                    managedDisk.DiskId,
                                    managedDisk.RecoveryTargetDiskAccountType,
                                    managedDisk.RecoveryReplicaDiskAccountType));
                        }
                    }
                    else if (this.AzureToAzureUpdateReplicationConfiguration != null && this.AzureToAzureUpdateReplicationConfiguration[0].IsManagedDisk)
                    {
                        managedDiskUpdateDetails = new List <A2AVmManagedDiskUpdateDetails>();
                        foreach (var managedDisk in this.AzureToAzureUpdateReplicationConfiguration)
                        {
                            managedDiskUpdateDetails.Add(
                                new A2AVmManagedDiskUpdateDetails(
                                    managedDisk.DiskId,
                                    managedDisk.RecoveryTargetDiskAccountType,
                                    managedDisk.RecoveryReplicaDiskAccountType));
                        }
                    }

                    providerSpecificInput = new A2AUpdateReplicationProtectedItemInput()
                    {
                        RecoveryCloudServiceId           = this.RecoveryCloudServiceId,
                        RecoveryResourceGroupId          = this.RecoveryResourceGroupId,
                        RecoveryBootDiagStorageAccountId = this.RecoveryBootDiagStorageAccountId,
                        ManagedDiskUpdateDetails         = managedDiskUpdateDetails,
                        DiskEncryptionInfo = this.A2AEncryptionDetails(provider)
                    };

                    vMNicInputDetailsList = getNicListToUpdate(providerSpecificDetails.VmNics);
                }

                var updateReplicationProtectedItemInputProperties =
                    new UpdateReplicationProtectedItemInputProperties
                {
                    RecoveryAzureVMName            = vmName,
                    RecoveryAzureVMSize            = vmSize,
                    SelectedRecoveryAzureNetworkId = vmRecoveryNetworkId,
                    SelectedSourceNicId            = primaryNic,
                    VmNics      = vMNicInputDetailsList,
                    LicenseType =
                        licenseType ==
                        Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType.ToString()
                                ? Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType
                                : Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .WindowsServer,
                    RecoveryAvailabilitySetId = availabilitySetId,
                    ProviderSpecificDetails   = providerSpecificInput
                };

                if (provider is HyperVReplicaAzureReplicationDetails || provider is InMageAzureV2ReplicationDetails)
                {
                    updateReplicationProtectedItemInputProperties.SelectedSourceNicId = primaryNic;
                }
                var input = new UpdateReplicationProtectedItemInput
                {
                    Properties = updateReplicationProtectedItemInputProperties
                };

                var response = this.RecoveryServicesClient.UpdateVmProperties(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name,
                    input);

                var jobResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryJobDetails(
                    PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                this.WriteObject(new ASRJob(jobResponse));
            }
        }
Пример #7
0
        /// <summary>
        ///     ProcessRecord of the command.
        /// </summary>
        public override void ExecuteSiteRecoveryCmdlet()
        {
            base.ExecuteSiteRecoveryCmdlet();

            if (this.ShouldProcess(
                    this.InputObject.FriendlyName,
                    VerbsCommon.Set))
            {
                var replicationProtectedItemResponse = this.RecoveryServicesClient
                                                       .GetAzureSiteRecoveryReplicationProtectedItem(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name);

                var provider = replicationProtectedItemResponse.Properties.ProviderSpecificDetails;

                // Check for Replication Provider type HyperVReplicaAzure/InMageAzureV2
                if (!(provider is HyperVReplicaAzureReplicationDetails) &&
                    !(provider is InMageAzureV2ReplicationDetails))
                {
                    this.WriteWarning(
                        Resources.UnsupportedReplicationProvidedForUpdateVmProperties);
                    return;
                }

                // Check for at least one option
                if (string.IsNullOrEmpty(this.Name) &&
                    string.IsNullOrEmpty(this.Size) &&
                    string.IsNullOrEmpty(this.PrimaryNic) &&
                    string.IsNullOrEmpty(this.RecoveryNetworkId) &&
                    string.IsNullOrEmpty(this.RecoveryResourceGroupId) &&
                    string.IsNullOrEmpty(this.LicenseType))
                {
                    this.WriteWarning(Resources.ArgumentsMissingForUpdateVmProperties);
                    return;
                }

                // Both primary & recovery inputs should be present
                if (string.IsNullOrEmpty(this.PrimaryNic) ^
                    string.IsNullOrEmpty(this.RecoveryNetworkId))
                {
                    this.WriteWarning(Resources.NetworkArgumentsMissingForUpdateVmProperties);
                    return;
                }

                var          vmName = this.Name;
                var          vmSize = this.Size;
                var          vmRecoveryNetworkId     = this.RecoveryNetworkId;
                var          licenseType             = this.LicenseType;
                var          recoveryResourceGroupId = this.RecoveryResourceGroupId;
                var          vMNicInputDetailsList   = new List <VMNicInputDetails>();
                VMNicDetails vMNicDetailsToBeUpdated;
                var          providerSpecificInput = new UpdateReplicationProtectedItemProviderInput();

                if (provider is HyperVReplicaAzureReplicationDetails)
                {
                    var providerSpecificDetails =
                        (HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse
                        .Properties.ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new HyperVReplicaAzureUpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId
                        };
                    }

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        //licenseType = providerSpecificDetails.LicenseType;
                    }

                    if (!string.IsNullOrEmpty(this.PrimaryNic))
                    {
                        if (providerSpecificDetails.VmNics != null)
                        {
                            vMNicDetailsToBeUpdated =
                                providerSpecificDetails.VmNics.SingleOrDefault(
                                    n => string.Compare(
                                        n.NicId,
                                        this.PrimaryNic,
                                        StringComparison.OrdinalIgnoreCase) ==
                                    0);
                            if (vMNicDetailsToBeUpdated != null)
                            {
                                var vMNicInputDetails = new VMNicInputDetails();

                                vMNicInputDetails.NicId = this.PrimaryNic;
                                vMNicInputDetails.RecoveryVMSubnetName      = this.RecoveryNicSubnetName;
                                vMNicInputDetails.ReplicaNicStaticIPAddress =
                                    this.RecoveryNicStaticIPAddress;
                                vMNicInputDetails.SelectionType =
                                    string.IsNullOrEmpty(this.NicSelectionType)
                                        ? Constants.SelectedByUser : this.NicSelectionType;
                                vMNicInputDetailsList.Add(vMNicInputDetails);

                                var vMNicDetailsListRemaining =
                                    providerSpecificDetails.VmNics.Where(
                                        n => string.Compare(
                                            n.NicId,
                                            this.PrimaryNic,
                                            StringComparison.OrdinalIgnoreCase) !=
                                        0);
                                foreach (var nDetails in vMNicDetailsListRemaining)
                                {
                                    vMNicInputDetails = new VMNicInputDetails();

                                    vMNicInputDetails.NicId = nDetails.NicId;
                                    vMNicInputDetails.RecoveryVMSubnetName =
                                        nDetails.RecoveryVMSubnetName;
                                    vMNicInputDetails.ReplicaNicStaticIPAddress =
                                        nDetails.ReplicaNicStaticIPAddress;
                                    vMNicInputDetails.SelectionType = nDetails.SelectionType;
                                    vMNicInputDetailsList.Add(vMNicInputDetails);
                                }
                            }
                            else
                            {
                                throw new PSInvalidOperationException(
                                          Resources.NicNotFoundInVMForUpdateVmProperties);
                            }
                        }
                    }
                    else
                    {
                        VMNicInputDetails vMNicInputDetails;
                        foreach (var nDetails in providerSpecificDetails.VmNics)
                        {
                            vMNicInputDetails = new VMNicInputDetails();

                            vMNicInputDetails.NicId = nDetails.NicId;
                            vMNicInputDetails.RecoveryVMSubnetName      = nDetails.RecoveryVMSubnetName;
                            vMNicInputDetails.ReplicaNicStaticIPAddress =
                                nDetails.ReplicaNicStaticIPAddress;
                            vMNicInputDetails.SelectionType = nDetails.SelectionType;
                            vMNicInputDetailsList.Add(vMNicInputDetails);
                        }
                    }
                }
                else if (provider is InMageAzureV2ReplicationDetails)
                {
                    var providerSpecificDetails =
                        (InMageAzureV2ReplicationDetails)replicationProtectedItemResponse.Properties
                        .ProviderSpecificDetails;

                    if (string.IsNullOrEmpty(this.RecoveryResourceGroupId))
                    {
                        recoveryResourceGroupId =
                            providerSpecificDetails.RecoveryAzureResourceGroupId;
                    }

                    var deploymentType = Utilities.GetValueFromArmId(
                        providerSpecificDetails.RecoveryAzureStorageAccount,
                        ARMResourceTypeConstants.Providers);
                    if (deploymentType.ToLower()
                        .Contains(Constants.Classic.ToLower()))
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = recoveryResourceGroupId,
                            RecoveryAzureV2ResourceGroupId = null
                        };
                    }
                    else
                    {
                        providerSpecificInput =
                            new InMageAzureV2UpdateReplicationProtectedItemInput
                        {
                            RecoveryAzureV1ResourceGroupId = null,
                            RecoveryAzureV2ResourceGroupId = recoveryResourceGroupId
                        };
                    }

                    if (string.IsNullOrEmpty(this.Name))
                    {
                        vmName = providerSpecificDetails.RecoveryAzureVMName;
                    }

                    if (string.IsNullOrEmpty(this.Size))
                    {
                        vmSize = providerSpecificDetails.RecoveryAzureVMSize;
                    }

                    if (string.IsNullOrEmpty(this.RecoveryNetworkId))
                    {
                        vmRecoveryNetworkId = providerSpecificDetails
                                              .SelectedRecoveryAzureNetworkId;
                    }

                    if (string.IsNullOrEmpty(this.LicenseType))
                    {
                        //licenseType = providerSpecificDetails.LicenseType;
                    }

                    if (!string.IsNullOrEmpty(this.PrimaryNic))
                    {
                        if (providerSpecificDetails.VmNics != null)
                        {
                            vMNicDetailsToBeUpdated =
                                providerSpecificDetails.VmNics.SingleOrDefault(
                                    n => string.Compare(
                                        n.NicId,
                                        this.PrimaryNic,
                                        StringComparison.OrdinalIgnoreCase) ==
                                    0);
                            if (vMNicDetailsToBeUpdated != null)
                            {
                                var vMNicInputDetails = new VMNicInputDetails();

                                vMNicInputDetails.NicId = this.PrimaryNic;
                                vMNicInputDetails.RecoveryVMSubnetName      = this.RecoveryNicSubnetName;
                                vMNicInputDetails.ReplicaNicStaticIPAddress =
                                    this.RecoveryNicStaticIPAddress;
                                vMNicInputDetails.SelectionType =
                                    string.IsNullOrEmpty(this.NicSelectionType)
                                        ? Constants.SelectedByUser : this.NicSelectionType;
                                vMNicInputDetailsList.Add(vMNicInputDetails);

                                var vMNicDetailsListRemaining =
                                    providerSpecificDetails.VmNics.Where(
                                        n => string.Compare(
                                            n.NicId,
                                            this.PrimaryNic,
                                            StringComparison.OrdinalIgnoreCase) !=
                                        0);
                                foreach (var nDetails in vMNicDetailsListRemaining)
                                {
                                    vMNicInputDetails = new VMNicInputDetails();

                                    vMNicInputDetails.NicId = nDetails.NicId;
                                    vMNicInputDetails.RecoveryVMSubnetName =
                                        nDetails.RecoveryVMSubnetName;
                                    vMNicInputDetails.ReplicaNicStaticIPAddress =
                                        nDetails.ReplicaNicStaticIPAddress;
                                    vMNicInputDetails.SelectionType = nDetails.SelectionType;
                                    vMNicInputDetailsList.Add(vMNicInputDetails);
                                }
                            }
                            else
                            {
                                throw new PSInvalidOperationException(
                                          Resources.NicNotFoundInVMForUpdateVmProperties);
                            }
                        }
                    }
                    else
                    {
                        VMNicInputDetails vMNicInputDetails;
                        foreach (var nDetails in providerSpecificDetails.VmNics)
                        {
                            vMNicInputDetails = new VMNicInputDetails();

                            vMNicInputDetails.NicId = nDetails.NicId;
                            vMNicInputDetails.RecoveryVMSubnetName      = nDetails.RecoveryVMSubnetName;
                            vMNicInputDetails.ReplicaNicStaticIPAddress =
                                nDetails.ReplicaNicStaticIPAddress;
                            vMNicInputDetails.SelectionType = nDetails.SelectionType;
                            vMNicInputDetailsList.Add(vMNicInputDetails);
                        }
                    }
                }

                var updateReplicationProtectedItemInputProperties =
                    new UpdateReplicationProtectedItemInputProperties
                {
                    RecoveryAzureVMName            = vmName,
                    RecoveryAzureVMSize            = vmSize,
                    SelectedRecoveryAzureNetworkId = vmRecoveryNetworkId,
                    VmNics      = vMNicInputDetailsList,
                    LicenseType =
                        licenseType ==
                        Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType.ToString()
                                ? Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .NoLicenseType
                                : Management.RecoveryServices.SiteRecovery.Models.LicenseType
                        .WindowsServer,
                    ProviderSpecificDetails = providerSpecificInput
                };

                var input = new UpdateReplicationProtectedItemInput
                {
                    Properties = updateReplicationProtectedItemInputProperties
                };

                var response = this.RecoveryServicesClient.UpdateVmProperties(
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationFabrics),
                    Utilities.GetValueFromArmId(
                        this.InputObject.ID,
                        ARMResourceTypeConstants.ReplicationProtectionContainers),
                    this.InputObject.Name,
                    input);

                var jobResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryJobDetails(
                    PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

                this.WriteObject(new ASRJob(jobResponse));
            }
        }