forked from MyPureCloud/terraform-provider-genesyscloud
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c8015a
commit d76c7e9
Showing
8 changed files
with
520 additions
and
11 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"context" | ||
"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 { | ||
// TODO 1: . Get an instance of our proxy | ||
|
||
// TODO 2: Grab our queue name from the schema.ResourceData object | ||
name := d.Get("name").(string) | ||
|
||
log.Printf("Finding queue by name '%s'", name) | ||
return gcloud.WithRetries(ctx, 15*time.Second, func() *resource.RetryError { | ||
// 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) | ||
|
||
// 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 | ||
|
||
// TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name | ||
|
||
// TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil | ||
|
||
return nil | ||
}) | ||
} |
54 changes: 54 additions & 0 deletions
54
genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(nil, nil), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: generateSimpleRoutingQueueResource( | ||
resourceId, | ||
simpleQueueName, | ||
"null", | ||
"null", | ||
) + 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) | ||
} |
64 changes: 64 additions & 0 deletions
64
genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 := ®isterTestInstance{} | ||
|
||
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() | ||
} |
124 changes: 124 additions & 0 deletions
124
genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, bool) (*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, forceDelete bool) (*platformclientv2.APIResponse, error) { | ||
return p.deleteRoutingQueueAttr(ctx, p, id, forceDelete) | ||
} | ||
|
||
// 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, 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 | ||
} |
Oops, something went wrong.