Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / src / ssh_keys / sshKeySource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 import $ from 'jquery';
20 let Utils = require('utils/utils.js');
21 let rw = require('utils/rw.js');
22 const API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
23 const API_PORT = require('utils/rw.js').getSearchParams(window.location).api_port;
24 export default function(Alt){
25 const Actions = Alt.actions.global;
26 return {
27 getSshKey: {
28 remote (state) {
29 return new Promise((resolve,reject) => {
30 $.ajax({
31 url: 'api/ssh-key?api_server=' + API_SERVER,
32 type: 'GET',
33 beforeSend: Utils.addAuthorizationStub,
34 success: function (data) {
35 resolve(data);
36 }
37 }).fail(function(xhr){
38 console.log(xhr)
39 //Authentication and the handling of fail states should be wrapped up into a connection class.
40 Utils.checkAuthentication(xhr.status);
41 });
42 });
43 },
44 loading: Actions.getSshKeyLoading,
45 success: Actions.getSshKeySuccess,
46 error: Actions.getSshKeyError
47 },
48 saveSshKey: {
49 remote (state, pair) {
50 return new Promise((resolve, reject) => {
51 $.ajax({
52 url: 'api/ssh-key?api_server=' + API_SERVER,
53 type: 'POST',
54 beforeSend: Utils.addAuthorizationStub,
55 data: {
56 "key-pair": [pair]
57 },
58 success: function (data) {
59 resolve(pair);
60 },
61 error: function (err) {
62 console.log('There was an error saving the ssh key')
63 }
64 }).fail(function(xhr){
65 //Authentication and the handling of fail states should be wrapped up into a connection class.
66 Utils.checkAuthentication(xhr.status);
67 reject();
68 });
69 })
70 },
71 loading: Actions.saveSshKeyLoading,
72 success: Actions.saveSshKeySuccess,
73 error: Actions.saveSshKeyError
74 },
75 updateSshKey: {
76 remote (state, pair) {
77 return new Promise((resolve, reject) => {
78 $.ajax({
79 url: 'api/ssh-key?api_server=' + API_SERVER,
80 type: 'PUT',
81 beforeSend: Utils.addAuthorizationStub,
82 data: {
83 "key-pair": [pair]
84 },
85 success: function (data) {
86 resolve(pair);
87 },
88 error: function (err) {
89 console.log('There was an error updating the ssh key')
90 }
91 }).fail(function(xhr){
92 //Authentication and the handling of fail states should be wrapped up into a connection class.
93 Utils.checkAuthentication(xhr.status);
94 reject();
95 });
96 })
97 },
98 loading: Actions.saveSshKeyLoading,
99 success: Actions.updateSshKeySuccess,
100 error: Actions.saveSshKeyError
101 },
102 deleteSshKey: {
103 remote (state, name) {
104 return new Promise((resolve, reject) => {
105 $.ajax({
106 url: 'api/ssh-key/'+ encodeURI(name) + '?api_server=' + API_SERVER,
107 type: 'DELETE',
108 beforeSend: Utils.addAuthorizationStub,
109 success: function (data) {
110 resolve({
111 'status':'success',
112 name: name
113 });
114 },
115 error: function (err) {
116 console.log('There was an error updating the ssh key')
117 }
118 }).fail(function(xhr){
119 //Authentication and the handling of fail states should be wrapped up into a connection class.
120 Utils.checkAuthentication(xhr.status);
121 reject();
122 });
123 })
124 },
125 loading: Actions.saveSshKeyLoading,
126 success: Actions.deleteSshKeySuccess,
127 error: Actions.saveSshKeyError
128 }
129 }
130 }