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 9 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
46 changes: 33 additions & 13 deletions src/cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,35 +307,55 @@ func exportServices(c *opslevel.Client, shell *os.File, directory string) {
}
}

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!

member {
email = "%s"
role = "%s"
}`
membersBody.WriteString(fmt.Sprintf(memberConfig, member.User.Email, member.Role))
}
return membersBody.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)
teamBody := strings.Builder{}
This conversation was marked as resolved.
Show resolved Hide resolved
for _, team := range teams {
aliases := flattenAliases(team.Aliases)
if len(aliases) > 0 {
aliases = fmt.Sprintf("aliases = [\"%s\"]", aliases)
teamBody.WriteString(fmt.Sprintf("\n aliases = [\"%s\"]", aliases))
teamBody.WriteString(fmt.Sprintf("\n name = \"%s\"", team.Name))

if len(team.Group.Alias) > 0 {
This conversation was marked as resolved.
Show resolved Hide resolved
teamBody.WriteString(fmt.Sprintf("\n group = \"%s\"", team.Group.Alias))
}
membersOutput := getMembershipsAsTerraformConfig(team.Memberships.Nodes)
teamBody.WriteString(membersOutput)
if len(team.ParentTeam.Alias) > 0 {
teamBody.WriteString(fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias))
}
if len(team.Responsibilities) > 0 {
responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities)
teamBody.WriteString(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.String(),
))
shell.WriteString(fmt.Sprintf("terraform import opslevel_team.%s %s\n", team.Alias, team.Id))
teamBody.Reset()
}
shell.WriteString("##########\n\n")
}
Expand Down
Loading