400c0968e32aea583b39b95f98654b307f1ef5cc
[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 _ from 'lodash';
24
25
26 export default class SshKeyStore {
27 constructor() {
28 this.data = {
29 keys: [],
30 entities: {}
31 };
32 this.dataCache = _.cloneDeep(this.data);
33 this.newKey = {
34 name: '',
35 key: ''
36 };
37 this.bindActions(SshKeyActions);
38 this.registerAsync(SshKeySource);
39 this.exportPublicMethods({
40 updateNewKeyValue: this.updateNewKeyValue,
41 updateSshKeyPair: this.updateSshKeyPair,
42 editSshKeyPair: this.editSshKeyPair,
43 cancelEditSshKeyPair: this.cancelEditSshKeyPair,
44 saveEditSshKeyPair: this.saveEditSshKeyPair,
45 deleteSshKeyPair: this.deleteSshKeyPair,
46 updateEditSshKeyPair: this.updateEditSshKeyPair
47 });
48 }
49 updateNewKeyValue = (k, field) => {
50 let self = this;
51 return function(e) {
52 let value = e.target.value;
53 let ref = self.newKey;
54 ref[field] = value;
55 self.setState({newKey: ref});
56 }
57 }
58 editSshKeyPair = (k) => {
59 let self = this;
60 return function(e) {
61 let data = self.data;
62 let ref = data.entities[k];
63 ref.isEditable = true;
64 self.setState({data:data});
65 }
66 }
67 cancelEditSshKeyPair = (k) => {
68 let self = this;
69 return function(e) {
70 let data = _.cloneDeep(self.data);
71 data.entities[k].key = self.dataCache.entities[k].key;
72 data.entities[k].isEditable = false;
73 self.setState({data:data});
74 }
75 }
76 saveEditSshKeyPair = (data) => {
77 let self = this;
78 return function(e) {
79 if(self.validate(data)) {
80 self.getInstance().saveSshKey(self.trimName(data)).then(function() {})
81 } else {
82 self.alt.actions.global.showNotification('Make sure all fields are filled in and that you are using only alphanumeric characters for the name');
83 }
84 }
85 }
86 updateEditSshKeyPair = (k) => {
87 let self = this;
88 return function(e) {
89 self.getInstance().updateSshKey(self.cleanUpKeyPayload(self.data.entities[k]))
90 }
91 }
92 deleteSshKeyPair = (k) => {
93 let self = this;
94 return function(e) {
95 if(window.confirm('Are you sure you want to delete this key?')) {
96 self.getInstance().deleteSshKey(k);
97 }
98 }
99 }
100 saveSshKeySuccess = (data) => {
101 let keys = this.data;
102 keys.keys.push(data.name);
103 keys.entities[data.name] = {
104 name: data.name,
105 key: data.key,
106 isEditable: false
107 };
108 this.setState({
109 dataCache: _.cloneDeep(keys),
110 data: keys,
111 newKey: {
112 name: '',
113 key: ''
114 }
115 })
116 }
117 updateSshKeySuccess = (data) => {
118 let keys = this.data;
119 keys.entities[data.name] = {
120 name: data.name,
121 key: data.key,
122 isEditable: false
123 };
124 this.setState({
125 dataCache: _.cloneDeep(keys),
126 data: keys,
127 newKey: {
128 name: '',
129 key: ''
130 }
131 })
132 }
133 deleteSshKeySuccess = (data) => {
134 let keys = this.data;
135 keys.keys.splice(keys.keys.indexOf(data.name), 1);
136 delete keys.entities[data.name];
137 this.setState({
138 dataCache: _.cloneDeep(keys),
139 data: keys
140 })
141 }
142 saveEditSshKeyPairSuccess = () => {
143
144 }
145 getSshKeySuccess = (data) => {
146 let flattened = this.flattenKeys(data);
147 this.setState({
148 data: flattened,
149 dataCache: _.cloneDeep(flattened)
150 })
151 }
152 updateSshKeyPair = (k, field) => {
153 let self = this;
154 return function(e) {
155 let value = e.target.value;
156 let data = self.data;
157 let ref = data.entities[k];
158 ref[field] = value;
159 self.setState({data:data});
160 }
161 }
162 flattenKeys(data) {
163 var fd = {
164 keys:[],
165 entities: {}
166 };
167 data && data.map(function(d){
168 fd.keys.push(d.name);
169 fd.entities[d.name] = _.merge({isEditable: false}, d)
170 });
171 return fd;
172 }
173 cleanUpKeyPayload(payload) {
174 return {key: payload.key, name: payload.name};
175 }
176 validate(data) {
177 for (let k in data) {
178 if((data[k].trim() == '') || ((/[^\w _-]/).test(data[k]))) {
179 return false;
180 }
181 }
182 return true;
183 }
184 trimName(data) {
185 data.name = data.name.trim();
186 return data;
187 }
188
189 }