Skip to content

Commit

Permalink
Test Case, Bug Fixes, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMoyles committed Jan 20, 2025
1 parent 1c6dba0 commit 59665bb
Show file tree
Hide file tree
Showing 10 changed files with 1,738 additions and 657 deletions.
216 changes: 216 additions & 0 deletions docs/resources/knowledge_document_variation.md

Large diffs are not rendered by default.

39 changes: 0 additions & 39 deletions genesyscloud/knowledge/genesyscloud_knowledge_init_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,57 @@ package knowledgedocumentvariation
import (
"context"
"fmt"
"log"
rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"
"terraform-provider-genesyscloud/genesyscloud/util"

"github.com/mypurecloud/platform-client-sdk-go/v150/platformclientv2"
)

var internalProxy *variationRequestProxy

type createVariationFunc func(ctx context.Context, p *variationRequestProxy, documentVariationRequest *platformclientv2.Documentvariationrequest, knowledgeDocumentId, knowledgeBaseId string) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error)
type getAllVariationsFunc func(ctx context.Context, p *variationRequestProxy) (*[]platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error)
type getVariationRequestIdByNameFunc func(ctx context.Context, p *variationRequestProxy, name string) (string, *platformclientv2.APIResponse, bool, error)
type getAllVariationsFunc func(ctx context.Context, p *variationRequestProxy, knowledgeBaseId, documentId, documentState string, expand []string) (*[]platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error)
type getVariationRequestByIdFunc func(ctx context.Context, p *variationRequestProxy, documentVariationId string, documentId string, knowledgeBaseId string, documentState string, expand []string) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error)
type updateVariationRequestFunc func(ctx context.Context, p *variationRequestProxy, id string, documentVariationRequest *platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error)
type updateVariationRequestFunc func(ctx context.Context, p *variationRequestProxy, documentVariationId string, documentId string, knowledgeBaseId string, body platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error)
type deleteVariationRequestFunc func(ctx context.Context, p *variationRequestProxy, variationId, documentId, baseId string) (*platformclientv2.APIResponse, error)

type createKnowledgeKnowledgebaseDocumentVersionsFunc func(ctx context.Context, p *variationRequestProxy, knowledgeDocumentId, knowledgeBaseId string, version *platformclientv2.Knowledgedocumentversion) (*platformclientv2.Knowledgedocumentversion, *platformclientv2.APIResponse, error)

type GetAllKnowledgebaseEntitiesFunc func(ctx context.Context, p *variationRequestProxy, published bool) (*[]platformclientv2.Knowledgebase, *platformclientv2.APIResponse, error)

// variationRequestProxy contains all of the methods that call genesys cloud APIs.
type variationRequestProxy struct {
clientConfig *platformclientv2.Configuration
knowledgeApi *platformclientv2.KnowledgeApi
createVariationAttr createVariationFunc
getAllVariationsAttr getAllVariationsFunc
getVariationRequestIdByNameAttr getVariationRequestIdByNameFunc
getVariationRequestByIdAttr getVariationRequestByIdFunc
updateVariationRequestAttr updateVariationRequestFunc
deleteVariationRequestAttr deleteVariationRequestFunc

clientConfig *platformclientv2.Configuration
knowledgeApi *platformclientv2.KnowledgeApi
createVariationAttr createVariationFunc
getAllVariationsAttr getAllVariationsFunc
getVariationRequestByIdAttr getVariationRequestByIdFunc
updateVariationRequestAttr updateVariationRequestFunc
deleteVariationRequestAttr deleteVariationRequestFunc
createKnowledgeKnowledgebaseDocumentVersionsAttr createKnowledgeKnowledgebaseDocumentVersionsFunc
GetAllKnowledgebaseEntitiesAttr GetAllKnowledgebaseEntitiesFunc
variationCache rc.CacheInterface[platformclientv2.Documentvariationresponse]
}

// newVariationRequestProxy initializes the variation request proxy with all of the data needed to communicate with Genesys Cloud
func newVariationRequestProxy(clientConfig *platformclientv2.Configuration) *variationRequestProxy {
api := platformclientv2.NewKnowledgeApiWithConfig(clientConfig)
variationCache := rc.NewResourceCache[platformclientv2.Documentvariationresponse]()
return &variationRequestProxy{
clientConfig: clientConfig,
knowledgeApi: api,
createVariationAttr: createVariationFn,
getAllVariationsAttr: getAllVariationsFn,
getVariationRequestIdByNameAttr: getVariationRequestIdByNameFn,
getVariationRequestByIdAttr: getVariationRequestByIdFn,
updateVariationRequestAttr: updateVariationRequestFn,
deleteVariationRequestAttr: deleteVariationRequestFn,
clientConfig: clientConfig,
knowledgeApi: api,
createVariationAttr: createVariationFn,
getAllVariationsAttr: getAllVariationsFn,
getVariationRequestByIdAttr: getVariationRequestByIdFn,
updateVariationRequestAttr: updateVariationRequestFn,
deleteVariationRequestAttr: deleteVariationRequestFn,

createKnowledgeKnowledgebaseDocumentVersionsAttr: createKnowledgeKnowledgebaseDocumentVersionsFn,
GetAllKnowledgebaseEntitiesAttr: GetAllKnowledgebaseEntitiesFn,
variationCache: variationCache,
}
}

// getVariationRequestProxy acts as a singleton to for the internalProxy. It also ensures
// that we can still proxy our tests by directly setting internalProxy package variable
func getVariationRequestProxy(clientConfig *platformclientv2.Configuration) *variationRequestProxy {
if internalProxy == nil {
internalProxy = newVariationRequestProxy(clientConfig)
Expand All @@ -66,13 +68,8 @@ func (p *variationRequestProxy) CreateVariation(ctx context.Context, variationRe
}

// getVariationRequest retrieves all Genesys Cloud variation request
func (p *variationRequestProxy) getAllVariations(ctx context.Context) (*[]platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error) {
return p.getAllVariationsAttr(ctx, p)
}

// getVariationRequestIdByName returns a single Genesys Cloud variation request by a name
func (p *variationRequestProxy) getVariationRequestIdByName(ctx context.Context, name string) (string, *platformclientv2.APIResponse, bool, error) {
return p.getVariationRequestIdByNameAttr(ctx, p, name)
func (p *variationRequestProxy) getAllVariations(ctx context.Context, knowledgeBaseId, documentId, documentState string, expand []string) (*[]platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
return p.getAllVariationsAttr(ctx, p, knowledgeBaseId, documentId, documentState, expand)
}

// getVariationRequestById returns a single Genesys Cloud variation request by Id
Expand All @@ -81,8 +78,8 @@ func (p *variationRequestProxy) getVariationRequestById(ctx context.Context, doc
}

// updateVariationRequest updates a Genesys Cloud variation request
func (p *variationRequestProxy) updateVariationRequest(ctx context.Context, id string, variationRequest *platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error) {
return p.updateVariationRequestAttr(ctx, p, id, variationRequest)
func (p *variationRequestProxy) updateVariationRequest(ctx context.Context, documentVariationId string, documentId string, knowledgeBaseId string, body platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
return p.updateVariationRequestAttr(ctx, p, documentVariationId, documentId, knowledgeBaseId, body)
}

// deleteVariationRequest deletes a Genesys Cloud variation request by Id
Expand All @@ -94,74 +91,69 @@ func (p *variationRequestProxy) createKnowledgeKnowledgebaseDocumentVersions(ctx
return p.createKnowledgeKnowledgebaseDocumentVersionsAttr(ctx, p, knowledgeDocumentId, knowledgeBaseId, version)
}

func (p *variationRequestProxy) GetAllKnowledgebaseEntities(ctx context.Context, published bool) (*[]platformclientv2.Knowledgebase, *platformclientv2.APIResponse, error) {
return p.GetAllKnowledgebaseEntitiesAttr(ctx, p, published)
}

// createVariationFn is an implementation function for creating a Genesys Cloud variation request
func createVariationFn(ctx context.Context, p *variationRequestProxy, variationRequest *platformclientv2.Documentvariationrequest, knowledgeDocumentId, knowledgeBaseId string) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
return p.knowledgeApi.PostKnowledgeKnowledgebaseDocumentVariations(knowledgeBaseId, knowledgeDocumentId, *variationRequest)
}

// getAllVariationsFn is the implementation for retrieving all variation request in Genesys Cloud
func getAllVariationsFn(ctx context.Context, p *variationRequestProxy) (*[]platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error) {
var allDocumentVariationRequests []platformclientv2.Documentvariationrequest
const pageSize = 100
func getAllVariationsFn(ctx context.Context, p *variationRequestProxy, knowledgeBaseId, documentId, documentState string, expand []string) (*[]platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
var (
allVariations []platformclientv2.Documentvariationresponse
after string
resp *platformclientv2.APIResponse
)

documentVariationRequests, resp, err := p.knowledgeApi.GetKnowledgeKnowledgebaseDocumentVariations()
if err != nil {
return nil, resp, err
}
if documentVariationRequests.Entities == nil || len(*documentVariationRequests.Entities) == 0 {
return &allDocumentVariationRequests, resp, nil
}
for _, documentVariationRequest := range *documentVariationRequests.Entities {
allDocumentVariationRequests = append(allDocumentVariationRequests, documentVariationRequest)
}
const pageSize = "100"

for pageNum := 2; pageNum <= *documentVariationRequests.PageCount; pageNum++ {
documentVariationRequests, _, err := p.knowledgeApi.GetKnowledgeKnowledgebaseDocumentVariations()
for {
variations, resp, err := p.knowledgeApi.GetKnowledgeKnowledgebaseDocumentVariations(knowledgeBaseId, documentId, "", after, pageSize, documentState, expand)
if err != nil {
return nil, resp, err
}

if documentVariationRequests.Entities == nil || len(*documentVariationRequests.Entities) == 0 {
if variations.Entities == nil || len(*variations.Entities) == 0 {
break
}

for _, documentVariationRequest := range *documentVariationRequests.Entities {
allDocumentVariationRequests = append(allDocumentVariationRequests, documentVariationRequest)
}
}
allVariations = append(allVariations, *variations.Entities...)

return &allDocumentVariationRequests, resp, nil
}
if variations.NextUri == nil || *variations.NextUri == "" {
break
}

// getVariationRequestIdByNameFn is an implementation of the function to get a Genesys Cloud variation request by name
func getVariationRequestIdByNameFn(ctx context.Context, p *variationRequestProxy, name string) (string, *platformclientv2.APIResponse, bool, error) {
documentVariationRequests, resp, err := p.knowledgeApi.GetKnowledgeKnowledgebaseDocumentVariations()
if err != nil {
return "", resp, false, err
after, err := util.GetQueryParamValueFromUri(*variations.NextUri, "after")
if err != nil {
return nil, resp, err
}
if after == "" {
break
}
}

if documentVariationRequests.Entities == nil || len(*documentVariationRequests.Entities) == 0 {
return "", resp, true, err
for _, variation := range allVariations {
rc.SetCache(p.variationCache, *variation.Id, variation)
}

for _, documentVariationRequest := range *documentVariationRequests.Entities {
if *documentVariationRequest.Name == name {
log.Printf("Retrieved the variation request id %s by name %s", *documentVariationRequest.Id, name)
return *documentVariationRequest.Id, resp, false, nil
}
}

return "", resp, true, fmt.Errorf("Unable to find variation request with name %s", name)
return &allVariations, resp, nil
}

// getVariationRequestByIdFn is an implementation of the function to get a Genesys Cloud variation request by Id
func getVariationRequestByIdFn(ctx context.Context, p *variationRequestProxy, documentVariationId string, documentId string, knowledgeBaseId string, documentState string, expand []string) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
variation := rc.GetCacheItem(p.variationCache, documentVariationId)
if variation != nil {
return variation, nil, nil
}
return p.knowledgeApi.GetKnowledgeKnowledgebaseDocumentVariation(documentVariationId, documentId, knowledgeBaseId, documentState, expand)
}

// updateVariationRequestFn is an implementation of the function to update a Genesys Cloud variation request
func updateVariationRequestFn(ctx context.Context, p *variationRequestProxy, id string, variationRequest *platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationrequest, *platformclientv2.APIResponse, error) {
return p.knowledgeApi.PatchKnowledgeKnowledgebaseDocumentVariation(id, *variationRequest)
func updateVariationRequestFn(ctx context.Context, p *variationRequestProxy, documentVariationId string, documentId string, knowledgeBaseId string, body platformclientv2.Documentvariationrequest) (*platformclientv2.Documentvariationresponse, *platformclientv2.APIResponse, error) {
return p.knowledgeApi.PatchKnowledgeKnowledgebaseDocumentVariation(documentVariationId, documentId, knowledgeBaseId, body)
}

// deleteVariationRequestFn is an implementation function for deleting a Genesys Cloud variation request
Expand All @@ -172,3 +164,39 @@ func deleteVariationRequestFn(ctx context.Context, p *variationRequestProxy, var
func createKnowledgeKnowledgebaseDocumentVersionsFn(ctx context.Context, p *variationRequestProxy, knowledgeDocumentId, knowledgeBaseId string, version *platformclientv2.Knowledgedocumentversion) (*platformclientv2.Knowledgedocumentversion, *platformclientv2.APIResponse, error) {
return p.knowledgeApi.PostKnowledgeKnowledgebaseDocumentVersions(knowledgeBaseId, knowledgeDocumentId, *version)
}

func GetAllKnowledgebaseEntitiesFn(ctx context.Context, p *variationRequestProxy, published bool) (*[]platformclientv2.Knowledgebase, *platformclientv2.APIResponse, error) {
var (
after string
err error
knowledgeBaseEntities []platformclientv2.Knowledgebase
)

const pageSize = 100
for {
knowledgeBases, resp, getErr := p.knowledgeApi.GetKnowledgeKnowledgebases("", after, "", fmt.Sprintf("%v", pageSize), "", "", published, "", "")
if getErr != nil {
return nil, resp, fmt.Errorf("failed to get page of knowledge bases error: %s", getErr)
}

if knowledgeBases.Entities == nil || len(*knowledgeBases.Entities) == 0 {
break
}

knowledgeBaseEntities = append(knowledgeBaseEntities, *knowledgeBases.Entities...)

if knowledgeBases.NextUri == nil || *knowledgeBases.NextUri == "" {
break
}

after, err = util.GetQueryParamValueFromUri(*knowledgeBases.NextUri, "after")
if err != nil {
return nil, resp, fmt.Errorf("failed to parse after cursor from knowledge base nextUri: %s", err)
}
if after == "" {
break
}
}

return &knowledgeBaseEntities, nil, nil
}
Loading

0 comments on commit 59665bb

Please sign in to comment.