-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
290 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
// 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/diag" | ||
"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/booldefault" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ resource.Resource = &NotificationPublisherResource{} | ||
var _ resource.ResourceWithImportState = &NotificationPublisherResource{} | ||
|
||
func NewNotificationPublisherResource() resource.Resource { | ||
return &NotificationPublisherResource{} | ||
} | ||
|
||
// NotificationPublisherResource defines the resource implementation. | ||
type NotificationPublisherResource struct { | ||
client *dtrack.Client | ||
} | ||
|
||
// NotificationPublisherResourceModel describes the resource data model. | ||
type NotificationPublisherResourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Description types.String `tfsdk:"description"` | ||
PublisherClass types.String `tfsdk:"publisher_class"` | ||
DefaultPublisher types.Bool `tfsdk:"default_publisher"` | ||
TemplateMimeType types.String `tfsdk:"template_mime_type"` | ||
Template types.String `tfsdk:"template"` | ||
} | ||
|
||
func (r *NotificationPublisherResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_notification_publisher" | ||
} | ||
|
||
func (r *NotificationPublisherResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Notification publisher", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
MarkdownDescription: "Name of the rule", | ||
Required: true, | ||
}, | ||
"id": schema.StringAttribute{ | ||
MarkdownDescription: "Publisher UUID", | ||
Computed: true, | ||
}, | ||
"template_mime_type": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"template": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"publisher_class": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"description": schema.StringAttribute{ | ||
Optional: true, | ||
}, | ||
"default_publisher": schema.BoolAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: booldefault.StaticBool(false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *NotificationPublisherResource) 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 *NotificationPublisherResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var plan NotificationPublisherResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
dtPublisher, diags := TFPublisherToDTPublisher(ctx, plan) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
respPublisher, err := r.client.Notification.CreatePublisher(ctx, dtPublisher) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create notification publisher, got error: %s", err)) | ||
return | ||
} | ||
|
||
plan, diags = DTPublisherToTFPublisher(ctx, respPublisher) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) | ||
} | ||
|
||
func (r *NotificationPublisherResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var state NotificationPublisherResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
publishers, err := r.client.Notification.GetAllPublishers(ctx) | ||
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 notification publisher, got error: %s", err)) | ||
return | ||
} | ||
|
||
found := false | ||
for _, publisher := range publishers { | ||
if publisher.UUID.String() == state.ID.ValueString() { | ||
found = true | ||
newState, diags := DTPublisherToTFPublisher(ctx, publisher) | ||
resp.Diagnostics.Append(diags...) | ||
state = newState | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
resp.State.RemoveResource(ctx) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (r *NotificationPublisherResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var plan, state NotificationPublisherResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
plan.ID = state.ID | ||
|
||
dtPublisher, diags := TFPublisherToDTPublisher(ctx, plan) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
respPublisher, err := r.client.Notification.UpdatePublisher(ctx, dtPublisher) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update notification publisher, got error: %s", err)) | ||
return | ||
} | ||
|
||
state, diags = DTPublisherToTFPublisher(ctx, respPublisher) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (r *NotificationPublisherResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var state NotificationPublisherResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
err := r.client.Notification.DeletePublisher(ctx, uuid.MustParse(state.ID.ValueString())) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete notification publisher, got error: %s", err)) | ||
return | ||
} | ||
|
||
resp.State.RemoveResource(ctx) | ||
} | ||
|
||
func (r *NotificationPublisherResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} | ||
|
||
func DTPublisherToTFPublisher(ctx context.Context, dtPublisher dtrack.NotificationPublisher) (NotificationPublisherResourceModel, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
publisher := NotificationPublisherResourceModel{ | ||
ID: types.StringValue(dtPublisher.UUID.String()), | ||
Name: types.StringValue(dtPublisher.Name), | ||
Description: types.StringValue(dtPublisher.Description), | ||
PublisherClass: types.StringValue(dtPublisher.PublisherClass), | ||
DefaultPublisher: types.BoolValue(dtPublisher.DefaultPublisher), | ||
TemplateMimeType: types.StringValue(dtPublisher.TemplateMimeType), | ||
Template: types.StringValue(dtPublisher.Template), | ||
} | ||
|
||
return publisher, diags | ||
} | ||
|
||
func TFPublisherToDTPublisher(ctx context.Context, tfPublisher NotificationPublisherResourceModel) (dtrack.NotificationPublisher, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
publisher := dtrack.NotificationPublisher{ | ||
Name: tfPublisher.Name.ValueString(), | ||
Description: tfPublisher.Description.ValueString(), | ||
PublisherClass: tfPublisher.PublisherClass.ValueString(), | ||
DefaultPublisher: tfPublisher.DefaultPublisher.ValueBool(), | ||
TemplateMimeType: tfPublisher.TemplateMimeType.ValueString(), | ||
Template: tfPublisher.Template.ValueString(), | ||
} | ||
|
||
if tfPublisher.ID.IsUnknown() { | ||
publisher.UUID = uuid.Nil | ||
} else { | ||
publisher.UUID = uuid.MustParse(tfPublisher.ID.ValueString()) | ||
} | ||
|
||
return publisher, diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters