summaryrefslogtreecommitdiffstats
path: root/source/spec/promise-intents.coffee
blob: 985e81f1cac85e556896923c190d7e6fc0aa572b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#
# Author: Peter K. Lee (peter@corenova.com)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#
module.exports =
  '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()

  'query-reservation':
    (input, output, done) ->
      query = input.get()
      query.capacity = 'reserved'
      @invoke 'query-capacity', query
      .then (res) ->
        output.set 'reservations', res.get 'collections'
        output.set 'utilization', res.get 'utilization'
        done()
      .catch (e) -> done e

  'update-reservation':
    (input, output, done) ->
      # TODO: we shouldn't need this... need to check why leaf mandatory: true not being enforced
      unless (input.get 'reservation-id')?
        output.set result: 'error', message: "must provide 'reservation-id' parameter"
        return done()

      # 1. find the reservation
      reservation = @find 'ResourceReservation', input.get 'reservation-id'
      unless reservation?
        output.set result: 'error', message: 'no reservation found for specified identifier'
        return done()

      # 2. update the record with requested input
      reservation.invoke 'update', input.get()
      .then (res) ->
        # 3. save the updated record
        res.save()
        .then ->
          output.set result: 'ok', message: 'reservation update successful'
          done()
        .catch (err) ->
          output.set result: 'error', message: err
          done()
      .catch (err) ->
        output.set result: 'conflict', message: err
        done()

  'cancel-reservation':
    (input, output, done) ->
      # 1. find the reservation
      reservation = @find 'ResourceReservation', input.get 'reservation-id'
      unless reservation?
        output.set result: 'error', message: 'no reservation found for specified identifier'
        return done()

      # 2. destroy all traces of this reservation
      reservation.destroy()
      .then =>
        (@access 'promise.reservations').remove reservation.id
        output.set 'result', 'ok'
        output.set 'message', 'reservation canceled'
        done()
      .catch (e) ->
        output.set 'result', 'error'
        output.set 'message', e
        done()

  'query-capacity':
    (input, output, done) ->
      # 1. we gather up all collections that match the specified window
      window = input.get 'window'
      metric = input.get 'capacity'

      collections = switch metric
        when 'total'     then [ 'ResourcePool' ]
        when 'reserved'  then [ 'ResourceReservation' ]
        when 'usage'     then [ 'ResourceAllocation' ]
        when 'available' then [ 'ResourcePool', 'ResourceReservation', 'ResourceAllocation' ]

      matches = collections.reduce ((a, name) =>
        res = @find name,
          start: (value) -> (not window.end?)   or (new Date value) <= (new Date window.end)
          end:   (value) -> (not window.start?) or (new Date value) >= (new Date window.start)
          enabled: true
        a.concat res...
      ), []

      if window.scope is 'exclusive'
        # yes, we CAN query filter in one shot above but this makes logic cleaner...
        matches = matches.where
          start: (value) -> (not window.start?) or (new Date value) >= (new Date window.start)
          end:   (value) -> (not window.end?) or (new Date value) <= (new Date window.end)

      # exclude any identifiers specified
      matches = matches.without id: (input.get 'without')

      if metric is 'available'
        # excludes allocations with reservation property set (to prevent double count)
        matches = matches.without reservation: (v) -> v?

      output.set 'collections', matches
      unless (input.get 'show-utilization') is true
        return done()

      # 2. we calculate the deltas based on start/end times of each match item
      deltas = matches.reduce ((a, entry) ->
        b = entry.get()
        b.end ?= 'infiniteT'
        [ skey, ekey ] = [ (b.start.split 'T')[0], (b.end.split 'T')[0] ]
        a[skey] ?= count: 0, capacity: {}
        a[ekey] ?= count: 0, capacity: {}
        a[skey].count += 1
        a[ekey].count -= 1

        for k, v of b.capacity when v?
          a[skey].capacity[k] ?= 0
          a[ekey].capacity[k] ?= 0
          if entry.name is 'ResourcePool'
            a[skey].capacity[k] += v
            a[ekey].capacity[k] -= v
          else
            a[skey].capacity[k] -= v
            a[ekey].capacity[k] += v
        return a
      ), {}

      # 3. we then sort the timestamps and aggregate the deltas
      last = count: 0, capacity: {}
      usages = for timestamp in Object.keys(deltas).sort() when timestamp isnt 'infinite'
        entry = deltas[timestamp]
        entry.timestamp = (new Date timestamp).toJSON()
        entry.count += last.count
        for k, v of entry.capacity
          entry.capacity[k] += (last.capacity[k] ? 0)
        last = entry
        entry

      output.set 'utilization', usages
      done()

  'increase-capacity':
    (input, output, done) ->
      pool = @create 'ResourcePool', input.get()
      pool.save()
      .then (res) =>
        (@access 'promise.pools').push res
        output.set result: 'ok', message: 'capacity increase successful'
        output.set 'pool-id', res.id
        done()
      .catch (e) ->
        output.set result: 'error', message: e
        done()

  'decrease-capacity':
    (input, output, done) ->
      request = input.get()
      for k, v of request.capacity
        request.capacity[k] = -v
      pool = @create 'ResourcePool', request
      pool.save()
      .then (res) =>
        (@access 'promise.pools').push res
        output.set result: 'ok', message: 'capacity decrease successful'
        output.set 'pool-id', res.id
        done()
      .catch (e) ->
        output.set result: 'error', message: e
        done()

  # TEMPORARY (should go into VIM-specific module)
  'create-instance':
    (input, output, done) ->
      pid = input.get 'provider-id'
      if pid?
        provider = @find 'ResourceProvider', pid
        unless provider?
          output.set result: 'error', message: "no matching provider found for specified identifier: #{pid}"
          return done()
      else
        provider = (@find 'ResourceProvider')[0]
        unless provider?
          output.set result: 'error', message: "no available provider found for create-instance"
          return done()

      # calculate required capacity based on 'flavor' and other params
      flavor = provider.access "services.compute.flavors.#{input.get 'flavor'}"
      unless flavor?
        output.set result: 'error', message: "no such flavor found for specified identifier: #{pid}"
        return done()

      required =
        instances: 1
        cores:     flavor.get 'vcpus'
        ram:       flavor.get 'ram'
        gigabytes: flavor.get 'disk'

      rid = input.get 'reservation-id'
      if rid?
        reservation = @find 'ResourceReservation', rid
        unless reservation?
          output.set result: 'error', message: 'no valid reservation found for specified identifier'
          return done()
        unless (reservation.get 'active') is true
          output.set result: 'error', message: "reservation is currently not active"
          return done()
        available = reservation.get 'remaining'
      else
        available = @get 'promise.capacity.available'

      # TODO: need to verify whether 'provider' associated with this 'reservation'

      for k, v of required when v? and !!v
        unless available[k] >= v
          output.set result: 'conflict', message: "required #{k}=#{v} exceeds available #{available[k]}"
          return done()

      @create 'ResourceAllocation',
        reservation: rid
        capacity: required
      .save()
      .then (instance) =>
        url = provider.get 'services.compute.endpoint'
        payload =
          server:
            name: input.get 'name'
            imageRef: input.get 'image'
            flavorRef: input.get 'flavor'
        networks = (input.get 'networks').filter (x) -> x? and !!x
        if networks.length > 0
          payload.server.networks = networks.map (x) -> uuid: x

        request = @parent.require 'superagent'
        request
          .post "#{url}/servers"
          .send payload
          .set 'X-Auth-Token', provider.get 'token'
          .set 'Accept', 'application/json'
          .end (err, res) =>
            if err? or !res.ok
              instance.destroy()
              #console.error err
              return done res.error
            #console.log JSON.stringify res.body, null, 2
            instance.set 'instance-ref',
              provider: provider
              server: res.body.server.id
            (@access 'promise.allocations').push instance
            output.set result: 'ok', message: 'create-instance request accepted'
            output.set 'instance-id', instance.id
            done()
         return instance
      .catch (err) ->
        output.set result: 'error', mesage: err
        done()

  'destroy-instance':
    (input, output, done) ->
      # 1. find the instance
      instance = @find 'ResourceAllocation', input.get 'instance-id'
      unless instance?
        output.set result: 'error', message: 'no allocation found for specified identifier'
        return done()

      # 2. destroy all traces of this instance
      instance.destroy()
      .then =>
        # always remove internally
        (@access 'promise.allocations').remove instance.id
        ref = instance.get 'instance-ref'
        provider = (@access "promise.providers.#{ref.provider}")
        url = provider.get 'services.compute.endpoint'
        request = @parent.require 'superagent'
        request
          .delete "#{url}/servers/#{ref.server}"
          .set 'X-Auth-Token', provider.get 'token'
          .set 'Accept', 'application/json'
          .end (err, res) =>
            if err? or !res.ok
              console.error err
              return done res.error
            output.set 'result', 'ok'
            output.set 'message', 'instance destroyed and resource released back to pool'
            done()
        return instance
      .catch (e) ->
        output.set 'result', 'error'
        output.set 'message', e
        done()

  # TEMPORARY (should go into VIM-specific module)
  'add-provider':
    (input, output, done) ->
      app = @parent
      request = app.require 'superagent'

      payload = switch input.get 'provider-type'
        when 'openstack'
          auth:
            identity: 
               methods: [ "password" ]
               password:
                  user:
                     name: input.get 'username'
                     password: input.get 'password'
                     domain:
                        name: input.get 'user-domain-name'
            scope:
               project:
                  name: input.get 'project.name'
                  domain:
                     name: input.get 'project.domain-name'

      unless payload?
        return done 'Sorry, only openstack supported at this time'

      url = input.get 'endpoint'
      switch input.get 'strategy'
        when 'keystone', 'oauth'
          url += '/auth/tokens' unless /\/tokens$/.test url

      providers = @access 'promise.providers'
      request
        .post url
        .send payload
        .set 'Accept', 'application/json'
        .end (err, res) =>
          if err? or !res.ok then return done res.error
          #console.log JSON.stringify res.body, null, 2
          #console.log res.headers
          #console.log res.body.token.catalog
          provider = @create 'ResourceProvider',
            token: res.headers['x-subject-token']
            name: res.body.token.project.name
          provider.invoke 'update', res.body.token.catalog
          .then (res) ->
            res.save()
            .then ->
              providers.push res
              output.set 'result', 'ok'
              output.set 'provider-id', res.id
              done()
            .catch (err) ->
              output.set 'error', message: err
              done()
          .catch (err) ->
            output.set 'error', message: err
            done()

      # @using 'mano', ->
      #   @invoke 'add-provider', (input.get 'endpoint', 'region', 'username', 'password')
      #   .then (res) =>
      #     (@access 'promise.providers').push res
      #     output.set 'result', 'ok'
      #     output.set 'provider-id', res.id
      #     done()