Skip to content

Commit

Permalink
Add JujuLogger to SharedClient.
Browse files Browse the repository at this point in the history
Create a shim to map the terraform client logging to a juju Logger. This
is in anticipation of the juju 3.3.0 changes to apiclient.NewClient,
which will require a Logger.
  • Loading branch information
hmlanigan committed Feb 12, 2024
1 parent a46c15c commit 626fd21
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/juju/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package juju
import (
"context"
"fmt"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -61,6 +62,8 @@ type SharedClient interface {
ModelUUID(modelName string) (string, error)
RemoveModel(modelUUID string)

JujuLogger() *jujuLoggerShim

Debugf(msg string, additionalFields ...map[string]interface{})
Errorf(err error, msg string)
Tracef(msg string, additionalFields ...map[string]interface{})
Expand Down Expand Up @@ -254,3 +257,22 @@ func (sc *sharedClient) Warnf(msg string, additionalFields ...map[string]interfa
func getCurrentJujuUser(conn api.Connection) string {
return conn.AuthTag().Id()
}

func (sc *sharedClient) JujuLogger() *jujuLoggerShim {
return &jujuLoggerShim{sc: sc}
}

// A shim to translate the juju/loggo package Errorf into
// the tflog SubsystemError. Used by apiclient.NewClient.
type jujuLoggerShim struct {
sc *sharedClient
}

func (j jujuLoggerShim) Errorf(msg string, in ...interface{}) {
stringInt := make(map[string]interface{}, len(in)+1)
stringInt["error"] = msg
for i, v := range in {
stringInt[strconv.Itoa(i)] = v
}
tflog.SubsystemError(j.sc.subCtx, LogJujuClient, "juju api logging", map[string]interface{}{"error": msg})
}

0 comments on commit 626fd21

Please sign in to comment.