diff options
author | Peter Lee <peter@corenova.com> | 2016-01-29 15:21:56 -0800 |
---|---|---|
committer | Peter Lee <peter@corenova.com> | 2016-01-29 15:21:56 -0800 |
commit | a46af646972b6ff263fb207d28a59e7ce7417b5c (patch) | |
tree | 644d9cb2fead71ddf00c2a83219040288e23d471 /source/spec/promise-module.coffee | |
parent | e16a07cdc9016c03aff00cd10076dfd2d4a142da (diff) |
added copy of github.com/opnfv/promise source into the source directory
Change-Id: Ib212302a4132aa492f7b701a7ca02f54a7d0a6af
Diffstat (limited to 'source/spec/promise-module.coffee')
-rw-r--r-- | source/spec/promise-module.coffee | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/source/spec/promise-module.coffee b/source/spec/promise-module.coffee new file mode 100644 index 0000000..3eea482 --- /dev/null +++ b/source/spec/promise-module.coffee @@ -0,0 +1,72 @@ +module.exports = + '/opnfv-promise/promise/capacity/total': (prev) -> + @computed (-> + combine = (a, b) -> + for k, v of b.capacity when v? + a[k] ?= 0 + a[k] += v + return a + (@parent.get 'pools') + .filter (entry) -> entry.active is true + .reduce combine, {} + ), type: prev + + '/opnfv-promise/promise/capacity/reserved', (prev) -> + @computed (-> + combine = (a, b) -> + for k, v of b.capacity when v? + a[k] ?= 0 + a[k] += v + return a + (@parent.get 'reservations') + .filter (entry) -> entry.active is true + .reduce combine, {} + ), type: prev + + # rebind to be a computed property + '/opnfv-promise/promise/capacity/usage': (prev) -> + @computed (-> + combine = (a, b) -> + for k, v of b.capacity when v? + a[k] ?= 0 + a[k] += v + return a + (@parent.get 'allocations') + .filter (entry) -> entry.active is true + .reduce combine, {} + ), type: prev + + # rebind to be a computed property + '/opnfv-promise/promise/capacity/available': (prev) -> + @computed (-> + total = @get 'total' + reserved = @get 'reserved' + usage = @get 'usage' + for k, v of total when v? + total[k] -= reserved[k] if reserved[k]? + total[k] -= usage[k] if usage[k]? + total + ), type: prev + + '/opnfv-promise/create-reservation': + (input, output, done) -> + # 1. create the reservation record (empty) + reservation = @create 'ResourceReservation' + reservations = @access 'promise.reservations' + + # 2. update the record with requested input + reservation.invoke 'update', input.get() + .then (res) -> + # 3. save the record and add to list + res.save() + .then -> + reservations.push res + output.set result: 'ok', message: 'reservation request accepted' + output.set 'reservation-id', res.id + done() + .catch (err) -> + output.set result: 'error', message: err + done() + .catch (err) -> + output.set result: 'conflict', message: err + done() |