From 4fcef39bbb89a9c82884835fc6a9b7d4cbaa35f8 Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Fri, 29 Sep 2023 15:44:30 +0300 Subject: [PATCH] PRODENG-2370 re-enable terraform log out (#44) - TF_LOG=debug now shows terraform output ALSO - fixed skip_destroy (broken) - added skip_create to allow a resource definition to exist, but creation fails Signed-off-by: James Nesbitt --- docs/resources/config.md | 3 ++- internal/provider/launchpad_config_resource.go | 14 ++++++++++---- internal/provider/mcc_schema_1_4.go | 9 ++++++++- internal/provider/tools.go | 17 ++++++++++------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/docs/resources/config.md b/docs/resources/config.md index 5107f46..29064fc 100644 --- a/docs/resources/config.md +++ b/docs/resources/config.md @@ -91,7 +91,8 @@ resource "launchpad_config" "example" { ### Optional - `metadata` (Block, Optional) Metadata for the launchpad cluster (see [below for nested schema](#nestedblock--metadata)) -- `skip_destroy` (Boolean) Do not bother uninstalling on destroy +- `skip_create` (Boolean) Skip apply on create +- `skip_destroy` (Boolean) Skip reset on destroy - `spec` (Block, Optional) Launchpad install specifications (see [below for nested schema](#nestedblock--spec)) ### Read-Only diff --git a/internal/provider/launchpad_config_resource.go b/internal/provider/launchpad_config_resource.go index 16233b6..1eeeac9 100644 --- a/internal/provider/launchpad_config_resource.go +++ b/internal/provider/launchpad_config_resource.go @@ -57,7 +57,9 @@ func (r *LaunchpadConfigResource) Create(ctx context.Context, req resource.Creat return } - if r.testingMode { + if model.SkipCreate.ValueBool() { + resp.Diagnostics.AddWarning("skipping create", "Skipping the launchpad create because of configuration flag.") + } else if r.testingMode { resp.Diagnostics.AddWarning("testing mode warning", "launchpad config resource handler is in testing mode, no installation will be run.") } else if err := mke.Apply(false, false, 10); err != nil { @@ -88,7 +90,9 @@ func (r *LaunchpadConfigResource) Update(ctx context.Context, req resource.Updat return } - if r.testingMode { + if model.SkipCreate.ValueBool() { + resp.Diagnostics.AddWarning("skipping destroy", "Skipping the launchpad destroy because of configuration flag.") + } else if r.testingMode { resp.Diagnostics.AddWarning("testing mode warning", "launchpad config resource handler is in testing mode, no update will be run.") } else if err := mke.Apply(false, false, 10); err != nil { @@ -107,13 +111,15 @@ func (r *LaunchpadConfigResource) Update(ctx context.Context, req resource.Updat } func (r *LaunchpadConfigResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - _, mke := getterToModelAndProduct(ctx, &resp.Diagnostics, req.State.Get, false) + model, mke := getterToModelAndProduct(ctx, &resp.Diagnostics, req.State.Get, true) if resp.Diagnostics.HasError() { return } - if r.testingMode { + if model.SkipDestroy.ValueBool() { + resp.Diagnostics.AddWarning("skipping destroy", "Skipping the launchpad destroy because of configuration flag.") + } else if r.testingMode { resp.Diagnostics.AddWarning("testing mode warning", "launchpad config resource handler is in testing mode, no reset will be run.") } else if err := mke.Reset(); err != nil { diff --git a/internal/provider/mcc_schema_1_4.go b/internal/provider/mcc_schema_1_4.go index 4d5e75a..50fb99f 100644 --- a/internal/provider/mcc_schema_1_4.go +++ b/internal/provider/mcc_schema_1_4.go @@ -41,7 +41,13 @@ func launchpadSchema14() schema.Schema { }, "skip_destroy": schema.BoolAttribute{ - MarkdownDescription: "Do not bother uninstalling on destroy", + MarkdownDescription: "Skip reset on destroy", + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), + }, + "skip_create": schema.BoolAttribute{ + MarkdownDescription: "Skip apply on create", Optional: true, Computed: true, Default: booldefault.StaticBool(false), @@ -432,6 +438,7 @@ func launchpadSchema14() schema.Schema { type launchpadSchema14Model struct { Id types.String `tfsdk:"id"` + SkipCreate types.Bool `tfsdk:"skip_create"` SkipDestroy types.Bool `tfsdk:"skip_destroy"` Metadata launchpadSchema14ModelMetadata `tfsdk:"metadata"` diff --git a/internal/provider/tools.go b/internal/provider/tools.go index 44c0138..63e78ad 100644 --- a/internal/provider/tools.go +++ b/internal/provider/tools.go @@ -3,8 +3,6 @@ package provider import ( "context" "fmt" - "io" - "log" "time" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -15,16 +13,16 @@ import ( // AllLoggingToTFLog turns on passing of ruslog and log to tflog. func AllLoggingToTFLog() { logrus.AddHook(logrusTFLogHandler{}) + logrus.SetLevel(logrus.TraceLevel) // trace all log levels, as we don't know what to catch yet. + rig.SetLogger(rigTFLogLogger{}) - logrus.SetOutput(io.Discard) - log.SetOutput(io.Discard) // we should probably catch base log errors. } // logRusTFLogHandler a tflog handler which integrates logrus so that logrus output gets handled natively. type logrusTFLogHandler struct{} -// Fire off a logrus event. +// Receive a logrus event. func (lh logrusTFLogHandler) Fire(e *logrus.Entry) error { go func(event *logrus.Entry) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) @@ -42,7 +40,9 @@ func (lh logrusTFLogHandler) Levels() []logrus.Level { func logrusTFLogFire(ctx context.Context, e *logrus.Entry) { mes := e.Message - addFields := map[string]interface{}{} + addFields := map[string]interface{}{ + "pipe": "logrusTFLogFire", + } switch e.Level { case logrus.DebugLevel: @@ -57,6 +57,7 @@ func logrusTFLogFire(ctx context.Context, e *logrus.Entry) { } // rigTFLogLogger Logger that converts k0sProject logging to tflog. +// @NOTE we re-use the logrus levels for convenience - but this has nothing to do with logrus. type rigTFLogLogger struct { } @@ -82,7 +83,9 @@ func rigLoggerTFLogFire(level logrus.Level, entry string, values ...interface{}) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() - addFields := map[string]interface{}{} + addFields := map[string]interface{}{ + "pipe": "rigTFLogLogger", + } switch level { case logrus.DebugLevel: