b25b3254ea814b0364ad02f449977578151bb326
[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  * 
20  *
21  */
22 var API_SERVER = rw.getSearchParams(window.location).api_server;
23 var NODE_PORT = 3000;
24 var createConfigAgentAccountActions = require('./configAgentAccountActions.js');
25 var Utils = require('../../utils/utils.js');
26 import $ from 'jquery';
27 var createConfigAgentAccountSource = {
28   /**
29    * Creates a new Config Agent Account
30    * @param  {object} state Reference to parent store state.
31    * @param  {object} configAgentAccount Config Agent Account payload. Should resemble the following:
32    *                               {
33    *                                 "name": "Config-Agent-Account-One",
34    *                                 "config-agent-type":"type",
35    *                                 "type (juju)": {
36    *                                   "Type specific options"
37    *                                 }
38    *                               }
39    * @return {[type]}              [description]
40    */
41   create: function() {
42
43     return {
44       remote: function(state, configAgentAccount) {
45         return new Promise(function(resolve, reject) {
46           $.ajax({
47             url: '//' + window.location.hostname + ':3000/launchpad/config-agent-account?api_server=' + API_SERVER,
48             type:'POST',
49             beforeSend: Utils.addAuthorizationStub,
50             data: JSON.stringify(configAgentAccount),
51             contentType: "application/json",
52             success: function(data) {
53               resolve(data);
54             },
55             error: function(error) {
56               console.log("There was an error creating the config agent account: ", arguments);
57               reject(error);
58             }
59           }).fail(function(xhr){
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.createSuccess,
67       loading: createConfigAgentAccountActions.createLoading,
68       error: createConfigAgentAccountActions.createFail
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;