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

Feature/nofitication rule project tests #23

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/resources/notification_rule_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ Notification rule project

- `project_id` (String) ID of the project
- `rule_id` (String) ID of the notification rule

### Read-Only

- `id` (String) Synthetic notification rule project ID in the form of project_id/rule_id
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strings"

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/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -34,6 +34,7 @@ type NotificationRuleProjectResource struct {

// NotificationRuleProjectResourceModel describes the resource data model.
type NotificationRuleProjectResourceModel struct {
ID types.String `tfsdk:"id"`
ProjectID types.String `tfsdk:"project_id"`
RuleID types.String `tfsdk:"rule_id"`
}
Expand Down Expand Up @@ -61,6 +62,10 @@ func (r *NotificationRuleProjectResource) Schema(ctx context.Context, req resour
stringplanmodifier.RequiresReplace(),
},
},
"id": schema.StringAttribute{
MarkdownDescription: "Synthetic notification rule project ID in the form of project_id/rule_id",
Computed: true,
},
},
}
}
Expand All @@ -85,28 +90,40 @@ func (r *NotificationRuleProjectResource) Configure(ctx context.Context, req res
}

func (r *NotificationRuleProjectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan NotificationRuleProjectResourceModel
var plan, state NotificationRuleProjectResourceModel

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

ruleID, ruleIDDiags := utils.ParseAttributeUUID(plan.RuleID.ValueString(), "rule_id")
resp.Diagnostics.Append(ruleIDDiags...)

projectID, projectIDDiags := utils.ParseAttributeUUID(plan.ProjectID.ValueString(), "project_id")
resp.Diagnostics.Append(projectIDDiags...)

if resp.Diagnostics.HasError() {
return
}

_, err := r.client.Notification.AddProjectToRule(ctx, uuid.MustParse(plan.RuleID.String()), uuid.MustParse(plan.ProjectID.String()))
_, err := r.client.Notification.AddProjectToRule(ctx, ruleID, projectID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create API key, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create notification rule project, got error: %s", err))
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
state.ID = types.StringValue(makeNotificationRuleProjectID(ruleID, projectID))
state.RuleID = types.StringValue(ruleID.String())
state.ProjectID = types.StringValue(projectID.String())

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func (r *NotificationRuleProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state NotificationRuleProjectResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)

if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -148,12 +165,21 @@ func (r *NotificationRuleProjectResource) Delete(ctx context.Context, req resour
var state NotificationRuleProjectResourceModel

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

ruleID, ruleIDDiags := utils.ParseAttributeUUID(state.RuleID.ValueString(), "rule_id")
resp.Diagnostics.Append(ruleIDDiags...)

projectID, projectIDDiags := utils.ParseAttributeUUID(state.ProjectID.ValueString(), "project_id")
resp.Diagnostics.Append(projectIDDiags...)

if resp.Diagnostics.HasError() {
return
}

_, err := r.client.Notification.DeleteProjectFromRule(ctx, uuid.MustParse(state.RuleID.ValueString()), uuid.MustParse(state.ProjectID.ValueString()))
_, err := r.client.Notification.DeleteProjectFromRule(ctx, ruleID, projectID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete notification rule project relation, got error: %s", err))
return
Expand All @@ -169,6 +195,11 @@ func (r *NotificationRuleProjectResource) ImportState(ctx context.Context, req r
return
}

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), req.ID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), parts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("rule_id"), parts[1])...)
}

func makeNotificationRuleProjectID(ruleID uuid.UUID, projectID uuid.UUID) string {
return fmt.Sprintf("%s/%s", projectID.String(), ruleID.String())
}
Loading
Loading