diff --git a/.changes/unreleased/Bugfix-20231127-171854.yaml b/.changes/unreleased/Bugfix-20231127-171854.yaml new file mode 100644 index 00000000..69ab25fa --- /dev/null +++ b/.changes/unreleased/Bugfix-20231127-171854.yaml @@ -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 diff --git a/.changes/unreleased/Feature-20231127-171923.yaml b/.changes/unreleased/Feature-20231127-171923.yaml new file mode 100644 index 00000000..a214d4af --- /dev/null +++ b/.changes/unreleased/Feature-20231127-171923.yaml @@ -0,0 +1,3 @@ +kind: Feature +body: Support for exporting Team Memberships in Terraform +time: 2023-11-27T17:19:23.084993-05:00 diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 422f8b90..b7884c0d 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -307,35 +307,56 @@ func exportServices(c *opslevel.Client, shell *os.File, directory string) { } } +func getMembershipsAsTerraformConfig(members []opslevel.TeamMembership) string { + memberConfig := ` + member { + email = "%s" + role = "%s" + }` + + membersBody := strings.Builder{} + for _, member := range members { + 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{} 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 { + 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") }