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

Create 2023 v2 #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package simple_routing_queue

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
gcloud "terraform-provider-genesyscloud/genesyscloud"
"time"
)

/*
The data_source_genesyscloud_simple_routing_queue.go contains the data source implementation
for the resource.
*/

// 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
sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig
proxy := getSimpleRoutingQueueProxy(sdkConfig)

// CREATE-TODO 2: Grab our queue name from the schema.ResourceData object
name := d.Get("name").(string)

log.Printf("Finding queue with name '%s'", name)
return gcloud.WithRetries(ctx, 15*time.Second, func() *resource.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)
queueId, retryable, err := proxy.getRoutingQueueIdByName(ctx, name)

// 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
if err != nil && !retryable {
return resource.NonRetryableError(fmt.Errorf("error finding queue '%s': %v", name, err))
}

// CREATE-TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name
if retryable {
return resource.RetryableError(fmt.Errorf("no queue found with name '%s'", name))
}

// CREATE-TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil
d.SetId(queueId)
return nil
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package simple_routing_queue

import (
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
gcloud "terraform-provider-genesyscloud/genesyscloud"
"testing"
)

func TestAccDataSourceSimpleRoutingQueue(t *testing.T) {
var (
resourceId = "queue_resource"
dataSourceId = "queue_data"
simpleQueueName = "Create2023 queue " + uuid.NewString()

fullPathToResource = resourceName + "." + resourceId
fullPathToDataSource = "data." + resourceName + "." + dataSourceId
)

resource.Test(t, resource.TestCase{
PreCheck: func() { gcloud.TestAccPreCheck(t) },
ProviderFactories: gcloud.GetProviderFactories(providerResources, providerDataSources),
Steps: []resource.TestStep{
{
Config: generateSimpleRoutingQueueResource(
resourceId,
simpleQueueName,
"null",
"false",
) + generateSimpleRoutingQueueDataSource(
dataSourceId,
simpleQueueName,
fullPathToResource,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
fullPathToDataSource, "id",
fullPathToResource, "id",
),
),
},
},
})
}

func generateSimpleRoutingQueueDataSource(dataSourceId, queueName, dependsOn string) string {
return fmt.Sprintf(`
data "genesyscloud_simple_routing_queue" "%s" {
name = "%s"
depends_on = [%s]
}
`, dataSourceId, queueName, dependsOn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package simple_routing_queue

import (
"sync"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

/*
The genesyscloud_simple_routing_queue_init_test.go file is used to initialize the data sources and resources
used in testing the simple_routing_queues resource.

Please make sure you register ALL resources and data sources your test cases will use.
*/

// providerDataSources holds a map of all registered datasources
var providerDataSources map[string]*schema.Resource

// providerResources holds a map of all registered resources
var providerResources map[string]*schema.Resource

type registerTestInstance struct {
resourceMapMutex sync.RWMutex
datasourceMapMutex sync.RWMutex
}

// registerTestResources registers all resources used in the tests
func (r *registerTestInstance) registerTestResources() {
r.resourceMapMutex.Lock()
defer r.resourceMapMutex.Unlock()

providerResources[resourceName] = ResourceSimpleRoutingQueue()
}

// registerTestDataSources registers all data sources used in the tests.
func (r *registerTestInstance) registerTestDataSources() {
r.datasourceMapMutex.Lock()
defer r.datasourceMapMutex.Unlock()

providerDataSources[resourceName] = DataSourceSimpleRoutingQueue()
}

// initTestResources initializes all test resources and data sources.
func initTestResources() {
providerDataSources = make(map[string]*schema.Resource)
providerResources = make(map[string]*schema.Resource)

regInstance := &registerTestInstance{}

regInstance.registerTestDataSources()
regInstance.registerTestResources()
}

// TestMain is a "setup" function called by the testing framework when run the test
func TestMain(m *testing.M) {
// Run setup function before starting the test suite for simple_routing_queue package
initTestResources()

// Run the test suite for the simple_routing_queue package
m.Run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package simple_routing_queue

import (
"context"
"fmt"
"github.com/mypurecloud/platform-client-sdk-go/v105/platformclientv2"
)

type createRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type getRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.Queue, int, error)
type updateRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error)
type deleteRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.APIResponse, error)
type getRoutingQueueIdByNameFunc func(context.Context, *simpleRoutingQueueProxy, string) (id string, retryable bool, err error)

var internalProxy *simpleRoutingQueueProxy

type simpleRoutingQueueProxy struct {
routingApi *platformclientv2.RoutingApi
createRoutingQueueAttr createRoutingQueueFunc
getRoutingQueueAttr getRoutingQueueFunc
getRoutingQueueIdByNameAttr getRoutingQueueIdByNameFunc
updateRoutingQueueAttr updateRoutingQueueFunc
deleteRoutingQueueAttr deleteRoutingQueueFunc
}

// 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)
return &simpleRoutingQueueProxy{
routingApi: api,
createRoutingQueueAttr: createRoutingQueueFn,
getRoutingQueueAttr: getRoutingQueueFn,
getRoutingQueueIdByNameAttr: getRoutingQueueIdByNameFn,
updateRoutingQueueAttr: updateRoutingQueueFn,
deleteRoutingQueueAttr: deleteRoutingQueueFn,
}
}

// getSimpleRoutingQueueProxy 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 getSimpleRoutingQueueProxy(clientConfig *platformclientv2.Configuration) *simpleRoutingQueueProxy {
if internalProxy == nil {
internalProxy = newSimpleRoutingQueueProxy(clientConfig)
}
return internalProxy
}

// 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)
}

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

// 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)
}

// 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)
}

// deleteRoutingQueue deletes a Genesys Cloud Routing Queue
func (p *simpleRoutingQueueProxy) deleteRoutingQueue(ctx context.Context, id string) (*platformclientv2.APIResponse, error) {
return p.deleteRoutingQueueAttr(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) {
sdkQueue, response, err := proxy.routingApi.PostRoutingQueues(*queue)
if err != nil {
return nil, nil, fmt.Errorf("failed to create queue: %v", err)
}
return sdkQueue, response, err
}

func getRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, int, error) {
queue, response, err := proxy.routingApi.GetRoutingQueue(id)
if err != nil {
return nil, response.StatusCode, fmt.Errorf("failed to get routing queue by id '%s': %v", id, err)
}
return queue, 0, err
}

func getRoutingQueueIdByNameFn(ctx context.Context, proxy *simpleRoutingQueueProxy, name string) (string, bool, error) {
for pageNum := 1; ; pageNum++ {
const pageSize = 100
queues, _, getErr := proxy.routingApi.GetRoutingQueues(pageNum, pageSize, name, "", nil, nil, nil, false)
if getErr != nil {
return "", false, getErr
}

if queues.Entities == nil || len(*queues.Entities) == 0 {
return "", true, fmt.Errorf("no routing queues found with name %s", name)
}

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

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 deleteRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.APIResponse, error) {
response, err := proxy.routingApi.DeleteRoutingQueue(id, true)
if err != nil {
return nil, fmt.Errorf("failed to delete queue '%s': %v", id, err)
}
return response, err
}
Loading