/// <summary> /// Queries all, by default. /// </summary> private void GetByDefault() { ProtectionContainerListResponse protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(); this.WriteProtectionContainers(protectionContainerListResponse.ProtectionContainers); }
/// <summary> /// Queries by name. /// </summary> private void GetByName() { ProtectionContainerListResponse protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(); bool found = false; foreach ( ProtectionContainer protectionContainer in protectionContainerListResponse.ProtectionContainers) { if (0 == string.Compare(this.Name, protectionContainer.Name, true)) { this.WriteProtectionContainer(protectionContainer); found = true; } } if (!found) { throw new InvalidOperationException( string.Format( Properties.Resources.ProtectionContainerNotFound, this.Name, PSRecoveryServicesClient.asrVaultCreds.ResourceName)); } }
/// <summary> /// Queries by Name. /// </summary> private void GetByName() { try { var protectionContainerResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Fabric.Name, this.Name); if (protectionContainerResponse.ProtectionContainer != null) { this.WriteProtectionContainer(protectionContainerResponse.ProtectionContainer); } } catch (CloudException ex) { if (string.Compare(ex.Error.Code, "NotFound", StringComparison.OrdinalIgnoreCase) == 0) { throw new InvalidOperationException( string.Format( Properties.Resources.ProtectionContainerNotFound, this.Name, PSRecoveryServicesClient.asrVaultCreds.ResourceName)); } else { throw; } } }
/// <summary> /// Queries by ID. /// </summary> private void GetById() { ProtectionContainerResponse protectionContainerResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Id); this.WriteProtectionContainer(protectionContainerResponse.ProtectionContainer); }
/// <summary> /// Queries by friendly name. /// </summary> private void GetByFriendlyName() { ProtectionContainerListResponse protectionContainerListResponse; bool found = false; protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Fabric.Name); foreach ( ProtectionContainer protectionContainer in protectionContainerListResponse.ProtectionContainers) { if (0 == string.Compare(this.FriendlyName, protectionContainer.Properties.FriendlyName, StringComparison.OrdinalIgnoreCase)) { var protectionContainerByName = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Fabric.Name, protectionContainer.Name).ProtectionContainer; WriteProtectionContainer(protectionContainerByName); found = true; } } if (!found) { throw new InvalidOperationException( string.Format( Properties.Resources.ProtectionContainerNotFound, FriendlyName, PSRecoveryServicesClient.asrVaultCreds.ResourceName)); } }
/// <summary> /// Queries by friendly name. /// </summary> private void GetByFriendlyName() { ProtectionContainerListResponse protectionContainerListResponse; bool found = false; FabricListResponse fabricListResponse = RecoveryServicesClient.GetAzureSiteRecoveryFabric(); foreach (Fabric fabric in fabricListResponse.Fabrics) { // Do not process for fabrictype other than Vmm|HyperVSite if (String.Compare(fabric.Properties.CustomDetails.InstanceType, Constants.VMM) != 0 && String.Compare(fabric.Properties.CustomDetails.InstanceType, Constants.HyperVSite) != 0) { continue; } protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(fabric.Name); foreach ( ProtectionContainer protectionContainer in protectionContainerListResponse.ProtectionContainers) { if (0 == string.Compare(this.FriendlyName, protectionContainer.Properties.FriendlyName, StringComparison.OrdinalIgnoreCase)) { var protectionContainerByName = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(fabric.Name, protectionContainer.Name).ProtectionContainer; this.WriteProtectionContainer(protectionContainerByName); found = true; // break; //We can break if we are sure that we have clouds with unique name across fabrics } } } if (!found) { throw new InvalidOperationException( string.Format( Properties.Resources.ProtectionContainerNotFound, this.FriendlyName, PSRecoveryServicesClient.asrVaultCreds.ResourceName)); } }
/// <summary> /// Queries all Protection Containers under given Fabric. /// </summary> private void GetAll() { ProtectionContainerListResponse protectionContainerListResponse; FabricListResponse fabricListResponse = RecoveryServicesClient.GetAzureSiteRecoveryFabric(); foreach (Fabric fabric in fabricListResponse.Fabrics) { // Do not process for fabrictype other than Vmm|HyperVSite if (String.Compare(fabric.Properties.CustomDetails.InstanceType, Constants.VMM) != 0 && String.Compare(fabric.Properties.CustomDetails.InstanceType, Constants.HyperVSite) != 0) { continue; } protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(fabric.Name); this.WriteProtectionContainers(protectionContainerListResponse.ProtectionContainers); } }
/// <summary> /// Queries all Protection Containers under given Fabric. /// </summary> private void GetByFabric() { ProtectionContainerListResponse protectionContainerListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Fabric.Name); this.WriteProtectionContainers(protectionContainerListResponse.ProtectionContainers); }
/// <summary> /// ProcessRecord of the command. /// </summary> public override void ExecuteCmdlet() { switch (this.ParameterSetName) { case ASRParameterSets.ByPEObject: this.Id = this.ProtectionEntity.ID; this.ProtectionContainerId = this.ProtectionEntity.ProtectionContainerId; this.targetNameOrId = this.ProtectionEntity.Name; this.alreadyEnabled = this.ProtectionEntity.Protected; break; case ASRParameterSets.ByIDs: this.targetNameOrId = this.Id; ProtectionEntityResponse protectionEntityResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity( this.ProtectionContainerId, this.Id); this.alreadyEnabled = protectionEntityResponse.ProtectionEntity.Protected; this.targetNameOrId = protectionEntityResponse.ProtectionEntity.Name; break; } if (this.alreadyEnabled && this.Protection.Equals(Constants.EnableProtection, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException( string.Format( Properties.Resources.ProtectionEntityAlreadyEnabled, this.targetNameOrId)); } else if (!this.alreadyEnabled && this.Protection.Equals(Constants.DisableProtection, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException( Properties.Resources.ProtectionEntityAlreadyDisabled, this.targetNameOrId); } this.ConfirmAction( this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection), string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId), string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection), this.targetNameOrId, () => { try { if (this.Protection == Constants.EnableProtection) { string profileId = string.Empty; var input = new EnableProtectionInput(); if (this.ProtectionEntity == null) { var pe = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity( this.ProtectionContainerId, this.Id); this.ProtectionEntity = new ASRProtectionEntity(pe.ProtectionEntity); } // Get the replciation provider from profile object otherwise assume its E2E. // Let the call go without profileId set. string replicationProvider = null; if (this.ProtectionProfile != null) { profileId = this.ProtectionProfile.ID; replicationProvider = this.ProtectionProfile.ReplicationProvider; } else { string pcId = this.ProtectionContainerId ?? this.ProtectionEntity.ProtectionContainerId; var pc = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer( pcId); // PC will have all profiles associated with same replciation providers only. replicationProvider = pc.ProtectionContainer.AvailableProtectionProfiles.Count < 1 ? null : pc.ProtectionContainer.AvailableProtectionProfiles[0].ReplicationProvider; if (replicationProvider != Constants.HyperVReplica) { throw new Exception("Please provide the protection profile object. It can be chosen from available protection profiles of the protection container."); } } if (this.ParameterSetName == ASRParameterSets.ByIDs) { this.ValidateUsageById(replicationProvider, "Id"); } if (replicationProvider == Constants.HyperVReplicaAzure) { input.ProtectionProfileId = this.ProtectionProfile.ID; AzureEnableProtectionInput azureInput = new AzureEnableProtectionInput(); azureInput.HvHostVmId = this.ProtectionEntity.FabricObjectId; azureInput.VmName = this.ProtectionEntity.Name; azureInput.OSType = this.OS; if (string.IsNullOrWhiteSpace(this.OS)) { azureInput.OSType = this.ProtectionEntity.OS; } if (string.IsNullOrWhiteSpace(this.OSDiskName)) { azureInput.VHDId = this.ProtectionEntity.OSDiskId; } else { foreach (var disk in this.ProtectionEntity.Disks) { if (disk.Name == this.OSDiskName) { azureInput.VHDId = disk.Id; break; } } } input.ReplicationProviderInput = DataContractUtils.Serialize <AzureEnableProtectionInput>(azureInput); } else if (string.IsNullOrWhiteSpace(profileId)) { input = null; } else { input.ReplicationProviderInput = string.Empty; input.ProtectionProfileId = profileId; } this.jobResponse = RecoveryServicesClient.EnableProtection( this.ProtectionContainerId, this.Id, input); } else { this.jobResponse = RecoveryServicesClient.DisbleProtection( this.ProtectionContainerId, this.Id); } this.WriteJob(this.jobResponse.Job); if (this.WaitForCompletion.IsPresent) { this.WaitForJobCompletion(this.jobResponse.Job.ID); } } catch (Exception exception) { this.HandleException(exception); } }); }