diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md new file mode 100644 index 000000000..25b67cf71 --- /dev/null +++ b/docs/data-sources/project.md @@ -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 + +### 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) + + diff --git a/examples/data-sources/sentry_project/data-source.tf b/examples/data-sources/sentry_project/data-source.tf new file mode 100644 index 000000000..b8587ffc6 --- /dev/null +++ b/examples/data-sources/sentry_project/data-source.tf @@ -0,0 +1,6 @@ +# Retrieve a project +data "sentry_project" "default" { + organization = "my-organization" + + slug = "my-project" +} diff --git a/sentry/data_source_sentry_project.go b/sentry/data_source_sentry_project.go new file mode 100644 index 000000000..7c4026c64 --- /dev/null +++ b/sentry/data_source_sentry_project.go @@ -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()) +} diff --git a/sentry/data_source_sentry_project_test.go b/sentry/data_source_sentry_project_test.go new file mode 100644 index 000000000..f25fa0f6d --- /dev/null +++ b/sentry/data_source_sentry_project_test.go @@ -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) }, + 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 +} + ` +} diff --git a/sentry/provider.go b/sentry/provider.go index a4a561cf2..9a0ceb813 100644 --- a/sentry/provider.go +++ b/sentry/provider.go @@ -60,6 +60,7 @@ func NewProvider(version string) func() *schema.Provider { "sentry_organization": dataSourceSentryOrganization(), "sentry_organization_integration": dataSourceSentryOrganizationIntegration(), "sentry_team": dataSourceSentryTeam(), + "sentry_project": dataSourceSentryProject(), }, }