Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix: Graceful handling of 404 errors in github_emu_group_mapping resource #2385

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

coreylane
Copy link
Contributor

@coreylane coreylane commented Sep 12, 2024

Resolves #2384


Before the change?

  • The resourceGithubEMUGroupMappingRead function was not handling 404 errors correctly when an EMU external group ID was deleted outside of Terraform. This caused Terraform to fail with an error instead of gracefully removing the resource from the state.
Terraform planned the following actions, but then encountered a problem:

  # module.teams.github_team.this["testusers"] will be destroyed
  # (because key ["testusers"] is not in for_each map)
  - resource "github_team" "this" {
      - create_default_maintainer = false -> null
      - description               = "Updated: 2024-09-10T20:04:15Z" -> null
      - id                        = "12345678" -> null
      - members_count             = 5 -> null
      - name                      = "testusers" -> null
      - node_id                   = "T_XXXXXXXXXXXXX" -> null
      - privacy                   = "closed" -> null
      - slug                      = "testusers" -> null
        # (4 unchanged attributes hidden)
    }

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

╷
│ Error: GET https://api.github.com/orgs/MY-EMU-ORG/external-group/123456: 404 Not Found []
│ 
│   with module.teams.github_emu_group_mapping.this["testusers"],
│   on modules/teams/main.tf line 15, in resource "github_emu_group_mapping" "this":
│   15: resource "github_emu_group_mapping" "this" {

After the change?

  • The resourceGithubEMUGroupMappingRead function now properly handles 404 errors by removing the resource from Terraform's state when the EMU external group id is not found.
  • Added a check for 404 status code in the API response.
  • If a 404 is encountered, the resource ID is set to an empty string, effectively removing it from Terraform's state.
Terraform will perform the following actions:

  # module.teams.github_team.this["testusers"] will be destroyed
  # (because key ["testusers"] is not in for_each map)
  - resource "github_team" "this" {
      - create_default_maintainer = false -> null
      - description               = "Updated: 2024-09-12T01:04:25Z" -> null
      - id                        = "123456778" -> null
      - members_count             = 0 -> null
      - name                      = "testusers" -> null
      - node_id                   = "T_XXXXXXXXXXXX" -> null
      - privacy                   = "closed" -> null
      - slug                      = "testusers" -> null
        # (4 unchanged attributes hidden)
    }

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

Changes to Outputs:
  ~ teams                                = {
      ~ github_emu_group_mapping = {
          - testusers           = {
              - group_id  = 123456
              - id        = "teams/testusers/external-groups"
              - team_slug = "testusers"
            }
            # (12 unchanged attributes hidden)
        }
      ~ github_teams             = {
          - testusers           = {
              - create_default_maintainer = false
              - description               = "Updated: 2024-09-12T01:04:25Z"
              - id                        = "12345678"
              - ldap_dn                   = ""
              - members_count             = 0
              - name                      = "testusers"
              - node_id                   = "T_XXXXXXXXXXXXXXX"
              - parent_team_id            = ""
              - parent_team_read_id       = ""
              - parent_team_read_slug     = ""
              - privacy                   = "closed"
              - slug                      = "testusers"
            }
            # (12 unchanged attributes hidden)
        }
      ~ idp_groups               = {
          - testusers           = {
              - group_id   = 123456
              - group_name = "TestUsers"
              - updated_at = "2024-09-12T01:04:25Z"
            }
            # (12 unchanged attributes hidden)
        }
    }

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes
  • No

Copy link
Member

@kfcampbell kfcampbell left a comment

Choose a reason for hiding this comment

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

👍 thanks! I'll cut a release shortly.

@kfcampbell kfcampbell merged commit 2c5b3af into integrations:main Sep 13, 2024
1 check passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: r/github_emu_group_mapping fails to handle external deletions gracefully
3 participants