From f4cdf15032d98f56089ead1f53cc5d7488c3e044 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 16:12:06 -0500 Subject: [PATCH 01/10] better team export --- src/cmd/terraform.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 422f8b90..2ac9f6d3 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -311,9 +311,10 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { shell.WriteString("# Teams\n") teamConfig := `resource "opslevel_team" "%s" { + %s name = "%s" - manager_email = "%s" - parent = "%s" + %s + %s %s %s } @@ -326,13 +327,41 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { if len(aliases) > 0 { aliases = fmt.Sprintf("aliases = [\"%s\"]", aliases) } + + group := "" + if team.Group.Alias != "" { + group = fmt.Sprintf("group = [\"%s\"]", team.Group.Alias) + } + + membersOutput := "" + for _, member := range team.Memberships.Nodes { + memberConfig := `member { + email = "%s" + role = "%s" +} +` + + config.WriteString(templateConfig( + memberConfig, + member.User.Email, + member.Role, + )) + membersOutput += memberConfig + } + + parent := "" + if team.ParentTeam.Alias != "" { + parent = fmt.Sprintf("parent = [\"%s\"]", team.ParentTeam.Alias) + } + config.WriteString(templateConfig( teamConfig, team.Alias, - team.Name, - team.Manager.Email, - team.ParentTeam.Alias, aliases, + team.Name, + group, + membersOutput, + parent, buildMultilineStringArg("responsibilities", team.Responsibilities), )) shell.WriteString(fmt.Sprintf("terraform import opslevel_team.%s %s\n", team.Alias, team.Id)) From e3b14afdbdcd52c432441616e9f5d71e5faae98d Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 16:45:54 -0500 Subject: [PATCH 02/10] formatting --- src/cmd/terraform.go | 62 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 2ac9f6d3..da9a4c66 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -96,13 +96,13 @@ provider "opslevel" { graphqlClient := getClientGQL() - exportConstants(graphqlClient, constants) - exportRepos(graphqlClient, repos, bash) - exportServices(graphqlClient, bash, directory) + // exportConstants(graphqlClient, constants) + // exportRepos(graphqlClient, repos, bash) + // exportServices(graphqlClient, bash, directory) exportTeams(graphqlClient, teams, bash) - exportFilters(graphqlClient, filters, bash) - exportRubric(graphqlClient, rubric, bash) - exportChecks(graphqlClient, bash, directory) + // exportFilters(graphqlClient, filters, bash) + // exportRubric(graphqlClient, rubric, bash) + // exportChecks(graphqlClient, bash, directory) fmt.Println("Complete!") } @@ -311,58 +311,48 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { shell.WriteString("# Teams\n") teamConfig := `resource "opslevel_team" "%s" { - %s - name = "%s" - %s - %s - %s - %s +%s } ` resp, err := c.ListTeams(nil) teams := resp.Nodes cobra.CheckErr(err) for _, team := range teams { + teamBody := "" + aliases := flattenAliases(team.Aliases) if len(aliases) > 0 { - aliases = fmt.Sprintf("aliases = [\"%s\"]", aliases) + teamBody += fmt.Sprintf(" aliases = [\"%s\"]\n", aliases) } + teamBody += fmt.Sprintf(" name = \"%s\"\n", team.Name) - group := "" if team.Group.Alias != "" { - group = fmt.Sprintf("group = [\"%s\"]", team.Group.Alias) + teamBody += fmt.Sprintf(" group = \"%s\"\n", team.Group.Alias) } - membersOutput := "" for _, member := range team.Memberships.Nodes { - memberConfig := `member { - email = "%s" - role = "%s" -} + memberConfig := ` member { + email = "%s" + role = "%s" + } ` - - config.WriteString(templateConfig( - memberConfig, - member.User.Email, - member.Role, - )) - membersOutput += memberConfig + membersOutput += fmt.Sprintf(memberConfig, member.User.Email, member.Role) + } + if len(membersOutput) > 0 { + teamBody += membersOutput } - - parent := "" if team.ParentTeam.Alias != "" { - parent = fmt.Sprintf("parent = [\"%s\"]", team.ParentTeam.Alias) + teamBody += fmt.Sprintf(" parent = [\"%s\"]\n", team.ParentTeam.Alias) + } + responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities) + if len(responsibilities) > 0 { + teamBody += fmt.Sprintf(" %s", responsibilities) } config.WriteString(templateConfig( teamConfig, team.Alias, - aliases, - team.Name, - group, - membersOutput, - parent, - buildMultilineStringArg("responsibilities", team.Responsibilities), + teamBody, )) shell.WriteString(fmt.Sprintf("terraform import opslevel_team.%s %s\n", team.Alias, team.Id)) } From fc47d58f2d9886103796cb5fcc3ddfbb94af8975 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 17:00:31 -0500 Subject: [PATCH 03/10] formatting --- src/cmd/terraform.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index da9a4c66..4224a77c 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -310,8 +310,7 @@ 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" { -%s + teamConfig := `resource "opslevel_team" "%s" {%s } ` resp, err := c.ListTeams(nil) @@ -322,31 +321,31 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { aliases := flattenAliases(team.Aliases) if len(aliases) > 0 { - teamBody += fmt.Sprintf(" aliases = [\"%s\"]\n", aliases) + teamBody += fmt.Sprintf("\n aliases = [\"%s\"]", aliases) } - teamBody += fmt.Sprintf(" name = \"%s\"\n", team.Name) + teamBody += fmt.Sprintf("\n name = \"%s\"", team.Name) - if team.Group.Alias != "" { - teamBody += fmt.Sprintf(" group = \"%s\"\n", team.Group.Alias) + if len(team.Group.Alias) > 0 { + teamBody += fmt.Sprintf("\n group = \"%s\"", team.Group.Alias) } membersOutput := "" for _, member := range team.Memberships.Nodes { - memberConfig := ` member { + memberConfig := ` + member { email = "%s" role = "%s" - } -` + }` membersOutput += fmt.Sprintf(memberConfig, member.User.Email, member.Role) } if len(membersOutput) > 0 { teamBody += membersOutput } - if team.ParentTeam.Alias != "" { - teamBody += fmt.Sprintf(" parent = [\"%s\"]\n", team.ParentTeam.Alias) + if len(team.ParentTeam.Alias) > 0 { + teamBody += fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias) } - responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities) - if len(responsibilities) > 0 { - teamBody += fmt.Sprintf(" %s", responsibilities) + if len(team.Responsibilities) > 0 { + responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities) + teamBody += fmt.Sprintf("\n %s", responsibilities) } config.WriteString(templateConfig( From c242a9d87b5c140eb544b1ce06c91ecb38026503 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 17:07:58 -0500 Subject: [PATCH 04/10] line space --- src/cmd/terraform.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 4224a77c..065dfcc7 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -312,6 +312,7 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { teamConfig := `resource "opslevel_team" "%s" {%s } + ` resp, err := c.ListTeams(nil) teams := resp.Nodes From 197a64e8925075c64c09e61266f206067ad517e4 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 17:17:55 -0500 Subject: [PATCH 05/10] remove testing code --- src/cmd/terraform.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 065dfcc7..5a7504ea 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -96,13 +96,13 @@ provider "opslevel" { graphqlClient := getClientGQL() - // exportConstants(graphqlClient, constants) - // exportRepos(graphqlClient, repos, bash) - // exportServices(graphqlClient, bash, directory) + exportConstants(graphqlClient, constants) + exportRepos(graphqlClient, repos, bash) + exportServices(graphqlClient, bash, directory) exportTeams(graphqlClient, teams, bash) - // exportFilters(graphqlClient, filters, bash) - // exportRubric(graphqlClient, rubric, bash) - // exportChecks(graphqlClient, bash, directory) + exportFilters(graphqlClient, filters, bash) + exportRubric(graphqlClient, rubric, bash) + exportChecks(graphqlClient, bash, directory) fmt.Println("Complete!") } From 86a9fe445d3f22c118d71c3e08307507e2f59bbd Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Mon, 27 Nov 2023 17:19:32 -0500 Subject: [PATCH 06/10] changie --- .changes/unreleased/Bugfix-20231127-171854.yaml | 4 ++++ .changes/unreleased/Feature-20231127-171923.yaml | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 .changes/unreleased/Bugfix-20231127-171854.yaml create mode 100644 .changes/unreleased/Feature-20231127-171923.yaml 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 From 4cefc306fc518b50544b05c47aba20f43616c727 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Tue, 28 Nov 2023 11:47:24 -0500 Subject: [PATCH 07/10] use string.Builder{} --- src/cmd/terraform.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 5a7504ea..cc645af8 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -317,17 +317,14 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { resp, err := c.ListTeams(nil) teams := resp.Nodes cobra.CheckErr(err) + teamBody := strings.Builder{} for _, team := range teams { - teamBody := "" - aliases := flattenAliases(team.Aliases) - if len(aliases) > 0 { - teamBody += fmt.Sprintf("\n aliases = [\"%s\"]", aliases) - } - teamBody += fmt.Sprintf("\n name = \"%s\"", team.Name) + teamBody.WriteString(fmt.Sprintf("\n aliases = [\"%s\"]", aliases)) + teamBody.WriteString(fmt.Sprintf("\n name = \"%s\"", team.Name)) if len(team.Group.Alias) > 0 { - teamBody += fmt.Sprintf("\n group = \"%s\"", team.Group.Alias) + teamBody.WriteString(fmt.Sprintf("\n group = \"%s\"", team.Group.Alias)) } membersOutput := "" for _, member := range team.Memberships.Nodes { @@ -339,22 +336,23 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { membersOutput += fmt.Sprintf(memberConfig, member.User.Email, member.Role) } if len(membersOutput) > 0 { - teamBody += membersOutput + teamBody.WriteString(membersOutput) } if len(team.ParentTeam.Alias) > 0 { - teamBody += fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias) + teamBody.WriteString(fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias)) } if len(team.Responsibilities) > 0 { responsibilities := buildMultilineStringArg("responsibilities", team.Responsibilities) - teamBody += fmt.Sprintf("\n %s", responsibilities) + teamBody.WriteString(fmt.Sprintf("\n %s", responsibilities)) } config.WriteString(templateConfig( teamConfig, team.Alias, - teamBody, + teamBody.String(), )) shell.WriteString(fmt.Sprintf("terraform import opslevel_team.%s %s\n", team.Alias, team.Id)) + teamBody.Reset() } shell.WriteString("##########\n\n") } From edebe3c7eda0395690e3b839394204d89276f7a9 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Tue, 28 Nov 2023 11:53:04 -0500 Subject: [PATCH 08/10] remove unnecessary len check --- src/cmd/terraform.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index cc645af8..abacb503 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -335,9 +335,7 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { }` membersOutput += fmt.Sprintf(memberConfig, member.User.Email, member.Role) } - if len(membersOutput) > 0 { - teamBody.WriteString(membersOutput) - } + teamBody.WriteString(membersOutput) if len(team.ParentTeam.Alias) > 0 { teamBody.WriteString(fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias)) } From 0e02e33ff05408ff646bd324f181e39fe9a6949d Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Tue, 28 Nov 2023 11:59:03 -0500 Subject: [PATCH 09/10] getMembershipsAsTerraformConfig --- src/cmd/terraform.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index abacb503..362b773e 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -307,6 +307,19 @@ func exportServices(c *opslevel.Client, shell *os.File, directory string) { } } +func getMembershipsAsTerraformConfig(members []opslevel.TeamMembership) string { + membersBody := strings.Builder{} + for _, member := range members { + memberConfig := ` + 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") @@ -326,15 +339,7 @@ func exportTeams(c *opslevel.Client, config *os.File, shell *os.File) { if len(team.Group.Alias) > 0 { teamBody.WriteString(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) - } + membersOutput := getMembershipsAsTerraformConfig(team.Memberships.Nodes) teamBody.WriteString(membersOutput) if len(team.ParentTeam.Alias) > 0 { teamBody.WriteString(fmt.Sprintf("\n parent = [\"%s\"]", team.ParentTeam.Alias)) From 93831d7a90091ab8483f9250d5533c47fdd16950 Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Tue, 28 Nov 2023 13:07:11 -0500 Subject: [PATCH 10/10] move membersBody --- src/cmd/terraform.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cmd/terraform.go b/src/cmd/terraform.go index 362b773e..b7884c0d 100644 --- a/src/cmd/terraform.go +++ b/src/cmd/terraform.go @@ -308,13 +308,14 @@ func exportServices(c *opslevel.Client, shell *os.File, directory string) { } func getMembershipsAsTerraformConfig(members []opslevel.TeamMembership) string { - membersBody := strings.Builder{} - for _, member := range members { - memberConfig := ` + 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()