github.com/replicatedhq/ship@v0.55.0/web/init/src/redux/data/kustomizeOverlay/actions.js (about) 1 import "isomorphic-fetch"; 2 import { loadingData } from "../../ui/main/actions"; 3 import { getContentForStep } from "../appRoutes/actions"; 4 5 export const constants = { 6 RECEIVE_FILE_CONTENT: "RECEIVE_FILE_CONTENT", 7 RECEIVE_PATCH: "RECEIVE_PATCH", 8 RECEIVE_MODIFIED: "RECEIVE_MODIFIED", 9 }; 10 11 export function receiveFileContent(content, path) { 12 return { 13 type: constants.RECEIVE_FILE_CONTENT, 14 payload: { 15 content, 16 path, 17 } 18 }; 19 } 20 21 export function getFileContent(payload) { 22 return async (dispatch, getState) => { 23 let response; 24 dispatch(loadingData("fileContent", true)); 25 try { 26 const { apiEndpoint } = getState(); 27 const url = `${apiEndpoint}/kustomize/file`; 28 response = await fetch(url, { 29 method: "POST", 30 headers: { 31 "Accept": "application/json", 32 "Content-Type": "application/json" 33 }, 34 body: JSON.stringify({ path: payload }) 35 }); 36 if (!response.ok) { 37 dispatch(loadingData("fileContent", false)); 38 return; 39 } 40 const body = await response.json(); 41 dispatch(loadingData("fileContent", false)); 42 dispatch(receiveFileContent(body, payload)); 43 dispatch(receivePatch(body.overlay)); 44 } catch (error) { 45 dispatch(loadingData("fileContent", false)); 46 console.log(error) 47 return; 48 } 49 }; 50 } 51 52 export function saveKustomizeOverlay(payload) { 53 return async (dispatch, getState) => { 54 const { apiEndpoint } = getState(); 55 let response; 56 dispatch(loadingData("saveKustomize", true)); 57 try { 58 const url = `${apiEndpoint}/kustomize/save`; 59 response = await fetch(url, { 60 method: "POST", 61 headers: { 62 "Accept": "application/json", 63 "Content-Type": "application/json" 64 }, 65 body: JSON.stringify(payload) 66 }); 67 68 if (!response.ok) { 69 dispatch(loadingData("saveKustomize", false)); 70 71 if (response.status >= 400) { 72 const body = await response.json(); 73 if(body.error) { 74 throw new Error(body.detail); 75 } 76 } 77 78 return; 79 } 80 81 await response.json(); 82 dispatch(getFileContent(payload.path)); 83 dispatch(loadingData("saveKustomize", false)); 84 } catch (error) { 85 dispatch(loadingData("saveKustomize", false)); 86 throw new Error(error.message); 87 } 88 }; 89 } 90 91 export function deleteOverlay(path, type) { 92 return async (dispatch, getState) => { 93 const { apiEndpoint } = getState(); 94 let response; 95 const url = `${apiEndpoint}/kustomize/${type}?path=${path}`; 96 dispatch(loadingData("deleteOverlay", true)); 97 try { 98 response = await fetch(url, { 99 method: "DELETE", 100 headers: { 101 "Accept": "application/json", 102 "Content-Type": "application/json" 103 } 104 }); 105 if (!response.ok) { 106 dispatch(loadingData("deleteOverlay", false)); 107 return; 108 } 109 await response.json(); 110 dispatch(loadingData("deleteOverlay", false)); 111 dispatch(receivePatch("")); 112 dispatch(getFileContent(path)); 113 dispatch(getContentForStep("kustomize")); 114 } catch (error) { 115 dispatch(loadingData("deleteOverlay", false)); 116 console.log(error) 117 return; 118 } 119 }; 120 } 121 122 export function includeBase(path, type) { 123 return async (dispatch, getState) => { 124 const { apiEndpoint } = getState(); 125 let response; 126 const url = `${apiEndpoint}/kustomize/include`; 127 dispatch(loadingData("includeBase", true)); 128 try { 129 response = await fetch(url, { 130 method: "POST", 131 headers: { 132 "Accept": "application/json", 133 "Content-Type": "application/json" 134 }, 135 body: JSON.stringify({ path }), 136 }); 137 if (!response.ok) { 138 dispatch(loadingData("includeBase", false)); 139 return; 140 } 141 await response.json(); 142 dispatch(loadingData("includeBase", false)); 143 dispatch(getFileContent(path)); 144 dispatch(getContentForStep("kustomize")); 145 } catch (error) { 146 dispatch(loadingData("includeBase", false)); 147 console.log(error) 148 return; 149 } 150 }; 151 } 152 153 export function finalizeKustomizeOverlay() { 154 return async (dispatch, getState) => { 155 const { apiEndpoint } = getState(); 156 let response; 157 dispatch(loadingData("finalizeKustomize", true)); 158 try { 159 const url = `${apiEndpoint}/kustomize/finalize`; 160 response = await fetch(url, { 161 method: "POST", 162 headers: { 163 "Accept": "application/json", 164 "Content-Type": "application/json" 165 } 166 }); 167 if (!response.ok) { 168 dispatch(loadingData("finalizeKustomize", false)); 169 return; 170 } 171 await response.json(); 172 dispatch(loadingData("finalizeKustomize", false)); 173 } catch (error) { 174 dispatch(loadingData("finalizeKustomize", false)); 175 console.log(error) 176 return; 177 } 178 }; 179 } 180 181 export function receivePatch(patch) { 182 return { 183 type: constants.RECEIVE_PATCH, 184 payload: { 185 patch, 186 } 187 }; 188 } 189 190 export function generatePatch(payload) { 191 return async (dispatch, getState) => { 192 const { apiEndpoint } = getState(); 193 try { 194 const url = `${apiEndpoint}/kustomize/patch`; 195 const response = await fetch(url, { 196 method: "POST", 197 headers: { 198 "Accept": "application/json", 199 "Content-Type": "application/json" 200 }, 201 body: JSON.stringify(payload) 202 }); 203 let { patch } = await response.json(); 204 if (!response.ok) { 205 patch = payload.current; 206 } 207 dispatch(receivePatch(patch)); 208 } catch (error) { 209 console.log(error) 210 return; 211 } 212 }; 213 } 214 215 export function applyPatch(payload) { 216 return async (dispatch, getState) => { 217 const { apiEndpoint } = getState(); 218 try { 219 const url = `${apiEndpoint}/kustomize/apply`; 220 const response = await fetch(url, { 221 method: "POST", 222 headers: { 223 "Accept": "application/json", 224 "Content-Type": "application/json" 225 }, 226 body: JSON.stringify(payload) 227 }); 228 const { modified } = await response.json(); 229 dispatch(receiveModified(modified, payload.patch)); 230 } catch (error) { 231 throw new Error("We weren't able to apply your patch, please verify your patch and try again."); 232 } 233 }; 234 } 235 236 export function receiveModified(modified, patch) { 237 return { 238 type: constants.RECEIVE_MODIFIED, 239 payload: { 240 modified, 241 patch, 242 } 243 }; 244 }