|
1 | 1 | package com.marklogic.appdeployer.command.restapis; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | - |
5 | | -import org.springframework.http.HttpMethod; |
6 | | - |
7 | 3 | import com.marklogic.appdeployer.AppConfig; |
8 | 4 | import com.marklogic.appdeployer.command.AbstractCommand; |
9 | 5 | import com.marklogic.appdeployer.command.CommandContext; |
|
15 | 11 | import com.marklogic.mgmt.appservers.ServerManager; |
16 | 12 | import com.marklogic.mgmt.restapis.RestApiManager; |
17 | 13 |
|
| 14 | +import java.io.File; |
| 15 | + |
18 | 16 | /** |
19 | 17 | * By default, when this command deletes a REST API server, it will delete the modules database but not the content |
20 | 18 | * database. The content database is expected to be deleted by an instance of DeployContentDatabasesCommand. If you're |
21 | 19 | * not using that command, just set deleteContentDatabase to true. |
22 | 20 | */ |
23 | 21 | public class DeployRestApiServersCommand extends AbstractCommand implements UndoableCommand { |
24 | 22 |
|
25 | | - private boolean deleteModulesDatabase = true; |
26 | | - private boolean deleteContentDatabase = false; |
27 | | - |
28 | | - private String restApiFilename; |
29 | | - |
30 | | - public DeployRestApiServersCommand() { |
31 | | - setExecuteSortOrder(SortOrderConstants.DEPLOY_REST_API_SERVERS); |
32 | | - } |
33 | | - |
34 | | - public DeployRestApiServersCommand(String restApiFilename) { |
35 | | - this(); |
36 | | - this.restApiFilename = restApiFilename; |
37 | | - } |
38 | | - |
39 | | - public DeployRestApiServersCommand(boolean deleteContentDatabase) { |
40 | | - this(); |
41 | | - this.deleteContentDatabase = deleteContentDatabase; |
42 | | - } |
43 | | - |
44 | | - public DeployRestApiServersCommand(String restApiFilename, boolean deleteContentDatabase) { |
45 | | - this(); |
46 | | - this.restApiFilename = restApiFilename; |
47 | | - this.deleteContentDatabase = deleteContentDatabase; |
48 | | - } |
49 | | - |
50 | | - @Override |
51 | | - public Integer getUndoSortOrder() { |
52 | | - return SortOrderConstants.DELETE_REST_API_SERVERS; |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public void execute(CommandContext context) { |
57 | | - String payload = getRestApiPayload(context); |
58 | | - if (payload != null) { |
59 | | - RestApiManager mgr = new RestApiManager(context.getManageClient()); |
60 | | - AppConfig appConfig = context.getAppConfig(); |
61 | | - |
62 | | - mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, false)); |
63 | | - |
64 | | - if (appConfig.isTestPortSet()) { |
65 | | - mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, true)); |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - protected String getRestApiPayload(CommandContext context) { |
71 | | - File f = findRestApiConfigFile(context); |
72 | | - if (f.exists()) { |
73 | | - return copyFileToString(f); |
74 | | - } else { |
75 | | - logger.info(format("Could not find REST API file at %s, will use default payload", f.getAbsolutePath())); |
76 | | - return getDefaultRestApiPayload(); |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - protected File findRestApiConfigFile(CommandContext context) { |
81 | | - if (restApiFilename != null) { |
82 | | - return new File(context.getAppConfig().getConfigDir().getBaseDir(), restApiFilename); |
83 | | - } else { |
84 | | - return context.getAppConfig().getConfigDir().getRestApiFile(); |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public void undo(CommandContext context) { |
90 | | - deleteTestRestServer(context); |
91 | | - deleteMainRestServer(context); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * If we have a test REST API, we first modify it to point at Documents for the modules database so we can safely |
96 | | - * delete each REST API |
97 | | - * |
98 | | - * @param context |
99 | | - */ |
100 | | - protected void deleteTestRestServer(CommandContext context) { |
101 | | - final AppConfig appConfig = context.getAppConfig(); |
102 | | - final ManageClient manageClient = context.getManageClient(); |
103 | | - |
104 | | - ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName()); |
105 | | - if (appConfig.isTestPortSet() && mgr.exists(appConfig.getTestRestServerName())) { |
106 | | - mgr.setModulesDatabaseToDocuments(appConfig.getTestRestServerName()); |
107 | | - context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() { |
108 | | - @Override |
109 | | - public boolean execute() { |
110 | | - return deleteRestApi(appConfig.getTestRestServerName(), appConfig.getGroupName(), manageClient, |
111 | | - false, true); |
112 | | - } |
113 | | - }); |
114 | | - } |
115 | | - } |
116 | | - |
117 | | - protected void deleteMainRestServer(CommandContext context) { |
118 | | - final AppConfig appConfig = context.getAppConfig(); |
119 | | - final ManageClient manageClient = context.getManageClient(); |
120 | | - |
121 | | - ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName()); |
122 | | - |
123 | | - String payload = getRestApiPayload(context); |
124 | | - payload = tokenReplacer.replaceTokens(payload, appConfig, false); |
125 | | - final String serverName = new RestApiManager(manageClient).extractNameFromJson(payload); |
126 | | - |
127 | | - if (mgr.exists(serverName)) { |
128 | | - context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() { |
129 | | - @Override |
130 | | - public boolean execute() { |
131 | | - return deleteRestApi(serverName, appConfig.getGroupName(), manageClient, deleteModulesDatabase, |
132 | | - deleteContentDatabase); |
133 | | - } |
134 | | - }); |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - protected String getDefaultRestApiPayload() { |
139 | | - return RestApiUtil.buildDefaultRestApiJson(); |
140 | | - } |
141 | | - |
142 | | - /** |
143 | | - * TODO Move this to RestApiManager. |
144 | | - * |
145 | | - * @return |
146 | | - */ |
147 | | - protected boolean deleteRestApi(String serverName, String groupName, ManageClient manageClient, |
148 | | - boolean includeModules, boolean includeContent) { |
149 | | - if (new ServerManager(manageClient, groupName).exists(serverName)) { |
150 | | - String path = format("/v1/rest-apis/%s?", serverName); |
151 | | - if (includeModules) { |
152 | | - path += "include=modules&"; |
153 | | - } |
154 | | - if (includeContent) { |
155 | | - path += "include=content"; |
156 | | - } |
157 | | - logger.info("Deleting REST API, path: " + path); |
158 | | - manageClient.getRestTemplate().exchange(manageClient.buildUri(path), HttpMethod.DELETE, null, String.class); |
159 | | - logger.info("Deleted REST API"); |
160 | | - return true; |
161 | | - } else { |
162 | | - logger.info(format("Server %s does not exist, not deleting", serverName)); |
163 | | - return false; |
164 | | - } |
165 | | - } |
166 | | - |
167 | | - public boolean isDeleteModulesDatabase() { |
168 | | - return deleteModulesDatabase; |
169 | | - } |
170 | | - |
171 | | - public void setDeleteModulesDatabase(boolean includesModules) { |
172 | | - this.deleteModulesDatabase = includesModules; |
173 | | - } |
174 | | - |
175 | | - public boolean isDeleteContentDatabase() { |
176 | | - return deleteContentDatabase; |
177 | | - } |
178 | | - |
179 | | - public void setDeleteContentDatabase(boolean includeContent) { |
180 | | - this.deleteContentDatabase = includeContent; |
181 | | - } |
182 | | - |
183 | | - public String getRestApiFilename() { |
184 | | - return restApiFilename; |
185 | | - } |
186 | | - |
187 | | - public void setRestApiFilename(String restApiFilename) { |
188 | | - this.restApiFilename = restApiFilename; |
189 | | - } |
| 23 | + private boolean deleteModulesDatabase = true; |
| 24 | + private boolean deleteContentDatabase = false; |
| 25 | + |
| 26 | + private String restApiFilename; |
| 27 | + |
| 28 | + public DeployRestApiServersCommand() { |
| 29 | + setExecuteSortOrder(SortOrderConstants.DEPLOY_REST_API_SERVERS); |
| 30 | + } |
| 31 | + |
| 32 | + public DeployRestApiServersCommand(String restApiFilename) { |
| 33 | + this(); |
| 34 | + this.restApiFilename = restApiFilename; |
| 35 | + } |
| 36 | + |
| 37 | + public DeployRestApiServersCommand(boolean deleteContentDatabase) { |
| 38 | + this(); |
| 39 | + this.deleteContentDatabase = deleteContentDatabase; |
| 40 | + } |
| 41 | + |
| 42 | + public DeployRestApiServersCommand(String restApiFilename, boolean deleteContentDatabase) { |
| 43 | + this(); |
| 44 | + this.restApiFilename = restApiFilename; |
| 45 | + this.deleteContentDatabase = deleteContentDatabase; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Integer getUndoSortOrder() { |
| 50 | + return SortOrderConstants.DELETE_REST_API_SERVERS; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void execute(CommandContext context) { |
| 55 | + String payload = getRestApiPayload(context); |
| 56 | + if (payload != null) { |
| 57 | + RestApiManager mgr = new RestApiManager(context.getManageClient()); |
| 58 | + AppConfig appConfig = context.getAppConfig(); |
| 59 | + |
| 60 | + mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, false)); |
| 61 | + |
| 62 | + if (appConfig.isTestPortSet()) { |
| 63 | + mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, true)); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + protected String getRestApiPayload(CommandContext context) { |
| 69 | + File f = findRestApiConfigFile(context); |
| 70 | + if (f.exists()) { |
| 71 | + return copyFileToString(f); |
| 72 | + } else { |
| 73 | + logger.info(format("Could not find REST API file at %s, will use default payload", f.getAbsolutePath())); |
| 74 | + return getDefaultRestApiPayload(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + protected File findRestApiConfigFile(CommandContext context) { |
| 79 | + if (restApiFilename != null) { |
| 80 | + return new File(context.getAppConfig().getConfigDir().getBaseDir(), restApiFilename); |
| 81 | + } else { |
| 82 | + return context.getAppConfig().getConfigDir().getRestApiFile(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void undo(CommandContext context) { |
| 88 | + deleteTestRestServer(context); |
| 89 | + deleteMainRestServer(context); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * If we have a test REST API, we first modify it to point at Documents for the modules database so we can safely |
| 94 | + * delete each REST API |
| 95 | + * |
| 96 | + * @param context |
| 97 | + */ |
| 98 | + protected void deleteTestRestServer(CommandContext context) { |
| 99 | + final AppConfig appConfig = context.getAppConfig(); |
| 100 | + final ManageClient manageClient = context.getManageClient(); |
| 101 | + |
| 102 | + ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName()); |
| 103 | + if (appConfig.isTestPortSet() && mgr.exists(appConfig.getTestRestServerName())) { |
| 104 | + mgr.setModulesDatabaseToDocuments(appConfig.getTestRestServerName()); |
| 105 | + context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() { |
| 106 | + @Override |
| 107 | + public boolean execute() { |
| 108 | + return deleteRestApi(appConfig.getTestRestServerName(), appConfig.getGroupName(), manageClient, |
| 109 | + false, true); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + protected void deleteMainRestServer(CommandContext context) { |
| 116 | + final AppConfig appConfig = context.getAppConfig(); |
| 117 | + final ManageClient manageClient = context.getManageClient(); |
| 118 | + |
| 119 | + ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName()); |
| 120 | + |
| 121 | + String payload = getRestApiPayload(context); |
| 122 | + payload = tokenReplacer.replaceTokens(payload, appConfig, false); |
| 123 | + final String serverName = new RestApiManager(manageClient).extractNameFromJson(payload); |
| 124 | + |
| 125 | + if (mgr.exists(serverName)) { |
| 126 | + context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() { |
| 127 | + @Override |
| 128 | + public boolean execute() { |
| 129 | + return deleteRestApi(serverName, appConfig.getGroupName(), manageClient, deleteModulesDatabase, |
| 130 | + deleteContentDatabase); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected String getDefaultRestApiPayload() { |
| 137 | + return RestApiUtil.buildDefaultRestApiJson(); |
| 138 | + } |
| 139 | + |
| 140 | + protected boolean deleteRestApi(String serverName, String groupName, ManageClient manageClient, |
| 141 | + boolean includeModules, boolean includeContent) { |
| 142 | + return new RestApiManager(manageClient).deleteRestApi(serverName, groupName, includeModules, includeContent); |
| 143 | + } |
| 144 | + |
| 145 | + public boolean isDeleteModulesDatabase() { |
| 146 | + return deleteModulesDatabase; |
| 147 | + } |
| 148 | + |
| 149 | + public void setDeleteModulesDatabase(boolean includesModules) { |
| 150 | + this.deleteModulesDatabase = includesModules; |
| 151 | + } |
| 152 | + |
| 153 | + public boolean isDeleteContentDatabase() { |
| 154 | + return deleteContentDatabase; |
| 155 | + } |
| 156 | + |
| 157 | + public void setDeleteContentDatabase(boolean includeContent) { |
| 158 | + this.deleteContentDatabase = includeContent; |
| 159 | + } |
| 160 | + |
| 161 | + public String getRestApiFilename() { |
| 162 | + return restApiFilename; |
| 163 | + } |
| 164 | + |
| 165 | + public void setRestApiFilename(String restApiFilename) { |
| 166 | + this.restApiFilename = restApiFilename; |
| 167 | + } |
190 | 168 |
|
191 | 169 | } |
0 commit comments