AzBuilder / terrakube

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

fix: Change lock logic for local execution in CLI driven workflow #1270

Closed alfespa17 closed 3 weeks ago

alfespa17 commented 3 weeks ago

This will fix the logic to run a terraform plan using the CLI driven workflow when the workspaces is locked like the following configuration.

image

Now when running the terraform command it will show the difference like the below:

user@pop-os:~/git/simple-terraform$ 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" {
      + create_duration  = "2m"
      + 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:
  + creation_time = "2m"
  + fake_data     = {
      + data     = "Hello World"
      + resource = {
          + resource1 = "fake"
        }
    }

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform
apply" now.
user@pop-os:~/git/simple-terraform$ terraform plan
╷
│ Error: Error acquiring the state lock
│ 
│ Error message: workspace already locked (lock ID: "simple/locked-local")
│ Lock Info:
│   ID:        b624d3b4-c763-fe4c-8c7e-2bac7ff6220b
│   Path:      
│   Operation: OperationTypePlan
│   Who:       user@pop-os
│   Version:   1.5.7
│   Created:   2024-09-04 19:10:17.557721232 +0000 UTC
│   Info:      
│ 
│ 
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.

The endpoint to lock the workspace should return http code 409 and the following content when trying to lock a workspace that is already "locked"

{
    "errors": [
        {
            "status": "409",
            "title": "conflict",
            "detail": "Unable to lock workspace. The workspace is already locked."
        }
    ]
}

Fix #1248

sonarcloud[bot] commented 3 weeks ago

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud