Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the /plan and /apply endpoints #997

Merged
merged 11 commits into from
Jul 27, 2022
Merged

Conversation

remilapeyre
Copy link
Contributor

See #937

@cep21
Copy link
Contributor

cep21 commented Apr 22, 2020

This is awesome. I really hope it can get picked up into master!

@lkysow lkysow added the api-endpoints Adding API endpoints to Atlantis label May 25, 2020
@lkysow
Copy link
Member

lkysow commented Aug 18, 2020

Hey sorry I haven't been able to get to these. They're the top of my review list now for the next release.

@llamahunter
Copy link
Contributor

Are these programmatic endpoints coming soon?

@lkysow
Copy link
Member

lkysow commented Oct 23, 2020

Hi All,
Sorry but I haven't found the time to get to these. I really apologize. I'm investigating other options to get these and other PRs reviewed more quickly.

@jamengual
Copy link
Contributor

@remilapeyre can you fix the conclicts?

@remilapeyre
Copy link
Contributor Author

Hi, I will try to come back to this some time this week. Also, I did not thought too long about the payload for the API you are welcome to share your thoughts about it.

@jamengual jamengual added the waiting-on-review Waiting for a review from a maintainer label Jan 31, 2021
@jayceebernardino
Copy link

Is there a plan to get this in?

@infestonn
Copy link

Any updates?

@jamengual
Copy link
Contributor

We have been busy with other stuff so it is hard to say if this will get in before is reviewed. I do not have an ETA for you guys.

@remilapeyre
Copy link
Contributor Author

Hi, due to lack of time to work on this, I won't have time to rebase the work or make all the corrections too get it merged, but feel free to reuse any part that may be useful if you want to open another PR

@gmaghera
Copy link
Contributor

Is this still something planned?

@smitthakkar96
Copy link

@nishkrishnan any plans to take this forward. I can help to rebase and address any code review comments if needed.

@nitrocode
Copy link
Member

If this gets merged, Atlantis will be on its way to implement "Drift Detection" which is a notable feature in Spacelift and Terraform Enterprise.

@jamengual
Copy link
Contributor

@nitrocode Someone will have to pick this up and finish it, it was created long ago and I will have to be updated it to the current master.

@remilapeyre remilapeyre requested a review from a team as a code owner July 19, 2022 22:02
@lilincmu lilincmu removed the request for review from a team July 19, 2022 23:08
@lilincmu lilincmu marked this pull request as draft July 20, 2022 00:12
@lilincmu lilincmu marked this pull request as draft July 20, 2022 00:12
@@ -501,3 +501,7 @@ func (g *GithubClient) DownloadRepoConfigFile(pull models.PullRequest) (bool, []
func (g *GithubClient) SupportsSingleFileDownload(repo models.Repo) bool {
return true
}

func (g *GithubClient) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return "", fmt.Errorf("not yet implemented")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed for plan/apply endpoints to work for github? If so can we also prioritize this before merging? Most users of Atlantis are probably on GitHub

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Implemented.

@cep21
Copy link
Contributor

cep21 commented Jul 20, 2022

A full drift detection workflow also needs to list all the valid callers of /plan. Otherwise we could manually parts the atlantis.yaml file. Is a /list also in scope?

}
if result.HasErrors() {
code = http.StatusInternalServerError
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the apiApply function call and the next 2 if statements are the only differences when compared to the Plan function. Can we combine these 2 functions together and use an if statement to check if it should do an apply or not? That way it would DRY up the code a bit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand there is plenty of room for improvement, but I'm less sure about the specific change you are proposing. Could you suggest the change you described to avoid confusion? Much appreciated!

@lilincmu
Copy link
Contributor

A quick question about the API: how will the plan output work? I'm guessing the plan endpoint will have to return the body of the plan file so that the same plan file can be sent to the apply endpoint, otherwise your apply may happen with extra plans.

Is that right?

The output of plan endpoint currently is not the body of the plan file, but the output from terraform execution. I think the decision was made to avoid the back and forth transport of plan files in this POC version. And yes, the apply will cause extra plans.

A full drift detection workflow also needs to list all the valid callers of /plan. Otherwise we could manually parts the atlantis.yaml file. Is a /list also in scope?

No I think the main focus for this PR is to have a POC first, so that people can test with it and provide more feedback. However, it's highly encouraged to open an issue and elaborate more on features like the /list endpoint you described.

@lilincmu lilincmu marked this pull request as ready for review July 21, 2022 00:58
@lilincmu lilincmu requested a review from a team July 21, 2022 00:59
@lilincmu
Copy link
Contributor

@runatlantis/maintainers May I have another look at this PR? I've done most of the changes I felt necessary, but please let me know if there's anything missing or can be improved!

@lilincmu
Copy link
Contributor

api/plan

Sample request body

{
    "Repository": "<redacted>",
    "Ref": "main",
    "Type": "Gitlab",
    "Paths": [{
        "Directory": ".",
        "Workspace": "default"
    }]
}

Sample response

{
    "Error": null,
    "Failure": "",
    "ProjectResults": [
        {
            "Command": 1,
            "RepoRelDir": ".",
            "Workspace": "default",
            "Error": null,
            "Failure": "",
            "PlanSuccess": {
                "TerraformOutput": "\nTerraform used the selected providers to generate the following execution\nplan. Resource actions are indicated with the following symbols:\n+ create\n\nTerraform will perform the following actions:\n\n  # null_resource.null_1 will be created\n+ resource \"null_resource\" \"null_1\" {\n      + id = (known after apply)\n    }\n\nPlan: 1 to add, 0 to change, 0 to destroy.\n\n",
                "LockURL": "<redacted>",
                "RePlanCmd": "atlantis plan -d .",
                "ApplyCmd": "atlantis apply -d .",
                "HasDiverged": false
            },
            "PolicyCheckSuccess": null,
            "ApplySuccess": "",
            "VersionSuccess": "",
            "ProjectName": ""
        }
    ],
    "PlansDeleted": false
}

api/apply

Sample request body

{
    "Repository": "<redacted>",
    "Ref": "main",
    "Type": "Gitlab",
    "Paths": [{
        "Directory": ".",
        "Workspace": "default"
    }]
}

Sample response

{
    "Error": null,
    "Failure": "",
    "ProjectResults": [
        {
            "Command": 0,
            "RepoRelDir": ".",
            "Workspace": "default",
            "Error": null,
            "Failure": "",
            "PlanSuccess": null,
            "PolicyCheckSuccess": null,
            "ApplySuccess": "null_resource.null_1: Creating...\nnull_resource.null_1: Creation complete after 0s [id=7523377471507728929]\n\nApply complete! Resources: 1 added, 0 changed, 0 destroyed.\n\nOutputs:\n\ninstance_arn = \"<redacted>\"\n",
            "VersionSuccess": "",
            "ProjectName": ""
        }
    ],
    "PlansDeleted": false
}

@jamengual jamengual merged commit b409cb8 into runatlantis:master Jul 27, 2022
@smitthakkar96
Copy link

Good job and thanks to everyone involved. This was much needed 😍

@jamengual
Copy link
Contributor

please test this a lot on the prerelease

https://github.com/runatlantis/atlantis/releases/tag/v0.19.8-pre.20220722

We need a lot of feedback.

@nitrocode
Copy link
Member

Thank you @remilapeyre and @lilincmu and everyone involved! This is fantastic.

@jurgen-weber-deltatre
Copy link

YES!@!!!!! I am excited. :)

@jayceebernardino
Copy link

Thank you so much for this!

@Dilergore
Copy link

I think documentation would be required for this, especially about the request/response bodies. BTW this is awesome, thanks!

@vincentgna
Copy link
Contributor

vincentgna commented Aug 18, 2022

Please note a caveat regarding WebAuthentication middleware: #2455 (for ppl getting Unauthorized this may help resolve your problem)

@lilincmu
Copy link
Contributor

lilincmu commented Aug 23, 2022

If you have policy checks enabled, the /plan endpoint probably won't work, as described in #2456.

@nitrocode nitrocode mentioned this pull request Nov 3, 2022
1 task
@nitrocode
Copy link
Member

Relevant repo

https://github.com/cresta/atlantis-drift-detection

krrrr38 pushed a commit to krrrr38/atlantis that referenced this pull request Dec 16, 2022
* Add the /plan and /apply endpoints

* Resolve conflicts

* Fix wrong merge

* Add missing methods for mocks

* Fix linting error

* Fix linting error

* Move api plan/apply into APIController

* Extract commond code into helper functions

* Implement GetCloneURL for GitHub

Co-authored-by: Li Lin <li.lin@hashicorp.com>
@nitrocode nitrocode mentioned this pull request Jan 16, 2023
7 tasks
@nitrocode nitrocode added this to the v0.19.8 milestone Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-endpoints Adding API endpoints to Atlantis waiting-on-review Waiting for a review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.