Skip to content

Commit

Permalink
first pass at stubbing out alert variable data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Dec 20, 2024
1 parent f349ad5 commit dd05ad7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
8 changes: 7 additions & 1 deletion client/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ type RecipientDetails struct {
WebhookPayloads *WebhookPayloads `json:"webhook_payloads,omitempty"`
}

type NotificationVariable struct {
Name string `json:"name"`
Value string `json:"value"`
}

type NotificationRecipientDetails struct {
PDSeverity PagerDutySeverity `json:"pagerduty_severity,omitempty"`
Variables []NotificationVariable `json:"variables,omitempty"`
PDSeverity PagerDutySeverity `json:"pagerduty_severity,omitempty"`
}

type WebhookPayloads struct {
Expand Down
12 changes: 12 additions & 0 deletions internal/models/notification_recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ var NotificationRecipientAttrType = map[string]attr.Type{

type NotificationRecipientDetailsModel struct {
PDSeverity types.String `tfsdk:"pagerduty_severity"`
Variables types.Set `tfsdk:"variable"`
}

var NotificationRecipientDetailsAttrType = map[string]attr.Type{
"pagerduty_severity": types.StringType,
"variable": types.SetType{ElemType: types.ObjectType{AttrTypes: NotificationVariableAttrType}},
}

type NotificationVariableModel struct {
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
}

var NotificationVariableAttrType = map[string]attr.Type{
"name": types.StringType,
"value": types.StringType,
}
82 changes: 80 additions & 2 deletions internal/provider/notification_recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -85,6 +87,33 @@ func notificationRecipientSchema(allowedTypes []client.RecipientType) schema.Set
},
},
},
"variable": schema.SetNestedBlock{
Description: "The variables to set with the Webhook notification.",
Validators: []validator.Set{
setvalidator.SizeAtMost(10),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Description: "The name of the variable",
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(webhookTemplateNameRegex, "must be an alphanumeric string beginning with a lowercase letter"),
},
},
"value": schema.StringAttribute{
Description: "An optional default value for the variable",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
stringvalidator.LengthAtMost(256),
},
},
},
},
},
},
},
}
Expand Down Expand Up @@ -155,8 +184,10 @@ func expandNotificationRecipients(ctx context.Context, set types.Set, diags *dia
if diags.HasError() {
return nil
}

rcpt.Details = &client.NotificationRecipientDetails{
PDSeverity: client.PagerDutySeverity(details[0].PDSeverity.ValueString()),
Variables: expandNotificationVariables(ctx, details[0].Variables, diags),
}
}
clientRecips[i] = rcpt
Expand Down Expand Up @@ -207,7 +238,8 @@ func notificationRecipientModelToObjectValue(ctx context.Context, r models.Notif
if diags.HasError() {
return basetypes.ObjectValue{}
}
detailsObjVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": details[0].PDSeverity})

detailsObjVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": details[0].PDSeverity, "variable": details[0].Variables})
diags.Append(d...)
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, []attr.Value{detailsObjVal})
diags.Append(d...)
Expand Down Expand Up @@ -236,7 +268,7 @@ func notificationRecipientToModel(ctx context.Context, r client.NotificationReci
func notificationRecipientDetailsToList(ctx context.Context, details *client.NotificationRecipientDetails, diags *diag.Diagnostics) basetypes.ListValue {
var result basetypes.ListValue
if details != nil {
detailsObj := map[string]attr.Value{"pagerduty_severity": types.StringValue(string(details.PDSeverity))}
detailsObj := map[string]attr.Value{"pagerduty_severity": types.StringValue(string(details.PDSeverity)), "variable": flattenNotificationVariables(ctx, details.Variables, diags)}
objVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrType, detailsObj)
diags.Append(d...)
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, []attr.Value{objVal})
Expand All @@ -247,3 +279,49 @@ func notificationRecipientDetailsToList(ctx context.Context, details *client.Not

return result
}

func notificationVariableToObjectValue(v client.NotificationVariable, diags *diag.Diagnostics) basetypes.ObjectValue {
variableObj := map[string]attr.Value{
"name": types.StringValue(v.Name),
"value": types.StringValue(v.Value),
}
varObjVal, d := types.ObjectValue(models.NotificationVariableAttrType, variableObj)
diags.Append(d...)

return varObjVal
}

func flattenNotificationVariables(ctx context.Context, vars []client.NotificationVariable, diags *diag.Diagnostics) types.Set {
if len(vars) == 0 || vars == nil {
return types.SetNull(types.ObjectType{AttrTypes: models.NotificationVariableAttrType})
}

var notifVarValues []attr.Value
for _, v := range vars {
notifVarValues = append(notifVarValues, notificationVariableToObjectValue(v, diags))
}
notifVarResult, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: models.WebhookHeaderAttrType}, notifVarValues)
diags.Append(d...)

return notifVarResult
}

func expandNotificationVariables(ctx context.Context, set types.Set, diags *diag.Diagnostics) []client.NotificationVariable {
var notifVars []models.NotificationVariableModel
diags.Append(set.ElementsAs(ctx, &notifVars, false)...)
if diags.HasError() {
return nil
}

clientNotifVars := make([]client.NotificationVariable, len(notifVars))
for i, v := range notifVars {
notifVar := client.NotificationVariable{
Name: v.Name.ValueString(),
Value: v.Value.ValueString(),
}

clientNotifVars[i] = notifVar
}

return clientNotifVars
}
2 changes: 1 addition & 1 deletion internal/provider/notification_recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ func notificationRecipientModelsToSet(n []models.NotificationRecipientModel) typ
}

func severityStringToValue(s string) []attr.Value {
return []attr.Value{types.ObjectValueMust(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": types.StringValue(s)})}
return []attr.Value{types.ObjectValueMust(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": types.StringValue(s), "variable": types.SetNull(types.ObjectType{AttrTypes: models.NotificationVariableAttrType})})}
}

0 comments on commit dd05ad7

Please sign in to comment.