This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
sandbox.js
1693 lines (1499 loc) · 51.3 KB
/
sandbox.js
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2012-2013 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
/**
Sandbox APIs mimicking communications with the Go and Python backends.
@module env
@submodule env.sandbox
*/
YUI.add('juju-env-sandbox', function(Y) {
var sandboxModule = Y.namespace('juju.environments.sandbox');
var models = Y.namespace('juju.models');
var CLOSEDERROR = 'INVALID_STATE_ERR : Connection is closed.';
/**
A client connection for interacting with a sandbox environment.
@class ClientConnection
*/
function ClientConnection(config) {
ClientConnection.superclass.constructor.apply(this, arguments);
}
ClientConnection.NAME = 'sandbox-client-connection';
ClientConnection.ATTRS = {
juju: {} // Required.
};
Y.extend(ClientConnection, Y.Base, {
/**
Initialize.
@method initializer
@return {undefined} Nothing.
*/
initializer: function() {
this.connected = false;
},
/**
React to a new message from Juju.
You are expected to monkeypatch this method, as with websockets.
@method onmessage
@param {Object} event An object with a JSON string on the "data"
attribute.
@return {undefined} Nothing.
*/
onmessage: function(event) {},
/**
Immediately give message to listener (contrast with receive).
Uses onmessage to deliver message, as with websockets.
@method receiveNow
@param {Object} data An object to be sent as JSON to the listener.
@param {Boolean} failSilently A flag to turn off the error when the
connection is closed. This exists to handle a race condition between
receiveNow and receive, when the connection closes between the two.
@return {undefined} Nothing.
*/
receiveNow: function(data, failSilently) {
if (this.connected) {
this.onmessage({data: Y.JSON.stringify(data)});
} else if (!failSilently) {
throw CLOSEDERROR;
}
},
/**
Give message to listener asynchronously (contrast with receiveNow).
Uses onmessage to deliver message, as with websockets.
@method receive
@param {Object} data An object to be sent as JSON to the listener.
@return {undefined} Nothing.
*/
receive: function(data) {
if (this.connected) {
Y.soon(this.receiveNow.bind(this, data, true));
} else {
throw CLOSEDERROR;
}
},
/**
Send a JSON string to the API.
@method send
@param {String} data A JSON string of the data to be sent.
@return {undefined} Nothing.
*/
send: function(data) {
if (this.connected) {
this.get('juju').receive(Y.JSON.parse(data));
} else {
throw CLOSEDERROR;
}
},
/**
React to an opening connection.
You are expected to monkeypatch this method, as with websockets.
@method onopen
@return {undefined} Nothing.
*/
onopen: function() {},
/**
Explicitly open the connection.
This does not have an analog with websockets, but requiring an explicit
"open" means less magic is necessary. It is responsible for changing
the "connected" state, for calling the onopen hook, and for calling
the sandbox juju.open with itself.
@method open
@return {undefined} Nothing.
*/
open: function() {
if (!this.connected) {
this.connected = true;
this.get('juju').open(this);
this.onopen();
}
},
/**
React to a closing connection.
You are expected to monkeypatch this method, as with websockets.
@method onclose
@return {undefined} Nothing.
*/
onclose: function() {},
/**
Close the connection.
This is responsible for changing the "connected" state, for calling the
onclosed hook, and for calling the sandbox juju.close.
@method close
@return {undefined} Nothing.
*/
close: function() {
if (this.connected) {
this.connected = false;
this.get('juju').close();
this.onclose();
}
}
});
sandboxModule.ClientConnection = ClientConnection;
/** Helper function method for generating operation methods
* with a callback. Returns a method with a callback wired
* in to continue the operation when done. The returned
* method should be passed the data mapping to invoke.
*
* @method ASYNC_OP
* @param {Object} context PyJujuAPI Instance.
* @param {String} rpcName Name of method on fakebackend.
* @param {Array} args String list of arguments to extract
* from passed data. Used in order
* listed as arguments to the RPC call.
* @return {undefined} sends to client implicitly.
*/
var ASYNC_OP = function(context, rpcName, args) {
return Y.bind(function(data) {
var state = this.get('state');
var client = this.get('client');
var vargs = Y.Array.map(args, function(i) {
return data[i];
});
var callback = function(reply) {
if (reply.error) {
data.error = reply.error;
data.err = reply.error;
} else {
data.result = reply.result;
}
client.receiveNow(data);
};
// Add our generated callback to arguments.
vargs.push(callback);
state[rpcName].apply(state, vargs);
}, context);
};
/** Helper method for normalizing error handling
* around sync operations with the fakebackend.
* Returned method can directly return to the caller.
*
* @method OP
* @param {Object} context PyJujuAPI instance.
* @param {String} rpcName name of method on fakebackend to invoke.
* @param {Array} args String Array of arguments to pass from
* data to fakebackend.
* @param {Object} data Operational data to be munged into a fakebackend call.
* @return {Object} result depends on underlying rpc method.
*/
var OP = function(context, rpcName, args, data) {
var state = context.get('state');
var client = context.get('client');
var vargs = Y.Array.map(args, function(i) {
return data[i];
});
var reply = state[rpcName].apply(state, vargs);
if (reply.error) {
data.error = reply.error;
data.err = reply.error;
} else {
data.result = reply.result;
}
client.receiveNow(data);
};
/**
A sandbox Juju environment using the Python API.
@class PyJujuAPI
*/
function PyJujuAPI(config) {
PyJujuAPI.superclass.constructor.apply(this, arguments);
}
PyJujuAPI.NAME = 'sandbox-py-juju-api';
PyJujuAPI.ATTRS = {
state: {}, // Required.
client: {}, // Set in the "open" method.
deltaInterval: {value: 1000} // In milliseconds.
};
Y.extend(PyJujuAPI, Y.Base, {
/**
Initializes.
@method initializer
@return {undefined} Nothing.
*/
initializer: function() {
this.connected = false;
},
/**
Opens the connection to the sandbox Juju environment.
Called by ClientConnection, which sends itself.
@method open
@param {Object} client A ClientConnection.
@return {undefined} Nothing.
*/
open: function(client) {
if (!this.connected) {
this.connected = true;
this.set('client', client);
var state = this.get('state');
client.receive({
ready: true,
provider_type: state.get('providerType'),
default_series: state.get('defaultSeries')
});
this.deltaIntervalId = setInterval(
this.sendDelta.bind(this), this.get('deltaInterval'));
} else if (this.get('client') !== client) {
throw 'INVALID_STATE_ERR : Connection is open to another client.';
}
},
/**
A white-list for model attributes. This translates them from the raw ATTRS
to the format expected by the environment's delta handling.
@property _deltaWhitelist
@type {Object}
*/
_deltaWhitelist: {
service: ['charm', 'config', 'constraints', 'exposed', 'id', 'name',
'subordinate', 'annotations'],
machine: ['agent_state', 'public_address', 'machine_id', 'id',
'annotations'],
unit: ['agent_state', 'machine', 'number', 'service', 'id',
'annotations'],
relation: ['relation_id', 'type', 'endpoints', 'scope', 'id'],
annotation: ['annotations']
},
/**
Given attrs or a model object and a whitelist of desired attributes,
return an attrs hash of only the desired attributes.
@method _getDeltaAttrs
@private
@param {Object} attrs A model or attrs hash.
@param {Array} whitelist A list of desired attributes.
@return {Object} A hash of the whitelisted attributes of the attrs object.
*/
_getDeltaAttrs: function(attrs, whitelist) {
if (attrs.getAttrs) {
attrs = attrs.getAttrs();
}
// For fuller verisimilitude, we could convert some of the
// underlines in the attribute names to dashes. That is currently
// unnecessary.
var filtered = {};
Y.each(whitelist, function(name) {
filtered[name] = attrs[name];
});
return filtered;
},
/**
Prepare a delta of events to send to the client since the last time they
asked. The deltas list is prepared nearly the same way depending on Py
or Go implentation, but the data within the individual deltas must be
structured dependent on the backend. This method is called using `apply`
from within the appropriate sandbox so that `this._deltaWhitelist` and
`self._getDeltaAttrs` can structure the delta according to the juju type.
@method _prepareDelta
@return {Array} An array of deltas events.
*/
_prepareDelta: function() {
var self = this;
var state = this.get('state');
var deltas = [];
var changes = state.nextChanges();
if (changes && changes.error) {
changes = null;
}
var annotations = state.nextAnnotations();
if (annotations && annotations.error) {
annotations = null;
}
if (changes || annotations) {
Y.each(this._deltaWhitelist, function(whitelist, changeType) {
var collectionName = changeType + 's';
if (changes) {
Y.each(changes[collectionName], function(change) {
var attrs = self._getDeltaAttrs(change[0], whitelist);
var action = change[1] ? 'change' : 'remove';
// The unit changeType is actually "serviceUnit" in the Python
// stream. Our model code handles either, so we're not modifying
// it for now.
var delta = [changeType, action, attrs];
deltas.push(delta);
});
}
if (annotations) {
Y.each(annotations[changeType + 's'], function(attrs, key) {
if (!changes || !changes[key]) {
attrs = self._getDeltaAttrs(attrs, whitelist);
// Special case environment handling.
if (changeType === 'annotation') {
changeType = 'annotations';
attrs = attrs.annotations;
}
deltas.push([changeType, 'change', attrs]);
}
});
}
});
}
return deltas;
},
/**
Send a delta of events to the client from since the last time they asked.
@method sendDelta
@return {undefined} Nothing.
*/
sendDelta: function() {
var deltas = this._prepareDelta();
if (deltas.length) {
this.get('client').receiveNow({op: 'delta', result: deltas});
}
},
/**
Closes the connection to the sandbox Juju environment.
Called by ClientConnection.
@method close
@return {undefined} Nothing.
*/
close: function() {
if (this.connected) {
this.connected = false;
clearInterval(this.deltaIntervalId);
delete this.deltaIntervalId;
this.set('client', undefined);
}
},
/**
Do any extra work to destroy the object.
@method destructor
@return {undefined} Nothing.
*/
destructor: function() {
this.close(); // Make sure the setInterval is cleared!
},
/**
Receives messages from the client and dispatches them.
@method receive
@param {Object} data A hash of data sent from the client.
@return {undefined} Nothing.
*/
receive: function(data) {
// Make a shallow copy of the received data because handlers will mutate
// it to add an "err" or "result".
if (this.connected) {
this['performOp_' + data.op](Y.merge(data));
} else {
throw CLOSEDERROR;
}
},
/**
Handles login operations from the client. Called by "receive".
client.receive will receive all sent values back, transparently,
plus a "result" value that will be true or false, representing whether
the authentication succeeded or failed.
@method performOp_login
@param {Object} data A hash minimally of user and password.
@return {undefined} Nothing.
*/
performOp_login: function(data) {
data.result = this.get('state').login(data.user, data.password);
this.get('client').receive(data);
},
/**
Handles deploy operations from client. Called by receive.
client.receive will receive all sent values back, transparently.
If there is an error, the reply will also have an "err" with a string
describing the error.
@method performOp_deploy
@param {Object} data A hash minimally of charm_url, and optionally also
service_name, config, config_raw, and num_units.
@return {undefined} Nothing.
*/
performOp_deploy: function(data) {
var client = this.get('client');
var callback = function(result) {
if (result.error) {
data.err = result.error;
}
client.receiveNow(data);
};
this.get('state').deploy(data.charm_url, callback, {
name: data.service_name,
config: data.config,
configYAML: data.config_raw,
constraints: data.constraints,
unitCount: data.num_units
});
},
/**
Handles add unit operations from the client.
@method performOp_add_unit
@param {Object} data Contains service_name and num_units required for
adding additional units.
*/
performOp_add_unit: function(data) {
var res = this.get('state').addUnit(data.service_name, data.num_units);
if (res.error) {
data.err = res.error;
} else {
data.result = Y.Array.map(res.units, function(unit) {
return unit.id;
});
}
// respond with the new data or error
this.get('client').receiveNow(data);
},
/**
get_service from the client.
@method performOp_get_service
@param {Object} data contains service_name.
*/
performOp_get_service: function(data) {
OP(this, 'getService', ['service_name'], data);
},
/**
destroy_service from the client.
@method performOp_destroy_service
@param {Object} data contains service_name.
*/
performOp_destroy_service: function(data) {
OP(this, 'destroyService', ['service_name'], data);
},
/**
get_charm from the client.
@method performOp_get_charm
@param {Object} data contains service_name.
*/
performOp_get_charm: function(data) {
ASYNC_OP(this, 'getCharm', ['charm_url'])(data);
},
/**
set_constraints from the client.
@method performOp_set_constraints
@param {Object} data contains service_name and constraints as either a
key/value map or an array of "key=value" strings..
*/
performOp_set_constraints: function(data) {
OP(this, 'setConstraints', ['service_name', 'constraints'], data);
},
/**
set_config from the client.
@method performOp_set_config
@param {Object} data contains service_name and a config mapping
of key/value pairs.
*/
performOp_set_config: function(data) {
OP(this, 'setConfig', ['service_name', 'config'], data);
},
/**
* Update annotations rpc
*
* @method performOp_update_annotations
* @param {Object} data with entity name and payload.
*/
performOp_update_annotations: function(data) {
OP(this, 'updateAnnotations', ['entity', 'data'], data);
},
/**
* Perform 'resolved' operation.
* @method performOp_resolved
* @param {Object} data with unitName and optional relation name.
*/
performOp_resolved: function(data) {
OP(this, 'resolved', ['unit_name', 'relation_name'], data);
},
/**
* Perform 'export' operation.
* @method performOp_export
*/
performOp_exportEnvironment: function(data) {
OP(this, 'exportEnvironment', [], data);
},
/**
* Perform 'import' operation.
* @method performOp_importEnvironment
*/
performOp_importEnvironment: function(data) {
ASYNC_OP(this, 'importEnvironment', ['envData'])(data);
},
/**
* Perform 'importDeployer' operation.
* @method performOp_importDeployer
*/
performOp_importDeployer: function(data) {
ASYNC_OP(this, 'importDeployer', ['YAMLData', 'name'])(data);
// Explicitly trigger a delta after an import.
this.sendDelta();
},
/**
Handles the remove unit operations from the client
@method performOp_remove_unit
@param {Object} data Contains unit_names to remove and a calback.
*/
performOp_remove_units: function(data) {
var res = this.get('state').removeUnits(data.unit_names);
if (res.error && res.error.length > 0) {
data.err = res.error;
data.result = false;
} else {
data.result = true;
}
// respond with the new data or error
this.get('client').receiveNow(data);
},
/**
Handles exposing a service request from the client.
@method performOp_expose
@param {Object} data Contains service_name to expose and a callback.
*/
performOp_expose: function(data) {
var res = this.get('state').expose(data.service_name);
data.err = res.error;
data.result = (res.error === undefined);
this.get('client').receiveNow(data);
},
/**
Handles unexposing a service request from the client.
@method performOp_unexpose
@param {Object} data contains service_name to unexpose and a callback.
*/
performOp_unexpose: function(data) {
var res = this.get('state').unexpose(data.service_name);
data.err = res.error;
data.result = (res.error === undefined);
this.get('client').receiveNow(data);
},
/**
Handles adding a relation between two supplied services from the client
@method performOp_add_relation
@param {Object} data Object contains the operation, two endpoint strings
and request id.
*/
performOp_add_relation: function(data) {
var relation = this.get('state').addRelation(
data.endpoint_a, data.endpoint_b, true);
if (relation === false) {
// If everything checks out but could not create a new relation model
data.err = 'Unable to create relation';
this.get('client').receiveNow(data);
return;
}
if (relation.error) {
data.err = relation.error;
this.get('client').receive(data);
return;
}
// Normalize endpoints so that they are in the format
// serviceName: { name: 'interface-name' }
var epA = {}, epB = {};
epA[relation.endpoints[0][0]] = relation.endpoints[0][1];
epB[relation.endpoints[1][0]] = relation.endpoints[1][1];
data.result = {
endpoints: [epA, epB],
id: relation.relationId,
// interface is a reserved word
'interface': relation.type,
scope: relation.scope,
request_id: data.request_id
};
this.get('client').receive(data);
},
/**
Handles removing a relation between two supplied services from the client
@method performOp_remove_relation
@param {Object} data Object contains the operation, two endpoint strings
and request id.
*/
performOp_remove_relation: function(data) {
var relation = this.get('state').removeRelation(
data.endpoint_a, data.endpoint_b);
if (relation.error) {
data.err = relation.error;
} else {
data.result = true;
}
this.get('client').receive(data);
}
});
sandboxModule.PyJujuAPI = PyJujuAPI;
/**
A sandbox Juju environment using the Go API.
@class GoJujuAPI
*/
function GoJujuAPI(config) {
GoJujuAPI.superclass.constructor.apply(this, arguments);
}
GoJujuAPI.NAME = 'sandbox-go-juju-api';
GoJujuAPI.ATTRS = {
state: {},
client: {},
nextRequestId: {}, // The current outstanding "Next" RPC call ID.
deltaInterval: {value: 1000} // In milliseconds.
};
Y.extend(GoJujuAPI, Y.Base, {
/**
Initializes.
@method initializer
@return {undefined} Nothing.
*/
initializer: function() {
this.connected = false;
},
/**
Opens the connection to the sandbox Juju environment.
Called by ClientConnection, which sends itself.
@method open
@param {Object} client A ClientConnection.
@return {undefined} Nothing.
*/
open: function(client) {
if (!this.connected) {
this.connected = true;
this.set('client', client);
} else if (this.get('client') !== client) {
throw 'INVALID_STATE_ERR : Connection is open to another client.';
}
},
/**
Closes the connection to the sandbox Juju environment.
Called by ClientConnection.
@method close
@return {undefined} Nothing.
*/
close: function() {
if (this.connected) {
this.connected = false;
this.set('client', undefined);
}
},
/**
Do any extra work to destroy the object.
@method destructor
@return {undefined} Nothing.
*/
destructor: function() {
this.close();
},
/**
Receives messages from the client and dispatches them.
@method receive
@param {Object} data A hash of data sent from the client.
@return {undefined} Nothing.
*/
receive: function(data) {
if (this.connected) {
var client = this.get('client');
this['handle' + data.Type + data.Request](data,
client, this.get('state'));
} else {
throw CLOSEDERROR;
}
},
/**
Handle Login messages to the state object.
@method handleAdminLogin
@param {Object} data The contents of the API arguments.
@param {Object} client The active ClientConnection.
@param {Object} state An instance of FakeBackend.
@return {undefined} Side effects only.
*/
handleAdminLogin: function(data, client, state) {
data.Error = !state.login(data.Params.AuthTag, data.Params.Password);
client.receive(data);
},
/**
Handle GUIToken Login messages to the state object.
@method handleGUITokenLogin
@param {Object} data The contents of the API arguments.
@param {Object} client The active ClientConnection.
@param {Object} state An instance of FakeBackend.
@return {undefined} Side effects only.
*/
handleGUITokenLogin: function(data, client, state) {
var response = state.tokenlogin(data.Params.Token);
if (response) {
client.receive({
RequestId: data.RequestId,
Response: {AuthTag: response[0], Password: response[1]}
});
} else {
client.receive({
RequestId: data.RequestId,
Error: 'unknown, fulfilled, or expired token',
ErrorCode: 'unauthorized access',
Response: {}
});
}
},
/**
Handle EnvironmentInfo messages.
@method handleClientEnvironmentInfo
@param {Object} data The contents of the API arguments.
@param {Object} client The active ClientConnection.
@param {Object} state An instance of FakeBackend.
@return {undefined} Side effects only.
*/
handleClientEnvironmentInfo: function(data, client, state) {
client.receive({
RequestId: data.RequestId,
Response: {
ProviderType: state.get('providerType'),
DefaultSeries: state.get('defaultSeries'),
Name: 'Sandbox'
}
});
},
/**
Handle EnvironmentGet messages.
@method handleClientEnvironmentGet
@param {Object} data The contents of the API arguments.
@param {Object} client The active ClientConnection.
@param {Object} state An instance of FakeBackend.
*/
handleClientEnvironmentGet: function(data, client, state) {
client.receive({
RequestId: data.RequestId,
Response: {
// For now only the MAAS server is required by the GUI.
Config: {'maas-server': state.get('maasServer')}
}
});
},
/**
A white-list for model attributes. This translates them from the raw ATTRS
to the format expected by the environment's delta handling.
@property _deltaWhitelist
@type {Object}
*/
_deltaWhitelist: {
service: {
Name: 'id',
Exposed: 'exposed',
CharmURL: 'charm',
Life: 'life',
'Constraints': function(attrs) {
var constraints = attrs.constraints || {};
// Since juju-core sends the tags constraint as a list of strings,
// we need to convert the value to an array.
var tags = constraints.tags;
if (tags) {
constraints.tags = tags.split(',');
}
return constraints;
},
Config: 'config',
Subordinate: 'subordinate'
},
machine: {
Id: 'id',
Addresses: 'addresses',
InstanceId: 'instance_id',
Status: 'agent_state',
StateInfo: 'agent_status_info',
StatusData: 'agent_state_data',
'HardwareCharacteristics': function(attrs) {
var hardware = attrs.hardware || {};
return {
Arch: hardware.arch,
CpuCores: hardware.cpuCores,
CpuPower: hardware.cpuPower,
Mem: hardware.mem,
RootDisk: hardware.disk
};
},
Jobs: 'jobs',
Life: 'life',
Series: 'series',
SupportedContainers: 'supportedContainers',
'SupportedContainersKnown': function() {
return true;
}
},
unit: {
Name: 'id',
'Service': 'service',
'Series': function(attrs, self) {
var db = self.get('state').db;
var service = db.services.getById(attrs.service);
if (service) {
var charm = db.charms.getById(service.get('charm'));
return charm.get('series');
} else {
return null; // Probably unit/service was deleted.
}
},
'CharmURL': function(attrs, self) {
var db = self.get('state').db;
var service = db.services.getById(attrs.service);
if (service) {
return service.get('charm');
} else {
return null; // Probably unit/service was deleted.
}
},
PublicAddress: 'public_address',
PrivateAddress: 'private_address',
MachineId: 'machine',
Ports: 'open_ports',
Status: 'agent_state',
StatusInfo: 'agent_state_info',
StatusData: 'agent_state_data',
Subordinate: 'subordinate'
},
relation: {
Key: 'relation_id',
'Endpoints': function(relation, goAPI) {
var result = [];
if (relation.endpoints.length === 1) {
return;
}
relation.endpoints.forEach(function(endpoint, index) {
result.push({
Relation: {
Name: endpoint[1].name,
Role: (index) ? 'server' : 'client',
Interface: relation.type,
Scope: relation.scope
},
ServiceName: endpoint[0]
});
});
return result;
}
},
annotation: {
'Tag': function(entity) {
return entity.id;
},
'Annotations': function(entity) {
return entity.annotations;
}
}
},
/**
Given attrs or a model object and a whitelist of desired attributes,