From ffe17023ab7025d5a950a2eabfd9f9f1dc709487 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Mon, 11 Sep 2023 14:06:00 -0600 Subject: [PATCH] Add an example for the configuring and using the RPC provider: Signed-off-by: Jacob Weinstock --- examples/rpc/main.go | 94 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++ go.sum | 13 ++++++ logging/logging.go | 19 +++++++++ 4 files changed, 130 insertions(+) create mode 100644 examples/rpc/main.go diff --git a/examples/rpc/main.go b/examples/rpc/main.go new file mode 100644 index 00000000..097ccfbc --- /dev/null +++ b/examples/rpc/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "time" + + "github.com/bmc-toolbox/bmclib/v2" + "github.com/bmc-toolbox/bmclib/v2/logging" + "github.com/bmc-toolbox/bmclib/v2/providers/rpc" +) + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + // Start the test consumer + go testConsumer(ctx) + time.Sleep(100 * time.Millisecond) + + log := logging.ZeroLogger("info") + opts := []bmclib.Option{ + bmclib.WithLogger(log), + bmclib.WithPerProviderTimeout(5 * time.Second), + bmclib.WithRPCOpt(rpc.Provider{ + ConsumerURL: "http://localhost:8800", + // Opts are not required. + Opts: rpc.Opts{ + HMAC: rpc.HMACOpts{ + Secrets: rpc.Secrets{rpc.SHA256: {"superSecret1"}}, + }, + Signature: rpc.SignatureOpts{ + HeaderName: "X-Bespoke-Signature", + IncludedPayloadHeaders: []string{"X-Bespoke-Timestamp"}, + }, + Request: rpc.RequestOpts{ + TimestampHeader: "X-Bespoke-Timestamp", + }, + }, + }), + } + host := "127.0.1.1" + user := "admin" + pass := "admin" + c := bmclib.NewClient(host, user, pass, opts...) + if err := c.Open(ctx); err != nil { + panic(err) + } + defer c.Close(ctx) + + state, err := c.GetPowerState(ctx) + if err != nil { + panic(err) + } + log.Info("power state", "state", state) + log.Info("metadata for GetPowerState", "metadata", c.GetMetadata()) + + ok, err := c.SetPowerState(ctx, "on") + if err != nil { + panic(err) + } + log.Info("set power state", "ok", ok) + log.Info("metadata for SetPowerState", "metadata", c.GetMetadata()) + + <-ctx.Done() +} + +func testConsumer(ctx context.Context) error { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + req := rpc.RequestPayload{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + rp := rpc.ResponsePayload{ + ID: req.ID, + Host: req.Host, + } + switch req.Method { + case rpc.PowerGetMethod: + rp.Result = "on" + case rpc.PowerSetMethod: + + case rpc.BootDeviceMethod: + + default: + w.WriteHeader(http.StatusNotFound) + } + b, _ := json.Marshal(rp) + w.Write(b) + }) + + return http.ListenAndServe(":8800", nil) +} diff --git a/go.mod b/go.mod index 2172e83e..d73adff9 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,13 @@ require ( github.com/bombsimon/logrusr/v2 v2.0.1 github.com/ghodss/yaml v1.0.0 github.com/go-logr/logr v1.2.4 + github.com/go-logr/zerologr v1.2.3 github.com/google/go-cmp v0.5.9 github.com/hashicorp/go-multierror v1.1.1 github.com/jacobweinstock/iamt v0.0.0-20230502042727-d7cdbe67d9ef github.com/jacobweinstock/registrar v0.4.7 github.com/pkg/errors v0.9.1 + github.com/rs/zerolog v1.30.0 github.com/sirupsen/logrus v1.8.1 github.com/stmcginnis/gofish v0.14.0 github.com/stretchr/testify v1.8.0 @@ -29,6 +31,8 @@ require ( github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect golang.org/x/sys v0.1.0 // indirect diff --git a/go.sum b/go.sum index 487556f6..cdb371e3 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d h1:cQ30Wa8mhLzK github.com/bmc-toolbox/common v0.0.0-20230220061748-93ff001f4a1d/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c= github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM= github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -19,6 +20,9 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/zerologr v1.2.3 h1:up5N9vcH9Xck3jJkXzgyOxozT14R47IyDODz8LM1KSs= +github.com/go-logr/zerologr v1.2.3/go.mod h1:BxwGo7y5zgSHYR1BjbnHPyF/5ZjVKfKxAZANVu6E8Ho= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -37,10 +41,17 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c= +github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= @@ -65,6 +76,8 @@ golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw= diff --git a/logging/logging.go b/logging/logging.go index 47289d7f..9edb4728 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -5,6 +5,8 @@ import ( "github.com/bombsimon/logrusr/v2" "github.com/go-logr/logr" + "github.com/go-logr/zerologr" + "github.com/rs/zerolog" "github.com/sirupsen/logrus" ) @@ -26,3 +28,20 @@ func DefaultLogger() logr.Logger { return logrusr.New(logrusLog) } + +// ZeroLogger is a logr.Logger implementation that uses zerolog. +// This logger handles nested structs better than the logrus implementation. +func ZeroLogger(level string) logr.Logger { + zl := zerolog.New(os.Stdout) + zl = zl.With().Caller().Timestamp().Logger() + var l zerolog.Level + switch level { + case "debug": + l = zerolog.DebugLevel + default: + l = zerolog.InfoLevel + } + zl = zl.Level(l) + + return zerologr.New(&zl) +}