Skip to content

Commit

Permalink
Merge pull request PagerDuty#808 from cjgajard/tpf-business-service
Browse files Browse the repository at this point in the history
[CSGI-2475] Migrate Resource `pagerduty_business_service` to TF Plugin Framework
  • Loading branch information
imjaroiswebdev authored Feb 23, 2024
2 parents 1bb952e + 9b00358 commit 620f5d2
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 213 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,4 @@ PAGERDUTY_ACC_SCHEDULE_USED_BY_EP_W_1_LAYER=1 make testacc TESTARGS="-run PagerD
| `PAGERDUTY_ACC_INCIDENT_CUSTOM_FIELDS` | Custom Fields |
| `PAGERDUTY_ACC_LICENSE_NAME` | Licenses |
| `PAGERDUTY_ACC_SCHEDULE_USED_BY_EP_W_1_LAYER` | Schedule |
| `PAGERDUTY_ACC_EXTERNAL_PROVIDER_VERSION` | Modifies the version used to compare plans between sdkv2 and framework implementations. Default `~> 3.6`. |
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Serve() {
// terraform-plugin-framework
providerserver.NewProtocol5(pagerdutyplugin.New()),
// terraform-plugin-sdk
pagerduty.Provider().GRPCProvider,
pagerduty.Provider(pagerduty.IsMuxed).GRPCProvider,
)
if err != nil {
log.Fatal(err)
Expand Down
11 changes: 10 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import (
"github.com/heimweh/go-pagerduty/persistentconfig"
)

const (
IsMuxed = true
IsNotMuxed = false
)

// Provider represents a resource provider in Terraform
func Provider() *schema.Provider {
func Provider(isMux bool) *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"skip_credentials_validation": {
Expand Down Expand Up @@ -145,6 +150,10 @@ func Provider() *schema.Provider {
},
}

if isMux {
delete(p.ResourcesMap, "pagerduty_business_service")
}

p.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
terraformVersion := p.TerraformVersion
if terraformVersion == "" {
Expand Down
6 changes: 3 additions & 3 deletions pagerduty/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var testAccProvider *schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)

func init() {
testAccProvider = Provider()
testAccProvider = Provider(IsNotMuxed)
testAccProviders = map[string]*schema.Provider{
"pagerduty": testAccProvider,
}
Expand All @@ -31,13 +31,13 @@ func init() {
}

func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
if err := Provider(IsNotMuxed).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProviderImpl(t *testing.T) {
var _ *schema.Provider = Provider()
var _ *schema.Provider = Provider(IsNotMuxed)
}

func TestAccPagerDutyProviderAuthMethods_Basic(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pagerduty/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/heimweh/go-pagerduty/pagerduty"
)

// Deprecated: Migrated to pagerdutyplugin.resourceBusinessService. Kept for testing purposes.
func resourcePagerDutyBusinessService() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyBusinessServiceCreate,
Expand Down
199 changes: 0 additions & 199 deletions pagerduty/resource_pagerduty_business_service_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ func TestAccPagerDutyBusinessService_import(t *testing.T) {
poc := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyBusinessServiceConfig(name, desc, poc),
},

{
ResourceName: "pagerduty_business_service.foo",
ImportState: true,
Expand Down
13 changes: 10 additions & 3 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
"os"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type Provider struct{}
type Provider struct {
client *pagerduty.Client
}

func (p *Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "pagerduty"
Expand Down Expand Up @@ -53,10 +56,12 @@ func (p *Provider) DataSources(ctx context.Context) [](func() datasource.DataSou
}

func (p *Provider) Resources(ctx context.Context) [](func() resource.Resource) {
return [](func() resource.Resource){}
return [](func() resource.Resource){
func() resource.Resource { return &resourceBusinessService{} },
}
}

func New() provider.Provider {
func New() *Provider {
return &Provider{}
}

Expand Down Expand Up @@ -159,7 +164,9 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
if err != nil {
resp.Diagnostics.AddError("Cannot obtain plugin client", err.Error())
}
p.client = client
resp.DataSourceData = client
resp.ResourceData = client
}

type UseAppOauthScopedToken struct {
Expand Down
19 changes: 17 additions & 2 deletions pagerdutyplugin/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
pd "github.com/PagerDuty/terraform-provider-pagerduty/pagerduty"
)

var testAccProvider = New()

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("PAGERDUTY_PARALLEL"); v != "" {
t.Parallel()
Expand All @@ -36,13 +38,26 @@ func testAccCheckAttributes(n string, fn func(map[string]string) error) resource
}
}

func testAccExternalProviders() map[string]resource.ExternalProvider {
// Using the latest release before the introduction of
// Terraform plugin framework
version := "~> 3.6"
if v := os.Getenv("PAGERDUTY_ACC_EXTERNAL_PROVIDER_VERSION"); v != "" {
version = v
}
m := map[string]resource.ExternalProvider{
"pagerduty": {Source: "pagerduty/pagerduty", VersionConstraint: version},
}
return m
}

func testAccProtoV5ProviderFactories() map[string]func() (tfprotov5.ProviderServer, error) {
return map[string]func() (tfprotov5.ProviderServer, error){
"pagerduty": func() (tfprotov5.ProviderServer, error) {
ctx := context.Background()
providers := []func() tfprotov5.ProviderServer{
pd.Provider().GRPCProvider,
providerserver.NewProtocol5(New()),
pd.Provider(pd.IsMuxed).GRPCProvider,
providerserver.NewProtocol5(testAccProvider),
}

muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
Expand Down
Loading

0 comments on commit 620f5d2

Please sign in to comment.