Skip to content

Commit

Permalink
Move pagerdutyplugin to protocol version 6
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Feb 29, 2024
1 parent 695c6cc commit d8a6533
Show file tree
Hide file tree
Showing 34 changed files with 1,784 additions and 222 deletions.
32 changes: 19 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"log"

pd "github.com/PagerDuty/terraform-provider-pagerduty/pagerduty"
pdp "github.com/PagerDuty/terraform-provider-pagerduty/pagerdutyplugin"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"

"github.com/PagerDuty/terraform-provider-pagerduty/pagerduty"
pagerdutyplugin "github.com/PagerDuty/terraform-provider-pagerduty/pagerdutyplugin"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
)

func main() {
Expand All @@ -19,21 +20,26 @@ func main() {
func Serve() {
ctx := context.Background()

muxServer, err := tf5muxserver.NewMuxServer(
upgradedSdkServer, err := tf5to6server.UpgradeServer(ctx, pd.Provider(pd.IsMuxed).GRPCProvider)
if err != nil {
log.Fatal(err)
}

muxServer, err := tf6muxserver.NewMuxServer(
ctx,
// terraform-plugin-framework
providerserver.NewProtocol5(pagerdutyplugin.New()),
// terraform-plugin-sdk
pagerduty.Provider(pagerduty.IsMuxed).GRPCProvider,
providerserver.NewProtocol6(pdp.New()),
func() tfprotov6.ProviderServer { return upgradedSdkServer },
)
if err != nil {
log.Fatal(err)
}

var serveOpts []tf5server.ServeOpt

address := "registry.terraform.io/pagerduty/pagerduty"
err = tf5server.Serve(address, muxServer.ProviderServer, serveOpts...)
if err != nil {
log.Fatal(err)
}

err = tf6server.Serve(address, muxServer.ProviderServer)
if err != nil {
log.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccDataSourcePagerDutyBusinessService_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyBusinessServiceConfig(name),
Expand Down
16 changes: 8 additions & 8 deletions pagerdutyplugin/data_source_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccDataSourcePagerDutyService_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyServiceConfig(username, email, service, escalationPolicy, teamname),
Expand All @@ -39,7 +39,7 @@ func TestAccDataSourcePagerDutyService_HasNoTeam(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyServiceConfig(username, email, service, escalationPolicy, teamname),
Expand All @@ -60,7 +60,7 @@ func TestAccDataSourcePagerDutyService_HasOneTeam(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyServiceConfig(username, email, service, escalationPolicy, teamname),
Expand Down Expand Up @@ -98,6 +98,8 @@ func testAccDataSourcePagerDutyService(src, n string) resource.TestCheckFunc {
}

func testAccDataSourcePagerDutyServiceConfig(username, email, service, escalationPolicy, teamname string) string {
noTeamEsc := fmt.Sprintf("tf-%s", acctest.RandString(5))
noTeamServ := fmt.Sprintf("tf-%s", acctest.RandString(5))
return fmt.Sprintf(`
resource "pagerduty_team" "team_one" {
name = "%s"
Expand All @@ -115,7 +117,7 @@ resource "pagerduty_team_membership" "team_membership_one" {
}
resource "pagerduty_escalation_policy" "no_team_ep" {
name = "no_team_ep"
name = "%s"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
Expand All @@ -127,7 +129,6 @@ resource "pagerduty_escalation_policy" "no_team_ep" {
}
resource "pagerduty_escalation_policy" "one_team_ep" {
depends_on = [pagerduty_team_membership.team_membership_one]
name = "%s"
num_loops = 2
Expand All @@ -142,14 +143,13 @@ resource "pagerduty_escalation_policy" "one_team_ep" {
}
resource "pagerduty_service" "no_team_service" {
name = "no_team_service"
name = "%s"
auto_resolve_timeout = 14400
acknowledgement_timeout = 600
escalation_policy = pagerduty_escalation_policy.no_team_ep.id
alert_creation = "create_incidents"
}
resource "pagerduty_service" "one_team_service" {
name = "%s"
auto_resolve_timeout = 14400
Expand All @@ -166,5 +166,5 @@ data "pagerduty_service" "one_team_service" {
name = pagerduty_service.one_team_service.name
}
`, teamname, username, email, service, escalationPolicy)
`, teamname, username, email, noTeamEsc, escalationPolicy, noTeamServ, service)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccDataSourcePagerDutyStandardsResourceScores_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyStandardsResourceScoresConfig(name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccDataSourcePagerDutyStandardsResourcesScores_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyStandardsResourcesScoresConfig(name),
Expand Down
4 changes: 2 additions & 2 deletions pagerdutyplugin/data_source_pagerduty_standards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccDataSourcePagerDutyStandards_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyStandardsConfig(name),
Expand All @@ -34,7 +34,7 @@ func TestAccDataSourcePagerDutyStandards_WithResourceType(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyStandardsConfigWithResourceType(name, resourceType),
Expand Down
2 changes: 1 addition & 1 deletion pagerdutyplugin/import_pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAccPagerDutyBusinessService_import(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
Steps: []resource.TestStep{
{
Expand Down
27 changes: 16 additions & 11 deletions pagerdutyplugin/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"os"
"testing"

pd "github.com/PagerDuty/terraform-provider-pagerduty/pagerduty"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

pd "github.com/PagerDuty/terraform-provider-pagerduty/pagerduty"
)

var testAccProvider = New()
Expand Down Expand Up @@ -51,16 +51,21 @@ func testAccExternalProviders() map[string]resource.ExternalProvider {
return m
}

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

upgradedSdkServer, err := tf5to6server.UpgradeServer(ctx, pd.Provider(pd.IsMuxed).GRPCProvider)
if err != nil {
return nil, err
}

muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
muxServer, err := tf6muxserver.NewMuxServer(
ctx,
providerserver.NewProtocol6(testAccProvider),
func() tfprotov6.ProviderServer { return upgradedSdkServer },
)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pagerdutyplugin/resource_pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAccPagerDutyBusinessService_Basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAccPagerDutyBusinessService_WithTeam(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
CheckDestroy: testAccCheckPagerDutyBusinessServiceDestroy,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAccPagerDutyBusinessService_SDKv2Compatibility(t *testing.T) {
),
},
{
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Config: commonConfig,
ConfigPlanChecks: resource.ConfigPlanChecks{PreApply: []plancheck.PlanCheck{plancheck.ExpectEmptyPlan()}},
},
Expand Down
Loading

0 comments on commit d8a6533

Please sign in to comment.