Skip to content

Commit 1879c96

Browse files
author
Nico Gelders
committed
added team/teams data resource
1 parent 79fb6fb commit 1879c96

File tree

12 files changed

+437
-2
lines changed

12 files changed

+437
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ Terraform provider to manage Dagster Cloud resources.
1515
| Deployment | :heavy_check_mark: | :x: |
1616
| Deployment settings | :heavy_check_mark: | :x: |
1717
| Code location | :heavy_check_mark: | :x: |
18-
| Team | :heavy_check_mark: | :x: |
18+
| Team(s) | :heavy_check_mark: | :heavy_check_mark: |
1919
| Team membership | :heavy_check_mark: | :x: |

docs/data-sources/team.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dagster_team Data Source - dagster"
4+
subcategory: ""
5+
description: |-
6+
Retrieve information about a Dagster Cloud team.
7+
---
8+
9+
# dagster_team (Data Source)
10+
11+
Retrieve information about a Dagster Cloud team.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "dagster_team" "team" {
17+
name = "my-team-name"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `name` (String) Name the Dagster Cloud team
27+
28+
### Read-Only
29+
30+
- `id` (String) Team id

docs/data-sources/teams.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dagster_teams Data Source - dagster"
4+
subcategory: ""
5+
description: |-
6+
Retrieve information about a Dagster Cloud teams.
7+
---
8+
9+
# dagster_teams (Data Source)
10+
11+
Retrieve information about a Dagster Cloud teams.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "dagster_teams" "teams" {
17+
regex_filter = "^my-team"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `regex_filter` (String) Regex filter to select the Dagster Cloud teams
27+
28+
### Read-Only
29+
30+
- `teams` (Attributes List) Teams (see [below for nested schema](#nestedatt--teams))
31+
32+
<a id="nestedatt--teams"></a>
33+
### Nested Schema for `teams`
34+
35+
Required:
36+
37+
- `name` (String) Name the Dagster Cloud team
38+
39+
Read-Only:
40+
41+
- `id` (String) Team id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "dagster_team" "team" {
2+
name = "my-team-name"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "dagster_teams" "teams" {
2+
regex_filter = "^my-team"
3+
}

internal/client/service/teams.go

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"regexp"
78

89
"github.com/Khan/genqlient/graphql"
910
"github.com/datarootsio/terraform-provider-dagster/internal/client/schema"
@@ -51,6 +52,29 @@ func (c *TeamsClient) GetTeamByName(ctx context.Context, name string) (schema.Te
5152
return schema.Team{}, &types.ErrNotFound{What: "Team", Key: "name", Value: name}
5253
}
5354

55+
func (c *TeamsClient) GetTeamsByRegex(ctx context.Context, regex string) ([]schema.Team, error) {
56+
teams, err := c.ListTeams(ctx)
57+
if err != nil {
58+
return []schema.Team{}, err
59+
}
60+
61+
regexExpression, err := regexp.Compile(regex)
62+
if err != nil {
63+
return []schema.Team{}, err
64+
}
65+
66+
matchedTeams := make([]schema.Team, 0)
67+
68+
for _, team := range teams {
69+
match := regexExpression.MatchString(team.Name)
70+
if match {
71+
matchedTeams = append(matchedTeams, team)
72+
}
73+
}
74+
75+
return matchedTeams, nil
76+
}
77+
5478
func (c *TeamsClient) GetTeamById(ctx context.Context, id string) (schema.Team, error) {
5579
teams, err := c.ListTeams(ctx)
5680
if err != nil {

internal/client/service/teams_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestTeamsService_BasicCRUD(t *testing.T) {
2222
teamName := "testing/my_team"
2323
teamNameRenamed := "testing/my_team_renamed"
2424

25-
// Ensure no teams with the test names exist
25+
// Ensure no teams with the test names exists
2626
_, err := teamsClient.GetTeamByName(ctx, teamName)
2727
assert.ErrorAs(t, err, &errNotFound)
2828

@@ -33,6 +33,10 @@ func TestTeamsService_BasicCRUD(t *testing.T) {
3333
assert.NoError(t, err)
3434
assert.Equal(t, teamName, teamCreated.Name, "Expected team names to be the same.")
3535

36+
t.Cleanup(func() {
37+
_ = teamsClient.DeleteTeam(ctx, teamCreated.Id)
38+
})
39+
3640
teamById, err := teamsClient.GetTeamById(ctx, teamCreated.Id)
3741
assert.NoError(t, err)
3842
assert.Equal(t, teamName, teamById.Name, "Expected team names to be the same.")
@@ -41,6 +45,10 @@ func TestTeamsService_BasicCRUD(t *testing.T) {
4145
assert.NoError(t, err)
4246
assert.Equal(t, teamName, teamByName.Name, "Expected team names to be the same.")
4347

48+
teams, err := teamsClient.GetTeamsByRegex(ctx, "^testing/")
49+
assert.NoError(t, err)
50+
assert.Len(t, teams, 1)
51+
4452
_, err = teamsClient.RenameTeam(ctx, teamNameRenamed, teamCreated.Id)
4553
assert.NoError(t, err)
4654

internal/provider/datasources/team.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package datasources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/datarootsio/terraform-provider-dagster/internal/client"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
)
12+
13+
// Ensure the implementation satisfies the expected interfaces.
14+
var (
15+
_ datasource.DataSource = &TeamDataSource{}
16+
_ datasource.DataSourceWithConfigure = &TeamDataSource{}
17+
)
18+
19+
type TeamDataSource struct {
20+
client client.DagsterClient
21+
}
22+
23+
type TeamDataSourceModel struct {
24+
Name types.String `tfsdk:"name"`
25+
Id types.String `tfsdk:"id"`
26+
}
27+
28+
//nolint:ireturn // required by Terraform API
29+
func NewTeamDataSource() datasource.DataSource {
30+
return &TeamDataSource{}
31+
}
32+
33+
// Metadata returns the data source type name.
34+
func (d *TeamDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
35+
resp.TypeName = req.ProviderTypeName + "_team"
36+
}
37+
38+
var teamAttributes = map[string]schema.Attribute{
39+
"name": schema.StringAttribute{
40+
Required: true,
41+
Computed: false,
42+
Description: "Name the Dagster Cloud team",
43+
},
44+
"id": schema.StringAttribute{
45+
Computed: true,
46+
Description: "Team id",
47+
},
48+
}
49+
50+
// Schema defines the schema for the data source.
51+
func (d *TeamDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
52+
resp.Schema = schema.Schema{
53+
Description: `Retrieve information about a Dagster Cloud team.`,
54+
Attributes: teamAttributes,
55+
}
56+
}
57+
58+
// Configure adds the provider-configured client to the data source.
59+
func (d *TeamDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
60+
if req.ProviderData == nil {
61+
return
62+
}
63+
64+
client, ok := req.ProviderData.(client.DagsterClient)
65+
if !ok {
66+
resp.Diagnostics.AddError(
67+
"Unexpected Data Source Configure Type",
68+
fmt.Sprintf("Expected client.DagsterClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
69+
)
70+
71+
return
72+
}
73+
74+
d.client = client
75+
}
76+
77+
// Read refreshes the Terraform state with the latest data.
78+
func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
79+
var data TeamDataSourceModel
80+
81+
// Read Terraform configuration data into the model
82+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
83+
84+
if resp.Diagnostics.HasError() {
85+
return
86+
}
87+
88+
team, err := d.client.TeamsClient.GetTeamByName(ctx, data.Name.ValueString())
89+
if err != nil {
90+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get team information, got error: %s", err))
91+
return
92+
}
93+
94+
data.Id = types.StringValue(team.Id)
95+
data.Name = types.StringValue(team.Name)
96+
97+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
98+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package datasources_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/datarootsio/terraform-provider-dagster/internal/testutils"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/terraform"
11+
)
12+
13+
func testAccTeamConfig(name string) string {
14+
return fmt.Sprintf(testutils.ProviderConfig+`
15+
data "dagster_team" "this" {
16+
name = "%s"
17+
}
18+
`, name)
19+
}
20+
21+
func TestAccTeam(t *testing.T) {
22+
name := "test-team"
23+
var teamId string
24+
var teamName string
25+
26+
resource.Test(t, resource.TestCase{
27+
PreCheck: func() { testutils.AccTestPreCheck(t) },
28+
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
29+
Steps: []resource.TestStep{
30+
{
31+
Config: testAccTeamConfig(name),
32+
Check: resource.ComposeAggregateTestCheckFunc(
33+
testutils.FetchValueFromState("data.dagster_team.this", "id", &teamId),
34+
testutils.FetchValueFromState("data.dagster_team.this", "name", &teamName),
35+
testTeamProperties(&teamName, &teamId),
36+
),
37+
},
38+
},
39+
})
40+
}
41+
42+
func testTeamProperties(name *string, id *string) resource.TestCheckFunc {
43+
return func(state *terraform.State) error {
44+
client := testutils.GetDagsterClientFromEnvVars()
45+
46+
team, err := client.TeamsClient.GetTeamByName(context.Background(), *name)
47+
if err != nil {
48+
return err
49+
}
50+
51+
if team.Name != *name {
52+
return fmt.Errorf("expected team name to be %s, got %s", *name, team.Name)
53+
}
54+
55+
if team.Id != *id {
56+
return fmt.Errorf("expected team id to be %s, got %s", *id, team.Id)
57+
}
58+
59+
return nil
60+
}
61+
}

0 commit comments

Comments
 (0)