OLD | NEW |
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 /** | 3 /** |
4 An in-memory fake Juju backend and supporting elements. | 4 An in-memory fake Juju backend and supporting elements. |
5 | 5 |
6 @module env | 6 @module env |
7 @submodule env.fakebackend | 7 @submodule env.fakebackend |
8 */ | 8 */ |
9 | 9 |
10 YUI.add('juju-env-fakebackend', function(Y) { | 10 YUI.add('juju-env-fakebackend', function(Y) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 Y.Object.isEmpty(this.annotations.relations) && | 197 Y.Object.isEmpty(this.annotations.relations) && |
198 Y.Object.isEmpty(this.annotations.annotations)) { | 198 Y.Object.isEmpty(this.annotations.annotations)) { |
199 result = null; | 199 result = null; |
200 } else { | 200 } else { |
201 result = this.annotations; | 201 result = this.annotations; |
202 this._resetAnnotations(); | 202 this._resetAnnotations(); |
203 } | 203 } |
204 return result; | 204 return result; |
205 }, | 205 }, |
206 | 206 |
| 207 /** |
| 208 Takes two string endpoints and splits it into usable parts. |
| 209 |
| 210 @method parseEndpointStrings |
| 211 @param {Array} endpoints an array of endpoint strings |
| 212 to split in the format wordpress:db. |
| 213 @return {Object} A hash with four keys: service (the associated |
| 214 service model), charm (the associated charm model for the |
| 215 service), name (the user-defined service name), and type (the |
| 216 charm-author-defined relation type name). |
| 217 */ |
| 218 parseEndpointStrings: function(endpoints) { |
| 219 return Y.Array.map(endpoints, |
| 220 function(endpoint) { |
| 221 var epData = endpoint.split(':'), |
| 222 result = { name: epData[0], type: epData[1] }; |
| 223 result.service = this.db.services.getById(result.name); |
| 224 if (result.service) { |
| 225 result.charm = this.db.charms.getById( |
| 226 result.service.get('charm')); |
| 227 } |
| 228 return result; |
| 229 }, this); |
| 230 }, |
| 231 |
207 | 232 |
208 /** | 233 /** |
209 Attempt to log a user in. | 234 Attempt to log a user in. |
210 | 235 |
211 @method login | 236 @method login |
212 @param {String} username The id of the user. | 237 @param {String} username The id of the user. |
213 @param {String} submittedPassword The user-submitted password. | 238 @param {String} submittedPassword The user-submitted password. |
214 @return {Bool} True if the authentication was successful. | 239 @return {Bool} True if the authentication was successful. |
215 */ | 240 */ |
216 login: function(username, submittedPassword) { | 241 login: function(username, submittedPassword) { |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 | 714 |
690 return { | 715 return { |
691 error: error, | 716 error: error, |
692 warning: warning | 717 warning: warning |
693 }; | 718 }; |
694 }, | 719 }, |
695 | 720 |
696 /** | 721 /** |
697 Add a relation between two services. | 722 Add a relation between two services. |
698 | 723 |
699 @method add_relation | 724 @method addRelation |
700 @param {String} endpointA A string representation of the service name | 725 @param {String} endpointA A string representation of the service name |
701 and endpoint connection type ie) wordpress:db. | 726 and endpoint connection type ie) wordpress:db. |
702 @param {String} endpointB A string representation of the service name | 727 @param {String} endpointB A string representation of the service name |
703 and endpoint connection type ie) wordpress:db. | 728 and endpoint connection type ie) wordpress:db. |
704 */ | 729 */ |
705 addRelation: function(endpointA, endpointB) { | 730 addRelation: function(endpointA, endpointB) { |
706 if (!this.get('authenticated')) { | 731 if (!this.get('authenticated')) { |
707 return UNAUTHENTICATEDERROR; | 732 return UNAUTHENTICATEDERROR; |
708 } | 733 } |
709 if ((typeof endpointA !== 'string') || | 734 if ((typeof endpointA !== 'string') || |
710 (typeof endpointB !== 'string')) { | 735 (typeof endpointB !== 'string')) { |
711 return {error: 'Two string endpoint names' + | 736 return {error: 'Two string endpoint names' + |
712 ' required to establish a relation'}; | 737 ' required to establish a relation'}; |
713 } | 738 } |
714 | 739 |
715 var endpointData = Y.Array.map( | 740 // Parses the endpoint strings to extract all required data. |
716 [endpointA, endpointB], | 741 var endpointData = this.parseEndpointStrings([endpointA, endpointB]); |
717 /** | |
718 Takes a string endpoint and splits it into usable parts. | |
719 @method endpointSplit | |
720 @param {String} endpoint the endpoint to split in the format | |
721 wordpress:db. | |
722 @return {Object} A hash with four keys: service (the associated | |
723 service model), charm (the associated charm model for the | |
724 service), name (the user-defined service name), and type (the | |
725 charm-author-defined relation type name). | |
726 */ | |
727 function(endpoint) { | |
728 var epData = endpoint.split(':'), | |
729 result = { name: epData[0], type: epData[1] }; | |
730 result.service = this.db.services.getById(result.name); | |
731 if (result.service) { | |
732 result.charm = this.db.charms.getById( | |
733 result.service.get('charm')); | |
734 } | |
735 return result; | |
736 }, | |
737 this); | |
738 | 742 |
739 // This error should never be hit but it's here JIC | 743 // This error should never be hit but it's here JIC |
740 if (!endpointData[0].charm || !endpointData[1].charm) { | 744 if (!endpointData[0].charm || !endpointData[1].charm) { |
741 return {error: 'Charm not loaded.'}; | 745 return {error: 'Charm not loaded.'}; |
742 } | 746 } |
743 // If there are matching interfaces this will contain an object of the | 747 // If there are matching interfaces this will contain an object of the |
744 // charm interface type and scope (if supplied). | 748 // charm interface type and scope (if supplied). |
745 var match = findMatch(endpointData); | 749 var match = findMatch(endpointData); |
746 | 750 |
747 // If there is an error fetching a valid interface and scope | 751 // If there is an error fetching a valid interface and scope |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 displayName: endpointData[0].type, | 792 displayName: endpointData[0].type, |
789 relation: relation | 793 relation: relation |
790 }; | 794 }; |
791 } | 795 } |
792 | 796 |
793 // Fallback error If the relation was not able to be created | 797 // Fallback error If the relation was not able to be created |
794 // for any reason other than what has already been checked for. | 798 // for any reason other than what has already been checked for. |
795 return false; | 799 return false; |
796 }, | 800 }, |
797 | 801 |
| 802 /** |
| 803 Removes a relation between two services. |
| 804 |
| 805 @method removeRelation |
| 806 @param {String} endpointA A string representation of the service name |
| 807 and endpoint connection type ie) wordpress:db. |
| 808 @param {String} endpointB A string representation of the service name |
| 809 and endpoint connection type ie) wordpress:db. |
| 810 */ |
| 811 removeRelation: function(endpointA, endpointB) { |
| 812 if (!this.get('authenticated')) { |
| 813 return UNAUTHENTICATEDERROR; |
| 814 } |
| 815 if ((typeof endpointA !== 'string') || |
| 816 (typeof endpointB !== 'string')) { |
| 817 return {error: 'Two string endpoint names' + |
| 818 ' required to establish a relation'}; |
| 819 } |
| 820 |
| 821 // Parses the endpoint strings to extract all required data. |
| 822 var endpointData = this.parseEndpointStrings([endpointA, endpointB]); |
| 823 |
| 824 // This error should never be hit but it's here JIC |
| 825 if (!endpointData[0].charm || !endpointData[1].charm) { |
| 826 return {error: 'Charm not loaded.'}; |
| 827 } |
| 828 |
| 829 var relation; |
| 830 this.db.relations.some(function(rel) { |
| 831 var endpoints = rel.getAttrs().endpoints; |
| 832 return [0, 1].some(function(index) { |
| 833 // Check to see if the service names match an existing relation |
| 834 if ((endpoints[index][0] === endpointData[0].name) && |
| 835 (endpoints[!index + 0][0] === endpointData[1].name)) { |
| 836 // Check to see if the interface names match |
| 837 if ((endpoints[index][1].name === endpointData[0].type) && |
| 838 (endpoints[!index + 0][1].name === endpointData[1].type)) { |
| 839 relation = rel; |
| 840 return true; |
| 841 } |
| 842 } |
| 843 }); |
| 844 }); |
| 845 |
| 846 if (relation) { |
| 847 // remove the relation from the relation db model list |
| 848 var result = this.db.relations.remove(relation); |
| 849 // add this change to the delta |
| 850 this.changes.relations[relation.get('id')] = [relation, false]; |
| 851 return result; |
| 852 } else { |
| 853 return {error: 'Relationship does not exist'}; |
| 854 } |
| 855 }, |
| 856 |
798 // updateAnnotations: function() { | 857 // updateAnnotations: function() { |
799 | 858 |
800 // }, | 859 // }, |
801 | 860 |
802 // getAnnotations: function() { | 861 // getAnnotations: function() { |
803 | 862 |
804 // }, | 863 // }, |
805 | 864 |
806 // removeAnnotations: function() { | 865 // removeAnnotations: function() { |
807 | 866 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 models.setAnnotations(entity, annotations); | 977 models.setAnnotations(entity, annotations); |
919 | 978 |
920 // Arrange delta stream updates. | 979 // Arrange delta stream updates. |
921 var annotationGroup = this._getAnnotationGroup(entity); | 980 var annotationGroup = this._getAnnotationGroup(entity); |
922 // Note that we pass true here, even removing an annotation | 981 // Note that we pass true here, even removing an annotation |
923 // is recorded as an object change/update. | 982 // is recorded as an object change/update. |
924 this.annotations[annotationGroup][entityName] = [entity, true]; | 983 this.annotations[annotationGroup][entityName] = [entity, true]; |
925 return {result: true}; | 984 return {result: true}; |
926 }, | 985 }, |
927 | 986 |
928 // removeRelation: function() { | 987 /** |
| 988 Sets the configuration settings on the supplied service to the supplied |
| 989 config object while leaving the settings untouched if they are not in the |
| 990 supplied config. |
929 | 991 |
930 // }, | 992 @method setConfig |
931 | 993 @param {String} serviceName the service id. |
| 994 @param {Object} config properties to set. |
| 995 */ |
932 setConfig: function(serviceName, config) { | 996 setConfig: function(serviceName, config) { |
933 if (!this.get('authenticated')) { | 997 if (!this.get('authenticated')) { |
934 return UNAUTHENTICATEDERROR; | 998 return UNAUTHENTICATEDERROR; |
935 } | 999 } |
936 var service = this.db.services.getById(serviceName); | 1000 var service = this.db.services.getById(serviceName); |
937 if (!service) { | 1001 if (!service) { |
938 return {error: 'Service "' + serviceName + '" does not exist.'}; | 1002 return {error: 'Service "' + serviceName + '" does not exist.'}; |
939 } | 1003 } |
940 | 1004 |
941 var existing = service.get('config'); | 1005 var existing = service.get('config'); |
942 if (!existing) { | 1006 if (!existing) { |
943 existing = {}; | 1007 existing = {}; |
944 } | 1008 } |
945 | 1009 |
946 if (!config) { | 1010 if (!config) { |
947 config = {}; | 1011 config = {}; |
948 } | 1012 } |
949 // Merge new constraints in. | 1013 // Merge new constraints in. |
950 existing = Y.mix(existing, config, true, undefined, 0, true); | 1014 existing = Y.mix(existing, config, true, undefined, 0, true); |
951 //TODO: validate the config. | 1015 //TODO: validate the config. |
952 // Reassign the attr. | 1016 // Reassign the attr. |
953 service.set('config', existing); | 1017 service.set('config', existing); |
954 // The callback indicates done, we can pass anything back. | 1018 // The callback indicates done, we can pass anything back. |
955 this.changes.services[service.get('id')] = [service, true]; | 1019 this.changes.services[service.get('id')] = [service, true]; |
956 return {result: existing}; | 1020 return {result: existing}; |
957 }, | 1021 }, |
958 | 1022 |
| 1023 /** |
| 1024 Sets the constraints on a service to restrict the type of machine to be |
| 1025 used for the service. |
| 1026 |
| 1027 @method setConstraints |
| 1028 @param {String} serviceName the service id. |
| 1029 @param {Object | Array} data either an array of strings "foo=bar" or an |
| 1030 object {foo: 'bar'}. |
| 1031 */ |
959 setConstraints: function(serviceName, data) { | 1032 setConstraints: function(serviceName, data) { |
960 var constraints = {}; | 1033 var constraints = {}; |
961 | 1034 |
962 if (!this.get('authenticated')) { | 1035 if (!this.get('authenticated')) { |
963 return UNAUTHENTICATEDERROR; | 1036 return UNAUTHENTICATEDERROR; |
964 } | 1037 } |
965 var service = this.db.services.getById(serviceName); | 1038 var service = this.db.services.getById(serviceName); |
966 if (!service) { | 1039 if (!service) { |
967 return {error: 'Service "' + serviceName + '" does not exist.'}; | 1040 return {error: 'Service "' + serviceName + '" does not exist.'}; |
968 } | 1041 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 | 1107 |
1035 Y.namespace('juju.environments').FakeBackend = FakeBackend; | 1108 Y.namespace('juju.environments').FakeBackend = FakeBackend; |
1036 | 1109 |
1037 }, '0.1.0', { | 1110 }, '0.1.0', { |
1038 requires: [ | 1111 requires: [ |
1039 'base', | 1112 'base', |
1040 'js-yaml', | 1113 'js-yaml', |
1041 'juju-models' | 1114 'juju-models' |
1042 ] | 1115 ] |
1043 }); | 1116 }); |
OLD | NEW |