Skip to content

Commit

Permalink
Merge pull request #78 from box/bulk-groups
Browse files Browse the repository at this point in the history
Bulk groups
  • Loading branch information
allenmichael authored Apr 9, 2018
2 parents 57274d8 + f825017 commit 0c1e921
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 32 deletions.
19 changes: 19 additions & 0 deletions BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Box.V2.Models;
using CsvHelper.Configuration;

namespace BoxCLI.CommandUtilities.CsvModels
{
public class BoxGroupMembershipMap : CsvClassMap<BoxGroupMembership>
{
public BoxGroupMembershipMap()
{
Map(m => m.Id);
References<BoxUserOnObjectMap>(m => m.User);
References<BoxGroupMap>(m => m.Group);
Map(m => m.CreatedAt);
Map(m => m.ModifiedAt);
Map(m => m.Role);
}
}
}
45 changes: 45 additions & 0 deletions BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipRequestMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Box.V2.Models;
using Box.V2.Models.Request;
using CsvHelper.Configuration;

namespace BoxCLI.CommandUtilities.CsvModels
{
public class BoxGroupMembershipRequestMap : CsvClassMap<BoxGroupMembershipRequest>
{
public BoxGroupMembershipRequestMap()
{
Map(m => m.Role);
Map(m => m.User).ConvertUsing<BoxRequestEntity>(row =>
{
var field = row.GetField("BoxUserId");
if (string.IsNullOrEmpty(field))
{
return null;
}
else
{
return new BoxRequestEntity()
{
Id = field
};
}
});
Map(m => m.Group).ConvertUsing<BoxGroupRequest>(row =>
{
var field = row.GetField("BoxGroupId");
if (string.IsNullOrEmpty(field))
{
return null;
}
else
{
return new BoxGroupRequest()
{
Id = field
};
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Box.V2.Models;
using Box.V2.Models.Request;
using CsvHelper.Configuration;

namespace BoxCLI.CommandUtilities.CsvModels
{
public class BoxGroupMembershipUpdateRequestMap : CsvClassMap<BoxGroupMembership>
{
public BoxGroupMembershipUpdateRequestMap()
{
Map(m => m.Id);
Map(m => m.Role);
}
}
}
17 changes: 17 additions & 0 deletions BoxCLI/CommandUtilities/CsvModels/BoxGroupUpdateRequestMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Box.V2.Models.Request;
using CsvHelper.Configuration;

namespace BoxCLI.CommandUtilities.CsvModels
{
public class BoxGroupUpdateRequestMap : CsvClassMap<BoxGroupRequest>
{
public BoxGroupUpdateRequestMap()
{
Map(m => m.Id);
Map(m => m.Name).Default("");
Map(m => m.InvitabilityLevel).Default("");
Map(m => m.MemberViewabilityLevel).Default("");
}
}
}
17 changes: 16 additions & 1 deletion BoxCLI/Commands/GroupSubCommands/GroupCreateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Box.V2.Models;
using Box.V2.Models.Request;
using BoxCLI.BoxHome;
using BoxCLI.BoxPlatform.Service;
using BoxCLI.CommandUtilities;
using BoxCLI.CommandUtilities.CommandOptions;
using BoxCLI.CommandUtilities.CsvModels;
using BoxCLI.CommandUtilities.Globalization;
using Microsoft.Extensions.CommandLineUtils;

Expand All @@ -19,6 +21,7 @@ public class GroupCreateCommand : GroupSubCommandBase
private CommandOption _save;
private CommandOption _bulkPath;
private CommandOption _idOnly;
private CommandOption _fileFormat;
private CommandLineApplication _app;
private IBoxHome _home;

Expand All @@ -34,6 +37,7 @@ public override void Configure(CommandLineApplication command)
_name = command.Argument("name", "Group name");
_bulkPath = BulkFilePathOption.ConfigureOption(command);
_save = SaveOption.ConfigureOption(command);
_fileFormat = FileFormatOption.ConfigureOption(command);
_inviteLevel = command.Option("-i|--invite",
"Specifies who can invite the group to collaborate. Enter admins_only, admins_and_members, or all_managed_users",
CommandOptionType.SingleValue);
Expand Down Expand Up @@ -64,7 +68,8 @@ private async Task RunCreate()
{
json = true;
}
await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(), json: json);
await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(),
json: json, overrideSaveFileFormat: this._fileFormat.Value());
return;
}
base.CheckForValue(this._name.Value, this._app, "A group name is required for this command");
Expand All @@ -80,6 +85,16 @@ private async Task RunCreate()
groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value());
}
var createdGroup = await boxClient.GroupsManager.CreateAsync(groupRequest);
if (_save.HasValue())
{
var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}";
Reporter.WriteInformation("Saving file...");
var listWrapper = new List<BoxGroup>();
listWrapper.Add(createdGroup);
var saved = base.WriteListResultsToReport<BoxGroup, BoxGroupMap>(listWrapper, fileName, fileFormat: this._fileFormat.Value());
Reporter.WriteInformation($"File saved: {saved}");
return;
}
if (this._idOnly.HasValue())
{
Reporter.WriteInformation(createdGroup.Id);
Expand Down
75 changes: 51 additions & 24 deletions BoxCLI/Commands/GroupSubCommands/GroupDeleteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Box.V2;
using BoxCLI.BoxHome;
using BoxCLI.BoxPlatform.Service;
using BoxCLI.CommandUtilities;
Expand All @@ -12,9 +13,12 @@ namespace BoxCLI.Commands.GroupSubCommands
public class GroupDeleteCommand : GroupSubCommandBase
{
private CommandArgument _groupId;

private CommandOption _bulkPath;

private CommandOption _dontPrompt;
private CommandLineApplication _app;
public GroupDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome home, LocalizedStringsResource names)
public GroupDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome home, LocalizedStringsResource names)
: base(boxPlatformBuilder, home, names)
{
}
Expand All @@ -24,6 +28,7 @@ public override void Configure(CommandLineApplication command)
command.Description = "Delete a group.";
_groupId = command.Argument("groupId",
"Id of group");
_bulkPath = BulkFilePathOption.ConfigureOption(command);
_dontPrompt = SuppressDeletePromptOption.ConfigureOption(command);
command.OnExecute(async () =>
{
Expand All @@ -40,29 +45,52 @@ protected async override Task<int> Execute()

private async Task RunDelete()
{
var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
if (this._bulkPath.HasValue())
{
var path = GeneralUtilities.TranslatePath(this._bulkPath.Value());
var ids = base.ReadFileForIds(path);
foreach (var id in ids)
{
try
{
if (await this.DeleteGroup(boxClient, id))
{
Reporter.WriteSuccess($"Deleted group {id}");
}
}
catch (Exception e)
{
Reporter.WriteError($"Error deleting group {id}.");
Reporter.WriteError(e.Message);
}
}
Reporter.WriteInformation("Finished deleting groups...");
return;
}
base.CheckForValue(this._groupId.Value, this._app, "A group ID is required for this command");

bool result;
if (this._dontPrompt.HasValue())
{
result = await this.DeleteGroup();
}
else
{
Reporter.WriteWarningNoNewLine("Are you sure you want to delete this group? y/N ");
var yNKey = "n";
yNKey = Console.ReadLine().ToLower();
if (yNKey != "y")
{
Reporter.WriteInformation("Aborted group deletion.");
return;
}
else
{
result = await this.DeleteGroup();
}
}
if(result)
if (this._dontPrompt.HasValue())
{
result = await this.DeleteGroup(boxClient, this._groupId.Value);
}
else
{
Reporter.WriteWarningNoNewLine("Are you sure you want to delete this group? y/N ");
var yNKey = "n";
yNKey = Console.ReadLine().ToLower();
if (yNKey != "y")
{
Reporter.WriteInformation("Aborted group deletion.");
return;
}
else
{
result = await this.DeleteGroup(boxClient, this._groupId.Value);
}
}
if (result)
{
Reporter.WriteSuccess($"Deleted group {this._groupId.Value}");
}
Expand All @@ -72,10 +100,9 @@ private async Task RunDelete()
}
}

private async Task<bool> DeleteGroup()
private async Task<bool> DeleteGroup(BoxClient boxClient, string groupId)
{
var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
return await boxClient.GroupsManager.DeleteAsync(this._groupId.Value);
return await boxClient.GroupsManager.DeleteAsync(groupId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Box.V2.Models.Request;
using BoxCLI.BoxHome;
using BoxCLI.BoxPlatform.Service;
using BoxCLI.CommandUtilities.CommandOptions;
using BoxCLI.CommandUtilities.Globalization;
using Microsoft.Extensions.CommandLineUtils;

Expand All @@ -12,6 +13,9 @@ public class GroupMembershipCreateCommand : GroupMembershipSubCommandBase
{
private CommandArgument _userId;
private CommandArgument _groupId;
private CommandOption _save;
private CommandOption _bulkPath;
private CommandOption _fileFormat;
private CommandOption _admin;
private CommandOption _member;
private CommandLineApplication _app;
Expand All @@ -27,6 +31,9 @@ public override void Configure(CommandLineApplication command)
{
_app = command;
command.Description = "Add a user to a group.";
_bulkPath = BulkFilePathOption.ConfigureOption(command);
_save = SaveOption.ConfigureOption(command);
_fileFormat = FileFormatOption.ConfigureOption(command);
_userId = command.Argument("userId",
"Id of user");
_groupId = command.Argument("groupId",
Expand All @@ -51,6 +58,17 @@ protected async override Task<int> Execute()

private async Task RunCreate()
{
if (this._bulkPath.HasValue())
{
var json = false;
if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
{
json = true;
}
await base.CreateGroupMembershipsFromFile(this._bulkPath.Value(), this._save.HasValue(),
json: json, overrideSaveFileFormat: this._fileFormat.Value());
return;
}
base.CheckForValue(this._userId.Value, this._app, "A user ID is required for this command.");
base.CheckForValue(this._groupId.Value, this._app, "A group ID is required for this command.");
var role = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Threading.Tasks;
using BoxCLI.BoxHome;
using BoxCLI.BoxPlatform.Service;
using BoxCLI.CommandUtilities;
using BoxCLI.CommandUtilities.CommandOptions;
using BoxCLI.CommandUtilities.Globalization;
using Microsoft.Extensions.CommandLineUtils;

Expand All @@ -10,6 +12,7 @@ namespace BoxCLI.Commands.GroupSubCommands.GroupMembershipSubCommands
public class GroupMembershipDeleteCommand : GroupMembershipSubCommandBase
{
private CommandArgument _membershipId;
private CommandOption _bulkPath;
private CommandLineApplication _app;
public GroupMembershipDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome boxHome, LocalizedStringsResource names)
: base(boxPlatformBuilder, boxHome, names)
Expand All @@ -22,7 +25,7 @@ public override void Configure(CommandLineApplication command)
command.Description = "Remove a user from a group.";
_membershipId = command.Argument("membershipId",
"Id of group membership");

_bulkPath = BulkFilePathOption.ConfigureOption(command);
command.OnExecute(async () =>
{
return await this.Execute();
Expand All @@ -38,8 +41,30 @@ protected async override Task<int> Execute()

private async Task RunDelete()
{
base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command.");
var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
if (this._bulkPath.HasValue())
{
var path = GeneralUtilities.TranslatePath(this._bulkPath.Value());
var ids = base.ReadFileForIds(path);
foreach (var id in ids)
{
try
{
if (await boxClient.GroupsManager.DeleteGroupMembershipAsync(id))
{
Reporter.WriteSuccess($"Deleted group membership {id}");
}
}
catch (Exception e)
{
Reporter.WriteError($"Error deleting group membership {id}.");
Reporter.WriteError(e.Message);
}
}
Reporter.WriteInformation("Finished deleting group memberships...");
return;
}
base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command.");
var result = await boxClient.GroupsManager.DeleteGroupMembershipAsync(this._membershipId.Value);
if(result)
{
Expand Down
Loading

0 comments on commit 0c1e921

Please sign in to comment.