Skip to content

Commit

Permalink
ref: Migrate organization data source to the plugin framework (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan authored Sep 8, 2024
1 parent 55089cc commit 8a5628a
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 89 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ data "sentry_organization" "org" {

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) The unique URL slug for this organization.
- `internal_id` (String) The internal ID for this organization.
- `name` (String) The human readable name for this organization.
94 changes: 94 additions & 0 deletions internal/provider/data_source_organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package provider

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jianyuan/go-sentry/v2/sentry"
)

var _ datasource.DataSource = &OrganizationDataSource{}
var _ datasource.DataSourceWithConfigure = &OrganizationDataSource{}

type OrganizationDataSourceModel struct {
Id types.String `tfsdk:"id"`
Slug types.String `tfsdk:"slug"`
Name types.String `tfsdk:"name"`
InternalId types.String `tfsdk:"internal_id"`
}

func (m *OrganizationDataSourceModel) Fill(org sentry.Organization) error {
m.Id = types.StringPointerValue(org.Slug)
m.Slug = types.StringPointerValue(org.Slug)
m.Name = types.StringPointerValue(org.Name)
m.InternalId = types.StringPointerValue(org.ID)

return nil
}

func NewOrganizationDataSource() datasource.DataSource {
return &OrganizationDataSource{}
}

type OrganizationDataSource struct {
baseDataSource
}

func (d *OrganizationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organization"
}

func (d *OrganizationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Sentry Organization data source.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The unique URL slug for this organization.",
Computed: true,
},
"slug": schema.StringAttribute{
MarkdownDescription: "The unique URL slug for this organization.",
Required: true,
},
"internal_id": schema.StringAttribute{
MarkdownDescription: "The internal ID for this organization.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The human readable name for this organization.",
Computed: true,
},
},
}
}

func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrganizationDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

org, apiResp, err := d.client.Organizations.Get(ctx, data.Slug.ValueString())
if apiResp.StatusCode == http.StatusNotFound {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Not found: %s", err.Error()))
return
}
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Read error: %s", err.Error()))
return
}

if err := data.Fill(*org); err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Fill error: %s", err))
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
56 changes: 56 additions & 0 deletions internal/provider/data_source_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,66 @@ package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/jianyuan/terraform-provider-sentry/internal/acctest"
)

func TestAccOrganizationDataSource(t *testing.T) {
rn := "data.sentry_organization.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOrganizationDataSourceConfig,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(rn, tfjsonpath.New("id"), knownvalue.StringExact(acctest.TestOrganization)),
statecheck.ExpectKnownValue(rn, tfjsonpath.New("slug"), knownvalue.StringExact(acctest.TestOrganization)),
statecheck.ExpectKnownValue(rn, tfjsonpath.New("name"), knownvalue.NotNull()),
statecheck.ExpectKnownValue(rn, tfjsonpath.New("internal_id"), knownvalue.NotNull()),
},
},
},
})
}

func TestAccOrganizationDataSource_MigrateFromPluginSDK(t *testing.T) {
rn := "data.sentry_organization.test"

checks := []statecheck.StateCheck{
statecheck.ExpectKnownValue(rn, tfjsonpath.New("slug"), knownvalue.StringExact(acctest.TestOrganization)),
statecheck.ExpectKnownValue(rn, tfjsonpath.New("name"), knownvalue.NotNull()),
statecheck.ExpectKnownValue(rn, tfjsonpath.New("internal_id"), knownvalue.NotNull()),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
acctest.ProviderName: {
Source: "jianyuan/sentry",
VersionConstraint: "0.12.3",
},
},
Config: testAccOrganizationDataSourceConfig,
ConfigStateChecks: checks,
},
{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Config: testAccOrganizationDataSourceConfig,
ConfigStateChecks: checks,
},
},
})
}

var testAccOrganizationDataSourceConfig = fmt.Sprintf(`
data "sentry_organization" "test" {
slug = "%s"
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (p *SentryProvider) DataSources(ctx context.Context) []func() datasource.Da
NewAllProjectsDataSource,
NewClientKeyDataSource,
NewIssueAlertDataSource,
NewOrganizationDataSource,
NewOrganizationIntegrationDataSource,
NewOrganizationMemberDataSource,
NewProjectDataSource,
Expand Down
56 changes: 0 additions & 56 deletions sentry/data_source_sentry_organization.go

This file was deleted.

31 changes: 0 additions & 31 deletions sentry/data_source_sentry_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,10 @@ package sentry

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/jianyuan/go-sentry/v2/sentry"
"github.com/jianyuan/terraform-provider-sentry/internal/acctest"
)

func TestAccSentryOrganizationDataSource_basic(t *testing.T) {
var organization sentry.Organization

rn := "data.sentry_organization.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSentryOrganizationDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckSentryOrganizationExists(rn, &organization),
resource.TestCheckResourceAttrSet(rn, "name"),
resource.TestCheckResourceAttr(rn, "slug", acctest.TestOrganization),
resource.TestCheckResourceAttrWith(rn, "internal_id", func(v string) error {
want := sentry.StringValue(organization.ID)
if v != want {
return fmt.Errorf("got organization ID %s; want %s", v, want)
}
return nil
}),
),
},
},
})
}

var testAccSentryOrganizationDataSourceConfig = fmt.Sprintf(`
data "sentry_organization" "test" {
slug = "%s"
Expand Down
1 change: 0 additions & 1 deletion sentry/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func NewProvider(version string) func() *schema.Provider {
DataSourcesMap: map[string]*schema.Resource{
"sentry_dashboard": dataSourceSentryDashboard(),
"sentry_metric_alert": dataSourceSentryMetricAlert(),
"sentry_organization": dataSourceSentryOrganization(),
"sentry_team": dataSourceSentryTeam(),
},
}
Expand Down

0 comments on commit 8a5628a

Please sign in to comment.