AzBuilder / terrakube

Open source IaC Automation and Collaboration Software.
https://docs.terrakube.io
Apache License 2.0
451 stars 34 forks source link

API to retrieve plan output #1039

Open igorbrites opened 1 week ago

igorbrites commented 1 week ago

Feature description 💡

When running plans on Terrakube remote executors, we can't save the plan on local files, as it shows the error below:

Error: Saving a generated plan is currently not supported
Terraform Cloud does not support saving the generated execution plan locally
at this time.

There are issues opened on Terraform and Opentofu to have this working on the client side, though Terraform Cloud has an API that allows us to retrieve the JSON plan just like terraform show -json planfile.

Is there any API call on Terrakube to do the same? Or is there another way to get the plan body? I could get the command output, though when it has errors it can be messy.

Anything else?

No response

alfespa17 commented 1 week ago

Hello @igorbrites

You could see where the binary terraform plan is saved in the backend storage using:

https://terrakube-api.minikube.net/api/v1/organization/{{ORG_ID}}/job/{{JOB_ID}}
{
    "data": {
        "type": "job",
        "id": "65",
        "attributes": {
            ...
            "terraformPlan": "http://terrakube-minio:9000/terrakube/tfstate/d9b58bd3-f3fc-4056-a026-1163297e80a8/3a1cfa51-7e41-4999-9409-fbf3235b1906/65/9f27fd65-450c-43d3-ab03-9bfa9c8b6cd9/terraformLibrary.tfPlan",
           ....
        },
        "relationships": {
            "organization": {
                "data": {
                    "type": "organization",
                    "id": "d9b58bd3-f3fc-4056-a026-1163297e80a8"
                }
            },
            "step": {
                "data": [
                    {
                        "type": "step",
                        "id": "477db67d-ebf1-42f1-83a4-590cb0c93bd6"
                    },
                    {
                        "type": "step",
                        "id": "6253e0be-15f2-4744-b1ef-a6a0b746826e"
                    },
                    {
                        "type": "step",
                        "id": "9f27fd65-450c-43d3-ab03-9bfa9c8b6cd9"
                    }
                ]
            },
            "workspace": {
                "data": {
                    "type": "workspace",
                    "id": "3a1cfa51-7e41-4999-9409-fbf3235b1906"
                }
            }
        }
    }
}

Write some script to download the file locally and just run:

terraform show -json terraformLibrary.tfPlan

Maybe this can help you

alfespa17 commented 1 week ago

I guess in the future we could add some endpoint similar to this to download the json terraform plan result

https://developer.hashicorp.com/terraform/cloud-docs/api-docs/plans#retrieve-the-json-execution-plan

alfespa17 commented 1 week ago

Other option could be to use this request:

https://terrakube-api.minikube.net/api/v1/organization/{{ORG_ID}}/job/{{JOB_ID}}/step?filter[step]=stepNumber==100
{
    "data": [
        {
            "type": "step",
            "id": "9f27fd65-450c-43d3-ab03-9bfa9c8b6cd9",
            "attributes": {
                "name": "Terraform Plan from Terraform CLI",
                "output": "https://terrakube-api.minikube.net/tfoutput/v1/organization/d9b58bd3-f3fc-4056-a026-1163297e80a8/job/65/step/9f27fd65-450c-43d3-ab03-9bfa9c8b6cd9",
                "status": "completed",
                "stepNumber": 100
            },
            "relationships": {
                "job": {
                    "data": {
                        "type": "job",
                        "id": "65"
                    }
                }
            }
        }
    ]
}

Once you get the property "output" you could download it using

image

The above is what the UI is using the print the result

alfespa17 commented 1 week ago

Using curl will print something like this:

user@pop-os:~/git/simple-terraform$ curl --location 'https://terrakube-api.minikube.net/tfoutput/v1/organization/d9b58bd3-f3fc-4056-a026-1163297e80a8/job/65/step/9f27fd65-450c-43d3-ab03-9bfa9c8b6cd9' \
--header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX'
***************************************
Running Terraform PLAN
***************************************

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.next will be created
  + resource "null_resource" "next" {
      + id = (known after apply)
    }

  # null_resource.next2 will be created
  + resource "null_resource" "next2" {
      + id = (known after apply)
    }

  # null_resource.next3 will be created
  + resource "null_resource" "next3" {
      + id = (known after apply)
    }

  # null_resource.next4 will be created
  + resource "null_resource" "next4" {
      + id = (known after apply)
    }

  # null_resource.previous will be created
  + resource "null_resource" "previous" {
      + id = (known after apply)
    }

  # time_sleep.wait_30_seconds will be created
  + resource "time_sleep" "wait_30_seconds" {
      + destroy_duration = "45s"
      + id               = (known after apply)
    }

  # module.time_module.random_integer.time will be created
  + resource "random_integer" "time" {
      + id     = (known after apply)
      + max    = 5
      + min    = 1
      + result = (known after apply)
    }

Plan: 7 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + fake_data     = {
      + data     = "Hello World"
      + resource = {
          + resource1 = "fake"
        }
    }
igorbrites commented 1 week ago

Awesome! I'll give it a shot, thanks!

igorbrites commented 1 week ago

I guess in the future we could add some endpoint similar to this to download the JSON terraform plan result

https://developer.hashicorp.com/terraform/cloud-docs/api-docs/plans#retrieve-the-json-execution-plan

IMHO we could have one to get the JSON and another to get the actual plan file. In my case, I use the plan file to create a comment on GitHub PRs, so I'm going to use the first option you provided.