Skip to content

Commit

Permalink
Added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dginty4 committed Jul 29, 2024
1 parent 8667a5b commit 7e7ac36
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"

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

Expand Down Expand Up @@ -34,11 +36,13 @@ type taskManagementWorkbinProxy struct {
getTaskManagementWorkbinByIdAttr getTaskManagementWorkbinByIdFunc
updateTaskManagementWorkbinAttr updateTaskManagementWorkbinFunc
deleteTaskManagementWorkbinAttr deleteTaskManagementWorkbinFunc
workbinCache rc.CacheInterface[platformclientv2.Workbin]
}

// newTaskManagementWorkbinProxy initializes the task management workbin proxy with all of the data needed to communicate with Genesys Cloud
func newTaskManagementWorkbinProxy(clientConfig *platformclientv2.Configuration) *taskManagementWorkbinProxy {
api := platformclientv2.NewTaskManagementApiWithConfig(clientConfig)
workbinCache := rc.NewResourceCache[platformclientv2.Workbin]()
return &taskManagementWorkbinProxy{
clientConfig: clientConfig,
taskManagementApi: api,
Expand All @@ -48,6 +52,7 @@ func newTaskManagementWorkbinProxy(clientConfig *platformclientv2.Configuration)
getTaskManagementWorkbinByIdAttr: getTaskManagementWorkbinByIdFn,
updateTaskManagementWorkbinAttr: updateTaskManagementWorkbinFn,
deleteTaskManagementWorkbinAttr: deleteTaskManagementWorkbinFn,
workbinCache: workbinCache,
}
}

Expand Down Expand Up @@ -143,11 +148,12 @@ func getTaskManagementWorkbinIdByNameFn(ctx context.Context, p *taskManagementWo

// getTaskManagementWorkbinByIdFn is an implementation of the function to get a Genesys Cloud task management workbin by Id
func getTaskManagementWorkbinByIdFn(ctx context.Context, p *taskManagementWorkbinProxy, id string) (taskManagementWorkbin *platformclientv2.Workbin, resp *platformclientv2.APIResponse, err error) {
workbin, resp, err := p.taskManagementApi.GetTaskmanagementWorkbin(id)
if err != nil {
return nil, resp, fmt.Errorf("failed to retrieve task management workbin by id %s: %s", id, err)
workbin := rc.GetCacheItem(p.workbinCache, id)
if workbin != nil {
return workbin, nil, nil
}
return workbin, resp, nil

return p.taskManagementApi.GetTaskmanagementWorkbin(id)
}

// updateTaskManagementWorkbinFn is an implementation of the function to update a Genesys Cloud task management workbin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"

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

Expand Down Expand Up @@ -35,11 +37,13 @@ type taskManagementWorkitemProxy struct {
getTaskManagementWorkitemByIdAttr getTaskManagementWorkitemByIdFunc
updateTaskManagementWorkitemAttr updateTaskManagementWorkitemFunc
deleteTaskManagementWorkitemAttr deleteTaskManagementWorkitemFunc
workitemCache rc.CacheInterface[platformclientv2.Workitem]
}

// newTaskManagementWorkitemProxy initializes the task management workitem proxy with all of the data needed to communicate with Genesys Cloud
func newTaskManagementWorkitemProxy(clientConfig *platformclientv2.Configuration) *taskManagementWorkitemProxy {
api := platformclientv2.NewTaskManagementApiWithConfig(clientConfig)
workitemCache := rc.NewResourceCache[platformclientv2.Workitem]()
return &taskManagementWorkitemProxy{
clientConfig: clientConfig,
taskManagementApi: api,
Expand All @@ -49,6 +53,7 @@ func newTaskManagementWorkitemProxy(clientConfig *platformclientv2.Configuration
getTaskManagementWorkitemByIdAttr: getTaskManagementWorkitemByIdFn,
updateTaskManagementWorkitemAttr: updateTaskManagementWorkitemFn,
deleteTaskManagementWorkitemAttr: deleteTaskManagementWorkitemFn,
workitemCache: workitemCache,
}
}

Expand Down Expand Up @@ -225,6 +230,11 @@ func getTaskManagementWorkitemIdByNameFn(ctx context.Context, p *taskManagementW

// getTaskManagementWorkitemByIdFn is an implementation of the function to get a Genesys Cloud task management workitem by Id
func getTaskManagementWorkitemByIdFn(ctx context.Context, p *taskManagementWorkitemProxy, id string) (taskManagementWorkitem *platformclientv2.Workitem, resp *platformclientv2.APIResponse, err error) {
workitem := rc.GetCacheItem(p.workitemCache, id)
if workitem != nil {
return workitem, nil, nil
}

return p.taskManagementApi.GetTaskmanagementWorkitem(id, "")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"terraform-provider-genesyscloud/genesyscloud/provider"
resourceExporter "terraform-provider-genesyscloud/genesyscloud/resource_exporter"
registrar "terraform-provider-genesyscloud/genesyscloud/resource_register"
"terraform-provider-genesyscloud/genesyscloud/task_management_worktype_status"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/validators"

Expand Down Expand Up @@ -117,6 +118,7 @@ func ResourceTaskManagementWorkitem() *schema.Resource {
Description: `The id of the current status of the Workitem.`,
Optional: true,
Computed: true,
StateFunc: task_management_worktype_status.ModifyStatusIdStateValue,
Type: schema.TypeString,
},
`workbin_id`: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
routingQueue "terraform-provider-genesyscloud/genesyscloud/routing_queue"
routingSkill "terraform-provider-genesyscloud/genesyscloud/routing_skill"

"terraform-provider-genesyscloud/genesyscloud/task_management_worktype_status"
"terraform-provider-genesyscloud/genesyscloud/user_roles"
"terraform-provider-genesyscloud/genesyscloud/util"
"testing"
Expand Down Expand Up @@ -199,11 +200,12 @@ func TestAccResourceTaskManagementWorkitem(t *testing.T) {
workitemRes,
workitem1.name,
workitem1.worktype_id,
fmt.Sprintf("depends_on = [genesyscloud_task_management_worktype_status.%s, genesyscloud_task_management_worktype_status.%s]", statusResourceOpen, statusResourceClosed),
fmt.Sprintf("status_id = genesyscloud_task_management_worktype_status.%s.id", statusResourceOpen),
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "name", workitem1.name),
resource.TestCheckResourceAttrPair("genesyscloud_task_management_workitem."+workitemRes, "worktype_id", "genesyscloud_task_management_worktype."+wtResName, "id"),
task_management_worktype_status.ValidateStatusIds("genesyscloud_task_management_workitem."+workitemRes, "status_id", "genesyscloud_task_management_worktype_status."+statusResourceOpen, "id"),
),
},
// Update workitem with more fields
Expand All @@ -224,7 +226,7 @@ func TestAccResourceTaskManagementWorkitem(t *testing.T) {
"data.genesyscloud_auth_division_home."+homeDivRes+".id",
),
) +
generateWorkitemResource(workitemRes, workitem1Update, "depends_on = [genesyscloud_user_roles.user_role_1]"),
generateWorkitemResource(workitemRes, workitem1Update, ""),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "name", workitem1Update.name),
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "description", workitem1Update.description),
Expand All @@ -234,6 +236,7 @@ func TestAccResourceTaskManagementWorkitem(t *testing.T) {
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "date_expires", workitem1Update.date_expires),
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "duration_seconds", fmt.Sprintf("%d", workitem1Update.duration_seconds)),
resource.TestCheckResourceAttr("genesyscloud_task_management_workitem."+workitemRes, "ttl", fmt.Sprintf("%d", workitem1Update.ttl)),
task_management_worktype_status.ValidateStatusIds("genesyscloud_task_management_workitem."+workitemRes, "status_id", "genesyscloud_task_management_worktype_status."+statusResourceOpen, "id"),
resource.TestCheckResourceAttrPair("genesyscloud_task_management_workitem."+workitemRes, "workbin_id", "genesyscloud_task_management_workbin."+wbResourceId, "id"),
resource.TestCheckResourceAttrPair("genesyscloud_task_management_workitem."+workitemRes, "assignee_id", "genesyscloud_user."+userResId1, "id"),
resource.TestCheckResourceAttrPair("genesyscloud_task_management_workitem."+workitemRes, "external_contact_id", "genesyscloud_externalcontacts_contact."+externalContactResId1, "id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package task_management_workitem
import (
"encoding/json"
"fmt"
"strings"
"terraform-provider-genesyscloud/genesyscloud/task_management_worktype_status"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/resourcedata"

Expand All @@ -24,7 +26,7 @@ func getWorkitemCreateFromResourceData(d *schema.ResourceData) (*platformclientv
return nil, err
}

return &platformclientv2.Workitemcreate{
workItem := platformclientv2.Workitemcreate{
Name: platformclientv2.String(d.Get("name").(string)),
TypeId: platformclientv2.String(d.Get("worktype_id").(string)),
Description: platformclientv2.String(d.Get("description").(string)),
Expand All @@ -47,7 +49,16 @@ func getWorkitemCreateFromResourceData(d *schema.ResourceData) (*platformclientv

CustomFields: customFields,
ScoredAgents: buildWorkitemScoredAgents(d.Get("scored_agents").([]interface{})),
}, nil
}

// If the user makes a reference to a status that is managed by terraform the id will look like this <worktypeId>/<statusId>
// so we need to extract just the status id from any status references that look like this
if workItem.StatusId != nil && strings.Contains(*workItem.StatusId, "/") {
_, id := task_management_worktype_status.SplitWorktypeStatusTerraformId(*workItem.StatusId)
workItem.StatusId = &id
}

return &workItem, nil
}

// getWorkitemUpdateFromResourceData maps data from schema ResourceData object to a platformclientv2.Workitemupdate
Expand All @@ -58,7 +69,7 @@ func getWorkitemUpdateFromResourceData(d *schema.ResourceData) (*platformclientv
}

// NOTE: The only difference from Workitemcreate is that you can't change the Worktype
return &platformclientv2.Workitemupdate{
workItem := platformclientv2.Workitemupdate{
Name: platformclientv2.String(d.Get("name").(string)),
Description: platformclientv2.String(d.Get("description").(string)),

Expand All @@ -80,7 +91,16 @@ func getWorkitemUpdateFromResourceData(d *schema.ResourceData) (*platformclientv

CustomFields: customFields,
ScoredAgents: buildWorkitemScoredAgents(d.Get("scored_agents").([]interface{})),
}, nil
}

// If the user makes a reference to a status that is managed by terraform the id will look like this <worktypeId>/<statusId>
// so we need to extract just the status id from any status references that look like this
if workItem.StatusId != nil && strings.Contains(*workItem.StatusId, "/") {
_, id := task_management_worktype_status.SplitWorktypeStatusTerraformId(*workItem.StatusId)
workItem.StatusId = &id
}

return &workItem, nil
}

// buildCustomFieldsNillable builds a Genesys Cloud *[]platformclientv2.Workitemscoredagent from a JSON string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net/http"

rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"

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

Expand Down Expand Up @@ -40,11 +42,14 @@ type taskManagementProxy struct {
updateTaskManagementWorkitemSchemaAttr updateTaskManagementWorkitemSchemaFunc
deleteTaskManagementWorkitemSchemaAttr deleteTaskManagementWorkitemSchemaFunc
getTaskManagementWorkitemSchemaDeletedStatusAttr getTaskManagementWorkitemSchemaDeletedStatusFunc
workitemSchemaCache rc.CacheInterface[platformclientv2.Dataschema]
}

// newTaskManagementProxy initializes the task management proxy with all of the data needed to communicate with Genesys Cloud
func newTaskManagementProxy(clientConfig *platformclientv2.Configuration) *taskManagementProxy {
api := platformclientv2.NewTaskManagementApiWithConfig(clientConfig)
workitemSchemaCache := rc.NewResourceCache[platformclientv2.Dataschema]()

return &taskManagementProxy{
clientConfig: clientConfig,
taskManagementApi: api,
Expand All @@ -55,6 +60,7 @@ func newTaskManagementProxy(clientConfig *platformclientv2.Configuration) *taskM
updateTaskManagementWorkitemSchemaAttr: updateTaskManagementWorkitemSchemaFn,
deleteTaskManagementWorkitemSchemaAttr: deleteTaskManagementWorkitemSchemaFn,
getTaskManagementWorkitemSchemaDeletedStatusAttr: getTaskManagementWorkitemSchemaDeletedStatusFn,
workitemSchemaCache: workitemSchemaCache,
}
}

Expand Down Expand Up @@ -152,11 +158,11 @@ func getTaskManagementWorkitemSchemasByNameFn(ctx context.Context, p *taskManage

// getTaskManagementWorkitemSchemaByIdFn is an implementation of the function to get a Genesys Cloud task management workitem schema by Id
func getTaskManagementWorkitemSchemaByIdFn(ctx context.Context, p *taskManagementProxy, id string) (schema *platformclientv2.Dataschema, resp *platformclientv2.APIResponse, err error) {
schema, resp, err = p.taskManagementApi.GetTaskmanagementWorkitemsSchema(id)
if err != nil {
return nil, resp, fmt.Errorf("failed to retrieve task management workitem schema by id %s: %v", id, err)
workitemSchema := rc.GetCacheItem(p.workitemSchemaCache, id)
if workitemSchema != nil {
return schema, nil, nil
}
return schema, resp, nil
return p.taskManagementApi.GetTaskmanagementWorkitemsSchema(id)
}

// updateTaskManagementWorkitemSchemaFn is an implementation of the function to update a Genesys Cloud task management workitem schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"

"github.com/mypurecloud/platform-client-sdk-go/v133/platformclientv2"
)
Expand Down Expand Up @@ -37,11 +38,13 @@ type TaskManagementWorktypeProxy struct {
getTaskManagementWorktypeByNameAttr getTaskManagementWorktypeByNameFunc
updateTaskManagementWorktypeAttr updateTaskManagementWorktypeFunc
deleteTaskManagementWorktypeAttr deleteTaskManagementWorktypeFunc
worktypeCache rc.CacheInterface[platformclientv2.Worktype]
}

// newTaskManagementWorktypeProxy initializes the task management worktype proxy with all the data needed to communicate with Genesys Cloud
func newTaskManagementWorktypeProxy(clientConfig *platformclientv2.Configuration) *TaskManagementWorktypeProxy {
api := platformclientv2.NewTaskManagementApiWithConfig(clientConfig)
worktypeCache := rc.NewResourceCache[platformclientv2.Worktype]()
return &TaskManagementWorktypeProxy{
clientConfig: clientConfig,
taskManagementApi: api,
Expand All @@ -52,6 +55,7 @@ func newTaskManagementWorktypeProxy(clientConfig *platformclientv2.Configuration
getTaskManagementWorktypeByIdAttr: getTaskManagementWorktypeByIdFn,
updateTaskManagementWorktypeAttr: updateTaskManagementWorktypeFn,
deleteTaskManagementWorktypeAttr: deleteTaskManagementWorktypeFn,
worktypeCache: worktypeCache,
}
}

Expand Down Expand Up @@ -195,6 +199,11 @@ func getTaskManagementWorktypeByNameFn(ctx context.Context, p *TaskManagementWor

// getTaskManagementWorktypeByIdFn is an implementation of the function to get a Genesys Cloud task management worktype by Id
func getTaskManagementWorktypeByIdFn(ctx context.Context, p *TaskManagementWorktypeProxy, id string) (taskManagementWorktype *platformclientv2.Worktype, resp *platformclientv2.APIResponse, err error) {
worktype := rc.GetCacheItem(p.worktypeCache, id)
if worktype != nil {
return worktype, nil, nil
}

return p.taskManagementApi.GetTaskmanagementWorktype(id, []string{})
}

Expand Down

0 comments on commit 7e7ac36

Please sign in to comment.