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

Export Teams and team memberships to Terraform #206

Merged
10 commits merged into from
Nov 28, 2023
Merged

Export Teams and team memberships to Terraform #206

10 commits merged into from
Nov 28, 2023

Conversation

ghost
Copy link

@ghost ghost commented Nov 27, 2023

Issues

After shipping OpsLevel/terraform-provider-opslevel#144 some customers can be in a state where their terraform Team configs are not using the new member { } blocks. This results in Team Memberships not being able to reconcile without kicking all members out of the terraform managed teams.

Also, the terraform export output is not compatible with the latest TF schema version.

Changelog

  • Change export team function
  • Minor changes: fix unnecessary line spaces, add line spaces between team resources to make it easier to read
  • Make a changie entry

Tophatting

Before:

resource "opslevel_team" "platform" {
  name = "platform"
  manager_email = ""
  aliases = ["platform"]
  
}
resource "opslevel_team" "platform_2" {
  name = "Platform"
  manager_email = ""
  aliases = ["platform_2"]
  
}

After (places a line space between resources):

resource "opslevel_team" "platform" {
  aliases = ["platform"]
  name = "platform"
  member {
    email = "[email protected]"
    role = "contributor"
  }
  member {
    email = "[email protected]"
    role = "contributor"
  }
}

resource "opslevel_team" "platform_2" {
  aliases = ["platform_2"]
  name = "Platform"
  group = "platform"
  member {
    email = "[email protected]"
    role = "contributor"
  }
  parent = ["engineering"]
}

@ghost ghost self-assigned this Nov 27, 2023
@ghost ghost force-pushed the export-teams branch from 53b673c to f4cdf15 Compare November 27, 2023 21:13
Copy link

codecov bot commented Nov 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ad801cd) 6.37% compared to head (93831d7) 6.37%.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #206   +/-   ##
=====================================
  Coverage   6.37%   6.37%           
=====================================
  Files          5       5           
  Lines        251     251           
=====================================
  Hits          16      16           
  Misses       235     235           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ghost ghost force-pushed the export-teams branch from 3ca8215 to fc47d58 Compare November 27, 2023 22:01
@ghost ghost changed the title better team export Export Teams to Terraform Nov 27, 2023
@ghost ghost changed the title Export Teams to Terraform Export Teams and team memberships to Terraform Nov 27, 2023
@ghost ghost marked this pull request as ready for review November 27, 2023 22:19
@ghost ghost requested review from davidbloss and rocktavious November 27, 2023 22:19
`
resp, err := c.ListTeams(nil)
teams := resp.Nodes
cobra.CheckErr(err)
for _, team := range teams {
teamBody := ""

aliases := flattenAliases(team.Aliases)
if len(aliases) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

As of 12 days ago team aliases are required so this len() check should be removed.

`
resp, err := c.ListTeams(nil)
teams := resp.Nodes
cobra.CheckErr(err)
for _, team := range teams {
teamBody := ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please introduce strings.Builder here?
We can set teamBody := strings.Builder{} just above this for loop then:

  1. sb.WriteString("strings below")
  2. sb.String() // to return the whole string to config.WriteString(templateConfig() below
  3. sb.Reset() // to reset the underlying buffer at the end of each loop iteration

Running this command is surprisingly slow and this would be a small step in a more optimized direction IMO

Copy link
Author

Choose a reason for hiding this comment

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

Added!

}`
membersOutput += fmt.Sprintf(memberConfig, member.User.Email, member.Role)
}
if len(membersOutput) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This if len() check is safe to remove since appending an empty string doesn't really change teamBody

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually where membersOutput starts down to here seems like a good candidate for a small function. Lmk if you would like to discuss :)
Something loosely like membersOutput := getMembershipsAsTerraformConfig(team.Memberships.Nodes)

Copy link
Author

Choose a reason for hiding this comment

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

Implemented these suggestions

@ghost ghost requested a review from davidbloss November 28, 2023 16:59
func getMembershipsAsTerraformConfig(members []opslevel.TeamMembership) string {
membersBody := strings.Builder{}
for _, member := range members {
memberConfig := `
Copy link
Contributor

Choose a reason for hiding this comment

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

Can memberConfig be moved out of the for loop?
Overall this looks good! 👍

Copy link
Author

Choose a reason for hiding this comment

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

Moved!

@ghost ghost requested a review from davidbloss November 28, 2023 18:07
Copy link
Contributor

@davidbloss davidbloss left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@ghost ghost merged commit 18e7845 into main Nov 28, 2023
6 checks passed
@ghost ghost deleted the export-teams branch November 28, 2023 20:03
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant