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 lifecycle rules feature on scaleway_object_bucket resource #35

Merged

Conversation

jgalais
Copy link
Contributor

@jgalais jgalais commented Apr 9, 2024

Hello,

I added lifecycle support on this module.

I tested this feature with the next configuration inside this module:

  lifecycle_rules = [
    {
      id      = "lifecycle_expiration_test"
      enabled = false
      expiration = [
        {
        days = 360
        }
      ]
    },
    {
      id      = "lifecycle_transition_test"
      enabled = false
      transition = [
        {
        days = 360
        storage_class = "GLACIER"
        }
      ]
    },
  ]

Additional information:

[X] terraform fmt applied
[X] terraform docs applied

Regards,
jgalais

@jgalais jgalais force-pushed the Feature_lifecycle_rules branch from 4efd64c to 81315fb Compare April 9, 2024 12:03
[X] terraform fmt applied
[X] terraform docs applied
@jgalais jgalais force-pushed the Feature_lifecycle_rules branch from 81315fb to fb186ef Compare April 9, 2024 12:41
@benoit-garcia
Copy link
Member

@jgalais Awesome!

However I made some test on my side and it appears we can only set 1 expiration block by rule. The following code throws an error during plan:

  lifecycle_rules = [
    {
      id      = "lifecycle_expiration_test"
      enabled = false
      expiration = [
        {
          days = 31
        },
        {
          days = 365
        }
      ]
    },

Also, we can only set 1 transition per rule. The following code plans ok, but get an HTTP 501 from Scaleway API during apply:

  lifecycle_rules = [
    {
      id      = "lifecycle_transition"
      enabled = false
      transition = [
        {
          days = 30
          storage_class = "ONEZONE_IA"
        },
        {
          days = 360
          storage_class = "GLACIER"
        }
      ]
    },
  ]

Do you think you could change your PR in order to reflect this?

@benoit-garcia benoit-garcia added the enhancement New feature or request label Apr 9, 2024
Copy link
Member

@benoit-garcia benoit-garcia left a comment

Choose a reason for hiding this comment

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

Bucket lifecycle can only contains 1 expiration and/or 1 transition per rules.

@jgalais
Copy link
Contributor Author

jgalais commented Apr 11, 2024

Hello,

I checked that.

The problem is complex:

  • On Terraform side:

The "expiration" and "transition" are terraform blocks.
The purpose of Terraform block is to reproduce a block multiple time when you have a resources where this block can be multiple.

The feature that allow to reproduce multiple time a terraform block is called "Dynamic-blocks (https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks).

  • On Scaleway side:

"Expiration" and "Transition" blocks can have only one value.

Terraform syntax is ok but Scaleway API doesn't allow that.

I think that we must to keep like that because:

  • It's already ok currently if you define 1 block
  • It's already ok for the futur if scaleway authorize multiple behaviors for the transition block

I have an example with this behavior.

The lifecycle_rules on AWS S3 buckets.
There are the same problem on AWS official Terraform module.

The only difference with Scaleway and AWS on this case is that the block "expiration" AND "transition" accept only one value for Scaleway where in AWS only the block "expiration" accept only one value.

Link: https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/blob/master/main.tf#L263

Expiration block:

     # Max 1 block - expiration
      dynamic "expiration" {
        for_each = try(flatten([rule.value.expiration]), [])

        content {
          date                         = try(expiration.value.date, null)
          days                         = try(expiration.value.days, null)
          expired_object_delete_marker = try(expiration.value.expired_object_delete_marker, null)
        }
      }`

Transition block:

      # Several blocks - transition
      dynamic "transition" {
        for_each = try(flatten([rule.value.transition]), [])

        content {
          date          = try(transition.value.date, null)
          days          = try(transition.value.days, null)
          storage_class = transition.value.storage_class
        }
      }

Regards,

@benoit-garcia
Copy link
Member

benoit-garcia commented Apr 14, 2024

You can still use dynamic blocks for this.

For the transition, you could:

  • change the variable definition so transitions are a single optional object instead of an optional list of objects:
variable "lifecycle_rules" {
  description = "Define bucket lifecycle configuration"
  type = list(object({
    id                                     = string
...
    transition = optional(object({
      days          = number
      storage_class = string
    }))
...
  }))
  default = []
}
  • Check the existence of this ojbect in the initialization of the dynamic block
resource "scaleway_object_bucket" "this" {
...
  dynamic "lifecycle_rule" {
    for_each = length(var.lifecycle_rules) == 0 ? [] : var.lifecycle_rules
    content {
...
      dynamic "transition" {
        for_each = lifecycle_rule.value["transition"] == null ? [] : [lifecycle_rule.value["transition"]]
        content {
          days          = transition.value["days"]
          storage_class = transition.value["storage_class"]
        }
      }
    }
  }
...
}

You can also use this method for setting the expiration days.

@jgalais
Copy link
Contributor Author

jgalais commented Apr 14, 2024

I done that.

@benoit-garcia benoit-garcia merged commit 04c6fb9 into scaleway-terraform-modules:main Apr 14, 2024
5 checks passed
@benoit-garcia
Copy link
Member

@jgalais Thanks!
It has been merged and released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

2 participants