Skip to content

Commit d62399e

Browse files
christina-moorejianyuan
authored andcommitted
feat: Add sentry project data source (#301)
1 parent acc60af commit d62399e

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

docs/data-sources/project.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "sentry_project Data Source - terraform-provider-sentry"
4+
subcategory: ""
5+
description: |-
6+
Sentry Project data source.
7+
---
8+
9+
# sentry_project (Data Source)
10+
11+
Sentry Project data source.
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Retrieve a project
17+
data "sentry_project" "default" {
18+
organization = "my-organization"
19+
20+
slug = "my-project"
21+
}
22+
```
23+
24+
<!-- schema generated by tfplugindocs -->
25+
## Schema
26+
27+
### Required
28+
29+
- `organization` (String) The slug of the organization the project belongs to.
30+
- `slug` (String) The unique URL slug for this project.
31+
32+
### Read-Only
33+
34+
- `id` (String) The ID of this resource.
35+
- `internal_id` (String) The internal ID for this project.
36+
- `is_public` (Boolean)
37+
38+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Retrieve a project
2+
data "sentry_project" "default" {
3+
organization = "my-organization"
4+
5+
slug = "my-project"
6+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package sentry
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/go-multierror"
7+
"github.com/hashicorp/terraform-plugin-log/tflog"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/jianyuan/go-sentry/v2/sentry"
11+
)
12+
13+
func dataSourceSentryProject() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Sentry Project data source.",
16+
17+
ReadContext: dataSourceSentryProjectRead,
18+
19+
Schema: map[string]*schema.Schema{
20+
"organization": {
21+
Description: "The slug of the organization the project belongs to.",
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"slug": {
26+
Description: "The unique URL slug for this project.",
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"internal_id": {
31+
Description: "The internal ID for this project.",
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"is_public": {
36+
Type: schema.TypeBool,
37+
Computed: true,
38+
},
39+
},
40+
}
41+
}
42+
43+
func dataSourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
44+
client := meta.(*sentry.Client)
45+
46+
org := d.Get("organization").(string)
47+
projectSlug := d.Get("slug").(string)
48+
49+
tflog.Debug(ctx, "Reading project", map[string]interface{}{
50+
"org": org,
51+
"project": projectSlug,
52+
})
53+
project, _, err := client.Projects.Get(ctx, org, projectSlug)
54+
if err != nil {
55+
return diag.FromErr(err)
56+
}
57+
58+
d.SetId(project.Slug)
59+
retErr := multierror.Append(
60+
d.Set("organization", project.Organization.Slug),
61+
d.Set("slug", project.Slug),
62+
d.Set("internal_id", project.ID),
63+
d.Set("is_public", project.IsPublic),
64+
)
65+
return diag.FromErr(retErr.ErrorOrNil())
66+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sentry
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccSentryProjectDataSource_basic(t *testing.T) {
11+
teamName := acctest.RandomWithPrefix("tf-team")
12+
projectName := acctest.RandomWithPrefix("tf-project")
13+
dn := "data.sentry_project.test"
14+
rn := "sentry_project.test"
15+
16+
var projectID string
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { acctest.PreCheck(t) },
20+
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccSentryProjectConfig_basic(teamName, projectName),
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckSentryProjectExists(rn, &projectID),
26+
resource.TestCheckResourceAttrPair(dn, "organization", rn, "organization"),
27+
resource.TestCheckResourceAttrPair(dn, "slug", rn, "slug"),
28+
resource.TestCheckResourceAttrPair(dn, "internal_id", rn, "internal_id"),
29+
resource.TestCheckResourceAttrPair(dn, "is_public", rn, "is_public"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccSentryProjectConfig_basic(teamName, projectName string) string {
37+
return testAccSentryProjectConfig_team(teamName, projectName) + `
38+
data "sentry_project" "test" {
39+
organization = sentry_project.test.organization
40+
slug = sentry_project.test.slug
41+
}
42+
`
43+
}

sentry/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func NewProvider(version string) func() *schema.Provider {
6060
"sentry_organization": dataSourceSentryOrganization(),
6161
"sentry_organization_integration": dataSourceSentryOrganizationIntegration(),
6262
"sentry_team": dataSourceSentryTeam(),
63+
"sentry_project": dataSourceSentryProject(),
6364
},
6465
}
6566

0 commit comments

Comments
 (0)