Skip to content

Commit

Permalink
added team/teams data resource (#64)
Browse files Browse the repository at this point in the history
Co-authored-by: Nico Gelders <[email protected]>
Co-authored-by: Quinten Bruynseraede <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2024
1 parent 79fb6fb commit 82e4a7a
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Terraform provider to manage Dagster Cloud resources.
| Deployment | :heavy_check_mark: | :x: |
| Deployment settings | :heavy_check_mark: | :x: |
| Code location | :heavy_check_mark: | :x: |
| Team | :heavy_check_mark: | :x: |
| Team(s) | :heavy_check_mark: | :heavy_check_mark: |
| Team membership | :heavy_check_mark: | :x: |
30 changes: 30 additions & 0 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dagster_team Data Source - dagster"
subcategory: ""
description: |-
Retrieve information about a Dagster Cloud team.
---

# dagster_team (Data Source)

Retrieve information about a Dagster Cloud team.

## Example Usage

```terraform
data "dagster_team" "team" {
name = "my-team-name"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the Dagster Cloud team

### Read-Only

- `id` (String) Team id
41 changes: 41 additions & 0 deletions docs/data-sources/teams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dagster_teams Data Source - dagster"
subcategory: ""
description: |-
Retrieve information about a Dagster Cloud teams.
---

# dagster_teams (Data Source)

Retrieve information about a Dagster Cloud teams.

## Example Usage

```terraform
data "dagster_teams" "teams" {
regex_filter = "^my-team"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `regex_filter` (String) Regex filter to select the Dagster Cloud teams based on the name of the team. (regex matching is done using `https://pkg.go.dev/regexp`)

### Read-Only

- `teams` (Attributes List) Teams (see [below for nested schema](#nestedatt--teams))

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

Required:

- `name` (String) Name of the Dagster Cloud team

Read-Only:

- `id` (String) Team id
3 changes: 3 additions & 0 deletions examples/data-sources/dagster_team/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "dagster_team" "team" {
name = "my-team-name"
}
3 changes: 3 additions & 0 deletions examples/data-sources/dagster_teams/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "dagster_teams" "teams" {
regex_filter = "^my-team"
}
24 changes: 24 additions & 0 deletions internal/client/service/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"regexp"

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

func (c *TeamsClient) GetTeamsByRegex(ctx context.Context, regex string) ([]schema.Team, error) {
teams, err := c.ListTeams(ctx)
if err != nil {
return []schema.Team{}, err
}

regexExpression, err := regexp.Compile(regex)
if err != nil {
return []schema.Team{}, err
}

matchedTeams := make([]schema.Team, 0)

for _, team := range teams {
match := regexExpression.MatchString(team.Name)
if match {
matchedTeams = append(matchedTeams, team)
}
}

return matchedTeams, nil
}

func (c *TeamsClient) GetTeamById(ctx context.Context, id string) (schema.Team, error) {
teams, err := c.ListTeams(ctx)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion internal/client/service/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestTeamsService_BasicCRUD(t *testing.T) {
teamName := "testing/my_team"
teamNameRenamed := "testing/my_team_renamed"

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

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

t.Cleanup(func() {
_ = teamsClient.DeleteTeam(ctx, teamCreated.Id)
})

teamById, err := teamsClient.GetTeamById(ctx, teamCreated.Id)
assert.NoError(t, err)
assert.Equal(t, teamName, teamById.Name, "Expected team names to be the same.")
Expand All @@ -41,6 +45,10 @@ func TestTeamsService_BasicCRUD(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, teamName, teamByName.Name, "Expected team names to be the same.")

teams, err := teamsClient.GetTeamsByRegex(ctx, "^testing/")
assert.NoError(t, err)
assert.Len(t, teams, 1)

_, err = teamsClient.RenameTeam(ctx, teamNameRenamed, teamCreated.Id)
assert.NoError(t, err)

Expand Down
98 changes: 98 additions & 0 deletions internal/provider/datasources/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package datasources

import (
"context"
"fmt"

"github.com/datarootsio/terraform-provider-dagster/internal/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &TeamDataSource{}
_ datasource.DataSourceWithConfigure = &TeamDataSource{}
)

type TeamDataSource struct {
client client.DagsterClient
}

type TeamDataSourceModel struct {
Name types.String `tfsdk:"name"`
Id types.String `tfsdk:"id"`
}

//nolint:ireturn // required by Terraform API
func NewTeamDataSource() datasource.DataSource {
return &TeamDataSource{}
}

// Metadata returns the data source type name.
func (d *TeamDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_team"
}

var teamAttributes = map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Computed: false,
Description: "Name of the Dagster Cloud team",
},
"id": schema.StringAttribute{
Computed: true,
Description: "Team id",
},
}

// Schema defines the schema for the data source.
func (d *TeamDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: `Retrieve information about a Dagster Cloud team.`,
Attributes: teamAttributes,
}
}

// Configure adds the provider-configured client to the data source.
func (d *TeamDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(client.DagsterClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected client.DagsterClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

// Read refreshes the Terraform state with the latest data.
func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data TeamDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

team, err := d.client.TeamsClient.GetTeamByName(ctx, data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get team information, got error: %s", err))
return
}

data.Id = types.StringValue(team.Id)
data.Name = types.StringValue(team.Name)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
61 changes: 61 additions & 0 deletions internal/provider/datasources/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package datasources_test

import (
"context"
"fmt"
"testing"

"github.com/datarootsio/terraform-provider-dagster/internal/testutils"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func testAccTeamConfig(name string) string {
return fmt.Sprintf(testutils.ProviderConfig+`
data "dagster_team" "this" {
name = "%s"
}
`, name)
}

func TestAccTeam(t *testing.T) {
name := "test-team"
var teamId string
var teamName string

resource.Test(t, resource.TestCase{
PreCheck: func() { testutils.AccTestPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccTeamConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
testutils.FetchValueFromState("data.dagster_team.this", "id", &teamId),
testutils.FetchValueFromState("data.dagster_team.this", "name", &teamName),
testTeamProperties(&teamName, &teamId),
),
},
},
})
}

func testTeamProperties(name *string, id *string) resource.TestCheckFunc {
return func(state *terraform.State) error {
client := testutils.GetDagsterClientFromEnvVars()

team, err := client.TeamsClient.GetTeamByName(context.Background(), *name)
if err != nil {
return err
}

if team.Name != *name {
return fmt.Errorf("expected team name to be %s, got %s", *name, team.Name)
}

if team.Id != *id {
return fmt.Errorf("expected team id to be %s, got %s", *id, team.Id)
}

return nil
}
}
Loading

0 comments on commit 82e4a7a

Please sign in to comment.