Skip to content

Commit

Permalink
Add notification rule project relation resource
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Apr 8, 2024
1 parent b930632 commit 9837795
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/futurice/terraform-provider-dependencytrack
go 1.21

require (
github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3
github.com/futurice/dependency-track-client-go v0.0.0-20240408102005-c3ad5fb57202
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-docs v0.18.0
github.com/hashicorp/terraform-plugin-framework v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3 h1:LseLMUNGMHS0yvnMBWdIgBkpZrlVCCacCaDk+YDDghw=
github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3/go.mod h1:nSSUhNjXItvlllTmfE3BdooP1GtAuu6dDXFAHSem0Jk=
github.com/futurice/dependency-track-client-go v0.0.0-20240408102005-c3ad5fb57202 h1:RIhdzp/3/9vIx+lMOwJzmo8kLJoGp70JPSu6jlehqgU=
github.com/futurice/dependency-track-client-go v0.0.0-20240408102005-c3ad5fb57202/go.mod h1:nSSUhNjXItvlllTmfE3BdooP1GtAuu6dDXFAHSem0Jk=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
Expand Down
166 changes: 166 additions & 0 deletions internal/provider/notification_rule_project_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"fmt"

dtrack "github.com/futurice/dependency-track-client-go"
"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"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &NotificationRuleProjectResource{}
var _ resource.ResourceWithImportState = &NotificationRuleProjectResource{}

func NewNotificationRuleProjectResource() resource.Resource {
return &NotificationRuleProjectResource{}
}

// NotificationRuleProjectResource defines the resource implementation.
type NotificationRuleProjectResource struct {
client *dtrack.Client
}

// NotificationRuleProjectResourceModel describes the resource data model.
type NotificationRuleProjectResourceModel struct {
ProjectID types.String `tfsdk:"project_id"`
RuleID types.String `tfsdk:"rule_id"`
}

func (r *NotificationRuleProjectResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_notification_rule_project"
}

func (r *NotificationRuleProjectResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Notification rule project",

Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
MarkdownDescription: "ID of the project",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"rule_id": schema.StringAttribute{
MarkdownDescription: "ID of the notification rule",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}

func (r *NotificationRuleProjectResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*dtrack.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *dtrack.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = client
}

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

resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)

if resp.Diagnostics.HasError() {
return
}

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

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

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
}

// There is no API mehtod for a single rule, so we need to get all rules and filter
rules, err := r.client.Notification.GetAllRules(ctx)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read team, got error: %s", err))
return
}

found := false
for _, rule := range rules {
if rule.UUID.String() != state.RuleID.ValueString() {
continue
}

for _, project := range rule.Projects {
if project.UUID.String() == state.ProjectID.ValueString() {
found = true
break
}
}
}

if !found {
resp.State.RemoveResource(ctx)
return
}

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

func (r *NotificationRuleProjectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError("Internal Error", "Notification rule project relation resource is immutable")
}

func (r *NotificationRuleProjectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state NotificationRuleProjectResourceModel

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

if resp.Diagnostics.HasError() {
return
}

_, err := r.client.Notification.DeleteProjectFromRule(ctx, uuid.MustParse(state.RuleID.ValueString()), uuid.MustParse(state.ProjectID.ValueString()))
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete notification rule project relation, got error: %s", err))
return
}

resp.State.RemoveResource(ctx)
}

func (r *NotificationRuleProjectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (p *DependencyTrackProvider) Resources(ctx context.Context) []func() resour
NewProjectResource,
NewACLMappingResource,
NewNotificationRuleResource,
NewNotificationRuleProjectResource,
NewNotificationPublisherResource,
}
}
Expand Down
7 changes: 1 addition & 6 deletions internal/provider/team_api_key_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *TeamAPIKeyResource) Metadata(ctx context.Context, req resource.Metadata

func (r *TeamAPIKeyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Team permission",
MarkdownDescription: "API Key for a team",

Attributes: map[string]schema.Attribute{
"team_id": schema.StringAttribute{
Expand Down Expand Up @@ -113,11 +113,6 @@ func (r *TeamAPIKeyResource) Read(ctx context.Context, req resource.ReadRequest,
// NOTE: API only returns the API keys for the team when fetching all the teams
teams, err := r.client.Team.GetAll(ctx, dtrack.PageOptions{})
if err != nil {
if apiErr, ok := err.(*dtrack.APIError); ok && apiErr.StatusCode == 404 {
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read team, got error: %s", err))
return
}
Expand Down

0 comments on commit 9837795

Please sign in to comment.