Skip to content

Commit 4b472c5

Browse files
author
carycheng
authored
Multi zones (#485)
1 parent 60e4f4e commit 4b472c5

13 files changed

+706
-1
lines changed

Box.V2.Test.Integration/Box.V2.Test.Integration.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="BoxFilesManagerTestIntegration.cs" />
6363
<Compile Include="BoxFoldersManagerTestIntegration.cs" />
6464
<Compile Include="BoxLegalHoldPoliciesManagerTestIntegration.cs" />
65+
<Compile Include="BoxStoragePoliciesManagerTestIntegration.cs" />
6566
<Compile Include="BoxTasksManagerTestIntegration.cs" />
6667
<Compile Include="BoxMetadataManagerTestIntegration.cs" />
6768
<Compile Include="BoxResourceManagerTestIntegration.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using System.Linq;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Box.V2.Models;
5+
using Box.V2.Models.Request;
6+
using static Box.V2.Config.Constants;
7+
8+
namespace Box.V2.Test.Integration
9+
{
10+
[TestClass]
11+
public class BoxStoragePoliciesManagerTestIntegration : BoxResourceManagerTestIntegration
12+
{
13+
[TestMethod]
14+
public async Task AssignStoragePolicyAsync_LiveSession()
15+
{
16+
var assignment = await _client.StoragePoliciesManager.GetAssignmentAsync("user_240097255");
17+
}
18+
}
19+
}

Box.V2.Test/Box.V2.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="BoxResourceManagerTest.cs" />
8080
<Compile Include="BoxServiceTest.cs" />
8181
<Compile Include="BoxSearchManagerTest.cs" />
82+
<Compile Include="BoxStoragePoliciesManagerTest.cs" />
8283
<Compile Include="BoxTasksManagerTest.cs" />
8384
<Compile Include="BoxUsersManagerTest.cs" />
8485
<Compile Include="BoxCollaborationsManagerTest.cs" />

Box.V2.Test/BoxStoragePoliciesManagerTest.cs

+418
Large diffs are not rendered by default.

Box.V2/Box.V2.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="Managers\BoxMetadataManager.cs" />
7676
<Compile Include="Managers\BoxRetentionPoliciesManager.cs" />
7777
<Compile Include="Managers\BoxSharedItemsManager.cs" />
78+
<Compile Include="Managers\BoxStoragePoliciesManager.cs" />
7879
<Compile Include="Managers\BoxTasksManager.cs" />
7980
<Compile Include="Managers\BoxTermsOfServiceManager.cs" />
8081
<Compile Include="Managers\BoxWebhooksManager.cs" />
@@ -116,6 +117,8 @@
116117
<Compile Include="Models\BoxRetentionPolicyAssignment.cs" />
117118
<Compile Include="Models\BoxRetentionPolicy.cs" />
118119
<Compile Include="Models\BoxSessionUploadStatus.cs" />
120+
<Compile Include="Models\BoxStoragePolicy.cs" />
121+
<Compile Include="Models\BoxStoragePolicyAssignment.cs" />
119122
<Compile Include="Models\BoxTask.cs" />
120123
<Compile Include="Models\BoxTaskAssignment.cs" />
121124
<Compile Include="Models\BoxTermsOfService.cs" />

Box.V2/BoxClient.cs

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private void InitManagers()
134134
LegalHoldPoliciesManager = new BoxLegalHoldPoliciesManager(Config, _service, _converter, Auth, _asUser, _suppressNotifications);
135135
CollaborationWhitelistManager = new BoxCollaborationWhitelistManager(Config, _service, _converter, Auth, _asUser, _suppressNotifications);
136136
TermsOfServiceManager = new BoxTermsOfServiceManager(Config, _service, _converter, Auth, _asUser, _suppressNotifications);
137+
StoragePoliciesManager = new BoxStoragePoliciesManager(Config, _service, _converter, Auth, _asUser, _suppressNotifications);
137138

138139
// Init Resource Plugins Manager
139140
ResourcePlugins = new BoxResourcePlugins();
@@ -264,5 +265,10 @@ public BoxClient AddResourcePlugin<T>() where T : BoxResourceManager
264265
/// The manager that represents the terms of service endpoint
265266
/// </summary>
266267
public BoxTermsOfServiceManager TermsOfServiceManager { get; private set; }
268+
269+
/// <summary>
270+
/// The manager that represents the storage policies endpoint
271+
/// </summary>
272+
public BoxStoragePoliciesManager StoragePoliciesManager { get; private set; }
267273
}
268274
}

Box.V2/Config/BoxConfig.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static IBoxConfig CreateFromJsonString(string jsonString)
149149
public virtual Uri AuthCodeBaseUri { get { return new Uri(BoxAccountApiHostUri, Constants.AuthCodeString); } }
150150
public virtual Uri AuthCodeUri { get { return new Uri(AuthCodeBaseUri, string.Format("?response_type=code&client_id={0}&redirect_uri={1}", ClientId, RedirectUri)); } }
151151
public virtual Uri FoldersEndpointUri { get { return new Uri(BoxApiUri, Constants.FoldersString); } }
152-
public virtual Uri TermsOfServicesUri { get { return new Uri(BoxApiUri, Constants.TermsOfServicesString); } }
152+
public virtual Uri TermsOfServicesUri { get { return new Uri(BoxApiUri, Constants.TermsOfServicesString); } }
153153
public virtual Uri TermsOfServiceUserStatusesUri { get { return new Uri(BoxApiUri, Constants.TermsOfServiceUserStatusesString); } }
154154
public virtual Uri FilesEndpointUri { get { return new Uri(BoxApiUri, Constants.FilesString); } }
155155
public virtual Uri FilesUploadEndpointUri { get { return new Uri(BoxUploadApiUri, Constants.FilesUploadString); } }
@@ -181,6 +181,9 @@ public static IBoxConfig CreateFromJsonString(string jsonString)
181181
public virtual Uri DevicePinUri { get { return new Uri(BoxApiUri, Constants.DevicePinString); } }
182182
public virtual Uri CollaborationWhitelistEntryUri { get { return new Uri(BoxApiUri, Constants.CollaborationWhitelistEntryString); } }
183183
public virtual Uri CollaborationWhitelistTargetEntryUri { get { return new Uri(BoxApiUri, Constants.CollaborationWhitelistTargetEntryString); } }
184+
public virtual Uri StoragePoliciesUri { get { return new Uri(BoxApiUri, Constants.StoragePoliciesString); } }
185+
public virtual Uri StoragePolicyAssignmentsUri { get { return new Uri(BoxApiUri, Constants.StoragePolicyAssignmentsString); } }
186+
public virtual Uri StoragePolicyAssignmentsForTargetUri { get { return new Uri(BoxApiUri, Constants.StoragePolicyAssignmentsForTargetString); } }
184187

185188
/// <summary>
186189
/// Gets the shared items endpoint URI.

Box.V2/Config/Constants.cs

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public static class Constants
4949
public const string CollaborationWhitelistTargetEntryString = @"collaboration_whitelist_exempt_targets/";
5050
public const string TermsOfServicesString = @"terms_of_services/";
5151
public const string TermsOfServiceUserStatusesString = @"terms_of_service_user_statuses/";
52+
public const string StoragePoliciesString = @"storage_policies/";
53+
public const string StoragePolicyAssignmentsString = @"storage_policy_assignments/";
54+
public const string StoragePolicyAssignmentsForTargetString = @"storage_policy_assignments";
5255

5356
/// <summary>
5457
/// The shared items constant
@@ -99,6 +102,8 @@ public static class Constants
99102
public const string WebLinksEndpointString = BoxApiUriString + WebLinksString;
100103
public const string LegalHoldPoliciesEndpointString = BoxApiUriString + LegalHoldPoliciesString;
101104
public const string LegalHoldPolicyAssignmentsEndpointString = BoxApiUriString + LegalHoldPolicyAssignmentsString;
105+
public const string StoragePoliciesEndpointString = BoxApiUriString + StoragePoliciesString;
106+
public const string StoragePolicyAssignmentsEndpointString = BoxApiUriString + StoragePolicyAssignmentsString;
102107

103108
/*** Endpoint Paths ***/
104109
public const string ItemsPathString = @"{0}/items";
@@ -168,6 +173,8 @@ public static class Constants
168173
public const string TypeMetadataTemplate = "metadata_template";
169174
public const string TypeTermsOfService = "terms_of_service";
170175
public const string TypeTermsOfServiceUserStatuses = "terms_of_service_user_status";
176+
public const string TypeStoragePolicy = "storage_policy";
177+
public const string TypeStoragePolicyAssignment = "storage_policy_assignment";
171178
public const string TypeApplication = "application";
172179

173180
/*** File Preview ***/

Box.V2/Config/IBoxConfig.cs

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public interface IBoxConfig
6464
Uri CollaborationWhitelistTargetEntryUri { get; }
6565
Uri TermsOfServicesUri { get; }
6666
Uri TermsOfServiceUserStatusesUri { get; }
67+
Uri StoragePoliciesUri { get; }
68+
Uri StoragePolicyAssignmentsUri { get; }
69+
Uri StoragePolicyAssignmentsForTargetUri { get; }
6770

6871
/// <summary>
6972
/// Gets the shared items endpoint URI.

Box.V2/Converter/BoxItemConverter.cs

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ protected override BoxEntity Create(Type objectType, JObject jObject)
8383
return new BoxTermsOfService();
8484
case Constants.TypeTermsOfServiceUserStatuses:
8585
return new BoxTermsOfServiceUserStatuses();
86+
case Constants.TypeStoragePolicy:
87+
return new BoxStoragePolicy();
88+
case Constants.TypeStoragePolicyAssignment:
89+
return new BoxStoragePolicyAssignment();
8690
case Constants.TypeApplication:
8791
return new BoxApplication();
8892
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
using Box.V2.Auth;
2+
using Box.V2.Config;
3+
using Box.V2.Extensions;
4+
using Box.V2.Converter;
5+
using Box.V2.Models;
6+
using Box.V2.Services;
7+
using System.Threading.Tasks;
8+
using System;
9+
using Newtonsoft.Json.Linq;
10+
11+
namespace Box.V2.Managers
12+
{
13+
public class BoxStoragePoliciesManager : BoxResourceManager
14+
{
15+
/// <summary>
16+
/// Create a new BoxStoragePolicies object.
17+
/// </summary>
18+
public BoxStoragePoliciesManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth, string asUser = null, bool? suppressNotifications = null)
19+
: base(config, service, converter, auth, asUser, suppressNotifications) { }
20+
21+
/// <summary>
22+
/// Get details of a single Box Storage Policy.
23+
/// </summary>
24+
/// <param name="policyId">Id of the Box Storage Policy to retrieve.</param>
25+
/// <returns>If the Id is valid, information about the Box Storage Policy is returned. </returns>
26+
public async Task<BoxStoragePolicy> GetStoragePolicyAsync(String policyId)
27+
{
28+
policyId.ThrowIfNullOrWhiteSpace("policyId");
29+
30+
BoxRequest request = new BoxRequest(_config.StoragePoliciesUri, policyId)
31+
.Method(RequestMethod.Get);
32+
33+
IBoxResponse<BoxStoragePolicy> response = await ToResponseAsync<BoxStoragePolicy>(request).ConfigureAwait(false);
34+
35+
return response.ResponseObject;
36+
}
37+
38+
/// <summary>
39+
/// Get a list of Storage Policies that belong to your Enterprise.
40+
/// </summary>
41+
/// <param name="fields">Attribute(s) to include in the response.</param>
42+
/// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param>
43+
/// <param name="limit">Limit result size to this number. Defults to 100, maximum is 1,000.</param>
44+
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
45+
/// <returns>Returns the list of Storage Policies in your Enterprise that match the filer parameters (if passedin).</returns>
46+
public async Task<BoxCollectionMarkerBased<BoxStoragePolicy>> GetListStoragePoliciesAsync(string fields = null, string marker = null, int limit = 100, bool autoPaginate = false)
47+
{
48+
BoxRequest request = new BoxRequest(_config.StoragePoliciesUri)
49+
.Method(RequestMethod.Get)
50+
.Param("fields", fields)
51+
.Param("limit", limit.ToString())
52+
.Param("marker", marker);
53+
54+
if (autoPaginate)
55+
{
56+
return await AutoPaginateMarker<BoxStoragePolicy>(request, limit);
57+
}
58+
else
59+
{
60+
IBoxResponse<BoxCollectionMarkerBased<BoxStoragePolicy>> response = await ToResponseAsync<BoxCollectionMarkerBased<BoxStoragePolicy>>(request).ConfigureAwait(false);
61+
return response.ResponseObject;
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Get details of a single assignment.
67+
/// </summary>
68+
/// <param name="assignmentId">Id of the assignment.</param>
69+
/// <returns>If the assignmentId is valid, information about the assignment is returned.</returns>
70+
public async Task<BoxStoragePolicyAssignment> GetAssignmentAsync(string assignmentId)
71+
{
72+
assignmentId.ThrowIfNullOrWhiteSpace("assignmentId");
73+
74+
BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId)
75+
.Method(RequestMethod.Get);
76+
77+
IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false);
78+
79+
return response.ResponseObject;
80+
}
81+
82+
/// <summary>
83+
/// Get details of a Storage Policy Assignment for target entity.
84+
/// </summary>
85+
/// <param name="userId">User Id of the assignment.</param>
86+
/// <param name="entityType">Entity type of the storage policy assignment.</param>
87+
/// <returns></returns>
88+
public async Task<BoxStoragePolicyAssignment> GetAssignmentForTargetAsync(string entityId, string entityType = "user")
89+
{
90+
entityId.ThrowIfNullOrWhiteSpace("entityId");
91+
92+
BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsForTargetUri)
93+
.Method(RequestMethod.Get)
94+
.Param("resolved_for_type", entityType)
95+
.Param("resolved_for_id", entityId);
96+
97+
IBoxResponse<BoxCollectionMarkerBased<BoxStoragePolicyAssignment>> response = await ToResponseAsync<BoxCollectionMarkerBased<BoxStoragePolicyAssignment>>(request).ConfigureAwait(false);
98+
return response.ResponseObject.Entries[0];
99+
}
100+
101+
/// <summary>
102+
/// Update the storage policy information for storage policy assignment.
103+
/// </summary>
104+
/// <param name="assignmentId">Storage Policy assignment Id to update.</param>
105+
/// <param name="policyId">"The Id of the Storage Policy to update to."</param>
106+
/// <returns></returns> The updated Storage Policy object with new assignment.
107+
public async Task<BoxStoragePolicyAssignment> UpdateStoragePolicyAssignment(string assignmentId, String policyId)
108+
{
109+
policyId.ThrowIfNullOrWhiteSpace("policyId");
110+
111+
dynamic req = new JObject();
112+
dynamic storagePolicyObject = new JObject();
113+
114+
storagePolicyObject.type = "storage_policy";
115+
storagePolicyObject.id = policyId;
116+
req.storage_policy = storagePolicyObject;
117+
118+
string jsonStr = req.ToString();
119+
120+
BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId)
121+
.Method(RequestMethod.Put)
122+
.Payload(jsonStr);
123+
124+
IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false);
125+
return response.ResponseObject;
126+
}
127+
128+
/// <summary>
129+
/// Create a storage policy assignment to a Box User.
130+
/// </summary>
131+
/// <param name="userId">The user Id to create assignment for.</param>
132+
/// <param name="policyId">The Id of the storage policy to assign the user to.</param>
133+
/// <returns>The assignment object for the storage policy assignment to user.</returns>
134+
public async Task<BoxStoragePolicyAssignment> CreateAssignmentAsync(string userId, string policyId)
135+
{
136+
userId.ThrowIfNullOrWhiteSpace("userId");
137+
policyId.ThrowIfNullOrWhiteSpace("policyId");
138+
139+
dynamic req = new JObject();
140+
dynamic storagePolicyObject = new JObject();
141+
dynamic userObject = new JObject();
142+
143+
storagePolicyObject.type = "storage_policy";
144+
storagePolicyObject.id = policyId;
145+
146+
userObject.type = "user";
147+
userObject.id = userId;
148+
149+
req.storage_policy = storagePolicyObject;
150+
req.assigned_to = userObject;
151+
152+
string jsonStr = req.ToString();
153+
154+
BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsForTargetUri)
155+
.Method(RequestMethod.Post)
156+
.Payload(jsonStr);
157+
158+
IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false);
159+
return response.ResponseObject;
160+
}
161+
162+
/// <summary>
163+
/// Sends request to delete an existing assignment.
164+
/// </summary>
165+
/// <param name="assignmentId">Id of the storage policy assignment.</param>
166+
/// <returns>A successful request returns 204 No Content.</returns>
167+
public async Task<bool> DeleteAssignmentAsync(string assignmentId)
168+
{
169+
BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId)
170+
.Method(RequestMethod.Delete);
171+
172+
var response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false);
173+
174+
return response.Status == ResponseStatus.Success;
175+
}
176+
177+
/// <summary>
178+
/// Checks if a storage policy assignment exists. If it does not then create an assignment.
179+
/// </summary>
180+
/// <param name="userId">The id of the user to assign storage policy to.</param>
181+
/// <param name="storagePolicyId">The storage policy id to assign to user.</param>
182+
/// <returns></returns>
183+
public async Task<BoxStoragePolicyAssignment> AssignAsync(string userId, string storagePolicyId)
184+
{
185+
userId.ThrowIfNullOrWhiteSpace("userId");
186+
storagePolicyId.ThrowIfNullOrWhiteSpace("storagePolicyId");
187+
188+
var result = await GetAssignmentForTargetAsync(userId);
189+
190+
if(result.BoxStoragePolicy.Id.Equals(storagePolicyId))
191+
{
192+
return result;
193+
}
194+
195+
if(result.AssignedTo.Type.Equals("enterprise"))
196+
{
197+
return await CreateAssignmentAsync(userId, storagePolicyId);
198+
}
199+
200+
return await UpdateStoragePolicyAssignment(result.Id, storagePolicyId);
201+
}
202+
}
203+
}

Box.V2/Models/BoxStoragePolicy.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Box.V2.Models
4+
{
5+
public class BoxStoragePolicy : BoxEntity
6+
{
7+
public const string FieldName = "name";
8+
9+
/// <summary>
10+
/// The name of the Box Storage Policy.
11+
/// </summary>
12+
[JsonProperty(PropertyName = FieldName)]
13+
public string Name { get; private set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Box.V2.Models
4+
{
5+
public class BoxStoragePolicyAssignment : BoxEntity
6+
{
7+
public const string FieldStoragePolicy = "storage_policy";
8+
public const string FieldAssignedTo = "assigned_to";
9+
10+
/// <summary>
11+
/// The storage policy to assign to user.
12+
/// </summary>
13+
[JsonProperty(PropertyName = FieldStoragePolicy)]
14+
public BoxEntity BoxStoragePolicy { get; set; }
15+
16+
/// <summary>
17+
/// The Box User to assign the storage policy to.
18+
/// </summary>
19+
[JsonProperty(PropertyName = FieldAssignedTo)]
20+
public BoxEntity AssignedTo { get; set; }
21+
}
22+
}

0 commit comments

Comments
 (0)