Skip to content

Commit

Permalink
feat: Add sentry project data source (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
christina-moore authored and jianyuan committed Dec 10, 2023
1 parent acc60af commit d62399e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sentry_project Data Source - terraform-provider-sentry"
subcategory: ""
description: |-
Sentry Project data source.
---

# sentry_project (Data Source)

Sentry Project data source.

## Example Usage

```terraform
# Retrieve a project
data "sentry_project" "default" {
organization = "my-organization"
slug = "my-project"
}
```

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

### Required

- `organization` (String) The slug of the organization the project belongs to.
- `slug` (String) The unique URL slug for this project.

### Read-Only

- `id` (String) The ID of this resource.
- `internal_id` (String) The internal ID for this project.
- `is_public` (Boolean)


6 changes: 6 additions & 0 deletions examples/data-sources/sentry_project/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Retrieve a project
data "sentry_project" "default" {
organization = "my-organization"

slug = "my-project"
}
66 changes: 66 additions & 0 deletions sentry/data_source_sentry_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sentry

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/v2/sentry"
)

func dataSourceSentryProject() *schema.Resource {
return &schema.Resource{
Description: "Sentry Project data source.",

ReadContext: dataSourceSentryProjectRead,

Schema: map[string]*schema.Schema{
"organization": {
Description: "The slug of the organization the project belongs to.",
Type: schema.TypeString,
Required: true,
},
"slug": {
Description: "The unique URL slug for this project.",
Type: schema.TypeString,
Required: true,
},
"internal_id": {
Description: "The internal ID for this project.",
Type: schema.TypeString,
Computed: true,
},
"is_public": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*sentry.Client)

org := d.Get("organization").(string)
projectSlug := d.Get("slug").(string)

tflog.Debug(ctx, "Reading project", map[string]interface{}{
"org": org,
"project": projectSlug,
})
project, _, err := client.Projects.Get(ctx, org, projectSlug)
if err != nil {
return diag.FromErr(err)
}

d.SetId(project.Slug)
retErr := multierror.Append(
d.Set("organization", project.Organization.Slug),
d.Set("slug", project.Slug),
d.Set("internal_id", project.ID),
d.Set("is_public", project.IsPublic),
)
return diag.FromErr(retErr.ErrorOrNil())
}
43 changes: 43 additions & 0 deletions sentry/data_source_sentry_project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sentry

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccSentryProjectDataSource_basic(t *testing.T) {
teamName := acctest.RandomWithPrefix("tf-team")
projectName := acctest.RandomWithPrefix("tf-project")
dn := "data.sentry_project.test"
rn := "sentry_project.test"

var projectID string

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },

Check failure on line 19 in sentry/data_source_sentry_project_test.go

View workflow job for this annotation

GitHub Actions / Matrix Test (1.4.*)

undefined: acctest.PreCheck

Check failure on line 19 in sentry/data_source_sentry_project_test.go

View workflow job for this annotation

GitHub Actions / Matrix Test (1.5.*)

undefined: acctest.PreCheck

Check failure on line 19 in sentry/data_source_sentry_project_test.go

View workflow job for this annotation

GitHub Actions / Matrix Test (1.6.*)

undefined: acctest.PreCheck
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSentryProjectConfig_basic(teamName, projectName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSentryProjectExists(rn, &projectID),
resource.TestCheckResourceAttrPair(dn, "organization", rn, "organization"),
resource.TestCheckResourceAttrPair(dn, "slug", rn, "slug"),
resource.TestCheckResourceAttrPair(dn, "internal_id", rn, "internal_id"),
resource.TestCheckResourceAttrPair(dn, "is_public", rn, "is_public"),
),
},
},
})
}

func testAccSentryProjectConfig_basic(teamName, projectName string) string {
return testAccSentryProjectConfig_team(teamName, projectName) + `
data "sentry_project" "test" {
organization = sentry_project.test.organization
slug = sentry_project.test.slug
}
`
}
1 change: 1 addition & 0 deletions sentry/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func NewProvider(version string) func() *schema.Provider {
"sentry_organization": dataSourceSentryOrganization(),
"sentry_organization_integration": dataSourceSentryOrganizationIntegration(),
"sentry_team": dataSourceSentryTeam(),
"sentry_project": dataSourceSentryProject(),
},
}

Expand Down

0 comments on commit d62399e

Please sign in to comment.