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

fix(resource/scalingo_scm_repo_link): don't omit attributes anymore when creating repo link with review apps disabled #159

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

josephpage
Copy link
Contributor

@josephpage josephpage commented May 16, 2023

Issue description

This PR solves a problem I encountered recently: when creating a repo link using the resource scalingo_scm_repo_link while the deploy_review_apps_enabled = false, the properties delete_on_close_enabled, delete_stale_enabled, hours_before_delete_on_close and hours_before_delete_stale are not configured by the create operation, so what is configured in the Scalingo API and therefore in the Terraform state does not conform to what is written in the HCL code.

Here's an example:

variable "review_apps" {
  description = "Configuration of the review apps of the application."
  type = object({
    enabled = optional(bool, false)

    # By default: delete review apps 0 hours after closing the PR
    delete_on_close_enabled      = optional(bool, true)
    hours_before_delete_on_close = optional(string, "0")

    # By default: delete review apps after 5 days of inactivity (= no new deployment)
    delete_stale_enabled      = optional(bool, true)
    hours_before_delete_stale = optional(string, "168")

    # By default: do not create review apps for PRs from forks
    automatic_creation_from_forks_allowed = optional(bool, false)
  })
  default = {}
}

resource "scalingo_scm_repo_link" "repo" {
  app = scalingo_app.app.id

  source = var.repo_url
  auth_integration_uuid = data.scalingo_scm_integration.github.id

  auto_deploy_enabled = true
  branch              = "main"

  # Configuration for review apps
  deploy_review_apps_enabled            = var.review_apps.enabled
  delete_on_close_enabled               = var.review_apps.delete_on_close_enabled
  hours_before_delete_on_close          = var.review_apps.hours_before_delete_on_close
  delete_stale_enabled                  = var.review_apps.delete_stale_enabled
  hours_before_delete_stale             = var.review_apps.hours_before_delete_stale
  automatic_creation_from_forks_allowed = var.review_apps.automatic_creation_from_forks_allowed
}

The terraform plan command will propose the creation of this repo link:

$ terraform apply -target scalingo_scm_repo_link.repo
[...]
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:

  # scalingo_scm_repo_link.repo will be created
  + resource "scalingo_scm_repo_link" "repo" {
      + app                                   = "xxxxx80fb0de6000fxxxxxx"
      + auth_integration_uuid                 = "xxxxxx26-xxxx-456b-xxxx-f119d2xxxxxx"
      + auto_deploy_enabled                   = true
      + automatic_creation_from_forks_allowed = false
      + branch                                = "main"
      + delete_on_close_enabled               = true
      + delete_stale_enabled                  = true
      + deploy_review_apps_enabled            = false
      + hours_before_delete_on_close          = 0
      + hours_before_delete_stale             = 168
      + id                                    = (known after apply)
      + source                                = "https://github.com/my-org/repo"
    }

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

Next, let's use the terraform apply command, then redo the terraform plan to understand what happened, or rather what didn't happen as expected:

$ terraform apply -auto-approve
[...]
$ terraform plan
[...]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.log_manager_app.scalingo_scm_repo_link.scm_repo_link["github"] will be updated in-place
  ~ resource "scalingo_scm_repo_link" "scm_repo_link" {
      ~ delete_stale_enabled                  = false -> true
      ~ hours_before_delete_stale             = 0 -> 168
        id                                    = "xxxxxx80fb0de6000fxxxxxx"
        # (9 unchanged attributes hidden)
    }

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

It is only after a second execution of the terraform apply command that the properties have been properly configured:

$ terraform apply -auto-approve
[...]
$ terraform plan
[...]
No changes. Your infrastructure matches the configuration.

Issue resolution

This problem is due to a condition in the code, which is not necessary according to the API documentation. I therefore propose that the Terraform provider should not try to be smarter than the API, and should allow all properties to be configured, whether deploy_review_apps_enabled is enabled or not (especially as the auto-removal features continue to work on manually deployed review apps).

I look forward to seeing your feedback on this PR.

@josephpage josephpage marked this pull request as ready for review June 6, 2023 20:05
@josephpage
Copy link
Contributor Author

josephpage commented Jun 12, 2023

Hey @Soulou or @EtienneM, I would be pleased to have your review on this PR.

@EtienneM EtienneM requested a review from Soulou June 13, 2023 08:16
@josephpage
Copy link
Contributor Author

More than a year since this PR was created, what are you waiting for from me to be able to merge it?

@Frzk Frzk requested a review from leo-scalingo August 13, 2024 15:09
@EtienneM EtienneM removed the request for review from Soulou August 15, 2024 07:55
@EtienneM
Copy link
Member

Hello @josephpage,

We are really sorry about the delay to handle this PR. Thanks for raising the question here. The reason is mostly that it went under our radar so we forgot to talk about it internally.

We will get back to you as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants