Esempio n. 1
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                string vaultName         = resourceIdentifier.ResourceName;
                string resourceGroupName = resourceIdentifier.ResourceGroupName;

                WriteDebug(string.Format("Input params - Name:{0}, WorkloadType:{1}, " +
                                         "BackupManagementType: {2}, " +
                                         "RetentionPolicy:{3}, SchedulePolicy:{4}",
                                         Name, WorkloadType.ToString(),
                                         BackupManagementType.HasValue ? BackupManagementType.ToString() : "NULL",
                                         RetentionPolicy == null ? "NULL" : RetentionPolicy.ToString(),
                                         SchedulePolicy == null ? "NULL" : SchedulePolicy.ToString()));

                // validate policy name
                PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);

                // Validate if policy already exists
                if (PolicyCmdletHelpers.GetProtectionPolicyByName(
                        Name,
                        ServiceClientAdapter,
                        vaultName: vaultName,
                        resourceGroupName: resourceGroupName) != null)
                {
                    throw new ArgumentException(string.Format(Resources.PolicyAlreadyExistException, Name));
                }

                Dictionary <Enum, object> providerParameters = new Dictionary <Enum, object>();
                providerParameters.Add(VaultParams.VaultName, vaultName);
                providerParameters.Add(VaultParams.ResourceGroupName, resourceGroupName);
                providerParameters.Add(PolicyParams.PolicyName, Name);
                providerParameters.Add(PolicyParams.WorkloadType, WorkloadType);
                providerParameters.Add(PolicyParams.RetentionPolicy, RetentionPolicy);
                providerParameters.Add(PolicyParams.SchedulePolicy, SchedulePolicy);

                PsBackupProviderManager providerManager = new PsBackupProviderManager(providerParameters, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider =
                    providerManager.GetProviderInstance(WorkloadType, BackupManagementType);

                psBackupProvider.CreatePolicy();

                WriteDebug("Successfully created policy, now fetching it from service: " + Name);

                // now get the created policy and return
                ServiceClientModel.ProtectionPolicyResource policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                    Name,
                    ServiceClientAdapter,
                    vaultName: vaultName,
                    resourceGroupName: resourceGroupName);

                // now convert service Policy to PSObject
                WriteObject(ConversionHelpers.GetPolicyModel(policy));
            }, ShouldProcess(Name, VerbsCommon.New));
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                WriteDebug(string.Format("Input params - Name:{0}, WorkloadType:{1}, " +
                                         "BackupManagementType: {2}, " +
                                         "RetentionPolicy:{3}, SchedulePolicy:{4}",
                                         Name, WorkloadType.ToString(),
                                         BackupManagementType.HasValue ? BackupManagementType.ToString() : "NULL",
                                         RetentionPolicy == null ? "NULL" : RetentionPolicy.ToString(),
                                         SchedulePolicy == null ? "NULL" : SchedulePolicy.ToString()));

                // validate policy name
                PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);

                // Validate if policy already exists
                if (PolicyCmdletHelpers.GetProtectionPolicyByName(Name, ServiceClientAdapter) != null)
                {
                    throw new ArgumentException(string.Format(Resources.PolicyAlreadyExistException, Name));
                }

                PsBackupProviderManager providerManager =
                    new PsBackupProviderManager(new Dictionary <System.Enum, object>()
                {
                    { PolicyParams.PolicyName, Name },
                    { PolicyParams.WorkloadType, WorkloadType },
                    { PolicyParams.RetentionPolicy, RetentionPolicy },
                    { PolicyParams.SchedulePolicy, SchedulePolicy },
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider =
                    providerManager.GetProviderInstance(WorkloadType, BackupManagementType);
                psBackupProvider.CreatePolicy();

                WriteDebug("Successfully created policy, now fetching it from service: " + Name);

                // now get the created policy and return
                ServiceClientModel.ProtectionPolicyResponse policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                    Name,
                    ServiceClientAdapter);
                // now convert service Policy to PSObject
                WriteObject(ConversionHelpers.GetPolicyModel(policy.Item));
            });
        }
Esempio n. 3
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                string containerName = Container != null ? Container.Name : ResourceId.Split('/')[8];
                ConfirmAction(
                    Force.IsPresent,
                    string.Format(Resources.RegisterContainerWarning, containerName),
                    Resources.RegisterContainerMessage,
                    containerName, () =>
                {
                    base.ExecuteCmdlet();

                    ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                    string vaultName = resourceIdentifier.ResourceName;
                    string vaultResourceGroupName = resourceIdentifier.ResourceGroupName;

                    PsBackupProviderManager providerManager =
                        new PsBackupProviderManager(new Dictionary <Enum, object>()
                    {
                        { VaultParams.VaultName, vaultName },
                        { VaultParams.ResourceGroupName, vaultResourceGroupName },
                        { ContainerParams.Name, containerName },
                        { ContainerParams.ContainerType, ServiceClientHelpers.GetServiceClientWorkloadType(WorkloadType).ToString() },
                        { ContainerParams.BackupManagementType, BackupManagementType.ToString() },
                        { ContainerParams.Container, Container }
                    }, ServiceClientAdapter);

                    IPsBackupProvider psBackupProvider =
                        providerManager.GetProviderInstance(WorkloadType, BackupManagementType);
                    psBackupProvider.RegisterContainer();

                    // List containers
                    string backupManagementType = BackupManagementType.ToString();
                    ODataQuery <BMSContainerQueryObject> queryParams = new ODataQuery <BMSContainerQueryObject>(
                        q => q.FriendlyName == containerName &&
                        q.BackupManagementType == backupManagementType);

                    var listResponse = ServiceClientAdapter.ListContainers(queryParams,
                                                                           vaultName: vaultName, resourceGroupName: vaultResourceGroupName);
                    var containerModels = ConversionHelpers.GetContainerModelList(listResponse);
                    WriteObject(containerModels, enumerateCollection: true);
                });
            }, ShouldProcess(ResourceId, VerbsLifecycle.Register));
        }