Revert "BUG-410 -- update RIFT platform"
[osm/UI.git] / skyquake / plugins / launchpad / src / ssh_keys / sshKeyStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 import SshKeyActions from './sshKeyActions.js';
19 import SshKeySource from './sshKeySource.js';
20 import GUID from 'utils/guid.js';
21 import AppHeaderActions from 'widgets/header/headerActions.js';
22 import Alt from '../alt';
23 import _cloneDeep from 'lodash/cloneDeep';
24 import _merge from 'lodash/merge';
25
26
27 export default class SshKeyStore {
28 constructor() {
29 this.data = {
30 keys: [],
31 entities: {}
32 };
33 this.dataCache = _cloneDeep(this.data);
34 this.newKey = {
35 name: '',
36 key: ''
37 };
38 this.bindActions(SshKeyActions);
39 this.registerAsync(SshKeySource);
40 this.exportPublicMethods({
41 updateNewKeyValue: this.updateNewKeyValue,
42 updateSshKeyPair: this.updateSshKeyPair,
43 editSshKeyPair: this.editSshKeyPair,
44 cancelEditSshKeyPair: this.cancelEditSshKeyPair,
45 saveEditSshKeyPair: this.saveEditSshKeyPair,
46 deleteSshKeyPair: this.deleteSshKeyPair,
47 updateEditSshKeyPair: this.updateEditSshKeyPair
48 });
49 }
50 updateNewKeyValue = (k, field) => {
51 let self = this;
52 return function(e) {
53 let value = e.target.value;
54 let ref = self.newKey;
55 ref[field] = value;
56 self.setState({newKey: ref});
57 }
58 }
59 editSshKeyPair = (k) => {
60 let self = this;
61 return function(e) {
62 let data = self.data;
63 let ref = data.entities[k];
64 ref.isEditable = true;
65 self.setState({data:data});
66 }
67 }
68 cancelEditSshKeyPair = (k) => {
69 let self = this;
70 return function(e) {
71 let data = _cloneDeep(self.data);
72 data.entities[k].key = self.dataCache.entities[k].key;
73 data.entities[k].isEditable = false;
74 self.setState({data:data});
75 }
76 }
77 saveEditSshKeyPair = (data) => {
78 let self = this;
79 return function(e) {
80 if(self.validate(data)) {
81 self.getInstance().saveSshKey(self.trimName(data)).then(function() {})
82 } else {
83 self.alt.actions.global.showNotification('Make sure all fields are filled in and that you are using only alphanumeric characters for the name');
84 }
85 }
86 }
87 updateEditSshKeyPair = (k) => {
88 let self = this;
89 return function(e) {
90 self.getInstance().updateSshKey(self.cleanUpKeyPayload(self.data.entities[k]))
91 }
92 }
93 deleteSshKeyPair = (k) => {
94 let self = this;
95 return function(e) {
96 if(window.confirm('Are you sure you want to delete this key?')) {
97 self.getInstance().deleteSshKey(k);
98 }
99 }
100 }
101 saveSshKeySuccess = (data) => {
102 let keys = this.data;
103 keys.keys.push(data.name);
104 keys.entities[data.name] = {
105 name: data.name,
106 key: data.key,
107 isEditable: false
108 };
109 this.setState({
110 dataCache: _cloneDeep(keys),
111 data: keys,
112 newKey: {
113 name: '',
114 key: ''
115 }
116 })
117 }
118 updateSshKeySuccess = (data) => {
119 let keys = this.data;
120 keys.entities[data.name] = {
121 name: data.name,
122 key: data.key,
123 isEditable: false
124 };
125 this.setState({
126 dataCache: _cloneDeep(keys),
127 data: keys,
128 newKey: {
129 name: '',
130 key: ''
131 }
132 })
133 }
134 deleteSshKeySuccess = (data) => {
135 let keys = this.data;
136 keys.keys.splice(keys.keys.indexOf(data.name), 1);
137 delete keys.entities[data.name];
138 this.setState({
139 dataCache: _cloneDeep(keys),
140 data: keys
141 })
142 }
143 saveEditSshKeyPairSuccess = () => {
144
145 }
146 getSshKeySuccess = (data) => {
147 let flattened = this.flattenKeys(data);
148 this.setState({
149 data: flattened,
150 dataCache: _cloneDeep(flattened)
151 })
152 }
153 updateSshKeyPair = (k, field) => {
154 let self = this;
155 return function(e) {
156 let value = e.target.value;
157 let data = self.data;
158 let ref = data.entities[k];
159 ref[field] = value;
160 self.setState({data:data});
161 }
162 }
163 flattenKeys(data) {
164 var fd = {
165 keys:[],
166 entities: {}
167 };
168 data && data.map(function(d){
169 fd.keys.push(d.name);
170 fd.entities[d.name] = _merge({isEditable: false}, d)
171 });
172 return fd;
173 }
174 cleanUpKeyPayload(payload) {
175 return {key: payload.key, name: payload.name};
176 }
177 validate(data) {
178 if((data['name'].trim() == '') || ((/[^\w _-]/).test(data['name']))) {
179 return false;
180 }
181 return true;
182 }
183 trimName(data) {
184 data.name = data.name.trim();
185 return data;
186 }
187
188 }