Skip to content

Commit

Permalink
Merge pull request #121 from bigcommerce/data-source-teams
Browse files Browse the repository at this point in the history
Add firehydrant_teams and firehydrant_team data source
  • Loading branch information
Jeff Zellner authored Jul 31, 2023
2 parents 0134a60 + 2b891e6 commit 2a5d38d
Show file tree
Hide file tree
Showing 15 changed files with 778 additions and 78 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
ENHANCEMENTS:

* Bump golang from 1.16 to 1.18
* **New Data Source:** `firehydrant_team` ([#121](https://github.com/firehydrant/terraform-provider-firehydrant/pull/121))
* **New Data Source:** `firehydrant_teams` ([#121](https://github.com/firehydrant/terraform-provider-firehydrant/pull/121))
* resource/service: Added `auto_add_responding_team` attribute to service ([#117](https://github.com/firehydrant/terraform-provider-firehydrant/pull/117))
* data_source/service: Added `auto_add_responding_team` attribute to service ([#117](https://github.com/firehydrant/terraform-provider-firehydrant/pull/117))
* data_source/services: Added `auto_add_responding_team` attribute to service ([#117](https://github.com/firehydrant/terraform-provider-firehydrant/pull/117))
Expand Down
35 changes: 35 additions & 0 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
page_title: "FireHydrant Data Source: firehydrant_team"
subcategory: ""
---

# firehydrant_team Data Source

Use this data source to get information on a team matching the given criteria.

## Example Usage

Basic usage:

```hcl
data "firehydrant_team" "test-team" {
id = "157a83c4-17d1-4362-a4e8-a45412d19af2"
}
```

## Argument Reference

The following arguments are supported:

* `id` - (Required) The ID of the team to retrieve.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the team.
* `name` - The name of the team.
* `description` - A description of the team.
* `slug` - The slug for the team.
* `service_ids` - A set of IDs of the services associated with this team
* `owned_service_ids` - A set of IDs of the services owned by this team
44 changes: 44 additions & 0 deletions docs/data-sources/teams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
page_title: "FireHydrant Data Source: firehydrant_teams"
subcategory: ""
---

# firehydrant_teams Data Source

Use this data source to get information on all teams matching the given criteria.

## Example Usage

Basic usage:
```hcl
data "firehydrant_teams" "all-teams" {
}
```

Getting all teams with `database` in the name:
```hcl
data "firehydrant_teams" "database-named-teams" {
query = "database"
}
```

## Argument Reference

The following arguments are supported:

* `query` - (Optional) A query to search for teams by their name.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `teams` - All the teams matching the criteria specified by `query`.

The `teams` block contains:

* `id` - The ID of the team.
* `name` - The name of the team.
* `description` - A description of the team.
* `slug` - The slug for the team.
* `service_ids` - A set of IDs of the services associated with this team
* `owned_service_ids` - A set of IDs of the services owned by this team
11 changes: 11 additions & 0 deletions examples/teams.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
resource "firehydrant_team" "firefighters" {
name = "Firefighters"
}

data "firehydrant_team" "firefighters" {
id = "857a83c4-17d1-4362-a4e8-42d1a3d19ed1"
}

data "firehydrant_teams" "all_teams" {
}

data "firehydrant_teams" "test_teams" {
query = "Test"
}
75 changes: 4 additions & 71 deletions firehydrant/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ type Client interface {
Services() ServicesClient
Severities() SeveritiesClient
TaskLists() TaskListsClient

// Teams
GetTeam(ctx context.Context, id string) (*TeamResponse, error)
CreateTeam(ctx context.Context, req CreateTeamRequest) (*TeamResponse, error)
UpdateTeam(ctx context.Context, id string, req UpdateTeamRequest) (*TeamResponse, error)
DeleteTeam(ctx context.Context, id string) error
Teams() TeamsClient

// Users
GetUsers(ctx context.Context, params GetUserParams) (*UserResponse, error)
Expand Down Expand Up @@ -195,21 +190,9 @@ func (c *APIClient) TaskLists() TaskListsClient {
return &RESTTaskListsClient{client: c}
}

// GetTeam retrieves a team from FireHydrant
func (c *APIClient) GetTeam(ctx context.Context, id string) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.client().Get("teams/"+id).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not get team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
// Teams returns a TeamsClient interface for interacting with teams in FireHydrant
func (c *APIClient) Teams() TeamsClient {
return &RESTTeamsClient{client: c}
}

// GetUsers gets matching users in FireHydrant
Expand Down Expand Up @@ -245,53 +228,3 @@ func (c *APIClient) GetSchedules(ctx context.Context, params GetScheduleParams)

return scheduleResponse, nil
}

// CreateTeam creates a team in FireHydrant
func (c *APIClient) CreateTeam(ctx context.Context, req CreateTeamRequest) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.client().Post("teams").BodyJSON(&req).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not create team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
}

// UpdateTeam updates a team in FireHydrant
func (c *APIClient) UpdateTeam(ctx context.Context, id string, req UpdateTeamRequest) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.client().Patch("teams/"+id).BodyJSON(&req).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not update team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
}

// DeleteTeam deletes a team from FireHydrant
func (c *APIClient) DeleteTeam(ctx context.Context, id string) error {
apiError := &APIError{}
response, err := c.client().Delete("teams/"+id).Receive(nil, apiError)
if err != nil {
return errors.Wrap(err, "could not delete team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return err
}

return nil
}
121 changes: 121 additions & 0 deletions firehydrant/teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package firehydrant

import (
"context"
"github.com/dghubble/sling"
"github.com/pkg/errors"
)

// TeamsClient is an interface for interacting with teams on FireHydrant
type TeamsClient interface {
Get(ctx context.Context, slug string) (*TeamResponse, error)
List(ctx context.Context, req *TeamQuery) (*TeamsResponse, error)
Create(ctx context.Context, createReq CreateTeamRequest) (*TeamResponse, error)
Update(ctx context.Context, slug string, updateReq UpdateTeamRequest) (*TeamResponse, error)
Archive(ctx context.Context, slug string) error
}

// RESTTeamsClient implements the TeamsClient interface
type RESTTeamsClient struct {
client *APIClient
}

var _ TeamsClient = &RESTTeamsClient{}

func (c *RESTTeamsClient) restClient() *sling.Sling {
return c.client.client()
}

// Get retrieves a team from FireHydrant
func (c *RESTTeamsClient) Get(ctx context.Context, id string) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.restClient().Get("teams/"+id).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not get team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
}

// List retrieves a list of teams based on a team query
func (c *RESTTeamsClient) List(ctx context.Context, req *TeamQuery) (*TeamsResponse, error) {
teamsResponse := &TeamsResponse{}
apiError := &APIError{}
curPage := 1

for {
req.Page = curPage
response, err := c.restClient().Get("teams").QueryStruct(req).Receive(teamsResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not get teams")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

if teamsResponse.Pagination == nil || teamsResponse.Pagination.Last == curPage {
break
}
curPage += 1
}

return teamsResponse, nil
}

// Create creates a team
func (c *RESTTeamsClient) Create(ctx context.Context, createReq CreateTeamRequest) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.restClient().Post("teams").BodyJSON(&createReq).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not create team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
}

// Update updates a team in FireHydrant
func (c *RESTTeamsClient) Update(ctx context.Context, slug string, updateReq UpdateTeamRequest) (*TeamResponse, error) {
teamResponse := &TeamResponse{}
apiError := &APIError{}
response, err := c.restClient().Patch("teams/"+slug).BodyJSON(&updateReq).Receive(teamResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not update team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

return teamResponse, nil
}

// Archive archives a team in FireHydrant
func (c *RESTTeamsClient) Archive(ctx context.Context, slug string) error {
apiError := &APIError{}
response, err := c.restClient().Delete("teams/"+slug).Receive(nil, apiError)
if err != nil {
return errors.Wrap(err, "could not archive team")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 2a5d38d

Please sign in to comment.