Skip to content

Commit 7e385b7

Browse files
authored
Add datasource for teams (#510)
1 parent 4884bd7 commit 7e385b7

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

docs/data-sources/team.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_team Data Source - terraform-provider-grafana"
4+
subcategory: ""
5+
description: |-
6+
Official documentation https://grafana.com/docs/grafana/latest/administration/manage-users-and-permissions/manage-teams/HTTP API https://grafana.com/docs/grafana/latest/http_api/team/
7+
---
8+
9+
# grafana_team (Data Source)
10+
11+
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/manage-users-and-permissions/manage-teams/)
12+
* [HTTP API](https://grafana.com/docs/grafana/latest/http_api/team/)
13+
14+
## Example Usage
15+
16+
```terraform
17+
resource "grafana_team" "test" {
18+
name = "test-team"
19+
20+
}
21+
22+
data "grafana_team" "from_name" {
23+
name = grafana_team.test.name
24+
}
25+
```
26+
27+
<!-- schema generated by tfplugindocs -->
28+
## Schema
29+
30+
### Required
31+
32+
- `name` (String) The name of the Grafana team.
33+
34+
### Read-Only
35+
36+
- `id` (String) The ID of this resource.
37+
38+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "grafana_team" "test" {
2+
name = "test-team"
3+
4+
}
5+
6+
data "grafana_team" "from_name" {
7+
name = grafana_team.test.name
8+
}

grafana/data_source_team.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package grafana
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
gapi "github.com/grafana/grafana-api-golang-client"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
func DatasourceTeam() *schema.Resource {
14+
return &schema.Resource{
15+
Description: `
16+
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/manage-users-and-permissions/manage-teams/)
17+
* [HTTP API](https://grafana.com/docs/grafana/latest/http_api/team/)
18+
`,
19+
ReadContext: dataSourceTeamRead,
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "The name of the Grafana team.",
25+
},
26+
},
27+
}
28+
}
29+
30+
func findTeamWithName(client *gapi.Client, name string) (*gapi.Team, error) {
31+
searchTeam, err := client.SearchTeam(name)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
for _, f := range searchTeam.Teams {
37+
if f.Name == name {
38+
// Query the team by ID, that API has additional information
39+
return client.Team(f.ID)
40+
}
41+
}
42+
43+
return nil, fmt.Errorf("no team with name %q", name)
44+
}
45+
46+
func dataSourceTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
47+
client := meta.(*client).gapi
48+
name := d.Get("name").(string)
49+
team, err := findTeamWithName(client, name)
50+
51+
if err != nil {
52+
return diag.FromErr(err)
53+
}
54+
55+
id := strconv.FormatInt(team.ID, 10)
56+
d.SetId(id)
57+
d.Set("name", team.Name)
58+
59+
return nil
60+
}

grafana/data_source_team_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package grafana
2+
3+
import (
4+
"testing"
5+
6+
gapi "github.com/grafana/grafana-api-golang-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccDatasourceTeam(t *testing.T) {
11+
CheckOSSTestsEnabled(t)
12+
13+
var team gapi.Team
14+
checks := []resource.TestCheckFunc{
15+
testAccTeamCheckExists("grafana_team.test", &team),
16+
resource.TestCheckResourceAttr(
17+
"data.grafana_team.from_name", "name", "test-team",
18+
),
19+
resource.TestMatchResourceAttr(
20+
"data.grafana_team.from_name", "id", idRegexp,
21+
),
22+
}
23+
24+
resource.Test(t, resource.TestCase{
25+
ProviderFactories: testAccProviderFactories,
26+
CheckDestroy: testAccTeamCheckDestroy(&team),
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccExample(t, "data-sources/grafana_team/data-source.tf"),
30+
Check: resource.ComposeTestCheckFunc(checks...),
31+
},
32+
},
33+
})
34+
}

grafana/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func Provider(version string) func() *schema.Provider {
208208
"grafana_folder": DatasourceFolder(),
209209
"grafana_library_panel": DatasourceLibraryPanel(),
210210
"grafana_user": DatasourceUser(),
211+
"grafana_team": DatasourceTeam(),
211212
"grafana_organization": DatasourceOrganization(),
212213

213214
// Cloud

0 commit comments

Comments
 (0)