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
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions .changes/unreleased/Bugfix-20231127-171854.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Bugfix
body: Fix exporting Teams in Terraform to have up to have a schema compatible with
v0.8.13
time: 2023-11-27T17:18:54.983105-05:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20231127-171923.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: Support for exporting Team Memberships in Terraform
time: 2023-11-27T17:19:23.084993-05:00
43 changes: 31 additions & 12 deletions src/cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,49 @@ func exportServices(c *opslevel.Client, shell *os.File, directory string) {
func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) {
shell.WriteString("# Teams\n")

teamConfig := `resource "opslevel_team" "%s" {
name = "%s"
manager_email = "%s"
parent = "%s"
%s
%s
teamConfig := `resource "opslevel_team" "%s" {%s
}

`
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!


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.

aliases = fmt.Sprintf("aliases = [\"%s\"]", aliases)
teamBody += fmt.Sprintf("\n aliases = [\"%s\"]", aliases)
}
teamBody += fmt.Sprintf("\n name = \"%s\"", team.Name)

if len(team.Group.Alias) > 0 {
This conversation was marked as resolved.
Show resolved Hide resolved
teamBody += fmt.Sprintf("\n group = \"%s\"", team.Group.Alias)
}
membersOutput := ""
for _, member := range team.Memberships.Nodes {
memberConfig := `
member {
email = "%s"
role = "%s"
}`
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

teamBody += membersOutput
}
if len(team.ParentTeam.Alias) > 0 {
teamBody += fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias)
}
if len(team.Responsibilities) > 0 {
responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities)
teamBody += fmt.Sprintf("\n %s", responsibilities)
}

config.WriteString(templateConfig(
teamConfig,
team.Alias,
team.Name,
team.Manager.Email,
team.ParentTeam.Alias,
aliases,
buildMultilineStringArg("responsibilities", team.Responsibilities),
teamBody,
))
shell.WriteString(fmt.Sprintf("terraform import opslevel_team.%s %s\n", team.Alias, team.Id))
}
Expand Down
Loading