Skip to content

Commit

Permalink
Merge pull request #966 from ksamoray/debug_log_response
Browse files Browse the repository at this point in the history
Log NSX responses while provider is in debug mode
  • Loading branch information
ksamoray authored Sep 14, 2023
2 parents c40cc41 + 6956e5b commit 05c1042
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05c1042

Please sign in to comment.