Skip to content

Commit

Permalink
PRODENG-2370 re-enable terraform log out (#44)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
james-nesbitt authored Sep 29, 2023
1 parent 58c47dc commit 4fcef39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/resources/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions internal/provider/launchpad_config_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion internal/provider/mcc_schema_1_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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"`
Expand Down
17 changes: 10 additions & 7 deletions internal/provider/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package provider
import (
"context"
"fmt"
"io"
"log"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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 {
}

Expand All @@ -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:
Expand Down

0 comments on commit 4fcef39

Please sign in to comment.