Skip to content

Commit

Permalink
Standardise uuid handling in notification rule resource
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-futurice committed Jul 29, 2024
1 parent eb9425c commit 1e556ab
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions internal/provider/notificationrule/notification_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"

dtrack "github.com/futurice/dependency-track-client-go"
"github.com/futurice/terraform-provider-dependencytrack/internal/utils"
"github.com/google/uuid"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -237,12 +237,17 @@ func (r *NotificationRuleResource) Delete(ctx context.Context, req resource.Dele
var state NotificationRuleResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

ruleID, ruleIDDiags := utils.ParseAttributeUUID(state.ID.ValueString(), "id")
resp.Diagnostics.Append(ruleIDDiags...)
if resp.Diagnostics.HasError() {
return
}

err := r.client.Notification.DeleteRule(ctx, uuid.MustParse(state.ID.ValueString()))
err := r.client.Notification.DeleteRule(ctx, ruleID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete notification rule, got error: %s", err))
return
Expand Down Expand Up @@ -282,9 +287,14 @@ func DTRuleToTFRule(ctx context.Context, dtRule dtrack.NotificationRule) (Notifi
}

func TFRuleToDTRule(ctx context.Context, tfRule NotificationRuleResourceModel) (dtrack.NotificationRule, diag.Diagnostics) {
var diags diag.Diagnostics

publisherID, publisherIDDiags := utils.ParseAttributeUUID(tfRule.PublisherID.ValueString(), "publisher_id")
diags.Append(publisherIDDiags...)

rule := dtrack.NotificationRule{
Name: tfRule.Name.ValueString(),
Publisher: dtrack.NotificationPublisher{UUID: uuid.MustParse(tfRule.PublisherID.ValueString())},
Publisher: dtrack.NotificationPublisher{UUID: publisherID},
Scope: tfRule.Scope.ValueString(),
NotificationLevel: tfRule.NotificationLevel.ValueString(),
Enabled: tfRule.Enabled.ValueBool(),
Expand All @@ -294,16 +304,22 @@ func TFRuleToDTRule(ctx context.Context, tfRule NotificationRuleResourceModel) (
}

elements := make([]types.String, 0, len(tfRule.NotifyOn.Elements()))
diags := tfRule.NotifyOn.ElementsAs(ctx, &elements, false)
rule.NotifyOn = make([]string, len(elements))
for i := range elements {
rule.NotifyOn[i] = elements[i].ValueString()
notifyOnDiags := tfRule.NotifyOn.ElementsAs(ctx, &elements, false)
diags.Append(notifyOnDiags...)
if !notifyOnDiags.HasError() {
rule.NotifyOn = make([]string, len(elements))
for i := range elements {
rule.NotifyOn[i] = elements[i].ValueString()
}
}

if tfRule.ID.IsUnknown() {
rule.UUID = uuid.Nil
} else {
rule.UUID = uuid.MustParse(tfRule.ID.ValueString())
ruleID, ruleIDDiags := utils.ParseAttributeUUID(tfRule.ID.ValueString(), "id")
diags.Append(ruleIDDiags...)

rule.UUID = ruleID
}

return rule, diags
Expand Down

0 comments on commit 1e556ab

Please sign in to comment.