From 6956e5b93fc01f232b275cd176e5f6de1e6361a7 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Tue, 12 Sep 2023 16:33:01 +0300 Subject: [PATCH] Log NSX responses while provider is in debug mode Signed-off-by: Kobi Samoray --- nsxt/provider.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/nsxt/provider.go b/nsxt/provider.go index 93849cf6e..d27dab571 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -791,6 +791,21 @@ func (processor logRequestProcessor) Process(req *http.Request) error { return nil } +type logResponseAcceptor struct { +} + +func newLogResponseAcceptor() *logResponseAcceptor { + return &logResponseAcceptor{} +} + +func (processor logResponseAcceptor) Accept(req *http.Response) { + dumpResponse, err := httputil.DumpResponse(req, true) + if err != nil { + log.Fatal(err) + } + log.Printf("Received NSX response:\n%s", dumpResponse) +} + type bearerAuthHeaderProcessor struct { Token string } @@ -977,6 +992,8 @@ func getPolicyConnectorWithHeaders(clients interface{}, customHeaders *map[strin connectorOptions := []client.ConnectorOption{client.UsingRest(nil), client.WithHttpClient(c.PolicyHTTPClient), client.WithDecorators(retry.NewRetryDecorator(uint(c.CommonConfig.MaxRetries), retryFunc))} var requestProcessors []core.RequestProcessor + var responseAcceptors []core.ResponseAcceptor + if c.PolicySecurityContext != nil { connectorOptions = append(connectorOptions, client.WithSecurityContext(c.PolicySecurityContext)) } @@ -1004,13 +1021,17 @@ func getPolicyConnectorWithHeaders(clients interface{}, customHeaders *map[strin log.Printf("[INFO]: Session headers configured for policy objects") } - if os.Getenv("TF_LOG_PROVIDER") != "" { + if os.Getenv("TF_LOG_PROVIDER_NSX_HTTP") != "" { requestProcessors = append(requestProcessors, newLogRequestProcessor().Process) + responseAcceptors = append(responseAcceptors, newLogResponseAcceptor().Accept) } if len(requestProcessors) > 0 { connectorOptions = append(connectorOptions, client.WithRequestProcessors(requestProcessors...)) } + if len(responseAcceptors) > 0 { + connectorOptions = append(connectorOptions, client.WithResponseAcceptors(responseAcceptors...)) + } connector := client.NewConnector(c.Host, connectorOptions...) // Init NSX version on demand if not done yet // This is also our indication to apply licenses, in case of delayed connection