-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resource and data source for Alert Grouping Setting
- Loading branch information
Showing
32 changed files
with
3,018 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
pagerdutyplugin/data_source_pagerduty_alert_grouping_setting.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
pagerdutyplugin/data_source_pagerduty_alert_grouping_setting_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.