Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource and data source for Alert Grouping Setting #935

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/PagerDuty/terraform-provider-pagerduty
go 1.20

require (
github.com/PagerDuty/go-pagerduty v1.8.1-0.20240524180345-9b652f07c450
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.6.2
Expand Down Expand Up @@ -66,7 +66,7 @@ require (
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20240524180345-9b652f07c450 h1:G8EOmgL7i+vO6hYAnjpVbPDqUpPY7ThF6NfasZntop0=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20240524180345-9b652f07c450/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88 h1:y/icahuphX4xGMW4nLN+Bl4MbFUU4rEA9spwgcPIDJk=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX7IL/m9Y5LO+KQYv+t1CQOiFe6+SV2J7bE=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
Expand Down Expand Up @@ -189,8 +189,8 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ=
golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
1 change: 1 addition & 0 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func resourcePagerDutyService() *schema.Resource {
Optional: true,
Computed: true,
MaxItems: 1,
Deprecated: "Use a resource `pagerduty_alert_grouping_setting` instead",
ConflictsWith: []string{"alert_grouping", "alert_grouping_timeout"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down
110 changes: 110 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_alert_grouping_setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package pagerduty

import (
"context"
"fmt"
"log"

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

type dataSourceAlertGroupingSetting struct{ client *pagerduty.Client }

var _ datasource.DataSourceWithConfigure = (*dataSourceAlertGroupingSetting)(nil)

func (*dataSourceAlertGroupingSetting) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "pagerduty_alert_grouping_setting"
}

func (*dataSourceAlertGroupingSetting) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true},
"name": schema.StringAttribute{Required: true},
"description": schema.StringAttribute{Computed: true},
"type": schema.StringAttribute{Computed: true},
"services": schema.SetAttribute{
Computed: true,
ElementType: types.StringType,
},
},
Blocks: map[string]schema.Block{
"config": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"timeout": schema.Int64Attribute{
Computed: true,
},
"time_window": schema.Int64Attribute{
Computed: true,
},
"aggregate": schema.StringAttribute{
Optional: true,
},
"fields": schema.SetAttribute{
ElementType: types.StringType,
Optional: true,
},
},
},
},
}
}

func (d *dataSourceAlertGroupingSetting) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...)
}

func (d *dataSourceAlertGroupingSetting) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
log.Println("[INFO] Reading PagerDuty alert grouping setting")

var searchName types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &searchName)...)
if resp.Diagnostics.HasError() {
return
}

var cursorAfter, cursorBefore string
var found *pagerduty.AlertGroupingSetting
err := apiutil.All(ctx, func(offset int) (bool, error) {
resp, err := d.client.ListAlertGroupingSettings(ctx, pagerduty.ListAlertGroupingSettingsOptions{
After: cursorAfter,
Before: cursorBefore,
Limit: 100,
})
if err != nil {
return false, err
}

for _, alertGroupingSetting := range resp.AlertGroupingSettings {
if alertGroupingSetting.Name == searchName.ValueString() {
found = &alertGroupingSetting
break
}
}

return resp.After != "", nil
})
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty alert grouping setting %s", searchName),
err.Error(),
)
return
}

if found == nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to locate any alert grouping setting with the name: %s", searchName),
"",
)
return
}

model := flattenAlertGroupingSetting(found)
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
131 changes: 131 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_alert_grouping_setting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccDataSourcePagerDutyAlertGroupingSetting_Time(t *testing.T) {
name := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyAlertGroupingSettingTimeConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyAlertGroupingSetting("pagerduty_alert_grouping_setting.test", "data.pagerduty_alert_grouping_setting.by_name"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "name", name),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "type", "time"),
resource.TestCheckResourceAttrSet("data.pagerduty_alert_grouping_setting.by_name", "description"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "services.#", "1"),
),
},
},
})
}

func TestAccDataSourcePagerDutyAlertGroupingSetting_ContentBased(t *testing.T) {
name := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyAlertGroupingSettingContentBasedConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyAlertGroupingSetting("pagerduty_alert_grouping_setting.test", "data.pagerduty_alert_grouping_setting.by_name"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "name", name),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "type", "content_based"),
resource.TestCheckResourceAttrSet("data.pagerduty_alert_grouping_setting.by_name", "description"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "services.#", "1"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "config.time_window", "300"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "config.aggregate", "any"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "config.fields.#", "1"),
resource.TestCheckResourceAttr("data.pagerduty_alert_grouping_setting.by_name", "config.fields.0", "summary"),
),
},
},
})
}

func testAccDataSourcePagerDutyAlertGroupingSetting(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
srcR := s.RootModule().Resources[src]
srcA := srcR.Primary.Attributes

r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected to get a alert grouping setting ID from PagerDuty")
}

testAtts := []string{"id", "name"}

for _, att := range testAtts {
if a[att] != srcA[att] {
return fmt.Errorf("Expected the alert grouping setting %s to be: %s, but got: %s", att, srcA[att], a[att])
}
}

return nil
}
}

func testAccDataSourcePagerDutyAlertGroupingSettingTimeConfig(name string) string {
return fmt.Sprintf(`
data "pagerduty_escalation_policy" "test" {
name = "Default"
}

resource "pagerduty_service" "test" {
name = "%s"
escalation_policy = data.pagerduty_escalation_policy.test.id
}

resource "pagerduty_alert_grouping_setting" "test" {
name = "%[1]s"
type = "time"
services = [pagerduty_service.test.id]
config {}
}

data "pagerduty_alert_grouping_setting" "by_name" {
name = pagerduty_alert_grouping_setting.test.name
}
`, name)
}

func testAccDataSourcePagerDutyAlertGroupingSettingContentBasedConfig(name string) string {
return fmt.Sprintf(`
data "pagerduty_escalation_policy" "test" {
name = "Default"
}

resource "pagerduty_service" "test" {
name = "%s"
escalation_policy = data.pagerduty_escalation_policy.test.id
}

resource "pagerduty_alert_grouping_setting" "test" {
name = "%[1]s"
type = "content_based"
services = [pagerduty_service.test.id]
config {
aggregate = "any"
fields = ["summary"]
}
}

data "pagerduty_alert_grouping_setting" "by_name" {
name = pagerduty_alert_grouping_setting.test.name
}
`, name)
}
2 changes: 2 additions & 0 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro

func (p *Provider) DataSources(_ context.Context) [](func() datasource.DataSource) {
return [](func() datasource.DataSource){
func() datasource.DataSource { return &dataSourceAlertGroupingSetting{} },
func() datasource.DataSource { return &dataSourceBusinessService{} },
func() datasource.DataSource { return &dataSourceExtensionSchema{} },
func() datasource.DataSource { return &dataSourceIntegration{} },
Expand All @@ -69,6 +70,7 @@ func (p *Provider) DataSources(_ context.Context) [](func() datasource.DataSourc
func (p *Provider) Resources(_ context.Context) [](func() resource.Resource) {
return [](func() resource.Resource){
func() resource.Resource { return &resourceAddon{} },
func() resource.Resource { return &resourceAlertGroupingSetting{} },
func() resource.Resource { return &resourceBusinessService{} },
func() resource.Resource { return &resourceExtensionServiceNow{} },
func() resource.Resource { return &resourceExtension{} },
Expand Down
Loading
Loading