Source: managers/storage-policies.ts

  1. /**
  2. * @fileoverview Manager for the Storage Policies resource
  3. */
  4. // -----------------------------------------------------------------------------
  5. // Requirements
  6. // -----------------------------------------------------------------------------
  7. import httpStatus from 'http-status';
  8. import BoxClient from '../box-client';
  9. import errors from '../util/errors';
  10. import urlPath from '../util/url-path';
  11. // -----------------------------------------------------------------------------
  12. // Private
  13. // -----------------------------------------------------------------------------
  14. const BASE_PATH = '/storage_policies',
  15. ASSIGNMENTS_PATH = '/storage_policy_assignments';
  16. // -----------------------------------------------------------------------------
  17. // Public
  18. // -----------------------------------------------------------------------------
  19. /**
  20. * Simple manager for interacting with all Retention Policies endpoints and actions.
  21. *
  22. * @constructor
  23. * @param {BoxClient} client - The Box API Client that is responsible for making calls to the API
  24. * @returns {void}
  25. */
  26. class StoragePolicies {
  27. client: BoxClient;
  28. constructor(client: BoxClient) {
  29. this.client = client;
  30. }
  31. /**
  32. * Get information about a specific storage policy
  33. * @param {string} storagePolicyID The ID of the storage policy
  34. * @param {Object} [options] Optional parameters
  35. * @param {string} [options.fields] Comma-separated list of fields of the storage policy to retrieve
  36. * @param {Function} [callback] Passed the storage policy object if successful
  37. * @returns {Promise<Object>} Promise resolving to the storage policy object
  38. */
  39. get(
  40. storagePolicyID: string,
  41. options?: {
  42. fields?: string;
  43. },
  44. callback?: Function
  45. ) {
  46. var apiPath = urlPath(BASE_PATH, storagePolicyID),
  47. params = {
  48. qs: options,
  49. };
  50. return this.client.wrapWithDefaultHandler(this.client.get)(
  51. apiPath,
  52. params,
  53. callback
  54. );
  55. }
  56. /**
  57. * Get all available storage policies for the enterprise
  58. * @param {Object} [options] Optional parameters
  59. * @param {string} [options.fields] Comma-separated list of fields of the storage policy to retrieve
  60. * @param {Function} [callback] Passed a collection of storage policies if successful
  61. * @returns {Promise<Object>} Promise resolving to the collection of storage policies
  62. */
  63. getAll(
  64. options?: {
  65. fields?: string;
  66. },
  67. callback?: Function
  68. ) {
  69. var apiPath = urlPath(BASE_PATH),
  70. params = {
  71. qs: options,
  72. };
  73. return this.client.wrapWithDefaultHandler(this.client.get)(
  74. apiPath,
  75. params,
  76. callback
  77. );
  78. }
  79. /**
  80. * Assign a storage policy to a user
  81. * @param {string} storagePolicyID The ID of the storage policy to assign
  82. * @param {string} userID The ID of the user to assign the storage policy to
  83. * @param {Function} [callback] Passed the assignment object if successful
  84. * @returns {Promise<Object>} Promise resolving to the assignment object
  85. */
  86. assign(storagePolicyID: string, userID: string, callback?: Function) {
  87. return this.getAssignmentForTarget(userID)
  88. .then((assignment: any /* FIXME */) => {
  89. // Check if the assignment is already correct
  90. if (assignment.storage_policy.id === storagePolicyID) {
  91. return assignment;
  92. }
  93. // If the assignment is to an enterprise, we need to create a new
  94. // assignment for the user
  95. if (assignment.assigned_to.type === 'enterprise') {
  96. return this.createAssignment(storagePolicyID, userID);
  97. }
  98. // Update the user's existing assignment
  99. var update = {
  100. storage_policy: {
  101. type: 'storage_policy',
  102. id: storagePolicyID,
  103. },
  104. };
  105. return this.updateAssignment(assignment.id, update);
  106. })
  107. .asCallback(callback);
  108. }
  109. /**
  110. * Get information about a specific storage policy asisgnment by ID
  111. * @param {string} assignmentID The ID of the assignment
  112. * @param {Function} [callback] Passed the assignment object if successful
  113. * @returns {Promise<Object>} Promise resolving to the assignment object
  114. */
  115. getAssignment(assignmentID: string, callback?: Function) {
  116. var apiPath = urlPath(ASSIGNMENTS_PATH, assignmentID);
  117. return this.client.wrapWithDefaultHandler(this.client.get)(
  118. apiPath,
  119. null,
  120. callback
  121. );
  122. }
  123. /**
  124. * Gets the storage policy assignment for a specific user
  125. * @param {string} targetID The ID of the target
  126. * @param {Object} [options] Optional parameters
  127. * @param {string} [options.targetType=user] The type of the assignment target to resolve for
  128. * @param {Function} [callback] Passed the assignment object if successful
  129. * @returns {Promise<Object>} Promise resolving to the assignment object
  130. */
  131. getAssignmentForTarget(
  132. targetID: string,
  133. options?: {
  134. targetType?: string;
  135. },
  136. callback?: Function
  137. ) {
  138. options = Object.assign({ targetType: 'user' }, options);
  139. var apiPath = urlPath(ASSIGNMENTS_PATH),
  140. params = {
  141. qs: {
  142. resolved_for_type: options.targetType,
  143. resolved_for_id: targetID,
  144. },
  145. };
  146. return this.client
  147. .get(apiPath, params)
  148. .then((response: any /* FIXME */) => {
  149. if (response.statusCode !== httpStatus.OK) {
  150. // Unexpected status code, throw an error
  151. throw errors.buildUnexpectedResponseError(response);
  152. }
  153. // Unwrap the collection and give back just the assignment object
  154. return response.body.entries[0];
  155. })
  156. .asCallback(callback);
  157. }
  158. /**
  159. * Create a new storage policy assignment to a user
  160. * @param {string} storagePolicyID The ID of the storage policy to assign
  161. * @param {string} userID The ID of the user to assign the storage policy to
  162. * @param {Function} [callback] Passed the assignment object if successful
  163. * @returns {Promise<Object>} Promise resolving to the assignment object
  164. */
  165. createAssignment(
  166. storagePolicyID: string,
  167. userID: string,
  168. callback?: Function
  169. ) {
  170. var apiPath = urlPath(ASSIGNMENTS_PATH),
  171. params = {
  172. body: {
  173. storage_policy: {
  174. type: 'storage_policy',
  175. id: storagePolicyID,
  176. },
  177. assigned_to: {
  178. type: 'user',
  179. id: userID,
  180. },
  181. },
  182. };
  183. return this.client.wrapWithDefaultHandler(this.client.post)(
  184. apiPath,
  185. params,
  186. callback
  187. );
  188. }
  189. /**
  190. * Update a storage policy assignment
  191. * @param {string} assignmentID The ID of the storage policy assignment to update
  192. * @param {Object} updates The updates fields to apply
  193. * @param {Function} [callback] Passed the updated assignment object if successful
  194. * @returns {Promise<Object>} Promise resolving to the updated assignment object
  195. */
  196. updateAssignment(
  197. assignmentID: string,
  198. updates: Record<string, any>,
  199. callback?: Function
  200. ) {
  201. var apiPath = urlPath(ASSIGNMENTS_PATH, assignmentID),
  202. params = {
  203. body: updates,
  204. };
  205. return this.client.wrapWithDefaultHandler(this.client.put)(
  206. apiPath,
  207. params,
  208. callback
  209. );
  210. }
  211. /**
  212. * Remove a storage policy assignment, returning the user to the default policy
  213. * @param {string} assignmentID The ID of the assignment to remove
  214. * @param {Function} [callback] Passed nothing if successful
  215. * @returns {Promise<void>} Promise resolving if the removal succeeds
  216. */
  217. removeAssignment(assignmentID: string, callback?: Function) {
  218. var apiPath = urlPath(ASSIGNMENTS_PATH, assignmentID);
  219. return this.client.wrapWithDefaultHandler(this.client.del)(
  220. apiPath,
  221. null,
  222. callback
  223. );
  224. }
  225. }
  226. export = StoragePolicies;