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