runatlantis / atlantis

Terraform Pull Request Automation
https://www.runatlantis.io
Other
7.83k stars 1.06k forks source link

Enable Project key under Paths for the /api/plan api #5087

Open dimisjim opened 1 week ago

dimisjim commented 1 week ago

Community Note


Describe the user story

I would like to be able to call the atlantis /api/plan by specifying the project defined in the repo level yaml, instead of just either one of Directory or Workspace as mentioned at: https://www.runatlantis.io/docs/api-endpoints#post-api-plan

Describe the solution you'd like

Have parity in the plan api with the options provided in the repo atlantis config yaml, which defines the projects and workflows like this:

version: 3
projects:
- name: coolproject-dev
  workflow: coolproject-dev
  dir: coolproject
  autoplan:
    when_modified: ["*.tf", "env-vars/dev.tfvars"]

....

workflows:
  coolproject-dev:
    plan:
      steps:
      - init:
          extra_args: ["-backend-config=backend-configs/dev.hcl", "-reconfigure", "-upgrade"]
      - plan:
          extra_args: ["-var-file=env-vars/dev.tfvars"]
    apply:
      steps:
      - run: |
          ...

so that we can then be able to run:

http -b POST https://<atlantis-host>/api/plan \ 
    X-Atlantis-Token:<...> \
    <<<'{ "Repository": "coolorg/coolrepo", "Ref": "main", "Type": "Github", "Paths": [{"Directory": "coolproject"}, {"Project":"coolproject-dev"}]}'

Describe the drawbacks of your solution

Can't think of any

Describe alternatives you've considered

I've tried running the aforementioned command in the description of the solution section above, but I'm getting:

{ "error": "failed to build command: must specify project name: more than one project defined in 'atlantis.yaml' matched dir: 'coolproject' workspace: 'default'" }

and if I only specify the Directory using the name of the project, I am getting:

{
    "Error": null,
    "Failure": "",
    "PlansDeleted": false,
    "ProjectResults": [
        {
            "ApplySuccess": "",
            "Command": 1,
            "Error": {
                "RepoRelDir": "coolproject-dev"
            },
            "Failure": "",
            "ImportSuccess": null,
            "PlanSuccess": null,
            "PolicyCheckResults": null,
            "ProjectName": "",
            "RepoRelDir": "ilayer-dev",
            "SilencePRComments": null,
            "StateRmSuccess": null,
            "SubCommand": "",
            "VersionSuccess": "",
            "Workspace": "default"
        }
    ]
}

and in the logs:

"caller":"terraform/terraform_client.go:293","msg":"trying to detect required version: Failed to read module directory: Module directory /root/.atlantis/repos/org/coolrepo/0/default/coolproject-dev does not exist or cannot be read."

I would like to use Atlantis for drift checking, I could use some other mechanism of course, until this is implemented

johnvandeweghe commented 1 week ago

The source seems to have an undocumented Projects key in the request body it appears. It is a list of strings of project names to run. Does that solve your problem?