diff --git a/internal/juju/client.go b/internal/juju/client.go index bcc53bc6..5e723ef8 100644 --- a/internal/juju/client.go +++ b/internal/juju/client.go @@ -6,6 +6,7 @@ package juju import ( "context" "fmt" + "strconv" "sync" "time" @@ -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{}) @@ -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}) +}