Skip to content

feat: return team members and slack channels in team data source #53

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ Team data source
### Read-Only

- `id` (String) The ID of this resource.
- `members` (Attributes List) List of team members. (see [below for nested schema](#nestedatt--members))
- `slack_channels` (Attributes List) List of Slack channels associated with the team. (see [below for nested schema](#nestedatt--slack_channels))

<a id="nestedatt--members"></a>
### Nested Schema for `members`

Read-Only:

- `description` (String)
- `email` (String)
- `name` (String)


<a id="nestedatt--slack_channels"></a>
### Nested Schema for `slack_channels`

Read-Only:

- `name` (String)
- `notifications_enabled` (Boolean)
11 changes: 6 additions & 5 deletions internal/cortex/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Team struct {
SlackChannels []TeamSlackChannel `json:"slackChannels,omitempty"`
Links []TeamLink `json:"links,omitempty"`
TeamTag string `json:"teamTag"`
CortexTeam TeamCortexManaged `json:"cortexTeam,omitempty"`
}

type TeamMetadata struct {
Expand All @@ -50,9 +51,9 @@ type TeamMetadata struct {
}

type TeamMember struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
Email string `json:"email"`
Description string `json:"description,omitempty" tfsdk:"description"`
Name string `json:"name" tfsdk:"name"`
Email string `json:"email" tfsdk:"email"`
}

type TeamLink struct {
Expand All @@ -63,8 +64,8 @@ type TeamLink struct {
}

type TeamSlackChannel struct {
Name string `json:"name"`
NotificationsEnabled bool `json:"notificationsEnabled"`
Name string `json:"name" tfsdk:"name"`
NotificationsEnabled bool `json:"notificationsEnabled" tfsdk:"notifications_enabled"`
}

type TeamIdpGroup struct {
Expand Down
41 changes: 39 additions & 2 deletions internal/provider/team_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ type TeamDataSource struct {

// TeamDataSourceModel describes the data source data model.
type TeamDataSourceModel struct {
Id types.String `tfsdk:"id"`
Tag types.String `tfsdk:"tag"`
Id types.String `tfsdk:"id"`
Tag types.String `tfsdk:"tag"`
Members []cortex.TeamMember `tfsdk:"members"`
SlackChannels []cortex.TeamSlackChannel `tfsdk:"slack_channels"`
}

func (d *TeamDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -47,6 +49,37 @@ func (d *TeamDataSource) Schema(ctx context.Context, req datasource.SchemaReques
"id": schema.StringAttribute{
Computed: true,
},
"members": schema.ListNestedAttribute{
MarkdownDescription: "List of team members.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
},
"email": schema.StringAttribute{
Computed: true,
},
"description": schema.StringAttribute{
Computed: true,
},
},
},
},
"slack_channels": schema.ListNestedAttribute{
MarkdownDescription: "List of Slack channels associated with the team.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
},
"notifications_enabled": schema.BoolAttribute{
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -86,9 +119,13 @@ func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read team, got error: %s", err))
return
}

data.Id = types.StringValue(teamResponse.TeamTag)
data.Tag = types.StringValue(teamResponse.TeamTag)

data.Members = teamResponse.CortexTeam.Members
data.SlackChannels = teamResponse.SlackChannels

// Write to TF state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
80 changes: 59 additions & 21 deletions internal/provider/team_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
package provider_test

//func TestAccTeamDataSource(t *testing.T) {
// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// Steps: []resource.TestStep{
// // Read testing
// {
// Config: testAccTeamDataSourceConfig,
// Check: resource.ComposeAggregateTestCheckFunc(
// resource.TestCheckResourceAttr("data.cortex_team.platform_engineering", "tag", "platform_engineering"),
// ),
// },
// },
// })
//}
//
//const testAccTeamDataSourceConfig = `
//data "cortex_team" "engineering" {
// tag = "platform_engineering"
//}
//`
import (
"testing"

"github.com/cortexapps/terraform-provider-cortex/internal/cortex"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

type TestTeamDataSource struct {
Tag string
Members []cortex.TeamMember
SlackChannels []cortex.TeamSlackChannel
}

func (t *TestTeamDataSource) ToStubTeam() *cortex.Team {
return &cortex.Team{
TeamTag: t.Tag,
CortexTeam: cortex.TeamCortexManaged{
Members: t.Members,
},
SlackChannels: t.SlackChannels,
}
}

func testAccTeamDataSourceConfig(tag string) string {
return `
data "cortex_team" "test-team" {
tag = "` + tag + `"
}
`
}

func TestAccTeamDataSource(t *testing.T) {
stub := TestTeamDataSource{
Tag: "test-team",
Members: []cortex.TeamMember{
{Name: "max", Email: "[email protected]"},
},
SlackChannels: []cortex.TeamSlackChannel{
{Name: "test-channel", NotificationsEnabled: true},
},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccTeamDataSourceConfig(stub.Tag),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.cortex_team.test-team", "tag", stub.Tag),
resource.TestCheckResourceAttr("data.cortex_team.test-team", "members.0.name", stub.Members[0].Name),
resource.TestCheckResourceAttr("data.cortex_team.test-team", "members.0.email", stub.Members[0].Email),
resource.TestCheckResourceAttr("data.cortex_team.test-team", "slack_channels.0.name", stub.SlackChannels[0].Name),
resource.TestCheckResourceAttr("data.cortex_team.test-team", "slack_channels.0.notifications_enabled", "true"),
),
},
},
})
}