Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
New resource: rollbar_team_user_association (#63)
Browse files Browse the repository at this point in the history
* Implement new resource: rollbar_team_user_association

* don't set resource ID with the status
  • Loading branch information
davidji99 authored Jun 2, 2021
1 parent a113219 commit 7728036
Show file tree
Hide file tree
Showing 17 changed files with 533 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ jobs:
ROLLBAR_ACCOUNT_ACCESS_TOKEN: ${{ secrets.ROLLBAR_ACCOUNT_ACCESS_TOKEN }}
ROLLBAR_PD_API_KEY: ${{ secrets.ROLLBAR_PD_API_KEY }}
ROLLBAR_PROJECT_ACCESS_TOKEN: ${{ secrets.ROLLBAR_PROJECT_ACCESS_TOKEN }}
ROLLBAR_USER_EMAIL: ${{ secrets.ROLLBAR_USER_EMAIL }}
ROLLBAR_USER_EMAIL: ${{ secrets.ROLLBAR_USER_EMAIL }}
ROLLBAR_TEAM_ID: ${{ secrets.ROLLBAR_TEAM_ID }}
ROLLBAR_EMAIL_ADDRESS: ${{ secrets.ROLLBAR_EMAIL_ADDRESS }}
6 changes: 5 additions & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ The following parameters are available for running the test. The absence of some

* **TF_ACC** (`integer`) **Required** - must be set to `1`.
* **ROLLBAR_ACCOUNT_ACCESS_TOKEN** (`string`) - The account access token of the user running the test.
* **ROLLBAR_PROJECT_ACCESS_TOKEN** (`string`) - The account access token of the user running the test.
* **ROLLBAR_PROJECT_ACCESS_TOKEN** (`string`) - The project access token of the user running the test.
* **ROLLBAR_PD_API_KEY** (`string`) - A PagerDuty API key.
* **ROLLBAR_USER_EMAIL** (`string`) - A Rollbar user email address.
* **ROLLBAR_TEAM_ID** (`string`) - The ID of a rollbar team.
* **ROLLBAR_EMAIL_ADDRESS** (`string`) - An email address.

Please note: if you run the entire acceptance suite, you will need to set BOTH `ROLLBAR_ACCOUNT_ACCESS_TOKEN` & `ROLLBAR_PROJECT_ACCESS_TOKEN`.
Otherwise, certain tests require either token.
Expand Down
1 change: 0 additions & 1 deletion docs/resources/pagerduty_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ resource "rollbar_pagerduty_integration" "pd" {
The following arguments are supported:

* `service_key` - (Required) `<string>` Valid PagerDuty Service API Key. Must 32 characters long.

* `enabled` - (Required) `<boolean>` Enable the PagerDuty notifications globally

## Attributes Reference
Expand Down
7 changes: 0 additions & 7 deletions docs/resources/project_access_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,13 @@ resource "rollbar_project_access_token" "foobar" {
The following arguments are supported:

* `project_id` - (Required) `<string>` The ID of the project

* `name` - (Required) `<string>` Name of the project access token. Max length 32 characters.

* `scopes` - (Required) `<list(string)>` Scopes to assign to the create access token.
Valid options: `read`, `write`, `post_server_item`, `post_client_item`.

* `status` - (Required) `<string>` Enable or disable the access token. Valid options: `enabled`, `disabled`.

* `rate_limit_window_size` `<integer>` - Period of time (in seconds) for the rate limit. On **resource creation only**,
the valid options are the following: `0, 60, 300, 1800, 3600, 86400, 604800, 2592000`.
Otherwise, any value greater than `0`. If this argument is not set, the default is 60 seconds (1 minute).

* `rate_limit_window_count` `<integer>` - Number of requests for the defined rate limiting period.
Otherwise, any value greater than `0`. If this argument is not set, the default is 5000 calls.

Expand All @@ -71,9 +66,7 @@ Otherwise, any value greater than `0`. If this argument is not set, the default
The following attributes are exported:

* `cur_rate_limit_window_count` - How many remaining API calls are left for the access token.

* `date_created` - The timestamp in epoch of when the token was created.

* `access_token` - The actual access token. This value is set to `Sensitive`
and will not be shown in any non-debug `terraform` outputs.

Expand Down
6 changes: 3 additions & 3 deletions docs/resources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ description: |-

This resource is used to create and manage teams on Rollbar.

**NOTE:** The Rollbar API does not support updating existing teams, only through the UI.
-> **IMPORTANT!**
The Rollbar API does not support updating existing teams, only through the UI.
Therefore, you must update your configuration file(s) for this resource if you manually updated
the team name. Otherwise, your `terraform plan` will detect if a difference between the state file and remote.

## Example Usage

```hcl-terraform
# Create a new Rollbar team
resource "rollbar_team" "follbar" {
resource "rollbar_team" "foobar" {
name = "my_new_team"
access_level = "standard"
}
Expand All @@ -29,7 +30,6 @@ resource "rollbar_team" "follbar" {
The following arguments are supported:

* `name` - (Required) `<string>` Name of the team.

* `access_level` - (Required) `<string>` Access level of the team. Valid options: `standard`, `light`, `view`.
`standard` is the only access level you can choose in the UI. `light` and `view` are API-only team access levels.
`light` gives the team read and write access, but not to all settings. `view` gives the team read-only access.
Expand Down
1 change: 0 additions & 1 deletion docs/resources/team_project_association.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ resource "rollbar_team_project_association" "foobar" {
The following arguments are supported:

* `team_id` - (Required) `<string>` ID of existing team.

* `project_id` - (Required) `<string>` ID of existing project.

## Attributes Reference
Expand Down
81 changes: 81 additions & 0 deletions docs/resources/team_user_association.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
layout: "rollbar"
page_title: "Rollbar: rollbar_team_user_association"
sidebar_current: "docs-rollbar-resource-team-user-association"
description: |-
Provides a resource to create and manage the association between a team and user.
---

# rollbar_team_user_association

This resource is used to create and manage the association between a team and user.

### Unique resource lifecycle

If the specified `email` belongs to a new user:

* For resource creation, an invitation will be sent to the user to create a Rollbar account.
Once the account is created, the user will join the team.
* For resource deletion, if the invitation has been accepted, and the user has joined the team,
the user will be removed from the team. If the invitation has not been accepted, the invitation
will be revoked.

If the specified `email` belongs to an existing Rollbar user:

* For resource creation, the user will be immediately added to the team.
* For resource deletion, the user will be removed from the team.

## Example Usage

```hcl-terraform
data "rollbar_team" "foobar" {
id = "my_team_id"
}
data "rollbar_user" "foobar" {
email = "[email protected]"
}
resource "rollbar_team_user_association" "foobar" {
team_id = data.rollbar_team.foobar.id
email = data.rollbar_user.foobar.email
}
```

```hcl-terraform
resource "rollbar_team_user_association" "foobar" {
team_id = 123456
email = "[email protected]"
}
```

## Argument Reference

The following arguments are supported:

* `team_id` - (Required) `<string>` ID of existing team.
* `email` - (Required) `<string>` Email address of an existing Rollbar user or a new user.

## Attributes Reference

* `user_id` - Email address of a user.
* `invited_or_added` - Whether the user was either initially `invited` or `added`
to the Rollbar team.
* `invitation_status` - Status of the invitation. This attribute is set only if the user
had to first be invited to Rollbar in order to join the team.
* `invitation_id` - ID of the invitation. This attribute is set only if the user
had to first be invited to Rollbar in order to join the team.

## Import

Existing team user association can be imported using a composite value of the team ID and email address
separated by a colon.

For example:

```shell
$ terraform import rollbar_team_user_association.follbar 123:[email protected]
```

-> **IMPORTANT!**
You can only import a team user association if the user has already joined the team.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/davidji99/terraform-provider-rollbar
go 1.16

require (
github.com/davidji99/rollrest-go v0.1.5
github.com/davidji99/rollrest-go v0.1.7
github.com/hashicorp/terraform-plugin-sdk/v2 v2.6.1
github.com/stretchr/testify v1.7.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidji99/go-querystring v1.0.2 h1:zZUgCkhdFEsb0b1tS1zhn7FwjoRyp33J3be1av5sBi0=
github.com/davidji99/go-querystring v1.0.2/go.mod h1:67KzURpYgsT0d/eszawoSvtpI+34vczasEK3xgrxrqA=
github.com/davidji99/rollrest-go v0.1.5 h1:a2XfxyUY/LDJVaJOmBqxNgWf89qvCUpBkCZXn9N3GgI=
github.com/davidji99/rollrest-go v0.1.5/go.mod h1:avSXFeDrY9utfUC8HO8ooVFAf0Mugm2N9qhzdkBa01g=
github.com/davidji99/rollrest-go v0.1.7 h1:LdQtpoZJ9jyBm3pf0b0v2oc2mmKsDEcIiYlc0Wz6rfE=
github.com/davidji99/rollrest-go v0.1.7/go.mod h1:avSXFeDrY9utfUC8HO8ooVFAf0Mugm2N9qhzdkBa01g=
github.com/davidji99/simpleresty v0.2.3 h1:oUHimRSFUgGgJjd+65q3L9alzPF3vFBiXHKxVVmkvkU=
github.com/davidji99/simpleresty v0.2.3/go.mod h1:v1honpRzBWA0G2Ivpz3ozeaB4zRiYxvu8CAAAtAlHqY=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
Expand Down
14 changes: 14 additions & 0 deletions helper/test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import (

const (
TestConfigAccountAccessToken TestConfigKey = iota
TestConfigProjectAccessToken
TestConfigAcceptanceTestKey
TestConfigPagerDutyAPIKey
TestConfigUserEmailKey
TestConfigTeamID
TestConfigEmailAddress
)

var testConfigKeyToEnvName = map[TestConfigKey]string{
TestConfigAccountAccessToken: "ROLLBAR_ACCOUNT_ACCESS_TOKEN",
TestConfigProjectAccessToken: "ROLLBAR_PROJECT_ACCESS_TOKEN",
TestConfigPagerDutyAPIKey: "ROLLBAR_PD_API_KEY",
TestConfigUserEmailKey: "ROLLBAR_USER_EMAIL",
TestConfigTeamID: "ROLLBAR_TEAM_ID",
TestConfigEmailAddress: "ROLLBAR_EMAIL_ADDRESS",
TestConfigAcceptanceTestKey: resource.TestEnvVar,
}

Expand Down Expand Up @@ -78,3 +84,11 @@ func (t *TestConfig) GetUserEmailOrAbort(testing *testing.T) (val string) {
func (t *TestConfig) GetPagerDutyAPIKeyorAbort(testing *testing.T) (val string) {
return t.GetOrAbort(testing, TestConfigPagerDutyAPIKey)
}

func (t *TestConfig) GetTeamIDorAbort(testing *testing.T) (val string) {
return t.GetOrAbort(testing, TestConfigTeamID)
}

func (t *TestConfig) GetTeamEmailAddress(testing *testing.T) (val string) {
return t.GetOrAbort(testing, TestConfigEmailAddress)
}
6 changes: 6 additions & 0 deletions rollbar/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package rollbar

// UserNotFoundError is returned when searching for a user by a certain criteria.
type UserNotFoundError struct {
error
}
12 changes: 12 additions & 0 deletions rollbar/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import (
"time"
)

// getEmail extracts the email attribute generically from a Rollbar resource.
func getEmail(d *schema.ResourceData) string {
var email string
if v, ok := d.GetOk("email"); ok {
vs := v.(string)
log.Printf("[DEBUG] email: %s", vs)
email = vs
}

return email
}

// getTeamID extracts the team ID attribute generically from a Rollbar resource.
func getTeamID(d *schema.ResourceData) int {
var teamID int
Expand Down
43 changes: 43 additions & 0 deletions rollbar/import_rollbar_team_user_association_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rollbar

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"testing"
)

func TestAccRollbarTeamUserAssociationTest_importBasic(t *testing.T) {
teamID := testAccConfig.GetTeamIDorAbort(t)
email := testAccConfig.GetTeamEmailAddress(t)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRollbarTeamUserAssociation_basic(teamID, email),
},
{
ResourceName: "rollbar_team_user_association.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccRollbarTeamUserAssociationImportStateIdFunc("rollbar_team_user_association.foobar"),
},
},
})
}

func testAccRollbarTeamUserAssociationImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}

return fmt.Sprintf("%s:%s", rs.Primary.Attributes["team_id"],
rs.Primary.Attributes["email"]), nil
}
}
1 change: 1 addition & 0 deletions rollbar/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Provider() *schema.Provider {
"rollbar_project_access_token": resourceRollbarProjectAccessToken(),
"rollbar_team": resourceRollbarTeam(),
"rollbar_team_project_association": resourceRollbarTeamProjectAssociation(),
"rollbar_team_user_association": resourceRollbarTeamUserAssociation(),
},

ConfigureContextFunc: providerConfigure,
Expand Down
2 changes: 1 addition & 1 deletion rollbar/resource_rollbar_team_project_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func resourceRollbarTeamProjectAssociationCreate(ctx context.Context, d *schema.

d.SetId(fmt.Sprintf("%d:%d", int(result.GetResult().GetTeamID()), int(result.GetResult().GetProjectID())))

return diags
return resourceRollbarTeamProjectAssociationRead(ctx, d, meta)
}

func resourceRollbarTeamProjectAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Loading

0 comments on commit 7728036

Please sign in to comment.