Skip to content

Commit

Permalink
Log NSX responses while provider is in debug mode
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Sep 12, 2023
1 parent c40cc41 commit 6956e5b
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 6956e5b

Please sign in to comment.