UNPKG

5.5 kBtext/coffeescriptView Raw
1GoogleAPIAdminSDK = require "#{__dirname}/google_api_admin_sdk"
2_ = require 'underscore'
3utils = require "#{__dirname}/utils"
4{Query} = require "#{__dirname}/query.coffee"
5qs = require 'qs'
6sanitize = require 'sanitize-arguments'
7async = require 'async'
8retry = require 'retry'
9
10class OrgUnit extends GoogleAPIAdminSDK
11 # TODO: possibly make customer_id an option of OrgUnit
12 # customer_id, org_unit_path required
13 # if no patch_body is provided, update behaves like get
14 # fields returns only selected properties of an OrgUnit object
15 patch: (customer_id, org_unit_path, patch_body, fields, cb) =>
16 arglist = sanitize arguments, OrgUnit.patch, [String, String, Object, String, Function]
17 args = _.object ['customer_id', 'org_unit_path', 'patch_body', 'fields', 'cb'], arglist
18 die = utils.die_fn args.cb
19 if not args.customer_id? or not args.org_unit_path?
20 return die "OrgUnit::patch expected (String customer_id, String org_unit_path, [Object patch_body, String fields, callback])"
21 uri = "https://www.googleapis.com/admin/directory/v1/customer/#{args.customer_id}/orgunits/#{args.org_unit_path}"
22 uri += "?#{qs.stringify {fields: args.fields}}" if args.fields?
23 opts = { method: 'patch', uri: uri, json: (args.patch_body or true) }
24 q = new Query @, opts
25 return q unless args.cb?
26 q.exec args.cb
27
28 insert: (customer_id, properties, fields, cb) =>
29 arglist = sanitize arguments, OrgUnit.insert, [String, Object, String, Function]
30 args = _.object ['customer_id', 'properties', 'fields', 'cb'], arglist
31 die = utils.die_fn args.cb
32 if not args.customer_id? or not args.properties?
33 return die "OrgUnit::insert expected (String customer_id, Object properties[, String fields, callback])"
34 uri = "https://www.googleapis.com/admin/directory/v1/customer/#{args.customer_id}/orgunits"
35 uri += "?#{qs.stringify {fields: args.fields}}" if args.fields?
36 opts = { method: 'post', uri: uri, json: properties }
37 q = new Query @, opts
38 return q unless args.cb?
39 q.exec args.cb
40
41
42 delete: (customer_id, org_unit_path, cb) =>
43 arglist = sanitize arguments, OrgUnit.delete, [String, String, Function]
44 args = _.object ['customer_id', 'org_unit_path', 'cb'], arglist
45 die = utils.die_fn args.cb
46 if not args.customer_id? or not args.org_unit_path?
47 return die "OrgUnit::delete expected (String customer_id, String org_unit_path[, callback])"
48 uri = "https://www.googleapis.com/admin/directory/v1/customer/#{args.customer_id}/orgunits/#{args.org_unit_path}"
49 opts = { method: 'delete', uri: uri, json: true }
50 q = new Query @, opts
51 return q unless args.cb?
52 q.exec args.cb
53
54 list: (customer_id, params, cb) =>
55 arglist = sanitize arguments, OrgUnit.list, [String, Object, Function]
56 args = _.object ['customer_id', 'params', 'cb'], arglist
57 die = utils.die_fn args.cb
58 return die "OrgUnit::list expected (String customer_id[, Object params, callback])" if not args.customer_id?
59 opts = { json: true }
60 opts.uri = "https://www.googleapis.com/admin/directory/v1/customer/#{args.customer_id}/orgunits"
61 opts.uri += "?#{qs.stringify args.params}" if args.params?
62 q = new Query @, opts
63 return q unless args.cb?
64 q.exec args.cb
65
66 get: (customer_id, org_unit_path, fields, cb) =>
67 arglist = sanitize arguments, OrgUnit.get, [String, String, String, Function]
68 args = _.object ['customer_id', 'org_unit_path', 'fields', 'cb'], arglist
69 die = utils.die_fn args.cb
70 if not args.customer_id? or not args.org_unit_path?
71 return die "OrgUnit::get expected (String customer_id, String org_unit_path, [, String fields, callback])"
72 opts = { json: true }
73 opts.uri = "https://www.googleapis.com/admin/directory/v1/customer/#{args.customer_id}/orgunits/#{args.org_unit_path}"
74 opts.uri += "?#{qs.stringify {fields: args.fields}}" if args.fields?
75 q = new Query @, opts
76 return q unless args.cb?
77 q.exec args.cb
78
79 # takes customer_id, array of orgunit levels eg. ['/', 'Students', 'Schoolname', ...], and optional cache, and callback
80 # returns callback w/ args orgunit string '/Students/Schoolname' and cache of orgunits created '/', '/Students', '/Students/Schoolname'
81 findOrCreate: (customer_id, org_unit, cache, cb) =>
82 if _(cache).isFunction()
83 cb = cache
84 cache = {}
85 arglist = sanitize arguments, OrgUnit.findOrCreate, [String, Array, Function]
86 args = _.object ['customer_id', 'org_unit', 'cb'], arglist
87 die = utils.die_fn args.cb
88 if not args.customer_id? or not args.org_unit?
89 return die "OrgUnit::findOrCreate expected (String customer_id, Array org_unit, [callback])"
90 parent = '/'
91 async.eachSeries args.org_unit, (level, cb_es) =>
92 full_path = if parent is '/' then "/#{level}" else "#{parent}/#{level}"
93 if cache[full_path]?
94 parent = full_path
95 return cb_es()
96 @insert args.customer_id, { name: level, parentOrgUnitPath: parent }, (err, body) =>
97 # If the Ou already exists, Google returns error code 400 with the message 'Invalid Ou Id'
98 # Don't treat this as an error
99 if err? and not (err?.error?.code is 400 and err?.error?.message is 'Invalid Ou Id')
100 return cb_es "Unable to create org unit #{full_path}: #{JSON.stringify err}"
101 cache[full_path] = 1
102 parent = full_path
103 cb_es()
104 , (err) ->
105 return die err if err?
106 cb null, parent, cache
107
108module.exports = OrgUnit