c54c9ae276777980afde32b6283694ed2171c095
[osm/riftware.git] /
1
2 /*
3  * STANDARD_RIFT_IO_COPYRIGHT
4  */
5 var API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
6 var NODE_PORT = 3000;
7 var createConfigAgentAccountActions = require('./configAgentAccountActions.js');
8 var Utils = require('utils/utils.js');
9 import $ from 'jquery';
10 var createConfigAgentAccountSource = {
11   /**
12    * Creates a new Config Agent Account
13    * @param  {object} state Reference to parent store state.
14    * @param  {object} configAgentAccount Config Agent Account payload. Should resemble the following:
15    *                               {
16    *                                 "name": "Config-Agent-Account-One",
17    *                                 "config-agent-type":"type",
18    *                                 "type (juju)": {
19    *                                   "Type specific options"
20    *                                 }
21    *                               }
22    * @return {[type]}              [description]
23    */
24   create: function() {
25
26     return {
27       remote: function(state, configAgentAccount) {
28         return new Promise(function(resolve, reject) {
29           $.ajax({
30             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account?api_server=' + API_SERVER,
31             type:'POST',
32             beforeSend: Utils.addAuthorizationStub,
33             data: JSON.stringify(configAgentAccount),
34             contentType: "application/json",
35             success: function(data) {
36               resolve(data);
37             }
38             ,error: function(error) {
39               console.log("There was an error creating the config agent account: ", arguments);
40               reject(error);
41             }
42           }).fail(function(xhr){
43             console.log('checking authentication');
44             //Authentication and the handling of fail states should be wrapped up into a connection class.
45             Utils.checkAuthentication(xhr.status);
46           });
47
48         });
49       },
50       success: createConfigAgentAccountActions.createConfigAccountSuccess,
51       loading: createConfigAgentAccountActions.createConfigAccountLoading,
52       error: createConfigAgentAccountActions.createConfigAccountFailed
53     }
54   },
55
56   /**
57    * Updates a Config Agent Account
58    * @param  {object} state Reference to parent store state.
59    * @param  {object} configAgentAccount Config Agent Account payload. Should resemble the following:
60    *                               {
61    *                                 "name": "Config-Agent-Account-One",
62    *                                 "config-agent-type":"type",
63    *                                 "type (juju)": {
64    *                                   "Type specific options"
65    *                                 }
66    *                               }
67    * @return {[type]}              [description]
68    */
69   update: function() {
70
71     return {
72       remote: function(state, configAgentAccount) {
73         return new Promise(function(resolve, reject) {
74           $.ajax({
75             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account/' + configAgentAccount.name + '?api_server=' + API_SERVER,
76             type:'PUT',
77             beforeSend: Utils.addAuthorizationStub,
78             data: JSON.stringify(configAgentAccount),
79             contentType: "application/json",
80             success: function(data) {
81               resolve(data);
82             },
83             error: function(error) {
84               console.log("There was an error updating the config agent account: ", configAgentAccount.name, error);
85               reject(error);
86             }
87           }).fail(function(xhr){
88             //Authentication and the handling of fail states should be wrapped up into a connection class.
89             Utils.checkAuthentication(xhr.status);
90           });
91         });
92       },
93       success: createConfigAgentAccountActions.updateSuccess,
94       loading: createConfigAgentAccountActions.updateLoading,
95       error: createConfigAgentAccountActions.updateFail
96     }
97   },
98
99   /**
100    * Deletes a Config Agent Account
101    * @param  {object} state Reference to parent store state.
102    * @param  {object} configAgentAccount configAgentAccount to delete
103    * @return {[type]}              [description]
104    */
105   delete: function() {
106
107     return {
108       remote: function(state, configAgentAccount, cb) {
109         return new Promise(function(resolve, reject) {
110           $.ajax({
111             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account/' + configAgentAccount + '?api_server=' + API_SERVER,
112             type:'DELETE',
113             beforeSend: Utils.addAuthorizationStub,
114             success: function(data) {
115               resolve({data:data, cb:cb});
116             },
117             error: function(error) {
118               console.log("There was an error deleting the config agent account: ", configAgentAccount, error);
119               reject(error);
120             }
121           }).fail(function(xhr){
122             //Authentication and the handling of fail states should be wrapped up into a connection class.
123             Utils.checkAuthentication(xhr.status);
124           });
125         });
126       },
127       success: createConfigAgentAccountActions.deleteSuccess,
128       loading: createConfigAgentAccountActions.updateLoading,
129       error: createConfigAgentAccountActions.deleteFail
130     }
131   },
132   /**
133    * Get a config agent account
134    *
135    * @return {Promise}
136    */
137   getConfigAgentAccount: function() {
138     return {
139       remote: function(state, configAgentAccount) {
140         return new Promise(function(resolve, reject) {
141           $.ajax({
142             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account/' + configAgentAccount + '?api_server=' + API_SERVER,
143             type: 'GET',
144             beforeSend: Utils.addAuthorizationStub,
145             success: function(data) {
146               resolve({
147                 configAgentAccount: data
148               });
149             },
150             error: function(error) {
151               console.log('There was an error getting configAgentAccount', error);
152               reject(error);
153             }
154           }).fail(function(xhr){
155             //Authentication and the handling of fail states should be wrapped up into a connection class.
156             Utils.checkAuthentication(xhr.status);
157           });
158         });
159       },
160       success: createConfigAgentAccountActions.getConfigAgentAccountSuccess,
161       error: createConfigAgentAccountActions.getConfigAgentAccountFail
162     }
163   },
164   getConfigAgentAccounts: function() {
165     return {
166       remote: function() {
167         return new Promise(function(resolve, reject) {
168           $.ajax({
169             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account?api_server=' + API_SERVER,
170             type: 'GET',
171             beforeSend: Utils.addAuthorizationStub,
172             success: function(configAgentAccounts) {
173               resolve(configAgentAccounts);
174             }
175           }).fail(function(xhr){
176             //Authentication and the handling of fail states should be wrapped up into a connection class.
177             Utils.checkAuthentication(xhr.status);
178           });
179         });
180       },
181       success: createConfigAgentAccountActions.getConfigAgentAccountsSuccess,
182       error: createConfigAgentAccountActions.getConfigAgentAccountsFail
183     }
184   }
185 };
186
187 module.exports = createConfigAgentAccountSource;