private async Task AssignStoragePoliciesFromFile(string path,
                                                         bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false)
        {
            var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());

            if (!string.IsNullOrEmpty(path))
            {
                path = GeneralUtilities.TranslatePath(path);
            }
            try
            {
                var assignmentRequests = base.ReadFile <BoxStoragePolicyAssignmentRequest, BoxStoragePolicyAssignmentRequestMap>(path);
                var saveCreated        = new List <BoxStoragePolicyAssignment>();

                foreach (var assignmentRequest in assignmentRequests)
                {
                    BoxStoragePolicyAssignment newAssignment = null;
                    try
                    {
                        newAssignment = await boxClient.StoragePoliciesManager.AssignAsync(assignmentRequest.UserId, assignmentRequest.StoragePolicyId);
                    }
                    catch (Exception e)
                    {
                        Reporter.WriteError($"Couldn't assign storage policy {assignmentRequest.StoragePolicyId} to user {assignmentRequest.UserId}...");
                        Reporter.WriteError(e.Message);
                    }
                    if (newAssignment != null)
                    {
                        base.PrintStoragePolicyAssignment(newAssignment, json);
                        if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting())
                        {
                            saveCreated.Add(newAssignment);
                        }
                    }
                }
                if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting())
                {
                    var fileFormat = base._settings.GetBoxReportsFileFormatSetting();
                    if (!string.IsNullOrEmpty(overrideSaveFileFormat))
                    {
                        fileFormat = overrideSaveFileFormat;
                    }
                    var savePath = base._settings.GetBoxReportsFolderPath();
                    if (!string.IsNullOrEmpty(overrideSavePath))
                    {
                        savePath = overrideSavePath;
                    }
                    var fileName = $"{base._names.CommandNames.StoragePolicy}-{base._names.SubCommandNames.Assign}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}";
                    base.WriteListResultsToReport <BoxStoragePolicyAssignment, BoxStoragePolicyAssignmentMap>(saveCreated, fileName, savePath, fileFormat);
                }
            }
            catch (Exception e)
            {
                Reporter.WriteError(e.Message);
            }
        }
 protected virtual void PrintStoragePolicyAssignment(BoxStoragePolicyAssignment a, bool json = false)
 {
     if (json)
     {
         base.OutputJson(a);
         return;
     }
     else
     {
         Reporter.WriteInformation($"Storage Policy Assignment ID: {a.Id}");
         Reporter.WriteInformation($"Storage Policy ID: {a.BoxStoragePolicy.Id}");
         Reporter.WriteInformation($"Assigned To: {a.AssignedTo.Type} {a.AssignedTo.Id}");
     }
 }
Esempio n. 3
0
        public async Task GetAssignment_ValidResponse()
        {
            /*** Arrange ***/
            var responseString = @"{
                                        ""entries"": [
                                            {
                                                ""type"": ""storage_policy_assignment"",
                                                ""id"": ""user_5678"",
                                                ""storage_policy"": {
                                                    ""type"": ""storage_policy"",
                                                    ""id"": ""1234"",
                                                },
                                                ""assigned_to"": {
                                                    ""type"": ""user"",
                                                    ""id"": ""5678"",
                                                }
                                            }
                                        ]
                                    }";

            IBoxRequest boxRequest = null;
            var         storagePolicyAssignmentsForTargetUri = new Uri(Constants.StoragePolicyAssignmentsForTargetEndpointString);

            Config.SetupGet(x => x.StoragePolicyAssignmentsForTargetUri).Returns(storagePolicyAssignmentsForTargetUri);
            Handler.Setup(h => h.ExecuteAsync <BoxCollectionMarkerBased <BoxStoragePolicyAssignment> >(It.IsAny <IBoxRequest>()))
            .Returns(Task.FromResult <IBoxResponse <BoxCollectionMarkerBased <BoxStoragePolicyAssignment> > >(new BoxResponse <BoxCollectionMarkerBased <BoxStoragePolicyAssignment> >()
            {
                Status        = ResponseStatus.Success,
                ContentString = responseString
            }))
            .Callback <IBoxRequest>(r => boxRequest = r);

            /*** Act ***/
            BoxStoragePolicyAssignment result = await _storagePoliciesManager.GetAssignmentForTargetAsync("5678");

            /*** Assert ***/
            //Request check
            Assert.IsNotNull(boxRequest);
            Assert.AreEqual(RequestMethod.Get, boxRequest.Method);
            Assert.AreEqual(storagePolicyAssignmentsForTargetUri + "?resolved_for_type=user&resolved_for_id=5678", boxRequest.AbsoluteUri.AbsoluteUri);

            //Response check
            Assert.AreEqual("storage_policy_assignment", result.Type);
            Assert.AreEqual("user_5678", result.Id);
            Assert.IsNotNull(result.BoxStoragePolicy);
            Assert.IsNotNull(result.AssignedTo);
        }
Esempio n. 4
0
        public async Task CreateAssignment_ValidResponse()
        {
            /*** Arrange ***/
            string responseString = @"{
                                        ""type"": ""storage_policy_assignment"",
                                        ""id"": ""user_5678"",
                                        ""storage_policy"": {
                                            ""type"": ""storage_policy"",
                                            ""id"": ""1234"",
                                        },
                                        ""assigned_to"": {
                                            ""type"": ""user"",
                                            ""id"": ""5678"",
                                        }
                                    }";

            IBoxRequest boxRequest = null;
            Uri         storagePolicyAssignmentsUri = new Uri(Constants.StoragePolicyAssignmentsEndpointString);

            Config.SetupGet(x => x.StoragePolicyAssignmentsUri).Returns(storagePolicyAssignmentsUri);
            Handler.Setup(h => h.ExecuteAsync <BoxStoragePolicyAssignment>(It.IsAny <IBoxRequest>()))
            .Returns(Task.FromResult <IBoxResponse <BoxStoragePolicyAssignment> >(new BoxResponse <BoxStoragePolicyAssignment>()
            {
                Status        = ResponseStatus.Success,
                ContentString = responseString
            }))
            .Callback <IBoxRequest>(r => boxRequest = r);

            /*** Act ***/
            BoxStoragePolicyAssignment result = await _storagePoliciesManager.CreateAssignmentAsync("5678", "1234");

            /*** Assert ***/
            //Request check
            Assert.IsNotNull(boxRequest);
            Assert.AreEqual(RequestMethod.Post, boxRequest.Method);
            Assert.AreEqual(storagePolicyAssignmentsUri, boxRequest.AbsoluteUri.AbsoluteUri);

            //Response check
            Assert.AreEqual("storage_policy_assignment", result.Type);
            Assert.AreEqual("user_5678", result.Id);
            Assert.IsNotNull(result.BoxStoragePolicy);
            Assert.IsNotNull(result.AssignedTo);
        }