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

[CSGINS-1453] Add incident types resources #962

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ require (
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

replace github.com/PagerDuty/go-pagerduty => github.com/cjgajard/go-pagerduty v0.0.0-20250107134752-8a1269887cc8
cjgajard marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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.20241111225923-0ef8f340dd3c h1:Bnw38sqsVdQVOiuTealk57GnuRb86zyd5v/XW3BcPl8=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241111225923-0ef8f340dd3c/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 All @@ -11,6 +9,8 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cjgajard/go-pagerduty v0.0.0-20250107134752-8a1269887cc8 h1:HzQ4RmpMr9TsJYAxk47zPqa7ZvjPIuTJPOR1w/+rVuU=
github.com/cjgajard/go-pagerduty v0.0.0-20250107134752-8a1269887cc8/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
Expand Down
108 changes: 108 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_incident_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pagerduty

import (
"context"
"fmt"
"log"
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

type dataSourceIncidentType struct{ client *pagerduty.Client }

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

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

func (*dataSourceIncidentType) 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{Computed: true},
"type": schema.StringAttribute{Computed: true},
"display_name": schema.StringAttribute{Required: true},
"description": schema.StringAttribute{Computed: true},
"parent_type": schema.StringAttribute{Computed: true},
"enabled": schema.BoolAttribute{Computed: true},
},
}
}

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

func (d *dataSourceIncidentType) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
log.Println("[INFO] Reading PagerDuty incident type")

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

var found *pagerduty.IncidentType
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
response, err := d.client.ListIncidentTypes(ctx, pagerduty.ListIncidentTypesOptions{Filter: "all"})
if err != nil {
if util.IsBadRequestError(err) {
return retry.NonRetryableError(err)
}
return retry.RetryableError(err)
}
for _, it := range response.IncidentTypes {
if it.DisplayName == searchName.ValueString() {
found = &it
break
}
}
return nil
})
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty incident type %s", searchName),
err.Error(),
)
return
}

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

model := dataSourceIncidentTypeModel{
ID: types.StringValue(found.ID),
Name: types.StringValue(found.Name),
Type: types.StringValue(found.Type),
DisplayName: types.StringValue(found.DisplayName),
Description: types.StringValue(found.Description),
Enabled: types.BoolValue(found.Enabled),
}
if found.Parent != nil {
model.ParentType = types.StringValue(found.Parent.ID)
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

type dataSourceIncidentTypeModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
DisplayName types.String `tfsdk:"display_name"`
Description types.String `tfsdk:"description"`
ParentType types.String `tfsdk:"parent_type"`
Enabled types.Bool `tfsdk:"enabled"`
}
177 changes: 177 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_incident_type_custom_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package pagerduty

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

type dataSourceIncidentTypeCustomField struct{ client *pagerduty.Client }

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

func (*dataSourceIncidentTypeCustomField) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "pagerduty_incident_type_custom_field"
}

func (*dataSourceIncidentTypeCustomField) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true},
"incident_type": schema.StringAttribute{Required: true},
"display_name": schema.StringAttribute{Required: true},
"data_type": schema.StringAttribute{Computed: true},
"default_value": schema.StringAttribute{
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"description": schema.StringAttribute{Computed: true},
"enabled": schema.BoolAttribute{Computed: true},
"field_options": schema.ListAttribute{
Computed: true,
ElementType: incidentTypeFieldOptionObjectType,
},
"field_type": schema.StringAttribute{Computed: true},
"name": schema.StringAttribute{Computed: true},
"self": schema.StringAttribute{Computed: true},
"summary": schema.StringAttribute{Computed: true},
"type": schema.StringAttribute{Computed: true},
},
}
}

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

func (d *dataSourceIncidentTypeCustomField) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var searchName types.String
diags := req.Config.GetAttribute(ctx, path.Root("display_name"), &searchName)
if resp.Diagnostics.Append(diags...); diags.HasError() {
return
}

var searchIncidentType types.String
diags = req.Config.GetAttribute(ctx, path.Root("incident_type"), &searchIncidentType)
if resp.Diagnostics.Append(diags...); diags.HasError() {
return
}
incidentTypeID := searchIncidentType.ValueString()

log.Printf("[INFO] Reading PagerDuty incident type custom field %s %s", searchIncidentType, searchName)

var found *pagerduty.IncidentTypeField
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
response, err := d.client.ListIncidentTypeFields(ctx, incidentTypeID, pagerduty.ListIncidentTypeFieldsOptions{
Includes: []string{"field_options"},
})
if err != nil {
if util.IsBadRequestError(err) {
return retry.NonRetryableError(err)
}
return retry.RetryableError(err)
}
for _, f := range response.Fields {
if f.DisplayName == searchName.ValueString() {
found = &f
break
}
}
return nil
})
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty incident type custom field %s", searchName),
err.Error(),
)
return
}

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

defaultValue, _ := json.Marshal(found.DefaultValue)

elements := make([]attr.Value, 0, len(found.FieldOptions))
for _, opt := range found.FieldOptions {
dataObj := types.ObjectNull(incidentTypeFieldOptionDataObjectType.AttrTypes)
if opt.Data != nil {
dataObj = types.ObjectValueMust(incidentTypeFieldOptionDataObjectType.AttrTypes, map[string]attr.Value{
"value": types.StringValue(opt.Data.Value),
"data_type": types.StringValue(opt.Data.DataType),
})
}
obj := types.ObjectValueMust(incidentTypeFieldOptionObjectType.AttrTypes, map[string]attr.Value{
"id": types.StringValue(opt.ID),
"type": types.StringValue(opt.Type),
"data": dataObj,
})
elements = append(elements, obj)
}
fieldOptions := types.ListValueMust(incidentTypeFieldOptionObjectType, elements)

model := dataSourceIncidentTypeCustomFieldModel{
ID: types.StringValue(found.ID),
IncidentType: types.StringValue(found.IncidentType),
DisplayName: types.StringValue(found.DisplayName),
DataType: types.StringValue(found.DataType),
DefaultValue: jsontypes.NewNormalizedValue(string(defaultValue)),
Description: types.StringValue(found.Description),
Enabled: types.BoolValue(found.Enabled),
FieldOptions: fieldOptions,
FieldType: types.StringValue(found.FieldType),
Name: types.StringValue(found.Name),
Self: types.StringValue(found.Self),
Summary: types.StringValue(found.Summary),
Type: types.StringValue(found.Type),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

type dataSourceIncidentTypeCustomFieldModel struct {
ID types.String `tfsdk:"id"`
IncidentType types.String `tfsdk:"incident_type"`
DisplayName types.String `tfsdk:"display_name"`
DataType types.String `tfsdk:"data_type"`
DefaultValue jsontypes.Normalized `tfsdk:"default_value"`
Description types.String `tfsdk:"description"`
Enabled types.Bool `tfsdk:"enabled"`
FieldOptions types.List `tfsdk:"field_options"`
FieldType types.String `tfsdk:"field_type"`
Name types.String `tfsdk:"name"`
Self types.String `tfsdk:"self"`
Summary types.String `tfsdk:"summary"`
Type types.String `tfsdk:"type"`
}

var incidentTypeFieldOptionDataObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"value": types.StringType,
"data_type": types.StringType,
},
}

var incidentTypeFieldOptionObjectType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"type": types.StringType,
"data": incidentTypeFieldOptionDataObjectType,
},
}
4 changes: 4 additions & 0 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (p *Provider) DataSources(_ context.Context) [](func() datasource.DataSourc
func() datasource.DataSource { return &dataSourceAlertGroupingSetting{} },
func() datasource.DataSource { return &dataSourceBusinessService{} },
func() datasource.DataSource { return &dataSourceExtensionSchema{} },
func() datasource.DataSource { return &dataSourceIncidentTypeCustomField{} },
func() datasource.DataSource { return &dataSourceIncidentType{} },
func() datasource.DataSource { return &dataSourceIntegration{} },
func() datasource.DataSource { return &dataSourceJiraCloudAccountMapping{} },
func() datasource.DataSource { return &dataSourceLicenses{} },
Expand All @@ -76,6 +78,8 @@ func (p *Provider) Resources(_ context.Context) [](func() resource.Resource) {
func() resource.Resource { return &resourceBusinessService{} },
func() resource.Resource { return &resourceExtensionServiceNow{} },
func() resource.Resource { return &resourceExtension{} },
func() resource.Resource { return &resourceIncidentTypeCustomField{} },
func() resource.Resource { return &resourceIncidentType{} },
func() resource.Resource { return &resourceJiraCloudAccountMappingRule{} },
func() resource.Resource { return &resourceServiceDependency{} },
func() resource.Resource { return &resourceTagAssignment{} },
Expand Down
Loading
Loading