Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Feb 29, 2024
1 parent 188012f commit fa09d4b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions internal/models/notification_recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ type NotificationRecipientDetailsModel struct {
PDSeverity types.String `tfsdk:"pagerduty_severity"`
}

var NotificationRecipientAttrTypes = map[string]attr.Type{
var NotificationRecipientAttrType = map[string]attr.Type{
"id": types.StringType,
"type": types.StringType,
"target": types.StringType,
"notification_details": types.ListType{ElemType: types.ObjectType{AttrTypes: NotificationRecipientDetailsAttrTypes}},
"notification_details": types.ListType{ElemType: types.ObjectType{AttrTypes: NotificationRecipientDetailsAttrType}},
}

var NotificationRecipientDetailsAttrTypes = map[string]attr.Type{
var NotificationRecipientDetailsAttrType = map[string]attr.Type{
"pagerduty_severity": types.StringType,
}
2 changes: 1 addition & 1 deletion internal/provider/burn_alert_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (r *burnAlertResource) ImportState(ctx context.Context, req resource.Import
resp.Diagnostics.Append(resp.State.Set(ctx, &models.BurnAlertResourceModel{
ID: types.StringValue(id),
Dataset: types.StringValue(dataset),
Recipients: types.SetUnknown(types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}),
Recipients: types.SetUnknown(types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}),
})...)
}

Expand Down
24 changes: 12 additions & 12 deletions internal/provider/notification_recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func reconcileReadNotificationRecipientState(ctx context.Context, remote []clien
var recipients []models.NotificationRecipientModel
diags.Append(state.ElementsAs(ctx, &recipients, false)...)
if diags.HasError() {
return types.SetNull(types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes})
return types.SetNull(types.ObjectType{AttrTypes: models.NotificationRecipientAttrType})
}
mappedRecips := mapNotificationRecipientToState(ctx, remote, recipients, diags)

var values []attr.Value
for _, r := range mappedRecips {
values = append(values, notificationRecipientModelToObjectValue(ctx, r, diags))
}
result, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}, values)
result, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}, values)
diags.Append(d...)

return result
Expand Down Expand Up @@ -167,14 +167,14 @@ func expandNotificationRecipients(ctx context.Context, set types.Set, diags *dia

func flattenNotificationRecipients(ctx context.Context, n []client.NotificationRecipient, diags *diag.Diagnostics) types.Set {
if len(n) == 0 {
return types.SetNull(types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes})
return types.SetNull(types.ObjectType{AttrTypes: models.NotificationRecipientAttrType})
}

var values []attr.Value
for _, r := range n {
values = append(values, notificationRecipientToObjectValue(ctx, r, diags))
}
result, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}, values)
result, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}, values)
diags.Append(d...)

return result
Expand All @@ -187,7 +187,7 @@ func notificationRecipientToObjectValue(ctx context.Context, r client.Notificati
"target": types.StringValue(r.Target),
"notification_details": notificationRecipientDetailsToList(ctx, r.Details, diags),
}
recipObjVal, d := types.ObjectValue(models.NotificationRecipientAttrTypes, recipObj)
recipObjVal, d := types.ObjectValue(models.NotificationRecipientAttrType, recipObj)
diags.Append(d...)

return recipObjVal
Expand All @@ -207,16 +207,16 @@ func notificationRecipientModelToObjectValue(ctx context.Context, r models.Notif
if diags.HasError() {
return basetypes.ObjectValue{}
}
detailsObjVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrTypes, map[string]attr.Value{"pagerduty_severity": details[0].PDSeverity})
detailsObjVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": details[0].PDSeverity})
diags.Append(d...)
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes}, []attr.Value{detailsObjVal})
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, []attr.Value{detailsObjVal})
diags.Append(d...)
} else {
result = types.ListNull(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes})
result = types.ListNull(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType})
}

recipObj["notification_details"] = result
recipObjVal, d := types.ObjectValue(models.NotificationRecipientAttrTypes, recipObj)
recipObjVal, d := types.ObjectValue(models.NotificationRecipientAttrType, recipObj)
diags.Append(d...)

return recipObjVal
Expand All @@ -237,13 +237,13 @@ func notificationRecipientDetailsToList(ctx context.Context, details *client.Not
var result basetypes.ListValue
if details != nil {
detailsObj := map[string]attr.Value{"pagerduty_severity": types.StringValue(string(details.PDSeverity))}
objVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrTypes, detailsObj)
objVal, d := types.ObjectValue(models.NotificationRecipientDetailsAttrType, detailsObj)
diags.Append(d...)
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes}, []attr.Value{objVal})
result, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, []attr.Value{objVal})
diags.Append(d...)

} else {
result = types.ListNull(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes})
result = types.ListNull(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType})
}

return result
Expand Down
13 changes: 6 additions & 7 deletions internal/provider/notification_recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func Test_reconcileReadNotificationRecipientState(t *testing.T) {
elemType := types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}
elemType := types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}
type args struct {
remote []client.NotificationRecipient
state types.Set
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_reconcileReadNotificationRecipientState(t *testing.T) {
ID: types.StringValue("ijkl13579"),
Type: types.StringValue("pagerduty"),
Target: types.StringValue("test-pagerduty"),
Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes}, severityStringToValue("warning")),
Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, severityStringToValue("warning")),
},
}),
},
Expand All @@ -109,7 +109,7 @@ func Test_reconcileReadNotificationRecipientState(t *testing.T) {
state: notificationRecipientModelsToSet([]models.NotificationRecipientModel{
{ID: types.StringValue("abcd12345")},
{Type: types.StringValue("slack"), Target: types.StringValue("#test-foo")},
{ID: types.StringValue("ijkl13579"), Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes}, severityStringToValue("warning"))},
{ID: types.StringValue("ijkl13579"), Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, severityStringToValue("warning"))},
}),
},
want: notificationRecipientModelsToSet([]models.NotificationRecipientModel{
Expand All @@ -125,7 +125,7 @@ func Test_reconcileReadNotificationRecipientState(t *testing.T) {
state: notificationRecipientModelsToSet([]models.NotificationRecipientModel{
{ID: types.StringValue("abcd12345")},
{Type: types.StringValue("slack"), Target: types.StringValue("#test-channel")},
{ID: types.StringValue("ijkl13579"), Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrTypes}, severityStringToValue("warning"))},
{ID: types.StringValue("ijkl13579"), Details: types.ListValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientDetailsAttrType}, severityStringToValue("warning"))},
}),
},
want: notificationRecipientModelsToSet([]models.NotificationRecipientModel{
Expand All @@ -146,10 +146,9 @@ func notificationRecipientModelsToSet(n []models.NotificationRecipientModel) typ
for _, r := range n {
values = append(values, notificationRecipientModelToObjectValue(context.Background(), r, &diag.Diagnostics{}))
}
return types.SetValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}, values)
return types.SetValueMust(types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}, values)
}

func severityStringToValue(s string) []attr.Value {
detailsObj := map[string]attr.Value{"pagerduty_severity": types.StringValue(s)}
return []attr.Value{types.ObjectValueMust(models.NotificationRecipientDetailsAttrTypes, detailsObj)}
return []attr.Value{types.ObjectValueMust(models.NotificationRecipientDetailsAttrType, map[string]attr.Value{"pagerduty_severity": types.StringValue(s)})}
}
2 changes: 1 addition & 1 deletion internal/provider/trigger_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (r *triggerResource) ImportState(ctx context.Context, req resource.ImportSt
resp.Diagnostics.Append(resp.State.Set(ctx, &models.TriggerResourceModel{
ID: types.StringValue(id),
Dataset: types.StringValue(dataset),
Recipients: types.SetUnknown(types.ObjectType{AttrTypes: models.NotificationRecipientAttrTypes}),
Recipients: types.SetUnknown(types.ObjectType{AttrTypes: models.NotificationRecipientAttrType}),
})...)
}

Expand Down

0 comments on commit fa09d4b

Please sign in to comment.