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

SUP-1383 Fix teams block bug in v0.25.0 #370

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## Unreleased

- SUP-1375 Use context everywhere [[PR #362](https://github.com/buildkite/terraform-provider-buildkite/pull/362)] @jradtilbrook
- SUP-1383 Fix teams block bug in v0.25.0 [[PR #370](https://github.com/buildkite/terraform-provider-buildkite/pull/370)] @jradtilbrook

## [v0.25.0](https://github.com/buildkite/terraform-provider-buildkite/compare/v0.24.0...v0.25.0)

Expand Down
11 changes: 6 additions & 5 deletions buildkite/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ func (p *pipelineResource) Read(ctx context.Context, req resource.ReadRequest, r
}

func reconcileTeamPipelinesToState(model *pipelineResourceModel, data pipelineResponse) {

var stateTeams []*pipelineTeamModel

for _, team := range model.Teams {
for _, teamEdge := range data.GetTeams().Edges {
if team.TeamId.ValueString() == string(teamEdge.Node.Team.Id) {
if team.AccessLevel.ValueString() != string(teamEdge.Node.AccessLevel) {
team.AccessLevel = types.StringValue(string(teamEdge.Node.AccessLevel))
}
if team.Slug.ValueString() == string(teamEdge.Node.Team.Slug) {
// make sure we update all values in state for users migrating from an old version
team.TeamId = types.StringValue(teamEdge.Node.Team.Id)
team.PipelineTeamId = types.StringValue(teamEdge.Node.Id)
team.AccessLevel = types.StringValue(string(teamEdge.Node.AccessLevel))
stateTeams = append(stateTeams, team)
}
}
Expand Down Expand Up @@ -662,6 +662,7 @@ func (p *pipelineResource) Update(ctx context.Context, req resource.UpdateReques
return
}
state.BadgeUrl = types.StringValue(extraInfo.BadgeUrl)
state.ProviderSettings = make([]*providerSettingsModel, 0)
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
55 changes: 55 additions & 0 deletions buildkite/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,59 @@ func TestAccBuildkitePipeline(t *testing.T) {
},
})
})

t.Run("updating provider maintains teams", func(t *testing.T) {
pipelineName := acctest.RandString(12)
teamName := acctest.RandString(12)
config := fmt.Sprintf(`
resource "buildkite_team" "team" {
name = "%s"
default_team = false
default_member_role = "MEMBER"
privacy = "VISIBLE"
}
resource "buildkite_pipeline" "pipeline" {
name = "%s"
repository = "https://github.com/buildkite/terraform-provider-buildkite.git"
team {
slug = buildkite_team.team.slug
access_level = "BUILD_AND_READ"
}
}
`, teamName, pipelineName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
// create a pipeline and link a team using the old provider
Config: config,
ExternalProviders: map[string]resource.ExternalProvider{
"buildkite": {
Source: "registry.terraform.io/buildkite/buildkite",
VersionConstraint: "0.23.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh this is nice.

},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("buildkite_pipeline.pipeline", "team.#", "1"),
resource.TestCheckResourceAttr("buildkite_pipeline.pipeline", "provider_settings.#", "1"),
),
},
{
// now when using the new provider, we expect teams to still be 1 and no change to be made
Config: config,
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("buildkite_pipeline.pipeline", "team.#", "1"),
resource.TestCheckNoResourceAttr("buildkite_pipeline.pipeline", "provider_settings.#"),
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("buildkite_pipeline.pipeline", plancheck.ResourceActionUpdate),
},
},
},
},
})
})
}