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

chore!: default using optional keywork, merge rule map - v5.0.0 refactor #282

Conversation

Remy-Mollandin-SK5
Copy link

@Remy-Mollandin-SK5 Remy-Mollandin-SK5 commented Mar 31, 2023

Description

For now, module have been using a resource statement for each rule type (source_security_group, cidr_blocks, ipv6_cidr_blocks and self).

Using terraform 1.3 optional keywork with default, we should be able to simplify this logic and merge resources in a single map. It should also provide a more clear and concise source code.

  type = list(object({
    description              = optional(string, "Computed Ingress Rule")
    from_port                = optional(number)
    rule                     = optional(string, "_")
    to_port                  = optional(number)
    protocol                 = optional(string)
    cidr_blocks              = optional(list(string))
    ipv6_cidr_blocks         = optional(list(string))
    prefix_list_ids          = optional(list(string))
    self                     = optional(bool)
    source_security_group_id = optional(string)
  }))

Validation are enabled at the variable level to ensure at least one required field is set:

  validation {
    condition = alltrue([
      for rule in var.computed_ingress_with_custom_blocks : (
        rule.cidr_blocks != null ||
        rule.ipv6_cidr_blocks != null ||
        rule.prefix_list_ids != null ||
        rule.self != null ||
        rule.source_security_group_id != null
      )
    ])
    error_message = "Computed ingress rule must have at least one of cidr_blocks, ipv6_cidr_blocks, prefix_list_ids, self or source_security_group_id defined."
  }

It moves the logic of rule default values from:

lookup(
    var.ingress_with_self[count.index],
    "from_port",
    var.rules[lookup(var.ingress_with_self[count.index], "rule", "_")][0],
  )

to:

  from_port = coalesce(
    var.ingress_with_custom_blocks[count.index].from_port,
    var.rules[var.ingress_with_custom_blocks[count.index].rule][0],
  )

It also gives a solution to #281 as prefix_list_ids are now handled at the rule level.

Motivation and Context

Making the module easier to use and less error prone by merging variables that are closely related. It also increase the maintainability as most of the default logic is now handled directly by Terraform.

Breaking Changes

This is breaking the current usage as the rule logic is now handled by a new variable. All the old ones is going to be removed and there is no possibility to handle the changes using moved statements.

It would require a new major release and Terraform >= 1.3.

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects

For now only the complete example have been updated. The terraform apply is running fine in my personnal account.

  • I have executed pre-commit run -a on my pull request

pre-commit is installed and hooks enabled. However the pre-commit run -a command hang on terraform validate. Hooks during commit are clear.

Merge ingress and egress variable into a single custom_block variable
Now handle prefix list ids by rule correctly
@Remy-Mollandin-SK5 Remy-Mollandin-SK5 changed the title WIP chore: v5.0.0 refactor WIP chore: default using optional keywork, merge rule map - v5.0.0 refactor Mar 31, 2023
@Remy-Mollandin-SK5 Remy-Mollandin-SK5 changed the title WIP chore: default using optional keywork, merge rule map - v5.0.0 refactor chore!: default using optional keywork, merge rule map - v5.0.0 refactor Mar 31, 2023
@github-actions
Copy link

github-actions bot commented May 1, 2023

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label May 1, 2023
@github-actions
Copy link

This PR was automatically closed because of stale in 10 days

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant