use of com.tremolosecurity.argocd.targets.ArgoCDTarget in project OpenUnison by TremoloSecurity.
the class CreateGitRepository method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
String localType = task.renderTemplate(this.type, request);
String localName = task.renderTemplate(this.name, request);
String localRepoUrl = task.renderTemplate(this.repoUrl, request);
String localSshPrivateKey = task.renderTemplate(this.sshPrivateKey, request);
GitRepo repo = new GitRepo();
repo.setType(localType);
repo.setName(localName);
repo.setRepo(localRepoUrl);
repo.setSshPrivateKey(localSshPrivateKey);
Gson gson = new Gson();
String json = gson.toJson(repo);
// System.out.println(json);
ArgoCDTarget argo = (ArgoCDTarget) task.getConfigManager().getProvisioningEngine().getTarget(this.target).getProvider();
HttpCon con = null;
try {
con = argo.createConnection();
String url = new StringBuilder().append(argo.getUrl()).append("/api/v1/repositories").toString();
HttpPost post = new HttpPost(url);
StringEntity str = new StringEntity(json, ContentType.APPLICATION_JSON);
post.setEntity(str);
HttpResponse resp = con.getHttp().execute(post);
json = EntityUtils.toString(resp.getEntity());
if (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() >= 300) {
throw new ProvisioningException("Could not create repository - " + resp.getStatusLine().getStatusCode() + " / " + json);
}
task.getConfigManager().getProvisioningEngine().logAction(argo.getName(), true, ActionType.Add, approvalID, workflow, localName, localRepoUrl);
} catch (IOException e) {
throw new ProvisioningException("Could not create repository", e);
} finally {
if (con != null) {
try {
con.getHttp().close();
} catch (IOException e) {
}
con.getBcm().close();
}
}
return true;
}
Aggregations