-
Notifications
You must be signed in to change notification settings - Fork 927
[BUG] Ensure github_emu_group_mapping behaves correctly if mapping changes upstream
#3118
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
Open
deiga
wants to merge
18
commits into
integrations:main
Choose a base branch
from
F-Secure-web:fix-emu-group-mapping-read-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6af7b61
Move `toInt` and `toInt64` functions to `util.go`
deiga 927ebf7
`resource_github_emu_group_mapping`) Change `Read` to use `ListExtern…
deiga 5b8b43a
`resource_github_emu_group_mapping`: Refactor `Create` to have less u…
deiga ab63985
`emu_group_mapping`: Add `group_name` computed attribute
deiga a30be2f
Add matching of team ID into Read
deiga 1258865
Split Create and Update to separate functions
deiga 09ef993
Update Importer to use new ID pattern
deiga 61103ee
Add Schema migration for new ID format
deiga 2a27e67
Changes `group_id` to `ForceNew`
deiga 46fe6ca
Add skipped test while waiting for `terraform-plugin-testing`
deiga 6ba2803
Add `CustomizeDiff` function to determine if `team_slug` change needs…
deiga b87713b
Update docs
deiga 98c2b9d
Use `lookupTeamID` instead of `getTeamID`
deiga e4ea104
Move `Create` before `Read`
deiga 705b3b0
Replace `deep` package with `go-cmp`
deiga 4a72832
`ListExternalGroupsForTeamBySlug` does not return a nested `Teams` sl…
deiga 87ed6ed
Replace `go-github-mock` with `githubApiMock`
deiga 0d4e974
Remove unnecessry `matchTeamID` function
deiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func resourceGithubEMUGroupMappingResourceV0() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "team_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "Slug of the GitHub team.", | ||
| }, | ||
| "group_id": { | ||
| Type: schema.TypeInt, | ||
| Required: true, | ||
| Description: "Integer corresponding to the external group ID to be linked.", | ||
| }, | ||
| "etag": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceGithubEMUGroupMappingInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) { | ||
| orgName := meta.(*Owner).name | ||
| tflog.Trace(ctx, "GitHub EMU Group Mapping State before migration", map[string]any{"state": rawState, "owner": orgName}) | ||
|
|
||
| client := meta.(*Owner).v3client | ||
|
|
||
| teamSlug := rawState["team_slug"].(string) | ||
| // We need to bypass the etag because we need to get the latest group | ||
| ctx = context.WithValue(ctx, ctxEtag, nil) | ||
| groupsList, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, teamSlug) | ||
| if err != nil { | ||
| if resp != nil && (resp.StatusCode == http.StatusNotFound) { | ||
| // If the Group is not found, remove it from state | ||
| tflog.Info(ctx, "Removing EMU group mapping from state because team no longer exists in GitHub", map[string]any{ | ||
| "resource_id": rawState["id"], | ||
| }) | ||
| return nil, err | ||
| } | ||
| return nil, err | ||
| } | ||
| group := groupsList.Groups[0] | ||
| teamID, err := lookupTeamID(ctx, meta.(*Owner), teamSlug) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rawState["team_id"] = teamID | ||
| resourceID, err := buildID(strconv.FormatInt(teamID, 10), teamSlug, strconv.FormatInt(group.GetGroupID(), 10)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rawState["id"] = resourceID | ||
|
|
||
| tflog.Trace(ctx, "GitHub EMU Group Mapping State after migration", map[string]any{"state": rawState}) | ||
| return rawState, nil | ||
| } |
127 changes: 127 additions & 0 deletions
127
github/resource_github_emu_group_mapping_migration_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-github/v82/github" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| type ( | ||
| currentStateFunc func() map[string]any | ||
| expectedStateFunc func(t *testing.T) map[string]any | ||
| ) | ||
|
|
||
| var ( | ||
| testTeamID = 432574718 | ||
| testGroupID = 1234567890 | ||
| ) | ||
|
|
||
| func testResourceGithubEMUGroupMappingInstanceStateDataV0() map[string]any { | ||
| return map[string]any{ | ||
| "id": "teams/test-team/external-groups", | ||
| "team_slug": "test-team", | ||
| "group_id": testGroupID, | ||
| } | ||
| } | ||
|
|
||
| func testResourceGithubEMUGroupMappingInstanceStateDataV1(t *testing.T) map[string]any { | ||
| v0 := testResourceGithubEMUGroupMappingInstanceStateDataV0() | ||
| v0["team_id"] = int64(testTeamID) | ||
| resourceID, err := buildID(strconv.Itoa(testTeamID), v0["team_slug"].(string), strconv.Itoa(v0["group_id"].(int))) | ||
| if err != nil { | ||
| t.Fatalf("error building resource ID: %s", err) | ||
| } | ||
| v0["id"] = resourceID | ||
| return v0 | ||
| } | ||
|
|
||
| func buildMockResponsesForMigrationV0toV1() []*mockResponse { | ||
| return []*mockResponse{ | ||
| { | ||
| ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s/external-groups", "test-org", "test-team"), | ||
| ExpectedHeaders: map[string]string{ | ||
| "Accept": "application/vnd.github.v3+json", | ||
| }, | ||
| ResponseBody: fmt.Sprintf(` | ||
| { | ||
| "groups": [ | ||
| { | ||
| "group_id": %d, | ||
| "group_name": "test-group", | ||
| "updated_at": "2021-01-24T11:31:04-06:00" | ||
| } | ||
| ] | ||
| }`, int64(testGroupID)), | ||
| StatusCode: 201, | ||
| }, | ||
| { | ||
| ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s", "test-org", "test-team"), | ||
| ExpectedHeaders: map[string]string{ | ||
| "Accept": "application/vnd.github.v3+json", | ||
| }, | ||
| ResponseBody: fmt.Sprintf(` | ||
| { | ||
| "id": %d | ||
| } | ||
| `, testTeamID), | ||
| StatusCode: 200, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestGithub_MigrateEMUGroupMappingsState(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| meta := &Owner{ | ||
| name: "test-org", | ||
| } | ||
|
|
||
| for _, d := range []struct { | ||
| testName string | ||
| migrationFunc schema.StateUpgradeFunc | ||
| rawState currentStateFunc | ||
| want expectedStateFunc | ||
| buildMockResponses func() []*mockResponse | ||
| shouldError bool | ||
| }{ | ||
| { | ||
| testName: "migrates v0 to v1", | ||
| migrationFunc: resourceGithubEMUGroupMappingInstanceStateUpgradeV0, | ||
| rawState: testResourceGithubEMUGroupMappingInstanceStateDataV0, | ||
| want: testResourceGithubEMUGroupMappingInstanceStateDataV1, | ||
| buildMockResponses: buildMockResponsesForMigrationV0toV1, | ||
| shouldError: false, | ||
| }, | ||
| } { | ||
| t.Run(d.testName, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ts := githubApiMock(d.buildMockResponses()) | ||
| defer ts.Close() | ||
|
|
||
| httpCl := http.DefaultClient | ||
| httpCl.Transport = http.DefaultTransport | ||
|
|
||
| client := github.NewClient(httpCl) | ||
| u, _ := url.Parse(ts.URL + "/") | ||
| client.BaseURL = u | ||
| meta.v3client = client | ||
|
|
||
| currentState := d.rawState() | ||
| got, err := d.migrationFunc(t.Context(), currentState, meta) | ||
| expectedState := d.want(t) | ||
| if (err != nil) != d.shouldError { | ||
| t.Fatalf("unexpected error state: %s", err.Error()) | ||
| } | ||
| if diff := cmp.Diff(expectedState, got); !d.shouldError && diff != "" { | ||
| t.Fatal(diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.