OLD | NEW |
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 /** | 3 /** |
4 Sandbox APIs mimicking communications with the Go and Juju backends. | 4 Sandbox APIs mimicking communications with the Go and Juju backends. |
5 | 5 |
6 @module env | 6 @module env |
7 @submodule env.sandbox | 7 @submodule env.sandbox |
8 */ | 8 */ |
9 | 9 |
10 YUI.add('juju-env-sandbox', function(Y) { | 10 YUI.add('juju-env-sandbox', function(Y) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 data.error = reply.error; | 214 data.error = reply.error; |
215 data.err = reply.error; | 215 data.err = reply.error; |
216 } else { | 216 } else { |
217 data.result = reply.result; | 217 data.result = reply.result; |
218 } | 218 } |
219 client.receiveNow(data); | 219 client.receiveNow(data); |
220 }; | 220 }; |
221 | 221 |
222 | 222 |
223 /** | 223 /** |
| 224 Given attrs or a model object and a whitelist of desired attributes, |
| 225 return an attrs hash of only the desired attributes. |
| 226 |
| 227 @method _getDeltaAttrs |
| 228 @private |
| 229 @param {Object} attrs A models or attrs hash. |
| 230 @param {Array} whitelist A list of desired attributes. |
| 231 @return {Object} A hash of the whitelisted attributes of the attrs object. |
| 232 */ |
| 233 var _getDeltaAttrs = function(attrs, whitelist) { |
| 234 if (attrs.getAttrs) { |
| 235 attrs = attrs.getAttrs(); |
| 236 } |
| 237 // For fuller verisimilitude, we could convert some of the |
| 238 // underlines in the attribute names to dashes. That is currently |
| 239 // unnecessary. |
| 240 var filtered = {}; |
| 241 Y.each(whitelist, function(name) { |
| 242 filtered[name] = attrs[name]; |
| 243 }); |
| 244 return filtered; |
| 245 }; |
| 246 |
| 247 /** |
224 A sandbox Juju environment using the Python API. | 248 A sandbox Juju environment using the Python API. |
225 | 249 |
226 @class PyJujuAPI | 250 @class PyJujuAPI |
227 */ | 251 */ |
228 function PyJujuAPI(config) { | 252 function PyJujuAPI(config) { |
229 PyJujuAPI.superclass.constructor.apply(this, arguments); | 253 PyJujuAPI.superclass.constructor.apply(this, arguments); |
230 } | 254 } |
231 | 255 |
232 PyJujuAPI.NAME = 'sandbox-py-juju-api'; | 256 PyJujuAPI.NAME = 'sandbox-py-juju-api'; |
233 PyJujuAPI.ATTRS = { | 257 PyJujuAPI.ATTRS = { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 311 |
288 /** | 312 /** |
289 Send a delta of events to the client from since the last time they asked. | 313 Send a delta of events to the client from since the last time they asked. |
290 | 314 |
291 @method sendDelta | 315 @method sendDelta |
292 @return {undefined} Nothing. | 316 @return {undefined} Nothing. |
293 */ | 317 */ |
294 sendDelta: function() { | 318 sendDelta: function() { |
295 var state = this.get('state'); | 319 var state = this.get('state'); |
296 var changes = state.nextChanges(); | 320 var changes = state.nextChanges(); |
| 321 if (changes && changes.error) { |
| 322 changes = null; |
| 323 } |
297 var annotations = state.nextAnnotations(); | 324 var annotations = state.nextAnnotations(); |
| 325 if (annotations && annotations.error) { |
| 326 annotations = null; |
| 327 } |
298 if (changes || annotations) { | 328 if (changes || annotations) { |
299 if (!changes) { | |
300 changes = annotations; | |
301 } else { | |
302 changes = Y.mix(changes, annotations, | |
303 true, 0, null, true); | |
304 } | |
305 } | |
306 | |
307 if (changes && !changes.error) { | |
308 var deltas = []; | 329 var deltas = []; |
309 var response = {op: 'delta', result: deltas}; | 330 var response = {op: 'delta', result: deltas}; |
310 Y.each(this._deltaWhitelist, function(whitelist, changeType) { | 331 Y.each(this._deltaWhitelist, function(whitelist, changeType) { |
311 Y.each(changes[changeType + 's'], function(change) { | 332 var collectionName = changeType + 's'; |
312 var attrs = change[0]; | 333 if (changes) { |
313 if (attrs.getAttrs) { | 334 Y.each(changes[collectionName], function(change) { |
314 attrs = attrs.getAttrs(); | 335 var attrs = _getDeltaAttrs(change[0], whitelist); |
315 } | 336 var action = change[1] ? 'change' : 'remove'; |
316 var filtered = {}; | 337 // The unit changeType is actually "serviceUnit" in the Python |
317 Y.each(whitelist, function(name) { | 338 // stream. Our model code handles either, so we're not modifying |
318 filtered[name] = attrs[name]; | 339 // it for now. |
| 340 deltas.push([changeType, action, attrs]); |
319 }); | 341 }); |
320 // For fuller verisimilitude, we could convert some of the | 342 } |
321 // underlines in the attribute names to dashes. That is currently | 343 if (annotations) { |
322 // unnecessary. | 344 Y.each(annotations[changeType + 's'], function(attrs, key) { |
323 var action = change[1] ? 'change' : 'remove'; | 345 if (!changes || !changes[key]) { |
324 // The unit changeType is actually "serviceUnit" in the Python | 346 attrs = _getDeltaAttrs(attrs, whitelist); |
325 // stream. Our model code handles either, so we're not modifying | 347 deltas.push([changeType, 'change', attrs]); |
326 // it for now. | 348 } |
327 deltas.push([changeType, action, filtered]); | 349 }); |
328 }); | 350 } |
329 }); | 351 }); |
330 this.get('client').receiveNow(response); | 352 this.get('client').receiveNow(response); |
331 } | 353 } |
332 }, | 354 }, |
333 | 355 |
334 /** | 356 /** |
335 Closes the connection to the sandbox Juju environment. | 357 Closes the connection to the sandbox Juju environment. |
336 Called by ClientConnection. | 358 Called by ClientConnection. |
337 | 359 |
338 @method close | 360 @method close |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 }); | 641 }); |
620 | 642 |
621 sandboxModule.PyJujuAPI = PyJujuAPI; | 643 sandboxModule.PyJujuAPI = PyJujuAPI; |
622 }, '0.1.0', { | 644 }, '0.1.0', { |
623 requires: [ | 645 requires: [ |
624 'base', | 646 'base', |
625 'timers', | 647 'timers', |
626 'json-parse' | 648 'json-parse' |
627 ] | 649 ] |
628 }); | 650 }); |
OLD | NEW |