diff --git a/docs/data-sources/team.md b/docs/data-sources/team.md index 13587fa..c5509fb 100644 --- a/docs/data-sources/team.md +++ b/docs/data-sources/team.md @@ -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)) + + +### Nested Schema for `members` + +Read-Only: + +- `description` (String) +- `email` (String) +- `name` (String) + + + +### Nested Schema for `slack_channels` + +Read-Only: + +- `name` (String) +- `notifications_enabled` (Boolean) diff --git a/internal/cortex/teams.go b/internal/cortex/teams.go index e6fa5be..a43ac41 100644 --- a/internal/cortex/teams.go +++ b/internal/cortex/teams.go @@ -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 { @@ -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 { @@ -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 { diff --git a/internal/provider/team_data_source.go b/internal/provider/team_data_source.go index 85fee60..b72cf60 100644 --- a/internal/provider/team_data_source.go +++ b/internal/provider/team_data_source.go @@ -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) { @@ -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, + }, + }, + }, + }, }, } } @@ -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)...) } diff --git a/internal/provider/team_data_source_test.go b/internal/provider/team_data_source_test.go index 51d7028..6932e11 100644 --- a/internal/provider/team_data_source_test.go +++ b/internal/provider/team_data_source_test.go @@ -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: "Test User", Email: "test@example.com"}, + }, + 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"), + ), + }, + }, + }) +}