Skip to content

Commit

Permalink
chore: fix space teams mapping (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Aug 16, 2024
1 parent a10daef commit e3dccfb
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions octopusdeploy_framework/resource_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
Expand Down Expand Up @@ -109,11 +110,9 @@ func (s *spaceResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

data.SpaceManagersTeams, diags = types.SetValueFrom(ctx, types.StringType, removeSpaceManagers(ctx, createdSpace.SpaceManagersTeams))
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
effectiveTeams := s.getEffectiveTeams(ctx, createdSpace)
data.SpaceManagersTeams = types.SetValueMust(types.StringType, effectiveTeams)

tflog.Debug(ctx, fmt.Sprintf("state space %#v", data))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
util.Created(ctx, "space")
Expand Down Expand Up @@ -143,7 +142,9 @@ func (s *spaceResource) Read(ctx context.Context, req resource.ReadRequest, resp
data.IsTaskQueueStopped = types.BoolValue(spaceResult.TaskQueueStopped)
data.IsDefault = types.BoolValue(spaceResult.IsDefault)
data.SpaceManagersTeamMembers, _ = types.SetValueFrom(ctx, types.StringType, spaceResult.SpaceManagersTeamMembers)
data.SpaceManagersTeams, _ = types.SetValueFrom(ctx, types.StringType, removeSpaceManagers(ctx, spaceResult.SpaceManagersTeams))

effectiveTeams := s.getEffectiveTeams(ctx, spaceResult)
data.SpaceManagersTeams = types.SetValueMust(types.StringType, effectiveTeams)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Info(ctx, fmt.Sprintf("space read (%s)", data.ID))
Expand Down Expand Up @@ -209,12 +210,22 @@ func (s *spaceResource) Update(ctx context.Context, req resource.UpdateRequest,
plan.IsTaskQueueStopped = types.BoolValue(spaceResult.TaskQueueStopped)
plan.IsDefault = types.BoolValue(spaceResult.IsDefault)
plan.SpaceManagersTeamMembers, _ = types.SetValueFrom(ctx, types.StringType, spaceResult.SpaceManagersTeamMembers)
plan.SpaceManagersTeams, _ = types.SetValueFrom(ctx, types.StringType, removeSpaceManagers(ctx, updatedSpace.SpaceManagersTeams))

effectiveTeams := s.getEffectiveTeams(ctx, updatedSpace)
plan.SpaceManagersTeams = types.SetValueMust(types.StringType, effectiveTeams)

// save plan to state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

func (s *spaceResource) getEffectiveTeams(ctx context.Context, updatedSpace *spaces.Space) []attr.Value {
effectiveTeamMembers := make([]attr.Value, 0)
for _, t := range removeSpaceManagers(ctx, updatedSpace.SpaceManagersTeams) {
effectiveTeamMembers = append(effectiveTeamMembers, types.StringValue(t))
}
return effectiveTeamMembers
}

func (s *spaceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data schemas.SpaceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
Expand Down

0 comments on commit e3dccfb

Please sign in to comment.