Skip to content

Commit

Permalink
Completed all the CREATE-TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Aug 6, 2024
1 parent 31bb571 commit b3f4b42
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simple_routing_queue
import (
"context"
"log"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"time"

Expand All @@ -18,22 +19,32 @@ import (

// dataSourceSimpleRoutingQueueRead retrieves by search term the id in question
func dataSourceSimpleRoutingQueueRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// CREATE-TODO 1: Get an instance of our proxy
// CREATE-TODO 1: Replace '_ =' with 'sdkConfig :=' to capture our SDK configuration in a variable
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig

// CREATE-TODO 2: Grab our queue name from the schema.ResourceData object (done)
// CREATE-TODO 2: Get an instance of our proxy by passing sdkConfig into the method getSimpleRoutingQueueProxy
proxy := getSimpleRoutingQueueProxy(sdkConfig)

// CREATE-TODO 3: Grab our queue name from the schema.ResourceData object (this step is already complete)
name := d.Get("name").(string)

log.Printf("Finding queue by name '%s'", name)
return util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
// CREATE-TODO 3: Call to the proxy function getRoutingQueueIdByName(context.Context, string)
// This function returns values in the following order: queueId (string), retryable (bool), err (error)

// CREATE-TODO 4: If the error is not nil, and retryable equals false, return a resource.NonRetryableError
// to let the user know that an error occurred

// CREATE-TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name

// CREATE-TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil
// CREATE-TODO 4: Call to the proxy function proxyInstance.getRoutingQueueIdByName(context.Context, string), passing ctx and our name variable
// This function returns values in the following order: queueId (string), response (*platformclientv2.APIResponse), err (error), retryable (bool)
queueId, resp, err, retryable := proxy.getSimpleRoutingQueueIdByName(ctx, name)

// CREATE-TODO 5: If the error is not nil, and retryable equals false, return a resource.NonRetryableError
// to let the user know that an error occurred. If retryable is true, return a resource.RetryableError
if err != nil {
if !retryable {
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, err.Error(), resp))
}
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, err.Error(), resp))
}

// CREATE-TODO 6: If we made it this far, we can call d.SetId(queueId) and return nil
d.SetId(queueId)

return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ import (
"github.com/mypurecloud/platform-client-sdk-go/v133/platformclientv2"
)

type createRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type getRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type getAllRoutingQueuesFunc func(context.Context, *simpleRoutingQueueProxy) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error)
type updateRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type deleteRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, bool) (*platformclientv2.APIResponse, error)
type getRoutingQueueIdByNameFunc func(context.Context, *simpleRoutingQueueProxy, string) (id string, retryable bool, err error)
type createSimpleRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type getSimpleRoutingQueueByIdFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type getAllSimpleRoutingQueuesFunc func(context.Context, *simpleRoutingQueueProxy) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error)
type updateSimpleRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type deleteSimpleRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.APIResponse, error)
type getSimpleRoutingQueueIdByNameFunc func(context.Context, *simpleRoutingQueueProxy, string) (id string, response *platformclientv2.APIResponse, err error, retryable bool)

var internalProxy *simpleRoutingQueueProxy

type simpleRoutingQueueProxy struct {
routingApi *platformclientv2.RoutingApi
createRoutingQueueAttr createRoutingQueueFunc
getRoutingQueueAttr getRoutingQueueFunc
getAllRoutingQueuesAttr getAllRoutingQueuesFunc
getRoutingQueueIdByNameAttr getRoutingQueueIdByNameFunc
updateRoutingQueueAttr updateRoutingQueueFunc
deleteRoutingQueueAttr deleteRoutingQueueFunc
routingQueueCache rc.CacheInterface[platformclientv2.Queue]
routingApi *platformclientv2.RoutingApi
createSimpleRoutingQueueAttr createSimpleRoutingQueueFunc
getSimpleRoutingQueueByIdAttr getSimpleRoutingQueueByIdFunc
getAllSimpleRoutingQueuesAttr getAllSimpleRoutingQueuesFunc
getSimpleRoutingQueueIdByNameAttr getSimpleRoutingQueueIdByNameFunc
updateSimpleRoutingQueueAttr updateSimpleRoutingQueueFunc
deleteSimpleRoutingQueueAttr deleteSimpleRoutingQueueFunc
simpleRoutingQueueCache rc.CacheInterface[platformclientv2.Queue]
}

// newSimpleRoutingQueueProxy initializes the simple routing queue proxy with all the data needed to communicate with Genesys Cloud
func newSimpleRoutingQueueProxy(clientConfig *platformclientv2.Configuration) *simpleRoutingQueueProxy {
api := platformclientv2.NewRoutingApiWithConfig(clientConfig)
routingQueueCache := rc.NewResourceCache[platformclientv2.Queue]()
simpleRoutingQueueCache := rc.NewResourceCache[platformclientv2.Queue]()

return &simpleRoutingQueueProxy{
routingApi: api,
createRoutingQueueAttr: createRoutingQueueFn,
getRoutingQueueAttr: getRoutingQueueFn,
getRoutingQueueIdByNameAttr: getRoutingQueueIdByNameFn,
updateRoutingQueueAttr: updateRoutingQueueFn,
deleteRoutingQueueAttr: deleteRoutingQueueFn,
routingQueueCache: routingQueueCache,
routingApi: api,
createSimpleRoutingQueueAttr: createSimpleRoutingQueueFn,
getSimpleRoutingQueueByIdAttr: getSimpleRoutingQueueByIdFn,
getSimpleRoutingQueueIdByNameAttr: getSimpleRoutingQueueIdByNameFn,
updateSimpleRoutingQueueAttr: updateSimpleRoutingQueueFn,
deleteSimpleRoutingQueueAttr: deleteSimpleRoutingQueueFn,
simpleRoutingQueueCache: simpleRoutingQueueCache,
}
}

Expand All @@ -54,44 +54,44 @@ func getSimpleRoutingQueueProxy(clientConfig *platformclientv2.Configuration) *s
}

// createRoutingQueue creates a Genesys Cloud Routing Queue
func (p *simpleRoutingQueueProxy) createRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.createRoutingQueueAttr(ctx, p, queue)
func (p *simpleRoutingQueueProxy) createSimpleRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.createSimpleRoutingQueueAttr(ctx, p, queue)
}

// getRoutingQueue retrieves a Genesys Cloud Routing Queue by ID
func (p *simpleRoutingQueueProxy) getRoutingQueue(ctx context.Context, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.getRoutingQueueAttr(ctx, p, id)
func (p *simpleRoutingQueueProxy) getSimpleRoutingQueue(ctx context.Context, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.getSimpleRoutingQueueByIdAttr(ctx, p, id)
}

func (p *simpleRoutingQueueProxy) getAllRoutingQueues(ctx context.Context) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.getAllRoutingQueuesAttr(ctx, p)
func (p *simpleRoutingQueueProxy) getAllSimpleRoutingQueues(ctx context.Context) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.getAllSimpleRoutingQueuesAttr(ctx, p)
}

// getRoutingQueueIdByName retrieves a Genesys Cloud Routing Queue ID by its name
func (p *simpleRoutingQueueProxy) getRoutingQueueIdByName(ctx context.Context, name string) (string, bool, error) {
return p.getRoutingQueueIdByNameAttr(ctx, p, name)
func (p *simpleRoutingQueueProxy) getSimpleRoutingQueueIdByName(ctx context.Context, name string) (string, *platformclientv2.APIResponse, error, bool) {
return p.getSimpleRoutingQueueIdByNameAttr(ctx, p, name)
}

// updateRoutingQueue updates a Genesys Cloud Routing Queue
func (p *simpleRoutingQueueProxy) updateRoutingQueue(ctx context.Context, id string, queue *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.updateRoutingQueueAttr(ctx, p, id, queue)
func (p *simpleRoutingQueueProxy) updateSimpleRoutingQueue(ctx context.Context, id string, queue *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return p.updateSimpleRoutingQueueAttr(ctx, p, id, queue)
}

// deleteRoutingQueue deletes a Genesys Cloud Routing Queue
func (p *simpleRoutingQueueProxy) deleteRoutingQueue(ctx context.Context, id string, forceDelete bool) (*platformclientv2.APIResponse, error) {
return p.deleteRoutingQueueAttr(ctx, p, id, forceDelete)
func (p *simpleRoutingQueueProxy) deleteSimpleRoutingQueue(ctx context.Context, id string) (*platformclientv2.APIResponse, error) {
return p.deleteSimpleRoutingQueueAttr(ctx, p, id)
}

// createRoutingQueueFn is an implementation function for creating a Genesys Cloud Routing Queue
func createRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
func createSimpleRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return proxy.routingApi.PostRoutingQueues(*queue)
}

func getRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
func getSimpleRoutingQueueByIdFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return proxy.routingApi.GetRoutingQueue(id)
}

func getAllRoutingQueuesFn(ctx, proxy *simpleRoutingQueueProxy) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error) {
func getAllSimpleRoutingQueuesFn(ctx, proxy *simpleRoutingQueueProxy) (*[]platformclientv2.Queue, *platformclientv2.APIResponse, error) {
const pageSize = 100
var allQueues []platformclientv2.Queue

Expand Down Expand Up @@ -120,63 +120,55 @@ func getAllRoutingQueuesFn(ctx, proxy *simpleRoutingQueueProxy) (*[]platformclie
}

for _, queue := range allQueues {
rc.SetCache(proxy.routingQueueCache, *queue.Id, queue)
rc.SetCache(proxy.simpleRoutingQueueCache, *queue.Id, queue)
}

return &allQueues, nil, nil
}

func getRoutingQueueIdByNameFn(ctx context.Context, proxy *simpleRoutingQueueProxy, name string) (string, bool, error) {
func getSimpleRoutingQueueIdByNameFn(ctx context.Context, proxy *simpleRoutingQueueProxy, name string) (string, *platformclientv2.APIResponse, error, bool) {
const pageSize = 100
notFoundError := fmt.Errorf("no routing queues found with name %s", name)

queues, _, getErr := proxy.routingApi.GetRoutingQueues(1, pageSize, "", name, nil, nil, nil, "", false)
queues, resp, getErr := proxy.routingApi.GetRoutingQueues(1, pageSize, "", name, nil, nil, nil, "", false)
if getErr != nil {
return "", false, getErr
return "", resp, getErr, false
}

if queues.Entities == nil || len(*queues.Entities) == 0 {
return "", true, notFoundError
return "", nil, notFoundError, true
}

for _, queue := range *queues.Entities {
if queue.Name != nil && *queue.Name == name {
return *queue.Id, false, nil
return *queue.Id, nil, nil, false
}
}

for pageNum := 2; pageNum <= *queues.PageCount; pageNum++ {
queues, _, getErr := proxy.routingApi.GetRoutingQueues(pageNum, pageSize, "", name, nil, nil, nil, "", false)
queues, resp, getErr := proxy.routingApi.GetRoutingQueues(pageNum, pageSize, "", name, nil, nil, nil, "", false)
if getErr != nil {
return "", false, getErr
return "", resp, getErr, false
}

if queues.Entities == nil || len(*queues.Entities) == 0 {
return "", true, notFoundError
return "", nil, notFoundError, true
}

for _, queue := range *queues.Entities {
if queue.Name != nil && *queue.Name == name {
return *queue.Id, false, nil
return *queue.Id, nil, nil, false
}
}
}

return "", true, notFoundError
return "", nil, notFoundError, true
}

func updateRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string, body *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
queue, response, err := proxy.routingApi.PutRoutingQueue(id, *body)
if err != nil {
return nil, nil, fmt.Errorf("failed to update queue %s: %v", id, err)
}
return queue, response, err
func updateSimpleRoutingQueueFn(_ context.Context, proxy *simpleRoutingQueueProxy, id string, body *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) {
return proxy.routingApi.PutRoutingQueue(id, *body)
}

func deleteRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string, forceDelete bool) (*platformclientv2.APIResponse, error) {
response, err := proxy.routingApi.DeleteRoutingQueue(id, forceDelete)
if err != nil {
return nil, fmt.Errorf("failed to delete queue '%s': %v", id, err)
}
return response, err
func deleteSimpleRoutingQueueFn(_ context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.APIResponse, error) {
return proxy.routingApi.DeleteRoutingQueue(id, true)
}
Loading

0 comments on commit b3f4b42

Please sign in to comment.