Search in sources :

Example 1 with ArgoCDTarget

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Workflow(com.tremolosecurity.provisioning.core.Workflow) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ArgoCDTarget(com.tremolosecurity.argocd.targets.ArgoCDTarget) StringEntity(org.apache.http.entity.StringEntity) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) GitRepo(com.tremolosecurity.argocd.tasks.obj.GitRepo) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Aggregations

Gson (com.google.gson.Gson)1 ArgoCDTarget (com.tremolosecurity.argocd.targets.ArgoCDTarget)1 GitRepo (com.tremolosecurity.argocd.tasks.obj.GitRepo)1 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 Workflow (com.tremolosecurity.provisioning.core.Workflow)1 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)1 IOException (java.io.IOException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1