Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devtooling 843 remove log.fatalf #1294

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions genesyscloud/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,23 @@ func InitClientConfig(data *schema.ResourceData, version string, config *platfor
RetryWaitMax: time.Second * 30,
RetryMax: 20,
RequestLogHook: func(request *http.Request, count int) {
if count > 0 && request != nil {
log.Printf("Retry #%d for %s %s", count, request.Method, request.URL)
sdkDebugRequest := newSDKDebugRequest(request, count)
request.Header.Set("TF-Correlation-Id", sdkDebugRequest.TransactionId)
carnellj-genesys marked this conversation as resolved.
Show resolved Hide resolved
err, jsonStr := sdkDebugRequest.ToJSON()

if err != nil {
log.Printf("WARNING: Unable to log RequestLogHook: %s", err)
}
log.Printf(jsonStr)
},
ResponseLogHook: func(response *http.Response) {
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
log.Printf("Response %s for request:%s %s", response.Status, response.Request.Method, response.Request.URL)
sdkDebugResponse := newSDKDebugResponse(response)
err, jsonStr := sdkDebugResponse.ToJSON()

if err != nil {
log.Printf("WARNING: Unable to log ResponseLogHook: %s", err)
}
log.Printf(jsonStr)
},
}

Expand Down
74 changes: 74 additions & 0 deletions genesyscloud/provider/provider_sdk_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package provider

carnellj-genesys marked this conversation as resolved.
Show resolved Hide resolved
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"net/http"
)

type sdkDebugRequest struct {
DebugType string `json:"debug_type,omitempty"` //Indicates whether it is a request or response debug
TransactionId string `json:"transaction_id,omitempty"` //Unique id to link the request and response
InvocationCount int `json:"invocation_count,omitempty"` //Number of times the URL has been invoked. Will be greater then zero when it is a retry
InvocationMethod string `json:"invocation_method,omitempty"` //HTTP method that will be invoked
InvocationUrl string `json:"invocation_url,omitempty"` //HTTP URL
}

func (s *sdkDebugRequest) ToJSON() (err error, jsonStr string) {
jsonData, err := json.Marshal(s)
if err != nil {
fmt.Println(err)
return err, ""
}

// Print the JSON string
return nil, string(jsonData)
}

type sdkDebugResponse struct {
DebugType string `json:"debug_type,omitempty"` //Indicates whether it is a request or response debug
TransactionId string `json:"transaction_id,omitempty"` //Unique id to link the request and response
InvocationCount int `json:"invocation_count,omitempty"` //Number of times the URL has been invoked. Will be greater then zero when it is a retry
InvocationMethod string `json:"invocation_method,omitempty"` //HTTP method that will be invoked
InvocationUrl string `json:"invocation_url,omitempty"` //HTTP URL
InvocationStatusCode int `json:"invocation_status_code,omitempty"` //HTTP status code that has been returned
InvocationRetryAfter string `json:"invocation_retry_after,omitempty"` //Retry-After header value
}

func (s *sdkDebugResponse) ToJSON() (err error, jsonStr string) {
jsonData, err := json.Marshal(s)
if err != nil {
fmt.Println(err)
return err, ""
}

// Print the JSON string
return nil, string(jsonData)
}

func newSDKDebugRequest(request *http.Request, count int) *sdkDebugRequest {
transactionId := uuid.NewString()
return &sdkDebugRequest{

DebugType: "SDK DEBUG REQUEST",
TransactionId: transactionId,
InvocationCount: count,
InvocationMethod: request.Method,
InvocationUrl: request.URL.Path,
}
}

func newSDKDebugResponse(response *http.Response) *sdkDebugResponse {
transactionId := response.Request.Header.Get("TF-Correlation-Id")
return &sdkDebugResponse{

DebugType: "SDK DEBUG RESPONSE",
TransactionId: transactionId,
InvocationCount: 0,
InvocationMethod: response.Request.Method,
InvocationUrl: response.Request.URL.Path,
InvocationStatusCode: response.StatusCode,
InvocationRetryAfter: response.Request.Header.Get("Retry-After"),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,18 @@ func createMediaRetentionPolicy(ctx context.Context, d *schema.ResourceData, met
order := d.Get("order").(int)
description := d.Get("description").(string)
enabled := d.Get("enabled").(bool)
mediaPolicies := buildMediaPolicies(d, pp, ctx)
carnellj-genesys marked this conversation as resolved.
Show resolved Hide resolved
err, mediaPolicies := buildMediaPolicies(d, pp, ctx)

if err != nil {
util.BuildDiagnosticError(resourceName, "error while calling buildMediaPolicie()in createMediaRetention", err)
}

conditions := buildConditions(d)
actions := buildPolicyActionsFromResource(d, pp, ctx)
err, actions := buildPolicyActionsFromResource(d, pp, ctx)
if err != nil {
util.BuildDiagnosticError(resourceName, "error while calling buildPolicyActionsFromResource()", err)
}

policyErrors := buildPolicyErrors(d)

reqBody := platformclientv2.Policycreate{
Expand Down Expand Up @@ -121,11 +130,20 @@ func readMediaRetentionPolicy(ctx context.Context, d *schema.ResourceData, meta
resourcedata.SetNillableValueWithInterfaceArrayWithFunc(d, "conditions", retentionPolicy.Conditions, flattenConditions)
resourcedata.SetNillableValueWithInterfaceArrayWithFunc(d, "policy_errors", retentionPolicy.PolicyErrors, flattenPolicyErrors)

err, mediaPolicies := flattenMediaPolicies(retentionPolicy.MediaPolicies, pp, ctx)
if err != nil {
return retry.NonRetryableError(fmt.Errorf("Unable to flatten media policies in readMediaRetentionPolicy() method: %s", err))
}
if retentionPolicy.MediaPolicies != nil {
d.Set("media_policies", flattenMediaPolicies(retentionPolicy.MediaPolicies, pp, ctx))
d.Set("media_policies", mediaPolicies)
}

err, actions := flattenPolicyActions(retentionPolicy.Actions, pp, ctx)
if err != nil {
return retry.NonRetryableError(fmt.Errorf("Unable to flatten actions in readMediaRetentionPolicy(): %s", err))
}
if retentionPolicy.Actions != nil {
d.Set("actions", flattenPolicyActions(retentionPolicy.Actions, pp, ctx))
d.Set("actions", actions)
}
return cc.CheckState(d)
})
Expand All @@ -140,9 +158,14 @@ func updateMediaRetentionPolicy(ctx context.Context, d *schema.ResourceData, met
order := d.Get("order").(int)
description := d.Get("description").(string)
enabled := d.Get("enabled").(bool)
mediaPolicies := buildMediaPolicies(d, pp, ctx)
err, mediaPolicies := buildMediaPolicies(d, pp, ctx)

charliecon marked this conversation as resolved.
Show resolved Hide resolved
conditions := buildConditions(d)
actions := buildPolicyActionsFromResource(d, pp, ctx)
err, actions := buildPolicyActionsFromResource(d, pp, ctx)

if err != nil {
return util.BuildDiagnosticError(resourceName, "Error while retrieving buildPolicyActionsFromResource() function in updateMediaRenentionPolicy() method)", err)
}
policyErrors := buildPolicyErrors(d)

reqBody := platformclientv2.Policy{
Expand Down
Loading
Loading