-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdata_source_pagerduty_alert_grouping_setting.go
110 lines (96 loc) · 3.25 KB
/
data_source_pagerduty_alert_grouping_setting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 string
var found *pagerduty.AlertGroupingSetting
err := apiutil.All(ctx, func(offset int) (bool, error) {
resp, err := d.client.ListAlertGroupingSettings(ctx, pagerduty.ListAlertGroupingSettingsOptions{
After: cursorAfter,
Limit: 100,
})
if err != nil {
return false, err
}
for _, alertGroupingSetting := range resp.AlertGroupingSettings {
if alertGroupingSetting.Name == searchName.ValueString() {
found = &alertGroupingSetting
break
}
}
cursorAfter = resp.After
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)...)
}